| 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 | const DEEP_LINK_HOST: &str = "https://teams.cloud.microsoft"; |
| 6 | const OUTLOOK_COMPOSE: &str = "https://outlook.office.com/mail/deeplink/compose"; |
| 7 | |
| 8 | const ENCODE_COMPONENT: &AsciiSet = &NON_ALPHANUMERIC |
| 9 | .remove(b'-') |
| 10 | .remove(b'_') |
| 11 | .remove(b'.') |
| 12 | .remove(b'!') |
| 13 | .remove(b'~') |
| 14 | .remove(b'*') |
| 15 | .remove(b'\'') |
| 16 | .remove(b'(') |
| 17 | .remove(b')'); |
| 18 | |
| 19 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 20 | pub struct ChannelLinkInput { |
| 21 | pub tenant_id: String, |
| 22 | pub group_id: String, |
| 23 | pub channel_id: String, |
| 24 | pub team_name: String, |
| 25 | pub channel_name: String, |
| 26 | pub root_id: String, |
| 27 | pub message_id: String, |
| 28 | pub created_ms: i64, |
| 29 | } |
| 30 | |
| 31 | fn encode(value: &str) -> String { |
| 32 | utf8_percent_encode(value, ENCODE_COMPONENT).to_string() |
| 33 | } |
| 34 | |
| 35 | /// Outlook on the web opens a new mail with this subject and plain text body; nothing is sent. |
| 36 | pub fn outlook_compose_link(subject: &str, body: &str) -> String { |
| 37 | format!( |
| 38 | "{OUTLOOK_COMPOSE}?subject={}&body={}", |
| 39 | encode(subject), |
| 40 | encode(body) |
| 41 | ) |
| 42 | } |
| 43 | |
| 44 | /// `notes_oid` is the own object id, needed for the notes chat only. |
| 45 | pub fn chat_message_link( |
| 46 | conversation_id: &str, |
| 47 | message_id: &str, |
| 48 | notes_oid: Option<&str>, |
| 49 | ) -> String { |
| 50 | let mut context = json!({"contextType": "chat"}); |
| 51 | if let Some(oid) = notes_oid { |
| 52 | context["oid"] = json!(oid); |
| 53 | } |
| 54 | format!( |
| 55 | "{LINK_HOST}/l/message/{conversation_id}/{message_id}?context={}", |
| 56 | encode(&context.to_string()) |
| 57 | ) |
| 58 | } |
| 59 | |
| 60 | pub fn channel_message_link(input: &ChannelLinkInput) -> String { |
| 61 | format!( |
| 62 | "{LINK_HOST}/l/message/{}/{}?tenantId={}&groupId={}&parentMessageId={}&teamName={}&channelName={}&createdTime={}", |
| 63 | input.channel_id, |
| 64 | input.message_id, |
| 65 | encode(&input.tenant_id), |
| 66 | encode(&input.group_id), |
| 67 | encode(&input.root_id), |
| 68 | encode(&input.team_name), |
| 69 | encode(&input.channel_name), |
| 70 | input.created_ms |
| 71 | ) |
| 72 | } |
| 73 | |
| 74 | pub fn channel_tab_link(channel_id: &str, tab_id: &str, label: &str) -> String { |
| 75 | format!( |
| 76 | "{LINK_HOST}/l/channel/{channel_id}/tab%3A%3A{}?label={}", |
| 77 | encode(tab_id), |
| 78 | encode(label) |
| 79 | ) |
| 80 | } |
| 81 | |
| 82 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 83 | pub struct ChannelAddress { |
| 84 | pub tenant_id: String, |
| 85 | pub group_id: String, |
| 86 | pub channel_id: String, |
| 87 | pub channel_name: String, |
| 88 | pub membership_type: Option<String>, |
| 89 | } |
| 90 | |
| 91 | pub fn channel_link(address: &ChannelAddress) -> String { |
| 92 | let separate_roster = address.membership_type.as_deref().is_some_and(|kind| { |
| 93 | kind.eq_ignore_ascii_case("private") || kind.eq_ignore_ascii_case("shared") |
| 94 | }); |
| 95 | format!( |
| 96 | "{DEEP_LINK_HOST}/l/channel/{}/{}?groupId={}&tenantId={}{}", |
| 97 | encode(&address.channel_id), |
| 98 | encode(&address.channel_name), |
| 99 | encode(&address.group_id), |
| 100 | encode(&address.tenant_id), |
| 101 | if separate_roster { "&ngc=true" } else { "" } |
| 102 | ) |
| 103 | } |
| 104 | |
| 105 | pub fn team_link(team_thread_id: &str, group_id: &str, tenant_id: &str) -> String { |
| 106 | format!( |
| 107 | "{DEEP_LINK_HOST}/l/team/{}/conversations?groupId={}&tenantId={}", |
| 108 | encode(team_thread_id), |
| 109 | encode(group_id), |
| 110 | encode(tenant_id) |
| 111 | ) |
| 112 | } |
| 113 | |
| 114 | #[cfg(test)] |
| 115 | mod tests { |
| 116 | use super::*; |
| 117 | |
| 118 | #[test] |
| 119 | fn chat_link_keeps_ids_raw_and_encodes_context() { |
| 120 | assert_eq!( |
| 121 | chat_message_link("19:abc@thread.v2", "1791368000000", None), |
| 122 | "https://teams.microsoft.com/l/message/19:abc@thread.v2/1791368000000?context=%7B%22contextType%22%3A%22chat%22%7D" |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn outlook_compose_link_encodes_subject_and_body() { |
| 128 | assert_eq!( |
| 129 | outlook_compose_link("Release & plan", "Ada\nhi there"), |
| 130 | "https://outlook.office.com/mail/deeplink/compose?subject=Release%20%26%20plan&body=Ada%0Ahi%20there" |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | #[test] |
| 135 | fn notes_link_carries_the_object_id() { |
| 136 | assert_eq!( |
| 137 | chat_message_link("48:notes", "1791368000000", Some("aa-bb")), |
| 138 | "https://teams.microsoft.com/l/message/48:notes/1791368000000?context=%7B%22contextType%22%3A%22chat%22%2C%22oid%22%3A%22aa-bb%22%7D" |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn channel_link_encodes_names_and_orders_params() { |
| 144 | let input = ChannelLinkInput { |
| 145 | tenant_id: "tenant-1".into(), |
| 146 | group_id: "group-1".into(), |
| 147 | channel_id: "19:chan@thread.tacv2".into(), |
| 148 | team_name: "R&D Team".into(), |
| 149 | channel_name: "General: News".into(), |
| 150 | root_id: "1791368000000".into(), |
| 151 | message_id: "1791368000555".into(), |
| 152 | created_ms: 1791368000555, |
| 153 | }; |
| 154 | assert_eq!( |
| 155 | channel_message_link(&input), |
| 156 | "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" |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | #[test] |
| 161 | fn tab_link_points_at_the_tab_in_its_channel() { |
| 162 | assert_eq!( |
| 163 | channel_tab_link("19:chan@thread.tacv2", "a1-b2", "Sprint board"), |
| 164 | "https://teams.microsoft.com/l/channel/19:chan@thread.tacv2/tab%3A%3Aa1-b2?label=Sprint%20board" |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | fn address(membership_type: Option<&str>) -> ChannelAddress { |
| 169 | ChannelAddress { |
| 170 | tenant_id: "tenant-1".into(), |
| 171 | group_id: "group-1".into(), |
| 172 | channel_id: "19:chan@thread.tacv2".into(), |
| 173 | channel_name: "R&D News".into(), |
| 174 | membership_type: membership_type.map(str::to_owned), |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn channel_link_encodes_the_id_and_name_like_teams() { |
| 180 | assert_eq!( |
| 181 | channel_link(&address(Some("standard"))), |
| 182 | "https://teams.cloud.microsoft/l/channel/19%3Achan%40thread.tacv2/R%26D%20News?groupId=group-1&tenantId=tenant-1" |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | #[test] |
| 187 | fn private_and_shared_channel_links_carry_ngc() { |
| 188 | for kind in ["private", "shared"] { |
| 189 | assert!(channel_link(&address(Some(kind))).ends_with("&tenantId=tenant-1&ngc=true")); |
| 190 | } |
| 191 | assert!(!channel_link(&address(None)).contains("ngc")); |
| 192 | } |
| 193 | |
| 194 | #[test] |
| 195 | fn team_link_points_at_the_conversations_of_the_team_thread() { |
| 196 | assert_eq!( |
| 197 | team_link("19:general@thread.tacv2", "group-1", "tenant-1"), |
| 198 | "https://teams.cloud.microsoft/l/team/19%3Ageneral%40thread.tacv2/conversations?groupId=group-1&tenantId=tenant-1" |
| 199 | ); |
| 200 | } |
| 201 | } |