| 1 | use serde_json::{Value, json}; |
| 2 | |
| 3 | use crate::error::{Error, Result}; |
| 4 | use crate::signaling::{ |
| 5 | MeetingTarget, Participant, chat_thread_id, endpoint_state, subscribe_request, |
| 6 | }; |
| 7 | use crate::trouter_events::CallbackLinks; |
| 8 | |
| 9 | const RESURRECT: &str = "resurrect"; |
| 10 | |
| 11 | /// Conversation links are required: without them epconv answers 400 with an empty body. |
| 12 | pub fn resolve_body(from: &Participant, callbacks: &CallbackLinks, meeting_data: &Value) -> Value { |
| 13 | json!({ |
| 14 | "conversationRequest": subscribe_request(callbacks), |
| 15 | "participants": {"from": from.wire()}, |
| 16 | "groupChat": null, |
| 17 | "meetingInfo": null, |
| 18 | "meetingData": meeting_data, |
| 19 | "meetingPreferences": {"shouldResurrect": RESURRECT}, |
| 20 | "endpointState": endpoint_state(), |
| 21 | }) |
| 22 | } |
| 23 | |
| 24 | fn answer_text(body: &Value, pointer: &str) -> Option<String> { |
| 25 | body.pointer(pointer)? |
| 26 | .as_str() |
| 27 | .filter(|text| !text.is_empty()) |
| 28 | .map(str::to_owned) |
| 29 | } |
| 30 | |
| 31 | pub fn target_from_answer(body: &Value, meeting_data: &Value) -> Result<MeetingTarget> { |
| 32 | let not_found = || Error::Signaling("meeting not found".into()); |
| 33 | Ok(MeetingTarget { |
| 34 | thread_id: chat_thread_id(body).ok_or_else(not_found)?, |
| 35 | tenant_id: answer_text(body, "/meetingInfo/tenantId") |
| 36 | .or_else(|| answer_text(body, "/tenantId")) |
| 37 | .ok_or_else(not_found)?, |
| 38 | organizer_id: answer_text(body, "/meetingInfo/organizerId").ok_or_else(not_found)?, |
| 39 | meeting_data: Some(meeting_data.clone()), |
| 40 | }) |
| 41 | } |
| 42 | |
| 43 | #[cfg(test)] |
| 44 | mod tests { |
| 45 | use super::*; |
| 46 | |
| 47 | fn meeting_data() -> Value { |
| 48 | json!({"meetingCode": "234567890123", "passcode": "pw", "meetingUrl": "https://teams.microsoft.com/meet/234567890123?p=pw"}) |
| 49 | } |
| 50 | |
| 51 | fn from() -> Participant { |
| 52 | Participant { |
| 53 | mri: "8:orgid:me".into(), |
| 54 | display_name: "Me".into(), |
| 55 | endpoint_id: "endpoint".into(), |
| 56 | participant_id: "participant".into(), |
| 57 | language_id: "en-gb".into(), |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | #[test] |
| 62 | fn the_resolve_body_is_a_roster_subscription_by_meeting_data() { |
| 63 | let callbacks = CallbackLinks::new("https://trouter.example/callAgent", "call-1"); |
| 64 | let body = resolve_body(&from(), &callbacks, &meeting_data()); |
| 65 | assert_eq!(body["meetingData"], meeting_data()); |
| 66 | assert_eq!(body["groupChat"], Value::Null); |
| 67 | assert_eq!(body["meetingInfo"], Value::Null); |
| 68 | assert_eq!(body["participants"]["from"]["id"], "8:orgid:me"); |
| 69 | assert_eq!(body["meetingPreferences"]["shouldResurrect"], "resurrect"); |
| 70 | let links = body["conversationRequest"]["links"].as_object().unwrap(); |
| 71 | assert!(links.contains_key("conversationUpdate") && links.contains_key("conversationEnd")); |
| 72 | assert!( |
| 73 | body["conversationRequest"]["roster"]["rosterUpdate"] |
| 74 | .as_str() |
| 75 | .unwrap() |
| 76 | .contains("call-1") |
| 77 | ); |
| 78 | assert!(body.get("callInvitation").is_none()); |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn an_answer_with_thread_tenant_and_organizer_gives_a_target() { |
| 83 | let body = json!({ |
| 84 | "activeModalities": {"groupChat": {"threadId": "19:meeting_x@thread.v2", "messageId": "0"}}, |
| 85 | "meetingInfo": {"tenantId": "tenant-1", "organizerId": "organizer-1"}, |
| 86 | }); |
| 87 | let target = target_from_answer(&body, &meeting_data()).unwrap(); |
| 88 | assert_eq!(target.thread_id, "19:meeting_x@thread.v2"); |
| 89 | assert_eq!(target.tenant_id, "tenant-1"); |
| 90 | assert_eq!(target.organizer_id, "organizer-1"); |
| 91 | assert_eq!(target.meeting_data, Some(meeting_data())); |
| 92 | } |
| 93 | |
| 94 | #[test] |
| 95 | fn the_top_level_tenant_fills_a_missing_meeting_info_tenant() { |
| 96 | let body = json!({ |
| 97 | "activeModalities": {"groupChat": {"threadId": "19:x"}}, |
| 98 | "tenantId": "tenant-2", |
| 99 | "meetingInfo": {"organizerId": "organizer-1"}, |
| 100 | }); |
| 101 | assert_eq!( |
| 102 | target_from_answer(&body, &meeting_data()) |
| 103 | .unwrap() |
| 104 | .tenant_id, |
| 105 | "tenant-2" |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn an_answer_without_a_thread_or_organizer_is_not_found() { |
| 111 | let no_thread = json!({"meetingInfo": {"tenantId": "t", "organizerId": "o"}}); |
| 112 | assert!(target_from_answer(&no_thread, &meeting_data()).is_err()); |
| 113 | let no_organizer = json!({"activeModalities": {"groupChat": {"threadId": "19:x"}}, "meetingInfo": {"tenantId": "t"}}); |
| 114 | assert!(target_from_answer(&no_organizer, &meeting_data()).is_err()); |
| 115 | } |
| 116 | } |