| 1 | use serde_json::{Value, json}; |
| 2 | |
| 3 | use crate::trouter_events::CallbackLinks; |
| 4 | |
| 5 | const SPOTLIGHT: &str = "spotlight"; |
| 6 | const AUDIO: &str = "audio"; |
| 7 | const SPECIFIED: &str = "specified"; |
| 8 | const EVERYONE: &str = "all"; |
| 9 | |
| 10 | #[derive(Debug, Clone, Copy)] |
| 11 | pub struct Target<'a> { |
| 12 | pub mri: &'a str, |
| 13 | pub display_name: &'a str, |
| 14 | } |
| 15 | |
| 16 | impl Target<'_> { |
| 17 | fn wire(&self) -> Value { |
| 18 | json!({"id": self.mri, "displayName": self.display_name}) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | pub fn admit_body(from: Value, target: Target<'_>, callbacks: &CallbackLinks, cause_id: &str) -> Value { |
| 23 | json!({ |
| 24 | "participants": {"from": from, "to": [target.wire()]}, |
| 25 | "links": { |
| 26 | "admitFailure": callbacks.conversation("admitParticipantFailure"), |
| 27 | "admitSuccess": callbacks.conversation("admitParticipantSuccess"), |
| 28 | }, |
| 29 | "debugContent": {"causeId": cause_id}, |
| 30 | }) |
| 31 | } |
| 32 | |
| 33 | pub fn admit_all_body(from: Value, callbacks: &CallbackLinks, operation_id: &str) -> Value { |
| 34 | json!({ |
| 35 | "participants": {"from": from}, |
| 36 | "links": {"admitAllStatus": callbacks.conversation("admitAllStatus")}, |
| 37 | "operationId": operation_id, |
| 38 | "debugContent": {"causeId": operation_id}, |
| 39 | }) |
| 40 | } |
| 41 | |
| 42 | pub fn remove_participant_body(from: Value, target: Target<'_>, callbacks: &CallbackLinks) -> Value { |
| 43 | json!({ |
| 44 | "participants": {"from": from, "to": [target.wire()]}, |
| 45 | "links": { |
| 46 | "removeParticipantSuccess": callbacks.conversation("removeParticipantSuccess"), |
| 47 | "removeParticipantFailure": callbacks.conversation("removeParticipantFailure"), |
| 48 | }, |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | pub fn mute_participants_body(from: Value, mris: &[String]) -> Value { |
| 53 | mute_body(from, SPECIFIED, mris) |
| 54 | } |
| 55 | |
| 56 | pub fn mute_everyone_body(from: Value, others: &[String]) -> Value { |
| 57 | mute_body(from, EVERYONE, others) |
| 58 | } |
| 59 | |
| 60 | fn mute_body(from: Value, scope: &str, mris: &[String]) -> Value { |
| 61 | json!({ |
| 62 | "from": from, |
| 63 | "scope": scope, |
| 64 | "muteParticipants": mris.iter().map(|mri| json!({"id": mri})).collect::<Vec<_>>(), |
| 65 | "mediaTypes": [AUDIO], |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | pub fn spotlight_body(from: Value, sequence_number: u32, mri: &str, own_mri: &str) -> Value { |
| 70 | let mut body = json!({ |
| 71 | "from": from, |
| 72 | "publishedState": { |
| 73 | "stateType": SPOTLIGHT, |
| 74 | "level": "user", |
| 75 | "content": {}, |
| 76 | "sequenceNumber": sequence_number, |
| 77 | }, |
| 78 | }); |
| 79 | if mri != own_mri { |
| 80 | body["scope"] = json!(SPECIFIED); |
| 81 | body["to"] = json!([{"id": mri}]); |
| 82 | } |
| 83 | body |
| 84 | } |
| 85 | |
| 86 | #[cfg(test)] |
| 87 | mod tests { |
| 88 | use super::*; |
| 89 | |
| 90 | fn callbacks() -> CallbackLinks { |
| 91 | CallbackLinks::new("https://trouter.example/v4/f/x/", "call-1") |
| 92 | } |
| 93 | |
| 94 | fn from() -> Value { |
| 95 | json!({"id": "8:orgid:me"}) |
| 96 | } |
| 97 | |
| 98 | fn ana() -> Target<'static> { |
| 99 | Target { mri: "8:orgid:ana", display_name: "Ana" } |
| 100 | } |
| 101 | |
| 102 | #[test] |
| 103 | fn admitting_one_names_the_person_and_both_callbacks() { |
| 104 | let body = admit_body(from(), ana(), &callbacks(), "cause-1"); |
| 105 | assert_eq!(body["participants"]["from"]["id"], "8:orgid:me"); |
| 106 | assert_eq!(body["participants"]["to"], json!([{"id": "8:orgid:ana", "displayName": "Ana"}])); |
| 107 | assert!(body["links"]["admitSuccess"].as_str().unwrap().contains("/conversation/admitParticipantSuccess/")); |
| 108 | assert!(body["links"]["admitFailure"].as_str().unwrap().contains("/conversation/admitParticipantFailure/")); |
| 109 | assert_eq!(body["debugContent"]["causeId"], "cause-1"); |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn admitting_everyone_has_no_recipients() { |
| 114 | let body = admit_all_body(from(), &callbacks(), "op-1"); |
| 115 | assert!(body["participants"].get("to").is_none()); |
| 116 | assert_eq!(body["operationId"], "op-1"); |
| 117 | assert!(body["links"]["admitAllStatus"].as_str().unwrap().contains("/conversation/admitAllStatus/")); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn denying_and_removing_use_the_remove_body() { |
| 122 | let body = remove_participant_body(from(), ana(), &callbacks()); |
| 123 | assert_eq!(body["participants"]["to"][0]["id"], "8:orgid:ana"); |
| 124 | assert!(body["links"]["removeParticipantSuccess"].as_str().unwrap().contains("/conversation/removeParticipantSuccess/")); |
| 125 | assert!(body["links"]["removeParticipantFailure"].as_str().unwrap().contains("/conversation/removeParticipantFailure/")); |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn muting_one_or_all_only_changes_the_scope() { |
| 130 | let one = mute_participants_body(from(), &["8:orgid:ana".to_owned()]); |
| 131 | assert_eq!(one["scope"], "specified"); |
| 132 | assert_eq!(one["muteParticipants"], json!([{"id": "8:orgid:ana"}])); |
| 133 | assert_eq!(one["mediaTypes"], json!(["audio"])); |
| 134 | let everyone = mute_everyone_body(from(), &["8:orgid:ana".to_owned(), "8:orgid:bo".to_owned()]); |
| 135 | assert_eq!(everyone["scope"], "all"); |
| 136 | assert_eq!(everyone["muteParticipants"].as_array().unwrap().len(), 2); |
| 137 | } |
| 138 | |
| 139 | #[test] |
| 140 | fn spotlighting_someone_else_is_targeted_and_self_is_not() { |
| 141 | let other = spotlight_body(from(), 4, "8:orgid:ana", "8:orgid:me"); |
| 142 | assert_eq!(other["publishedState"]["stateType"], "spotlight"); |
| 143 | assert_eq!(other["publishedState"]["level"], "user"); |
| 144 | assert_eq!(other["publishedState"]["sequenceNumber"], 4); |
| 145 | assert_eq!(other["scope"], "specified"); |
| 146 | assert_eq!(other["to"], json!([{"id": "8:orgid:ana"}])); |
| 147 | let own = spotlight_body(from(), 5, "8:orgid:me", "8:orgid:me"); |
| 148 | assert!(own.get("scope").is_none()); |
| 149 | assert!(own.get("to").is_none()); |
| 150 | } |
| 151 | } |