| 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 { link: link.into(), participant_id: None } |
| 15 | } |
| 16 | |
| 17 | fn details(&self) -> Value { |
| 18 | match &self.participant_id { |
| 19 | Some(participant_id) => json!({"replaces": self.link, "replacementTargetParticipantId": participant_id}), |
| 20 | None => json!({"replaces": self.link}), |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | #[derive(Debug, Clone, Copy)] |
| 26 | pub struct TransferTarget<'a> { |
| 27 | pub mri: &'a str, |
| 28 | pub replaces: Option<&'a Replacement>, |
| 29 | } |
| 30 | |
| 31 | fn transferor(from: &Participant) -> Value { |
| 32 | json!({ |
| 33 | "details": { |
| 34 | "id": from.mri, |
| 35 | "endpointId": from.endpoint_id, |
| 36 | "participantId": from.participant_id, |
| 37 | "languageId": from.language_id, |
| 38 | }, |
| 39 | "authorizationToken": null, |
| 40 | }) |
| 41 | } |
| 42 | |
| 43 | pub fn transfer_body(from: &Participant, target: TransferTarget<'_>, callbacks: &CallbackLinks) -> Value { |
| 44 | let replacement = target.replaces.map(Replacement::details); |
| 45 | json!({ |
| 46 | "callTransfer": { |
| 47 | "target": {"id": target.mri}, |
| 48 | "transferor": transferor(from), |
| 49 | "links": { |
| 50 | "transferAcceptance": callbacks.call("transferAcceptance"), |
| 51 | "transferCompletion": callbacks.call("transferCompletion"), |
| 52 | }, |
| 53 | "replacementDetails": replacement, |
| 54 | "disableForwardingAndUnanswered": false, |
| 55 | "transferContext": null, |
| 56 | }, |
| 57 | }) |
| 58 | } |
| 59 | |
| 60 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 61 | pub enum TransferEvent { |
| 62 | Accepted, |
| 63 | Completed { code: i64, sub_code: i64 }, |
| 64 | } |
| 65 | |
| 66 | impl TransferEvent { |
| 67 | pub fn succeeded(&self) -> bool { |
| 68 | matches!(self, TransferEvent::Completed { code, .. } if *code == 0 || (200..300).contains(code)) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | pub fn completion(body: &Value) -> TransferEvent { |
| 73 | let completed = &body["transferCompletion"]; |
| 74 | TransferEvent::Completed { |
| 75 | code: completed["code"].as_i64().unwrap_or_default(), |
| 76 | sub_code: completed["subCode"].as_i64().unwrap_or_default(), |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | #[cfg(test)] |
| 81 | mod tests { |
| 82 | use super::*; |
| 83 | |
| 84 | fn me() -> Participant { |
| 85 | Participant { |
| 86 | mri: "8:orgid:me".into(), |
| 87 | display_name: "Me".into(), |
| 88 | endpoint_id: "ep-1".into(), |
| 89 | participant_id: "part-1".into(), |
| 90 | language_id: "en-gb".into(), |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | fn callbacks() -> CallbackLinks { |
| 95 | CallbackLinks::new("https://trouter.example/v4/f/x/", "call-1") |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn a_blind_transfer_names_the_target_and_the_transferor() { |
| 100 | let body = transfer_body(&me(), TransferTarget { mri: "8:orgid:ana", replaces: None }, &callbacks()); |
| 101 | let transfer = &body["callTransfer"]; |
| 102 | assert_eq!(transfer["target"], json!({"id": "8:orgid:ana"})); |
| 103 | assert_eq!(transfer["transferor"]["details"], json!({"id": "8:orgid:me", "endpointId": "ep-1", "participantId": "part-1", "languageId": "en-gb"})); |
| 104 | assert!(transfer["transferor"]["authorizationToken"].is_null()); |
| 105 | assert!(transfer["replacementDetails"].is_null()); |
| 106 | assert!(transfer["links"]["transferAcceptance"].as_str().unwrap().contains("/call/transferAcceptance/")); |
| 107 | assert!(transfer["links"]["transferCompletion"].as_str().unwrap().contains("/call/transferCompletion/")); |
| 108 | assert_eq!(transfer["disableForwardingAndUnanswered"], false); |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn a_consulted_transfer_replaces_the_consulted_persons_leg() { |
| 113 | let leg = Replacement { link: "https://cc.skype.com/leg/replacement".into(), participant_id: Some("leg-7".into()) }; |
| 114 | let body = transfer_body(&me(), TransferTarget { mri: "8:orgid:ana", replaces: Some(&leg) }, &callbacks()); |
| 115 | assert_eq!( |
| 116 | body["callTransfer"]["replacementDetails"], |
| 117 | json!({"replaces": "https://cc.skype.com/leg/replacement", "replacementTargetParticipantId": "leg-7"}) |
| 118 | ); |
| 119 | let whole_call = Replacement::of_call("https://cc.skype.com/replacement"); |
| 120 | let body = transfer_body(&me(), TransferTarget { mri: "8:orgid:ana", replaces: Some(&whole_call) }, &callbacks()); |
| 121 | assert_eq!(body["callTransfer"]["replacementDetails"], json!({"replaces": "https://cc.skype.com/replacement"})); |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn the_completion_callback_tells_success_from_failure() { |
| 126 | let done = completion(&json!({"transferCompletion": {"code": 0, "subCode": 0, "resultCategories": ["Success"]}})); |
| 127 | assert!(done.succeeded()); |
| 128 | let failed = completion(&json!({"transferCompletion": {"code": 603, "subCode": 10603}})); |
| 129 | assert_eq!(failed, TransferEvent::Completed { code: 603, sub_code: 10603 }); |
| 130 | assert!(!failed.succeeded()); |
| 131 | assert!(!TransferEvent::Accepted.succeeded()); |
| 132 | } |
| 133 | } |