| 1 | use percent_encoding::{AsciiSet, NON_ALPHANUMERIC, utf8_percent_encode}; |
| 2 | use serde_json::json; |
| 3 | |
| 4 | const LINK_HOST: &str = "https://teams.microsoft.com"; |
| 5 | |
| 6 | const ENCODE_COMPONENT: &AsciiSet = &NON_ALPHANUMERIC |
| 7 | .remove(b'-') |
| 8 | .remove(b'_') |
| 9 | .remove(b'.') |
| 10 | .remove(b'!') |
| 11 | .remove(b'~') |
| 12 | .remove(b'*') |
| 13 | .remove(b'\'') |
| 14 | .remove(b'(') |
| 15 | .remove(b')'); |
| 16 | |
| 17 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 18 | pub struct ChannelLinkInput { |
| 19 | pub tenant_id: String, |
| 20 | pub group_id: String, |
| 21 | pub channel_id: String, |
| 22 | pub team_name: String, |
| 23 | pub channel_name: String, |
| 24 | pub root_id: String, |
| 25 | pub message_id: String, |
| 26 | pub created_ms: i64, |
| 27 | } |
| 28 | |
| 29 | fn encode(value: &str) -> String { |
| 30 | utf8_percent_encode(value, ENCODE_COMPONENT).to_string() |
| 31 | } |
| 32 | |
| 33 | /// `notes_oid` is the own object id, needed for the notes chat only. |
| 34 | pub fn chat_message_link( |
| 35 | conversation_id: &str, |
| 36 | message_id: &str, |
| 37 | notes_oid: Option<&str>, |
| 38 | ) -> String { |
| 39 | let mut context = json!({"contextType": "chat"}); |
| 40 | if let Some(oid) = notes_oid { |
| 41 | context["oid"] = json!(oid); |
| 42 | } |
| 43 | format!( |
| 44 | "{LINK_HOST}/l/message/{conversation_id}/{message_id}?context={}", |
| 45 | encode(&context.to_string()) |
| 46 | ) |
| 47 | } |
| 48 | |
| 49 | pub fn channel_message_link(input: &ChannelLinkInput) -> String { |
| 50 | format!( |
| 51 | "{LINK_HOST}/l/message/{}/{}?tenantId={}&groupId={}&parentMessageId={}&teamName={}&channelName={}&createdTime={}", |
| 52 | input.channel_id, |
| 53 | input.message_id, |
| 54 | encode(&input.tenant_id), |
| 55 | encode(&input.group_id), |
| 56 | encode(&input.root_id), |
| 57 | encode(&input.team_name), |
| 58 | encode(&input.channel_name), |
| 59 | input.created_ms |
| 60 | ) |
| 61 | } |
| 62 | |
| 63 | #[cfg(test)] |
| 64 | mod tests { |
| 65 | use super::*; |
| 66 | |
| 67 | #[test] |
| 68 | fn chat_link_keeps_ids_raw_and_encodes_context() { |
| 69 | assert_eq!( |
| 70 | chat_message_link("19:abc@thread.v2", "1791368000000", None), |
| 71 | "https://teams.microsoft.com/l/message/19:abc@thread.v2/1791368000000?context=%7B%22contextType%22%3A%22chat%22%7D" |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | #[test] |
| 76 | fn notes_link_carries_the_object_id() { |
| 77 | assert_eq!( |
| 78 | chat_message_link("48:notes", "1791368000000", Some("aa-bb")), |
| 79 | "https://teams.microsoft.com/l/message/48:notes/1791368000000?context=%7B%22contextType%22%3A%22chat%22%2C%22oid%22%3A%22aa-bb%22%7D" |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | #[test] |
| 84 | fn channel_link_encodes_names_and_orders_params() { |
| 85 | let input = ChannelLinkInput { |
| 86 | tenant_id: "tenant-1".into(), |
| 87 | group_id: "group-1".into(), |
| 88 | channel_id: "19:chan@thread.tacv2".into(), |
| 89 | team_name: "R&D Team".into(), |
| 90 | channel_name: "General: News".into(), |
| 91 | root_id: "1791368000000".into(), |
| 92 | message_id: "1791368000555".into(), |
| 93 | created_ms: 1791368000555, |
| 94 | }; |
| 95 | assert_eq!( |
| 96 | channel_message_link(&input), |
| 97 | "https://teams.microsoft.com/l/message/19:chan@thread.tacv2/1791368000555?tenantId=tenant-1&groupId=group-1&parentMessageId=1791368000000&teamName=R%26D%20Team&channelName=General%3A%20News&createdTime=1791368000555" |
| 98 | ); |
| 99 | } |
| 100 | } |