BlueGit

ict rusty-teams

Files

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

remote.rs

remote.rs 20.1 KiB Download Raw
1use chatsvc::{
2 CardActions, ChatApp, ConversationRef, Conversations, InvokeRequest, InvokeResponse,
3 MemberHorizon, Messages, Receipts,
4};
5use chrono::{DateTime, Utc};
6use graph::{
7 Channel, Chat, DriveFolder, Graph, Member, Message, MessageExtras, MessageTarget,
8 OutgoingMention, Photo, Presence, SharedFile, Team, UploadDestination, UploadedFile, User,
9};
10
11use crate::error::{Error, Result};
12
13pub const CHATSVC_CHAT_WRITES: bool = false;
14
15#[derive(Debug, Clone)]
16pub struct RemotePage {
17 pub items: Vec<Message>,
18 pub next_link: Option<String>,
19}
20
21#[derive(Debug, Clone)]
22pub struct DeltaPage {
23 pub items: Vec<Message>,
24 pub next_link: Option<String>,
25 pub delta_link: Option<String>,
26}
27
28#[derive(Debug, Clone)]
29pub struct ChatsPage {
30 pub items: Vec<Chat>,
31 pub next_link: Option<String>,
32}
33
34#[allow(async_fn_in_trait)]
35pub trait Remote {
36 async fn me(&self) -> Result<User>;
37 async fn chats_page(&self, top: usize) -> Result<ChatsPage>;
38 async fn chats_at(&self, next_link: &str) -> Result<ChatsPage>;
39 async fn chat(&self, chat_id: &str) -> Result<Chat>;
40 async fn chat_members(&self, chat_id: &str) -> Result<Vec<Member>>;
41 async fn joined_teams(&self) -> Result<Vec<Team>>;
42 async fn channels_for_teams(&self, team_ids: &[String]) -> Result<Vec<Result<Vec<Channel>>>>;
43 async fn chat_messages(
44 &self,
45 chat_id: &str,
46 before: Option<DateTime<Utc>>,
47 top: usize,
48 ) -> Result<RemotePage>;
49 async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result<Message>;
50 async fn channel_messages(
51 &self,
52 team_id: &str,
53 channel_id: &str,
54 top: usize,
55 ) -> Result<RemotePage>;
56 async fn channel_messages_at(&self, next_link: &str) -> Result<RemotePage>;
57 async fn channel_message(
58 &self,
59 team_id: &str,
60 channel_id: &str,
61 message_id: &str,
62 ) -> Result<Message>;
63 async fn channel_reply(
64 &self,
65 team_id: &str,
66 channel_id: &str,
67 message_id: &str,
68 reply_id: &str,
69 ) -> Result<Message>;
70 async fn channel_delta(
71 &self,
72 team_id: &str,
73 channel_id: &str,
74 modified_after: Option<DateTime<Utc>>,
75 ) -> Result<DeltaPage>;
76 async fn channel_delta_at(&self, link: &str) -> Result<DeltaPage>;
77 async fn channel_replies(
78 &self,
79 team_id: &str,
80 channel_id: &str,
81 root_ids: &[String],
82 ) -> Result<Vec<Result<Vec<Message>>>>;
83 async fn send_chat_message(
84 &self,
85 chat_id: &str,
86 html: &str,
87 mentions: &[OutgoingMention],
88 extras: &MessageExtras,
89 ) -> Result<Message>;
90 async fn send_channel_message(
91 &self,
92 team_id: &str,
93 channel_id: &str,
94 html: &str,
95 subject: Option<&str>,
96 mentions: &[OutgoingMention],
97 extras: &MessageExtras,
98 ) -> Result<Message>;
99 async fn reply_to_channel_message(
100 &self,
101 team_id: &str,
102 channel_id: &str,
103 message_id: &str,
104 html: &str,
105 mentions: &[OutgoingMention],
106 extras: &MessageExtras,
107 ) -> Result<Message>;
108 async fn mark_chat_read(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()>;
109 async fn mark_chat_unread(
110 &self,
111 chat_id: &str,
112 user_id: &str,
113 tenant_id: &str,
114 last_read_at: DateTime<Utc>,
115 ) -> Result<()>;
116 async fn hide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()>;
117 async fn unhide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()>;
118 async fn leave_chat(&self, chat_id: &str, user_id: &str) -> Result<()>;
119 async fn muted_chat_states(&self) -> Result<Vec<(String, bool)>>;
120 async fn set_chat_muted(&self, chat_id: &str, muted: bool) -> Result<()>;
121 async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()>;
122 async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()>;
123 async fn edit_message(
124 &self,
125 target: &MessageTarget,
126 html: &str,
127 mentions: &[OutgoingMention],
128 extras: &MessageExtras,
129 ) -> Result<()>;
130 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()>;
131 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat>;
132 async fn create_group(
133 &self,
134 my_user_id: &str,
135 user_ids: &[String],
136 topic: Option<&str>,
137 ) -> Result<Chat>;
138 async fn search_people(&self, query: &str) -> Result<Vec<User>>;
139
140 async fn reply_with_quote(
141 &self,
142 _chat_id: &str,
143 _quoted_message_id: &str,
144 _html: &str,
145 _mentions: &[OutgoingMention],
146 _extras: &MessageExtras,
147 ) -> Result<Message> {
148 Err(Error::Unsupported("a quoted reply"))
149 }
150
151 async fn channel_files_folder(&self, _team_id: &str, _channel_id: &str) -> Result<DriveFolder> {
152 Err(Error::Unsupported("a channel files folder"))
153 }
154
155 async fn upload_file(
156 &self,
157 _destination: &UploadDestination,
158 _file_name: &str,
159 _bytes: &[u8],
160 _progress: &(dyn Fn(u8) + Send + Sync),
161 ) -> Result<UploadedFile> {
162 Err(Error::Unsupported("a file upload"))
163 }
164
165 async fn share_file(&self, _file: &UploadedFile, _user_ids: &[String]) -> Result<()> {
166 Err(Error::Unsupported("sharing a file"))
167 }
168
169 async fn delete_file(&self, _file: &UploadedFile) -> Result<()> {
170 Err(Error::Unsupported("deleting a file"))
171 }
172
173 async fn resolve_share(&self, _open_url: &str) -> Result<SharedFile> {
174 Err(Error::Unsupported("resolving a shared file"))
175 }
176
177 async fn download_range(
178 &self,
179 _download_url: &str,
180 _start: u64,
181 _end: Option<u64>,
182 ) -> Result<Vec<u8>> {
183 Err(Error::Unsupported("a file download"))
184 }
185
186 async fn hosted_content(&self, _url: &str) -> Result<Photo> {
187 Err(Error::Unsupported("hosted content"))
188 }
189
190 async fn user_photos(&self, _user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
191 Err(Error::Unsupported("profile photos"))
192 }
193
194 async fn presences(&self, _user_ids: &[String]) -> Result<Vec<Presence>> {
195 Err(Error::Unsupported("presence"))
196 }
197
198 async fn subscribe_presence(
199 &self,
200 _endpoint_id: &str,
201 _trouter_uri: &str,
202 _user_ids: &[String],
203 _purge: bool,
204 ) -> Result<()> {
205 Err(Error::Unsupported("presence subscriptions"))
206 }
207
208 async fn consumption_horizons(&self, _conversation_id: &str) -> Result<Vec<MemberHorizon>> {
209 Err(Error::Unsupported("read receipts"))
210 }
211
212 async fn chat_apps(&self, _chat_id: &str) -> Result<Vec<ChatApp>> {
213 Err(Error::Unsupported("chat apps"))
214 }
215
216 async fn invoke_card(&self, _invoke: InvokeRequest) -> Result<InvokeResponse> {
217 Err(Error::Unsupported("card actions"))
218 }
219}
220
221impl Remote for Graph {
222 async fn me(&self) -> Result<User> {
223 Ok(Graph::me(self).await?)
224 }
225
226 async fn chats_page(&self, top: usize) -> Result<ChatsPage> {
227 Ok(Graph::chats_page(self, top).await?.into())
228 }
229
230 async fn chats_at(&self, next_link: &str) -> Result<ChatsPage> {
231 Ok(Graph::chats_at(self, next_link).await?.into())
232 }
233
234 async fn chat(&self, chat_id: &str) -> Result<Chat> {
235 Ok(Graph::chat(self, chat_id).await?)
236 }
237
238 async fn chat_members(&self, chat_id: &str) -> Result<Vec<Member>> {
239 Ok(Graph::chat_members(self, chat_id).await?)
240 }
241
242 async fn joined_teams(&self) -> Result<Vec<Team>> {
243 Ok(Graph::joined_teams(self).await?)
244 }
245
246 async fn channels_for_teams(&self, team_ids: &[String]) -> Result<Vec<Result<Vec<Channel>>>> {
247 let listings = Graph::channels_for_teams(self, team_ids).await?;
248 Ok(listings
249 .into_iter()
250 .map(|listing| listing.map_err(Into::into))
251 .collect())
252 }
253
254 async fn chat_messages(
255 &self,
256 chat_id: &str,
257 before: Option<DateTime<Utc>>,
258 top: usize,
259 ) -> Result<RemotePage> {
260 Ok(Graph::chat_messages(self, chat_id, before, top)
261 .await?
262 .into())
263 }
264
265 async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result<Message> {
266 Ok(Graph::chat_message(self, chat_id, message_id).await?)
267 }
268
269 async fn channel_messages(
270 &self,
271 team_id: &str,
272 channel_id: &str,
273 top: usize,
274 ) -> Result<RemotePage> {
275 Ok(Graph::channel_messages(self, team_id, channel_id, top)
276 .await?
277 .into())
278 }
279
280 async fn channel_messages_at(&self, next_link: &str) -> Result<RemotePage> {
281 Ok(Graph::channel_messages_at(self, next_link).await?.into())
282 }
283
284 async fn channel_message(
285 &self,
286 team_id: &str,
287 channel_id: &str,
288 message_id: &str,
289 ) -> Result<Message> {
290 Ok(Graph::channel_message(self, team_id, channel_id, message_id).await?)
291 }
292
293 async fn channel_reply(
294 &self,
295 team_id: &str,
296 channel_id: &str,
297 message_id: &str,
298 reply_id: &str,
299 ) -> Result<Message> {
300 Ok(Graph::channel_reply(self, team_id, channel_id, message_id, reply_id).await?)
301 }
302
303 async fn channel_delta(
304 &self,
305 team_id: &str,
306 channel_id: &str,
307 modified_after: Option<DateTime<Utc>>,
308 ) -> Result<DeltaPage> {
309 Ok(
310 Graph::channel_delta(self, team_id, channel_id, modified_after)
311 .await?
312 .into(),
313 )
314 }
315
316 async fn channel_delta_at(&self, link: &str) -> Result<DeltaPage> {
317 Ok(Graph::channel_delta_at(self, link).await?.into())
318 }
319
320 async fn channel_replies(
321 &self,
322 team_id: &str,
323 channel_id: &str,
324 root_ids: &[String],
325 ) -> Result<Vec<Result<Vec<Message>>>> {
326 let listings = Graph::channel_replies(self, team_id, channel_id, root_ids).await?;
327 Ok(listings
328 .into_iter()
329 .map(|listing| listing.map_err(Into::into))
330 .collect())
331 }
332
333 async fn send_chat_message(
334 &self,
335 chat_id: &str,
336 html: &str,
337 mentions: &[OutgoingMention],
338 extras: &MessageExtras,
339 ) -> Result<Message> {
340 Ok(Graph::send_chat_message(self, chat_id, html, mentions, extras).await?)
341 }
342
343 async fn send_channel_message(
344 &self,
345 team_id: &str,
346 channel_id: &str,
347 html: &str,
348 subject: Option<&str>,
349 mentions: &[OutgoingMention],
350 extras: &MessageExtras,
351 ) -> Result<Message> {
352 Ok(
353 Graph::send_channel_message(self, team_id, channel_id, html, subject, mentions, extras)
354 .await?,
355 )
356 }
357
358 async fn reply_to_channel_message(
359 &self,
360 team_id: &str,
361 channel_id: &str,
362 message_id: &str,
363 html: &str,
364 mentions: &[OutgoingMention],
365 extras: &MessageExtras,
366 ) -> Result<Message> {
367 Ok(Graph::reply_to_channel_message(
368 self, team_id, channel_id, message_id, html, mentions, extras,
369 )
370 .await?)
371 }
372
373 async fn mark_chat_read(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> {
374 Ok(Graph::mark_chat_read(self, chat_id, user_id, tenant_id).await?)
375 }
376
377 async fn mark_chat_unread(
378 &self,
379 chat_id: &str,
380 user_id: &str,
381 tenant_id: &str,
382 last_read_at: DateTime<Utc>,
383 ) -> Result<()> {
384 Ok(Graph::mark_chat_unread(self, chat_id, user_id, tenant_id, last_read_at).await?)
385 }
386
387 async fn hide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> {
388 Ok(Graph::hide_chat(self, chat_id, user_id, tenant_id).await?)
389 }
390
391 async fn unhide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> {
392 Ok(Graph::unhide_chat(self, chat_id, user_id, tenant_id).await?)
393 }
394
395 async fn leave_chat(&self, chat_id: &str, user_id: &str) -> Result<()> {
396 Ok(Graph::leave_chat(self, chat_id, user_id).await?)
397 }
398
399 async fn muted_chat_states(&self) -> Result<Vec<(String, bool)>> {
400 Ok(Conversations::new(self.session())
401 .conversation_mute_states()
402 .await?)
403 }
404
405 async fn set_chat_muted(&self, chat_id: &str, muted: bool) -> Result<()> {
406 Ok(Conversations::new(self.session())
407 .set_alerts(chat_id, !muted)
408 .await?)
409 }
410
411 async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
412 if let Some(key) = chatsvc::emotion_key(reaction_type) {
413 return Ok(Messages::new(self.session())
414 .set_emotion(&conversation_ref(target), target.message_id(), key)
415 .await?);
416 }
417 Ok(Graph::set_reaction(self, target, reaction_type).await?)
418 }
419
420 async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
421 let keys = chatsvc::emotion_keys(reaction_type);
422 if !keys.is_empty() {
423 return Ok(Messages::new(self.session())
424 .unset_emotions(&conversation_ref(target), target.message_id(), keys)
425 .await?);
426 }
427 Ok(Graph::unset_reaction(self, target, reaction_type).await?)
428 }
429
430 async fn edit_message(
431 &self,
432 target: &MessageTarget,
433 html: &str,
434 mentions: &[OutgoingMention],
435 extras: &MessageExtras,
436 ) -> Result<()> {
437 match chatsvc_conversation(target) {
438 Some(conversation) if mentions.is_empty() => {
439 let html = format!("{}{html}{}", extras.kept_html(), extras.files_html());
440 Ok(Messages::new(self.session())
441 .edit_message(&conversation, target.message_id(), &html)
442 .await?)
443 }
444 Some(_) => Err(Error::Unsupported(
445 "editing a channel message with mentions",
446 )),
447 None => Ok(Graph::edit_message(self, target, html, mentions, extras).await?),
448 }
449 }
450
451 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
452 match chatsvc_conversation(target) {
453 Some(conversation) => Ok(Messages::new(self.session())
454 .soft_delete_message(&conversation, target.message_id())
455 .await?),
456 None => Ok(Graph::soft_delete_message(self, user_id, target).await?),
457 }
458 }
459
460 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat> {
461 Ok(Graph::create_one_on_one(self, my_user_id, user_id).await?)
462 }
463
464 async fn create_group(
465 &self,
466 my_user_id: &str,
467 user_ids: &[String],
468 topic: Option<&str>,
469 ) -> Result<Chat> {
470 Ok(Graph::create_group(self, my_user_id, user_ids, topic).await?)
471 }
472
473 async fn search_people(&self, query: &str) -> Result<Vec<User>> {
474 Ok(Graph::search_people(self, query).await?)
475 }
476
477 async fn reply_with_quote(
478 &self,
479 chat_id: &str,
480 quoted_message_id: &str,
481 html: &str,
482 mentions: &[OutgoingMention],
483 extras: &MessageExtras,
484 ) -> Result<Message> {
485 Ok(
486 Graph::reply_with_quote(self, chat_id, quoted_message_id, html, mentions, extras)
487 .await?,
488 )
489 }
490
491 async fn channel_files_folder(&self, team_id: &str, channel_id: &str) -> Result<DriveFolder> {
492 Ok(Graph::channel_files_folder(self, team_id, channel_id).await?)
493 }
494
495 async fn upload_file(
496 &self,
497 destination: &UploadDestination,
498 file_name: &str,
499 bytes: &[u8],
500 progress: &(dyn Fn(u8) + Send + Sync),
501 ) -> Result<UploadedFile> {
502 Ok(Graph::upload_file(self, destination, file_name, bytes, progress).await?)
503 }
504
505 async fn share_file(&self, file: &UploadedFile, user_ids: &[String]) -> Result<()> {
506 Ok(Graph::share_item(self, file, user_ids).await?)
507 }
508
509 async fn delete_file(&self, file: &UploadedFile) -> Result<()> {
510 Ok(Graph::delete_drive_item(self, &file.drive_id, &file.item_id).await?)
511 }
512
513 async fn resolve_share(&self, open_url: &str) -> Result<SharedFile> {
514 Ok(Graph::resolve_share(self, open_url).await?)
515 }
516
517 async fn download_range(
518 &self,
519 download_url: &str,
520 start: u64,
521 end: Option<u64>,
522 ) -> Result<Vec<u8>> {
523 Ok(Graph::download_range(self, download_url, start, end).await?)
524 }
525
526 async fn hosted_content(&self, url: &str) -> Result<Photo> {
527 Ok(Graph::download_hosted_content(self, url).await?)
528 }
529
530 async fn user_photos(&self, user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
531 let photos = Graph::user_photos(self, user_ids).await?;
532 Ok(photos
533 .into_iter()
534 .map(|photo| photo.map_err(Into::into))
535 .collect())
536 }
537
538 async fn presences(&self, user_ids: &[String]) -> Result<Vec<Presence>> {
539 Ok(Graph::presences(self, user_ids).await?)
540 }
541
542 async fn subscribe_presence(
543 &self,
544 endpoint_id: &str,
545 trouter_uri: &str,
546 user_ids: &[String],
547 purge: bool,
548 ) -> Result<()> {
549 Ok(Graph::subscribe_presence(self, endpoint_id, trouter_uri, user_ids, purge).await?)
550 }
551
552 async fn consumption_horizons(&self, conversation_id: &str) -> Result<Vec<MemberHorizon>> {
553 Ok(Receipts::new(self.session())
554 .consumption_horizons(conversation_id)
555 .await?)
556 }
557
558 async fn chat_apps(&self, chat_id: &str) -> Result<Vec<ChatApp>> {
559 Ok(CardActions::new(self.session()).chat_apps(chat_id).await?)
560 }
561
562 async fn invoke_card(&self, invoke: InvokeRequest) -> Result<InvokeResponse> {
563 Ok(CardActions::new(self.session()).invoke(invoke).await?)
564 }
565}
566
567fn chatsvc_conversation(target: &MessageTarget) -> Option<ConversationRef> {
568 match target {
569 MessageTarget::Chat { .. } => CHATSVC_CHAT_WRITES.then(|| conversation_ref(target)),
570 MessageTarget::Channel { .. } => Some(conversation_ref(target)),
571 }
572}
573
574fn conversation_ref(target: &MessageTarget) -> ConversationRef {
575 match target {
576 MessageTarget::Chat { chat_id, .. } => ConversationRef::chat(chat_id),
577 MessageTarget::Channel {
578 channel_id,
579 root_id: None,
580 ..
581 } => ConversationRef::channel_root(channel_id),
582 MessageTarget::Channel {
583 channel_id,
584 root_id: Some(root_id),
585 ..
586 } => ConversationRef::channel_reply(channel_id, root_id),
587 }
588}
589
590impl From<graph::Page<Message>> for RemotePage {
591 fn from(page: graph::Page<Message>) -> Self {
592 RemotePage {
593 items: page.items,
594 next_link: page.next_link,
595 }
596 }
597}
598
599impl From<graph::Page<Message>> for DeltaPage {
600 fn from(page: graph::Page<Message>) -> Self {
601 DeltaPage {
602 items: page.items,
603 next_link: page.next_link,
604 delta_link: page.delta_link,
605 }
606 }
607}
608
609impl From<graph::Page<Chat>> for ChatsPage {
610 fn from(page: graph::Page<Chat>) -> Self {
611 ChatsPage {
612 items: page.items,
613 next_link: page.next_link,
614 }
615 }
616}
617
618#[cfg(test)]
619mod tests {
620 use super::*;
621
622 #[test]
623 fn channel_targets_route_to_chatsvc() {
624 let root = MessageTarget::channel("t", "19:c@thread.tacv2", "m1", None);
625 let reply = MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1"));
626 assert_eq!(
627 chatsvc_conversation(&root),
628 Some(ConversationRef::channel_root("19:c@thread.tacv2"))
629 );
630 assert_eq!(
631 chatsvc_conversation(&reply),
632 Some(ConversationRef::channel_reply("19:c@thread.tacv2", "m1"))
633 );
634 }
635
636 #[test]
637 fn reactions_reach_chats_regardless_of_the_feature_switch() {
638 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
639 assert_eq!(
640 conversation_ref(&chat),
641 ConversationRef::chat("19:a@thread.v2")
642 );
643 }
644
645 #[test]
646 fn chats_follow_the_feature_switch() {
647 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
648 assert_eq!(chatsvc_conversation(&chat).is_some(), CHATSVC_CHAT_WRITES);
649 }
650}