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