BlueGit

ict rusty-teams

Files

rusty-teams / crates / core / src / message_link.rs

message_link.rs

message_link.rs 6.9 KiB Download Raw
1use percent_encoding::{AsciiSet, NON_ALPHANUMERIC, utf8_percent_encode};
2use serde_json::json;
3
4const LINK_HOST: &str = "https://teams.microsoft.com";
5const DEEP_LINK_HOST: &str = "https://teams.cloud.microsoft";
6const OUTLOOK_COMPOSE: &str = "https://outlook.office.com/mail/deeplink/compose";
7
8const 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)]
20pub 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
31fn 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.
36pub 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.
45pub 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
60pub fn chat_link(conversation_id: &str) -> String {
61 let context = json!({"contextType": "chat"});
62 format!(
63 "{LINK_HOST}/l/chat/{conversation_id}/conversations?context={}",
64 encode(&context.to_string())
65 )
66}
67
68pub fn channel_message_link(input: &ChannelLinkInput) -> String {
69 format!(
70 "{LINK_HOST}/l/message/{}/{}?tenantId={}&groupId={}&parentMessageId={}&teamName={}&channelName={}&createdTime={}",
71 input.channel_id,
72 input.message_id,
73 encode(&input.tenant_id),
74 encode(&input.group_id),
75 encode(&input.root_id),
76 encode(&input.team_name),
77 encode(&input.channel_name),
78 input.created_ms
79 )
80}
81
82pub fn channel_tab_link(channel_id: &str, tab_id: &str, label: &str) -> String {
83 format!(
84 "{LINK_HOST}/l/channel/{channel_id}/tab%3A%3A{}?label={}",
85 encode(tab_id),
86 encode(label)
87 )
88}
89
90#[derive(Debug, Clone, PartialEq, Eq)]
91pub struct ChannelAddress {
92 pub tenant_id: String,
93 pub group_id: String,
94 pub channel_id: String,
95 pub channel_name: String,
96 pub membership_type: Option<String>,
97}
98
99pub fn channel_link(address: &ChannelAddress) -> String {
100 let separate_roster = address.membership_type.as_deref().is_some_and(|kind| {
101 kind.eq_ignore_ascii_case("private") || kind.eq_ignore_ascii_case("shared")
102 });
103 format!(
104 "{DEEP_LINK_HOST}/l/channel/{}/{}?groupId={}&tenantId={}{}",
105 encode(&address.channel_id),
106 encode(&address.channel_name),
107 encode(&address.group_id),
108 encode(&address.tenant_id),
109 if separate_roster { "&ngc=true" } else { "" }
110 )
111}
112
113pub fn team_link(team_thread_id: &str, group_id: &str, tenant_id: &str) -> String {
114 format!(
115 "{DEEP_LINK_HOST}/l/team/{}/conversations?groupId={}&tenantId={}",
116 encode(team_thread_id),
117 encode(group_id),
118 encode(tenant_id)
119 )
120}
121
122#[cfg(test)]
123mod tests {
124 use super::*;
125
126 #[test]
127 fn chat_link_keeps_ids_raw_and_encodes_context() {
128 assert_eq!(
129 chat_message_link("19:abc@thread.v2", "1791368000000", None),
130 "https://teams.microsoft.com/l/message/19:abc@thread.v2/1791368000000?context=%7B%22contextType%22%3A%22chat%22%7D"
131 );
132 }
133
134 #[test]
135 fn chat_link_opens_the_conversation() {
136 assert_eq!(
137 chat_link("19:abc@thread.v2"),
138 "https://teams.microsoft.com/l/chat/19:abc@thread.v2/conversations?context=%7B%22contextType%22%3A%22chat%22%7D"
139 );
140 }
141
142 #[test]
143 fn outlook_compose_link_encodes_subject_and_body() {
144 assert_eq!(
145 outlook_compose_link("Release & plan", "Ada\nhi there"),
146 "https://outlook.office.com/mail/deeplink/compose?subject=Release%20%26%20plan&body=Ada%0Ahi%20there"
147 );
148 }
149
150 #[test]
151 fn notes_link_carries_the_object_id() {
152 assert_eq!(
153 chat_message_link("48:notes", "1791368000000", Some("aa-bb")),
154 "https://teams.microsoft.com/l/message/48:notes/1791368000000?context=%7B%22contextType%22%3A%22chat%22%2C%22oid%22%3A%22aa-bb%22%7D"
155 );
156 }
157
158 #[test]
159 fn channel_link_encodes_names_and_orders_params() {
160 let input = ChannelLinkInput {
161 tenant_id: "tenant-1".into(),
162 group_id: "group-1".into(),
163 channel_id: "19:chan@thread.tacv2".into(),
164 team_name: "R&D Team".into(),
165 channel_name: "General: News".into(),
166 root_id: "1791368000000".into(),
167 message_id: "1791368000555".into(),
168 created_ms: 1791368000555,
169 };
170 assert_eq!(
171 channel_message_link(&input),
172 "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"
173 );
174 }
175
176 #[test]
177 fn tab_link_points_at_the_tab_in_its_channel() {
178 assert_eq!(
179 channel_tab_link("19:chan@thread.tacv2", "a1-b2", "Sprint board"),
180 "https://teams.microsoft.com/l/channel/19:chan@thread.tacv2/tab%3A%3Aa1-b2?label=Sprint%20board"
181 );
182 }
183
184 fn address(membership_type: Option<&str>) -> ChannelAddress {
185 ChannelAddress {
186 tenant_id: "tenant-1".into(),
187 group_id: "group-1".into(),
188 channel_id: "19:chan@thread.tacv2".into(),
189 channel_name: "R&D News".into(),
190 membership_type: membership_type.map(str::to_owned),
191 }
192 }
193
194 #[test]
195 fn channel_link_encodes_the_id_and_name_like_teams() {
196 assert_eq!(
197 channel_link(&address(Some("standard"))),
198 "https://teams.cloud.microsoft/l/channel/19%3Achan%40thread.tacv2/R%26D%20News?groupId=group-1&tenantId=tenant-1"
199 );
200 }
201
202 #[test]
203 fn private_and_shared_channel_links_carry_ngc() {
204 for kind in ["private", "shared"] {
205 assert!(channel_link(&address(Some(kind))).ends_with("&tenantId=tenant-1&ngc=true"));
206 }
207 assert!(!channel_link(&address(None)).contains("ngc"));
208 }
209
210 #[test]
211 fn team_link_points_at_the_conversations_of_the_team_thread() {
212 assert_eq!(
213 team_link("19:general@thread.tacv2", "group-1", "tenant-1"),
214 "https://teams.cloud.microsoft/l/team/19%3Ageneral%40thread.tacv2/conversations?groupId=group-1&tenantId=tenant-1"
215 );
216 }
217}