| 1 | use serde_json::{Value, json}; |
| 2 | |
| 3 | use crate::signaling::Participant; |
| 4 | use crate::trouter_events::CallbackLinks; |
| 5 | |
| 6 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 7 | pub struct Replacement { |
| 8 | pub link: String, |
| 9 | pub participant_id: Option<String>, |
| 10 | } |
| 11 | |
| 12 | impl Replacement { |
| 13 | pub fn of_call(link: impl Into<String>) -> Self { |
| 14 | Replacement { |
| 15 | link: link.into(), |
| 16 | participant_id: None, |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | fn details(&self) -> Value { |
| 21 | match &self.participant_id { |
| 22 | Some(participant_id) => { |
| 23 | json!({"replaces": self.link, "replacementTargetParticipantId": participant_id}) |
| 24 | } |
| 25 | None => json!({"replaces": self.link}), |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | #[derive(Debug, Clone, Copy)] |
| 31 | pub struct TransferTarget<'a> { |
| 32 | pub mri: &'a str, |
| 33 | pub replaces: Option<&'a Replacement>, |
| 34 | } |
| 35 | |
| 36 | fn transferor(from: &Participant) -> Value { |
| 37 | json!({ |
| 38 | "details": { |
| 39 | "id": from.mri, |
| 40 | "endpointId": from.endpoint_id, |
| 41 | "participantId": from.participant_id, |
| 42 | "languageId": from.language_id, |
| 43 | }, |
| 44 | "authorizationToken": null, |
| 45 | }) |
| 46 | } |
| 47 | |
| 48 | pub fn transfer_body( |
| 49 | from: &Participant, |
| 50 | target: TransferTarget<'_>, |
| 51 | callbacks: &CallbackLinks, |
| 52 | ) -> Value { |
| 53 | let replacement = target.replaces.map(Replacement::details); |
| 54 | json!({ |
| 55 | "callTransfer": { |
| 56 | "target": {"id": target.mri}, |
| 57 | "transferor": transferor(from), |
| 58 | "links": { |
| 59 | "transferAcceptance": callbacks.call("transferAcceptance"), |
| 60 | "transferCompletion": callbacks.call("transferCompletion"), |
| 61 | }, |
| 62 | "replacementDetails": replacement, |
| 63 | "disableForwardingAndUnanswered": false, |
| 64 | "transferContext": null, |
| 65 | }, |
| 66 | }) |
| 67 | } |
| 68 | |
| 69 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 70 | pub enum TransferEvent { |
| 71 | Accepted, |
| 72 | Completed { code: i64, sub_code: i64 }, |
| 73 | } |
| 74 | |
| 75 | impl TransferEvent { |
| 76 | pub fn succeeded(&self) -> bool { |
| 77 | matches!(self, TransferEvent::Completed { code, .. } if *code == 0 || (200..300).contains(code)) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | pub fn completion(body: &Value) -> TransferEvent { |
| 82 | let completed = &body["transferCompletion"]; |
| 83 | TransferEvent::Completed { |
| 84 | code: completed["code"].as_i64().unwrap_or_default(), |
| 85 | sub_code: completed["subCode"].as_i64().unwrap_or_default(), |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | #[cfg(test)] |
| 90 | mod tests { |
| 91 | use super::*; |
| 92 | |
| 93 | fn me() -> Participant { |
| 94 | Participant { |
| 95 | mri: "8:orgid:me".into(), |
| 96 | display_name: "Me".into(), |
| 97 | endpoint_id: "ep-1".into(), |
| 98 | participant_id: "part-1".into(), |
| 99 | language_id: "en-gb".into(), |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | fn callbacks() -> CallbackLinks { |
| 104 | CallbackLinks::new("https://trouter.example/v4/f/x/", "call-1") |
| 105 | } |
| 106 | |
| 107 | #[test] |
| 108 | fn a_blind_transfer_names_the_target_and_the_transferor() { |
| 109 | let body = transfer_body( |
| 110 | &me(), |
| 111 | TransferTarget { |
| 112 | mri: "8:orgid:ana", |
| 113 | replaces: None, |
| 114 | }, |
| 115 | &callbacks(), |
| 116 | ); |
| 117 | let transfer = &body["callTransfer"]; |
| 118 | assert_eq!(transfer["target"], json!({"id": "8:orgid:ana"})); |
| 119 | assert_eq!( |
| 120 | transfer["transferor"]["details"], |
| 121 | json!({"id": "8:orgid:me", "endpointId": "ep-1", "participantId": "part-1", "languageId": "en-gb"}) |
| 122 | ); |
| 123 | assert!(transfer["transferor"]["authorizationToken"].is_null()); |
| 124 | assert!(transfer["replacementDetails"].is_null()); |
| 125 | assert!( |
| 126 | transfer["links"]["transferAcceptance"] |
| 127 | .as_str() |
| 128 | .unwrap() |
| 129 | .contains("/call/transferAcceptance/") |
| 130 | ); |
| 131 | assert!( |
| 132 | transfer["links"]["transferCompletion"] |
| 133 | .as_str() |
| 134 | .unwrap() |
| 135 | .contains("/call/transferCompletion/") |
| 136 | ); |
| 137 | assert_eq!(transfer["disableForwardingAndUnanswered"], false); |
| 138 | } |
| 139 | |
| 140 | #[test] |
| 141 | fn a_consulted_transfer_replaces_the_consulted_persons_leg() { |
| 142 | let leg = Replacement { |
| 143 | link: "https://cc.skype.com/leg/replacement".into(), |
| 144 | participant_id: Some("leg-7".into()), |
| 145 | }; |
| 146 | let body = transfer_body( |
| 147 | &me(), |
| 148 | TransferTarget { |
| 149 | mri: "8:orgid:ana", |
| 150 | replaces: Some(&leg), |
| 151 | }, |
| 152 | &callbacks(), |
| 153 | ); |
| 154 | assert_eq!( |
| 155 | body["callTransfer"]["replacementDetails"], |
| 156 | json!({"replaces": "https://cc.skype.com/leg/replacement", "replacementTargetParticipantId": "leg-7"}) |
| 157 | ); |
| 158 | let whole_call = Replacement::of_call("https://cc.skype.com/replacement"); |
| 159 | let body = transfer_body( |
| 160 | &me(), |
| 161 | TransferTarget { |
| 162 | mri: "8:orgid:ana", |
| 163 | replaces: Some(&whole_call), |
| 164 | }, |
| 165 | &callbacks(), |
| 166 | ); |
| 167 | assert_eq!( |
| 168 | body["callTransfer"]["replacementDetails"], |
| 169 | json!({"replaces": "https://cc.skype.com/replacement"}) |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | #[test] |
| 174 | fn the_completion_callback_tells_success_from_failure() { |
| 175 | let done = completion( |
| 176 | &json!({"transferCompletion": {"code": 0, "subCode": 0, "resultCategories": ["Success"]}}), |
| 177 | ); |
| 178 | assert!(done.succeeded()); |
| 179 | let failed = completion(&json!({"transferCompletion": {"code": 603, "subCode": 10603}})); |
| 180 | assert_eq!( |
| 181 | failed, |
| 182 | TransferEvent::Completed { |
| 183 | code: 603, |
| 184 | sub_code: 10603 |
| 185 | } |
| 186 | ); |
| 187 | assert!(!failed.succeeded()); |
| 188 | assert!(!TransferEvent::Accepted.succeeded()); |
| 189 | } |
| 190 | } |