BlueGit

ict rusty-teams

Files

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

remote.rs

fb874ac1 History
remote.rs 35.8 KiB Download Raw
1use chatsvc::{
2 CallLogEntry, CardActions, ChatApp, ConversationRef, ConversationState, Conversations,
3 ForcedAvailability, ForwardResult, Gif, Gifs, InvokeRequest, InvokeResponse, Language,
4 LanguageSettings, LanguageSettingsClient, LinkInfo, MemberHorizon, MessageLanguage,
5 MessageEmotions, MessageLinks, MessageWindow, Messages, PinnedMessage, Pins, PresenceService, PresenceStatus,
6 Receipts, RecordingPolicy, SavedMessage, ScheduledDraft, ScheduledDrafts, StatusNote, TranslateRequest,
7 Translation, TranslationTrigger, Translator, WindowDirection, WorkLocationKind,
8};
9use chrono::{DateTime, Utc};
10use graph::{
11 Channel, Chat, DriveEntry, DriveFolder, Graph, Member, Message, MessageExtras, MessageTarget,
12 OutgoingMention, Photo, Presence, SharedFile, Team, UploadDestination, UploadedFile, User,
13};
14
15use crate::error::{Error, Result};
16use crate::profile::PersonProfile;
17
18pub const CHATSVC_CHAT_WRITES: bool = false;
19
20#[derive(Debug, Clone)]
21pub struct RemotePage {
22 pub items: Vec<Message>,
23 pub next_link: Option<String>,
24}
25
26#[derive(Debug, Clone)]
27pub struct DeltaPage {
28 pub items: Vec<Message>,
29 pub next_link: Option<String>,
30 pub delta_link: Option<String>,
31}
32
33#[derive(Debug, Clone)]
34pub struct ChatsPage {
35 pub items: Vec<Chat>,
36 pub next_link: Option<String>,
37}
38
39#[allow(async_fn_in_trait)]
40pub trait Remote {
41 async fn me(&self) -> Result<User>;
42 async fn chats_page(&self, top: usize) -> Result<ChatsPage>;
43 async fn chats_at(&self, next_link: &str) -> Result<ChatsPage>;
44 async fn chat(&self, chat_id: &str) -> Result<Chat>;
45 async fn chat_members(&self, chat_id: &str) -> Result<Vec<Member>>;
46 async fn joined_teams(&self) -> Result<Vec<Team>>;
47 async fn channels_for_teams(&self, team_ids: &[String]) -> Result<Vec<Result<Vec<Channel>>>>;
48 async fn chat_messages(
49 &self,
50 chat_id: &str,
51 before: Option<DateTime<Utc>>,
52 top: usize,
53 ) -> Result<RemotePage>;
54 async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result<Message>;
55 async fn channel_messages(
56 &self,
57 team_id: &str,
58 channel_id: &str,
59 top: usize,
60 ) -> Result<RemotePage>;
61 async fn channel_messages_at(&self, next_link: &str) -> Result<RemotePage>;
62 async fn channel_message(
63 &self,
64 team_id: &str,
65 channel_id: &str,
66 message_id: &str,
67 ) -> Result<Message>;
68 async fn channel_reply(
69 &self,
70 team_id: &str,
71 channel_id: &str,
72 message_id: &str,
73 reply_id: &str,
74 ) -> Result<Message>;
75 async fn channel_delta(
76 &self,
77 team_id: &str,
78 channel_id: &str,
79 modified_after: Option<DateTime<Utc>>,
80 ) -> Result<DeltaPage>;
81 async fn channel_delta_at(&self, link: &str) -> Result<DeltaPage>;
82 async fn channel_replies(
83 &self,
84 team_id: &str,
85 channel_id: &str,
86 root_ids: &[String],
87 ) -> Result<Vec<Result<Vec<Message>>>>;
88 async fn send_chat_message(
89 &self,
90 chat_id: &str,
91 html: &str,
92 mentions: &[OutgoingMention],
93 extras: &MessageExtras,
94 ) -> Result<Message>;
95 async fn send_channel_message(
96 &self,
97 team_id: &str,
98 channel_id: &str,
99 html: &str,
100 subject: Option<&str>,
101 mentions: &[OutgoingMention],
102 extras: &MessageExtras,
103 ) -> Result<Message>;
104 async fn reply_to_channel_message(
105 &self,
106 team_id: &str,
107 channel_id: &str,
108 message_id: &str,
109 html: &str,
110 mentions: &[OutgoingMention],
111 extras: &MessageExtras,
112 ) -> Result<Message>;
113 async fn mark_chat_read(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()>;
114 async fn mark_chat_unread(
115 &self,
116 chat_id: &str,
117 user_id: &str,
118 tenant_id: &str,
119 last_read_at: DateTime<Utc>,
120 ) -> Result<()>;
121 async fn hide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()>;
122 async fn unhide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()>;
123 async fn leave_chat(&self, chat_id: &str, user_id: &str) -> Result<()>;
124 async fn chat_states(&self) -> Result<Vec<ConversationState>>;
125 async fn set_chat_muted(&self, chat_id: &str, muted: bool) -> Result<()>;
126 async fn set_channel_notifications(
127 &self,
128 channel_id: &str,
129 notifications: store::ChannelNotifications,
130 ) -> Result<()>;
131 async fn send_typing(&self, conversation: &ConversationRef, active: bool) -> Result<()>;
132 async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()>;
133 async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()>;
134 async fn edit_message(
135 &self,
136 target: &MessageTarget,
137 html: &str,
138 mentions: &[OutgoingMention],
139 extras: &MessageExtras,
140 ) -> Result<()>;
141 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()>;
142 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat>;
143 async fn create_group(
144 &self,
145 my_user_id: &str,
146 user_ids: &[String],
147 topic: Option<&str>,
148 ) -> Result<Chat>;
149 async fn search_people(&self, query: &str) -> Result<Vec<User>>;
150
151 async fn reply_with_quote(
152 &self,
153 _chat_id: &str,
154 _quoted_message_id: &str,
155 _html: &str,
156 _mentions: &[OutgoingMention],
157 _extras: &MessageExtras,
158 ) -> Result<Message> {
159 Err(Error::Unsupported("a quoted reply"))
160 }
161
162 async fn channel_files_folder(&self, _team_id: &str, _channel_id: &str) -> Result<DriveFolder> {
163 Err(Error::Unsupported("a channel files folder"))
164 }
165
166 async fn channel_files_root(&self, _team_id: &str, _channel_id: &str) -> Result<DriveEntry> {
167 Err(Error::Unsupported("a channel library"))
168 }
169
170 async fn channel_tab_web_url(
171 &self,
172 _team_id: &str,
173 _channel_id: &str,
174 _tab_id: &str,
175 ) -> Result<Option<String>> {
176 Err(Error::Unsupported("a channel tab link"))
177 }
178
179 async fn list_children(&self, _folder: &DriveFolder) -> Result<Vec<DriveEntry>> {
180 Err(Error::Unsupported("a folder listing"))
181 }
182
183 async fn create_folder(&self, _parent: &DriveFolder, _name: &str) -> Result<DriveEntry> {
184 Err(Error::Unsupported("creating a folder"))
185 }
186
187 async fn upload_file(
188 &self,
189 _destination: &UploadDestination,
190 _file_name: &str,
191 _bytes: &[u8],
192 _progress: &(dyn Fn(u8) + Send + Sync),
193 ) -> Result<UploadedFile> {
194 Err(Error::Unsupported("a file upload"))
195 }
196
197 async fn share_file(&self, _file: &UploadedFile, _user_ids: &[String]) -> Result<()> {
198 Err(Error::Unsupported("sharing a file"))
199 }
200
201 async fn delete_file(&self, _file: &UploadedFile) -> Result<()> {
202 Err(Error::Unsupported("deleting a file"))
203 }
204
205 async fn resolve_share(&self, _open_url: &str) -> Result<SharedFile> {
206 Err(Error::Unsupported("resolving a shared file"))
207 }
208
209 async fn resolve_drive_item(&self, _drive_id: &str, _item_id: &str) -> Result<SharedFile> {
210 Err(Error::Unsupported("resolving a drive item"))
211 }
212
213 async fn download_range(
214 &self,
215 _download_url: &str,
216 _start: u64,
217 _end: Option<u64>,
218 ) -> Result<Vec<u8>> {
219 Err(Error::Unsupported("a file download"))
220 }
221
222 async fn hosted_content(&self, _url: &str) -> Result<Photo> {
223 Err(Error::Unsupported("hosted content"))
224 }
225
226 async fn external_image(&self, _url: &str) -> Result<Photo> {
227 Err(Error::Unsupported("external images"))
228 }
229
230 async fn user_photos(&self, _user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
231 Err(Error::Unsupported("profile photos"))
232 }
233
234 async fn presences(&self, _user_ids: &[String]) -> Result<Vec<Presence>> {
235 Err(Error::Unsupported("presence"))
236 }
237
238 async fn person_profile(&self, _user_id: &str) -> Result<PersonProfile> {
239 Err(Error::Unsupported("profile cards"))
240 }
241
242 async fn subscribe_presence(
243 &self,
244 _endpoint_id: &str,
245 _trouter_uri: &str,
246 _user_ids: &[String],
247 _purge: bool,
248 ) -> Result<()> {
249 Err(Error::Unsupported("presence subscriptions"))
250 }
251
252 async fn own_status(&self, _user_id: &str) -> Result<PresenceStatus> {
253 Err(Error::Unsupported("own status"))
254 }
255
256 async fn set_availability(&self, _forced: Option<&ForcedAvailability>) -> Result<()> {
257 Err(Error::Unsupported("setting availability"))
258 }
259
260 async fn set_status_note(&self, _note: Option<&StatusNote>) -> Result<()> {
261 Err(Error::Unsupported("setting the status message"))
262 }
263
264 async fn set_work_location(
265 &self,
266 _location: Option<(WorkLocationKind, DateTime<Utc>)>,
267 ) -> Result<()> {
268 Err(Error::Unsupported("setting the work location"))
269 }
270
271 async fn consumption_horizons(&self, _conversation_id: &str) -> Result<Vec<MemberHorizon>> {
272 Err(Error::Unsupported("read receipts"))
273 }
274
275 async fn forward_messages(
276 &self,
277 _source_conversation_id: &str,
278 _target_conversation_id: &str,
279 _message_ids: &[String],
280 _comment_html: &str,
281 ) -> Result<ForwardResult> {
282 Err(Error::Unsupported("forwarding messages"))
283 }
284
285 async fn set_saved(
286 &self,
287 _conversation_id: &str,
288 _root_id: &str,
289 _message_id: &str,
290 _saved: bool,
291 ) -> Result<()> {
292 Err(Error::Unsupported("saving messages"))
293 }
294
295 async fn list_saved(&self) -> Result<Vec<SavedMessage>> {
296 Err(Error::Unsupported("saved messages"))
297 }
298
299 async fn list_call_logs(&self) -> Result<Vec<CallLogEntry>> {
300 Err(Error::Unsupported("call history"))
301 }
302
303 async fn chat_message_window(
304 &self,
305 _chat_id: &str,
306 _anchor_id: &str,
307 _direction: WindowDirection,
308 _page_size: usize,
309 ) -> Result<MessageWindow> {
310 Err(Error::Unsupported("message windows"))
311 }
312
313 async fn chat_messages_by_id(
314 &self,
315 _chat_id: &str,
316 _message_ids: &[String],
317 ) -> Result<Vec<Result<Message>>> {
318 Err(Error::Unsupported("batched message reads"))
319 }
320
321 async fn chat_pins(&self, _chat_id: &str) -> Result<Vec<PinnedMessage>> {
322 Err(Error::Unsupported("pinned messages"))
323 }
324
325 async fn pin_message(&self, _chat_id: &str, _message_id: &str) -> Result<()> {
326 Err(Error::Unsupported("pinning messages"))
327 }
328
329 async fn unpin_message(
330 &self,
331 _chat_id: &str,
332 _message_id: &str,
333 _parent_id: Option<&str>,
334 ) -> Result<()> {
335 Err(Error::Unsupported("pinning messages"))
336 }
337
338 async fn chat_apps(&self, _chat_id: &str) -> Result<Vec<ChatApp>> {
339 Err(Error::Unsupported("chat apps"))
340 }
341
342 async fn invoke_card(&self, _invoke: InvokeRequest) -> Result<InvokeResponse> {
343 Err(Error::Unsupported("card actions"))
344 }
345
346 async fn message_links(
347 &self,
348 _conversation: &ConversationRef,
349 _page_size: usize,
350 ) -> Result<Vec<MessageLinks>> {
351 Err(Error::Unsupported("link previews"))
352 }
353
354 async fn message_emotions(
355 &self,
356 _conversation: &ConversationRef,
357 _newest_message_id: &str,
358 _page_size: usize,
359 ) -> Result<Vec<MessageEmotions>> {
360 Err(Error::Unsupported("chat service reactions"))
361 }
362
363 async fn set_message_links(&self, _target: &MessageTarget, _links_json: &str) -> Result<()> {
364 Err(Error::Unsupported("link previews"))
365 }
366
367 async fn link_info(&self, _url: &str) -> Result<LinkInfo> {
368 Err(Error::Unsupported("link previews"))
369 }
370
371 async fn message_languages(
372 &self,
373 _conversation: &ConversationRef,
374 _page_size: usize,
375 ) -> Result<Vec<MessageLanguage>> {
376 Err(Error::Unsupported("message languages"))
377 }
378
379 async fn translate_chat_messages(
380 &self,
381 _chat_id: &str,
382 _to_language: &str,
383 _messages: &[TranslateRequest],
384 _trigger: &TranslationTrigger,
385 ) -> Result<Vec<Translation>> {
386 Err(Error::Unsupported("translation"))
387 }
388
389 async fn translate_channel_messages(
390 &self,
391 _team_id: &str,
392 _channel_id: &str,
393 _root_id: &str,
394 _to_language: &str,
395 _messages: &[TranslateRequest],
396 _trigger: &TranslationTrigger,
397 ) -> Result<Vec<Translation>> {
398 Err(Error::Unsupported("translation"))
399 }
400
401 async fn translation_languages(&self, _locale: &str) -> Result<Vec<Language>> {
402 Err(Error::Unsupported("translation"))
403 }
404
405 async fn language_settings(&self) -> Result<LanguageSettings> {
406 Err(Error::Unsupported("translation settings"))
407 }
408
409 async fn patch_language_settings(&self, _patch: serde_json::Value) -> Result<()> {
410 Err(Error::Unsupported("translation settings"))
411 }
412
413 async fn recording_policy(&self) -> Result<RecordingPolicy> {
414 Err(Error::Unsupported("recording policy"))
415 }
416
417 async fn search_gifs(&self, _query: &str) -> Result<Vec<Gif>> {
418 Err(Error::Unsupported("GIF search"))
419 }
420
421 async fn create_scheduled(
422 &self,
423 _inner_thread_id: &str,
424 _html: &str,
425 _send_at: DateTime<Utc>,
426 _display_name: &str,
427 ) -> Result<ScheduledDraft> {
428 Err(Error::Unsupported("schedule send"))
429 }
430
431 async fn scheduled_drafts(&self) -> Result<Vec<ScheduledDraft>> {
432 Err(Error::Unsupported("schedule send"))
433 }
434
435 async fn update_scheduled(
436 &self,
437 _draft: &ScheduledDraft,
438 _html: &str,
439 _display_name: &str,
440 ) -> Result<ScheduledDraft> {
441 Err(Error::Unsupported("schedule send"))
442 }
443
444 async fn cancel_scheduled(&self, _draft_id: &str) -> Result<()> {
445 Err(Error::Unsupported("schedule send"))
446 }
447}
448
449impl Remote for Graph {
450 async fn me(&self) -> Result<User> {
451 Ok(Graph::me(self).await?)
452 }
453
454 async fn chats_page(&self, top: usize) -> Result<ChatsPage> {
455 Ok(Graph::chats_page(self, top).await?.into())
456 }
457
458 async fn chats_at(&self, next_link: &str) -> Result<ChatsPage> {
459 Ok(Graph::chats_at(self, next_link).await?.into())
460 }
461
462 async fn chat(&self, chat_id: &str) -> Result<Chat> {
463 Ok(Graph::chat(self, chat_id).await?)
464 }
465
466 async fn chat_members(&self, chat_id: &str) -> Result<Vec<Member>> {
467 Ok(Graph::chat_members(self, chat_id).await?)
468 }
469
470 async fn joined_teams(&self) -> Result<Vec<Team>> {
471 Ok(Graph::joined_teams(self).await?)
472 }
473
474 async fn channels_for_teams(&self, team_ids: &[String]) -> Result<Vec<Result<Vec<Channel>>>> {
475 let listings = Graph::channels_for_teams(self, team_ids).await?;
476 Ok(listings
477 .into_iter()
478 .map(|listing| listing.map_err(Into::into))
479 .collect())
480 }
481
482 async fn chat_messages(
483 &self,
484 chat_id: &str,
485 before: Option<DateTime<Utc>>,
486 top: usize,
487 ) -> Result<RemotePage> {
488 Ok(Graph::chat_messages(self, chat_id, before, top)
489 .await?
490 .into())
491 }
492
493 async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result<Message> {
494 Ok(Graph::chat_message(self, chat_id, message_id).await?)
495 }
496
497 async fn channel_messages(
498 &self,
499 team_id: &str,
500 channel_id: &str,
501 top: usize,
502 ) -> Result<RemotePage> {
503 Ok(Graph::channel_messages(self, team_id, channel_id, top)
504 .await?
505 .into())
506 }
507
508 async fn channel_messages_at(&self, next_link: &str) -> Result<RemotePage> {
509 Ok(Graph::channel_messages_at(self, next_link).await?.into())
510 }
511
512 async fn channel_message(
513 &self,
514 team_id: &str,
515 channel_id: &str,
516 message_id: &str,
517 ) -> Result<Message> {
518 Ok(Graph::channel_message(self, team_id, channel_id, message_id).await?)
519 }
520
521 async fn channel_reply(
522 &self,
523 team_id: &str,
524 channel_id: &str,
525 message_id: &str,
526 reply_id: &str,
527 ) -> Result<Message> {
528 Ok(Graph::channel_reply(self, team_id, channel_id, message_id, reply_id).await?)
529 }
530
531 async fn channel_delta(
532 &self,
533 team_id: &str,
534 channel_id: &str,
535 modified_after: Option<DateTime<Utc>>,
536 ) -> Result<DeltaPage> {
537 Ok(
538 Graph::channel_delta(self, team_id, channel_id, modified_after)
539 .await?
540 .into(),
541 )
542 }
543
544 async fn channel_delta_at(&self, link: &str) -> Result<DeltaPage> {
545 Ok(Graph::channel_delta_at(self, link).await?.into())
546 }
547
548 async fn channel_replies(
549 &self,
550 team_id: &str,
551 channel_id: &str,
552 root_ids: &[String],
553 ) -> Result<Vec<Result<Vec<Message>>>> {
554 let listings = Graph::channel_replies(self, team_id, channel_id, root_ids).await?;
555 Ok(listings
556 .into_iter()
557 .map(|listing| listing.map_err(Into::into))
558 .collect())
559 }
560
561 async fn send_chat_message(
562 &self,
563 chat_id: &str,
564 html: &str,
565 mentions: &[OutgoingMention],
566 extras: &MessageExtras,
567 ) -> Result<Message> {
568 Ok(Graph::send_chat_message(self, chat_id, html, mentions, extras).await?)
569 }
570
571 async fn send_channel_message(
572 &self,
573 team_id: &str,
574 channel_id: &str,
575 html: &str,
576 subject: Option<&str>,
577 mentions: &[OutgoingMention],
578 extras: &MessageExtras,
579 ) -> Result<Message> {
580 Ok(
581 Graph::send_channel_message(self, team_id, channel_id, html, subject, mentions, extras)
582 .await?,
583 )
584 }
585
586 async fn reply_to_channel_message(
587 &self,
588 team_id: &str,
589 channel_id: &str,
590 message_id: &str,
591 html: &str,
592 mentions: &[OutgoingMention],
593 extras: &MessageExtras,
594 ) -> Result<Message> {
595 Ok(Graph::reply_to_channel_message(
596 self, team_id, channel_id, message_id, html, mentions, extras,
597 )
598 .await?)
599 }
600
601 async fn mark_chat_read(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> {
602 Ok(Graph::mark_chat_read(self, chat_id, user_id, tenant_id).await?)
603 }
604
605 async fn mark_chat_unread(
606 &self,
607 chat_id: &str,
608 user_id: &str,
609 tenant_id: &str,
610 last_read_at: DateTime<Utc>,
611 ) -> Result<()> {
612 Ok(Graph::mark_chat_unread(self, chat_id, user_id, tenant_id, last_read_at).await?)
613 }
614
615 async fn hide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> {
616 Ok(Graph::hide_chat(self, chat_id, user_id, tenant_id).await?)
617 }
618
619 async fn unhide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> {
620 Ok(Graph::unhide_chat(self, chat_id, user_id, tenant_id).await?)
621 }
622
623 async fn leave_chat(&self, chat_id: &str, user_id: &str) -> Result<()> {
624 Ok(Graph::leave_chat(self, chat_id, user_id).await?)
625 }
626
627 async fn chat_states(&self) -> Result<Vec<ConversationState>> {
628 Ok(Conversations::new(self.session())
629 .conversation_states()
630 .await?)
631 }
632
633 async fn set_chat_muted(&self, chat_id: &str, muted: bool) -> Result<()> {
634 Ok(Conversations::new(self.session())
635 .set_alerts(chat_id, !muted)
636 .await?)
637 }
638
639 async fn set_channel_notifications(
640 &self,
641 channel_id: &str,
642 notifications: store::ChannelNotifications,
643 ) -> Result<()> {
644 Ok(Conversations::new(self.session())
645 .set_channel_notifications(
646 channel_id,
647 crate::folders::chatsvc_notifications(notifications),
648 )
649 .await?)
650 }
651
652 async fn send_typing(&self, conversation: &ConversationRef, active: bool) -> Result<()> {
653 Ok(Messages::new(self.session())
654 .send_typing(conversation, active)
655 .await?)
656 }
657
658 async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
659 if let Some(key) = chatsvc::reaction_key(reaction_type) {
660 return Ok(Messages::new(self.session())
661 .set_emotion(&conversation_ref(target), target.message_id(), &key)
662 .await?);
663 }
664 Ok(Graph::set_reaction(self, target, reaction_type).await?)
665 }
666
667 async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
668 let keys = chatsvc::reaction_keys(reaction_type);
669 if !keys.is_empty() {
670 let keys: Vec<&str> = keys.iter().map(String::as_str).collect();
671 return Ok(Messages::new(self.session())
672 .unset_emotions(&conversation_ref(target), target.message_id(), &keys)
673 .await?);
674 }
675 Ok(Graph::unset_reaction(self, target, reaction_type).await?)
676 }
677
678 async fn edit_message(
679 &self,
680 target: &MessageTarget,
681 html: &str,
682 mentions: &[OutgoingMention],
683 extras: &MessageExtras,
684 ) -> Result<()> {
685 match chatsvc_conversation(target) {
686 Some(conversation) if mentions.is_empty() => {
687 let html = format!("{}{html}{}", extras.kept_html(), extras.files_html());
688 Ok(Messages::new(self.session())
689 .edit_message(&conversation, target.message_id(), &html)
690 .await?)
691 }
692 Some(_) => Err(Error::Unsupported(
693 "editing a channel message with mentions",
694 )),
695 None => Ok(Graph::edit_message(self, target, html, mentions, extras).await?),
696 }
697 }
698
699 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
700 match chatsvc_conversation(target) {
701 Some(conversation) => Ok(Messages::new(self.session())
702 .soft_delete_message(&conversation, target.message_id())
703 .await?),
704 None => Ok(Graph::soft_delete_message(self, user_id, target).await?),
705 }
706 }
707
708 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat> {
709 Ok(Graph::create_one_on_one(self, my_user_id, user_id).await?)
710 }
711
712 async fn create_group(
713 &self,
714 my_user_id: &str,
715 user_ids: &[String],
716 topic: Option<&str>,
717 ) -> Result<Chat> {
718 Ok(Graph::create_group(self, my_user_id, user_ids, topic).await?)
719 }
720
721 async fn search_people(&self, query: &str) -> Result<Vec<User>> {
722 Ok(Graph::search_people(self, query).await?)
723 }
724
725 async fn reply_with_quote(
726 &self,
727 chat_id: &str,
728 quoted_message_id: &str,
729 html: &str,
730 mentions: &[OutgoingMention],
731 extras: &MessageExtras,
732 ) -> Result<Message> {
733 Ok(
734 Graph::reply_with_quote(self, chat_id, quoted_message_id, html, mentions, extras)
735 .await?,
736 )
737 }
738
739 async fn channel_files_folder(&self, team_id: &str, channel_id: &str) -> Result<DriveFolder> {
740 Ok(Graph::channel_files_folder(self, team_id, channel_id).await?)
741 }
742
743 async fn channel_files_root(&self, team_id: &str, channel_id: &str) -> Result<DriveEntry> {
744 Ok(Graph::channel_files_root(self, team_id, channel_id).await?)
745 }
746
747 async fn channel_tab_web_url(
748 &self,
749 team_id: &str,
750 channel_id: &str,
751 tab_id: &str,
752 ) -> Result<Option<String>> {
753 Ok(Graph::channel_tab_web_url(self, team_id, channel_id, tab_id).await?)
754 }
755
756 async fn list_children(&self, folder: &DriveFolder) -> Result<Vec<DriveEntry>> {
757 Ok(Graph::list_children(self, folder).await?)
758 }
759
760 async fn create_folder(&self, parent: &DriveFolder, name: &str) -> Result<DriveEntry> {
761 Ok(Graph::create_folder(self, parent, name).await?)
762 }
763
764 async fn upload_file(
765 &self,
766 destination: &UploadDestination,
767 file_name: &str,
768 bytes: &[u8],
769 progress: &(dyn Fn(u8) + Send + Sync),
770 ) -> Result<UploadedFile> {
771 Ok(Graph::upload_file(self, destination, file_name, bytes, progress).await?)
772 }
773
774 async fn share_file(&self, file: &UploadedFile, user_ids: &[String]) -> Result<()> {
775 Ok(Graph::share_item(self, file, user_ids).await?)
776 }
777
778 async fn delete_file(&self, file: &UploadedFile) -> Result<()> {
779 Ok(Graph::delete_drive_item(self, &file.drive_id, &file.item_id).await?)
780 }
781
782 async fn resolve_share(&self, open_url: &str) -> Result<SharedFile> {
783 Ok(Graph::resolve_share(self, open_url).await?)
784 }
785
786 async fn resolve_drive_item(&self, drive_id: &str, item_id: &str) -> Result<SharedFile> {
787 Ok(Graph::resolve_drive_item(self, drive_id, item_id).await?)
788 }
789
790 async fn download_range(
791 &self,
792 download_url: &str,
793 start: u64,
794 end: Option<u64>,
795 ) -> Result<Vec<u8>> {
796 Ok(Graph::download_range(self, download_url, start, end).await?)
797 }
798
799 async fn hosted_content(&self, url: &str) -> Result<Photo> {
800 if chatsvc::is_link_image_url(url) {
801 let image = Messages::new(self.session()).link_image(url).await?;
802 return Ok(Photo {
803 bytes: image.bytes,
804 content_type: image.content_type,
805 });
806 }
807 Ok(Graph::download_hosted_content(self, url).await?)
808 }
809
810 async fn external_image(&self, url: &str) -> Result<Photo> {
811 Ok(Graph::download_external_image(self, url).await?)
812 }
813
814 async fn user_photos(&self, user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
815 let photos = Graph::user_photos(self, user_ids).await?;
816 Ok(photos
817 .into_iter()
818 .map(|photo| photo.map_err(Into::into))
819 .collect())
820 }
821
822 async fn presences(&self, user_ids: &[String]) -> Result<Vec<Presence>> {
823 Ok(Graph::presences(self, user_ids).await?)
824 }
825
826 async fn person_profile(&self, user_id: &str) -> Result<PersonProfile> {
827 crate::profile::load_profile(self, user_id).await
828 }
829
830 async fn subscribe_presence(
831 &self,
832 endpoint_id: &str,
833 trouter_uri: &str,
834 user_ids: &[String],
835 purge: bool,
836 ) -> Result<()> {
837 Ok(Graph::subscribe_presence(self, endpoint_id, trouter_uri, user_ids, purge).await?)
838 }
839
840 async fn own_status(&self, user_id: &str) -> Result<PresenceStatus> {
841 Ok(PresenceService::new(self.session()).status(user_id).await?)
842 }
843
844 async fn set_availability(&self, forced: Option<&ForcedAvailability>) -> Result<()> {
845 Ok(PresenceService::new(self.session())
846 .set_availability(forced)
847 .await?)
848 }
849
850 async fn set_status_note(&self, note: Option<&StatusNote>) -> Result<()> {
851 Ok(PresenceService::new(self.session()).set_note(note).await?)
852 }
853
854 async fn set_work_location(
855 &self,
856 location: Option<(WorkLocationKind, DateTime<Utc>)>,
857 ) -> Result<()> {
858 Ok(PresenceService::new(self.session())
859 .set_work_location(location)
860 .await?)
861 }
862
863 async fn consumption_horizons(&self, conversation_id: &str) -> Result<Vec<MemberHorizon>> {
864 Ok(Receipts::new(self.session())
865 .consumption_horizons(conversation_id)
866 .await?)
867 }
868
869 async fn forward_messages(
870 &self,
871 source_conversation_id: &str,
872 target_conversation_id: &str,
873 message_ids: &[String],
874 comment_html: &str,
875 ) -> Result<ForwardResult> {
876 Ok(Messages::new(self.session())
877 .forward_messages(
878 source_conversation_id,
879 target_conversation_id,
880 message_ids,
881 comment_html,
882 )
883 .await?)
884 }
885
886 async fn set_saved(
887 &self,
888 conversation_id: &str,
889 root_id: &str,
890 message_id: &str,
891 saved: bool,
892 ) -> Result<()> {
893 Ok(Messages::new(self.session())
894 .set_saved(conversation_id, root_id, message_id, saved)
895 .await?)
896 }
897
898 async fn list_saved(&self) -> Result<Vec<SavedMessage>> {
899 Ok(Messages::new(self.session()).list_saved().await?)
900 }
901
902 async fn list_call_logs(&self) -> Result<Vec<CallLogEntry>> {
903 Ok(Messages::new(self.session()).list_call_logs().await?)
904 }
905
906 async fn chat_message_window(
907 &self,
908 chat_id: &str,
909 anchor_id: &str,
910 direction: WindowDirection,
911 page_size: usize,
912 ) -> Result<MessageWindow> {
913 Ok(Messages::new(self.session())
914 .message_window(chat_id, anchor_id, direction, page_size)
915 .await?)
916 }
917
918 async fn chat_messages_by_id(
919 &self,
920 chat_id: &str,
921 message_ids: &[String],
922 ) -> Result<Vec<Result<Message>>> {
923 let messages = Graph::chat_messages_by_id(self, chat_id, message_ids).await?;
924 Ok(messages
925 .into_iter()
926 .map(|message| message.map_err(Into::into))
927 .collect())
928 }
929
930 async fn chat_pins(&self, chat_id: &str) -> Result<Vec<PinnedMessage>> {
931 Ok(Pins::new(self.session()).chat_pins(chat_id).await?)
932 }
933
934 async fn pin_message(&self, chat_id: &str, message_id: &str) -> Result<()> {
935 Ok(Pins::new(self.session())
936 .pin_message(chat_id, message_id)
937 .await?)
938 }
939
940 async fn unpin_message(
941 &self,
942 chat_id: &str,
943 message_id: &str,
944 parent_id: Option<&str>,
945 ) -> Result<()> {
946 Ok(Pins::new(self.session())
947 .unpin_message(chat_id, message_id, parent_id)
948 .await?)
949 }
950
951 async fn chat_apps(&self, chat_id: &str) -> Result<Vec<ChatApp>> {
952 Ok(CardActions::new(self.session()).chat_apps(chat_id).await?)
953 }
954
955 async fn invoke_card(&self, invoke: InvokeRequest) -> Result<InvokeResponse> {
956 Ok(CardActions::new(self.session()).invoke(invoke).await?)
957 }
958
959 async fn message_links(
960 &self,
961 conversation: &ConversationRef,
962 page_size: usize,
963 ) -> Result<Vec<MessageLinks>> {
964 Ok(Messages::new(self.session())
965 .list_message_links(conversation, page_size)
966 .await?)
967 }
968
969 async fn message_emotions(
970 &self,
971 conversation: &ConversationRef,
972 newest_message_id: &str,
973 page_size: usize,
974 ) -> Result<Vec<MessageEmotions>> {
975 Ok(Messages::new(self.session())
976 .message_emotions(&conversation.conversation_id(), newest_message_id, page_size)
977 .await?)
978 }
979
980 async fn set_message_links(&self, target: &MessageTarget, links_json: &str) -> Result<()> {
981 Ok(Messages::new(self.session())
982 .set_links(&conversation_ref(target), target.message_id(), links_json)
983 .await?)
984 }
985
986 async fn link_info(&self, url: &str) -> Result<LinkInfo> {
987 Ok(Messages::new(self.session()).link_info(url).await?)
988 }
989
990 async fn message_languages(
991 &self,
992 conversation: &ConversationRef,
993 page_size: usize,
994 ) -> Result<Vec<MessageLanguage>> {
995 Ok(Messages::new(self.session())
996 .list_language_stamps(conversation, page_size)
997 .await?)
998 }
999
1000 async fn translate_chat_messages(
1001 &self,
1002 chat_id: &str,
1003 to_language: &str,
1004 messages: &[TranslateRequest],
1005 trigger: &TranslationTrigger,
1006 ) -> Result<Vec<Translation>> {
1007 Ok(Translator::new(self.session())
1008 .translate_chat(chat_id, to_language, messages, trigger)
1009 .await?)
1010 }
1011
1012 async fn translate_channel_messages(
1013 &self,
1014 team_id: &str,
1015 channel_id: &str,
1016 root_id: &str,
1017 to_language: &str,
1018 messages: &[TranslateRequest],
1019 trigger: &TranslationTrigger,
1020 ) -> Result<Vec<Translation>> {
1021 Ok(Translator::new(self.session())
1022 .translate_channel(team_id, channel_id, root_id, to_language, messages, trigger)
1023 .await?)
1024 }
1025
1026 async fn translation_languages(&self, locale: &str) -> Result<Vec<Language>> {
1027 Ok(Translator::new(self.session()).languages(locale).await?)
1028 }
1029
1030 async fn language_settings(&self) -> Result<LanguageSettings> {
1031 Ok(LanguageSettingsClient::new(self.session()).read().await?)
1032 }
1033
1034 async fn recording_policy(&self) -> Result<RecordingPolicy> {
1035 Ok(LanguageSettingsClient::new(self.session())
1036 .read_recording_policy()
1037 .await?)
1038 }
1039
1040 async fn patch_language_settings(&self, patch: serde_json::Value) -> Result<()> {
1041 Ok(LanguageSettingsClient::new(self.session())
1042 .patch(patch)
1043 .await?)
1044 }
1045
1046 async fn search_gifs(&self, query: &str) -> Result<Vec<Gif>> {
1047 Ok(Gifs::new(self.session()).search(query).await?)
1048 }
1049
1050 async fn create_scheduled(
1051 &self,
1052 inner_thread_id: &str,
1053 html: &str,
1054 send_at: DateTime<Utc>,
1055 display_name: &str,
1056 ) -> Result<ScheduledDraft> {
1057 Ok(ScheduledDrafts::new(self.session())
1058 .create(inner_thread_id, html, send_at, display_name)
1059 .await?)
1060 }
1061
1062 async fn scheduled_drafts(&self) -> Result<Vec<ScheduledDraft>> {
1063 Ok(ScheduledDrafts::new(self.session()).list().await?)
1064 }
1065
1066 async fn update_scheduled(
1067 &self,
1068 draft: &ScheduledDraft,
1069 html: &str,
1070 display_name: &str,
1071 ) -> Result<ScheduledDraft> {
1072 Ok(ScheduledDrafts::new(self.session())
1073 .update(draft, html, display_name)
1074 .await?)
1075 }
1076
1077 async fn cancel_scheduled(&self, draft_id: &str) -> Result<()> {
1078 Ok(ScheduledDrafts::new(self.session())
1079 .cancel(draft_id)
1080 .await?)
1081 }
1082}
1083
1084fn chatsvc_conversation(target: &MessageTarget) -> Option<ConversationRef> {
1085 match target {
1086 MessageTarget::Chat { .. } => CHATSVC_CHAT_WRITES.then(|| conversation_ref(target)),
1087 MessageTarget::Channel { .. } => Some(conversation_ref(target)),
1088 }
1089}
1090
1091fn conversation_ref(target: &MessageTarget) -> ConversationRef {
1092 match target {
1093 MessageTarget::Chat { chat_id, .. } => ConversationRef::chat(chat_id),
1094 MessageTarget::Channel {
1095 channel_id,
1096 root_id: None,
1097 ..
1098 } => ConversationRef::channel_root(channel_id),
1099 MessageTarget::Channel {
1100 channel_id,
1101 root_id: Some(root_id),
1102 ..
1103 } => ConversationRef::channel_reply(channel_id, root_id),
1104 }
1105}
1106
1107impl From<graph::Page<Message>> for RemotePage {
1108 fn from(page: graph::Page<Message>) -> Self {
1109 RemotePage {
1110 items: page.items,
1111 next_link: page.next_link,
1112 }
1113 }
1114}
1115
1116impl From<graph::Page<Message>> for DeltaPage {
1117 fn from(page: graph::Page<Message>) -> Self {
1118 DeltaPage {
1119 items: page.items,
1120 next_link: page.next_link,
1121 delta_link: page.delta_link,
1122 }
1123 }
1124}
1125
1126impl From<graph::Page<Chat>> for ChatsPage {
1127 fn from(page: graph::Page<Chat>) -> Self {
1128 ChatsPage {
1129 items: page.items,
1130 next_link: page.next_link,
1131 }
1132 }
1133}
1134
1135#[cfg(test)]
1136mod tests {
1137 use super::*;
1138
1139 #[test]
1140 fn channel_targets_route_to_chatsvc() {
1141 let root = MessageTarget::channel("t", "19:c@thread.tacv2", "m1", None);
1142 let reply = MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1"));
1143 assert_eq!(
1144 chatsvc_conversation(&root),
1145 Some(ConversationRef::channel_root("19:c@thread.tacv2"))
1146 );
1147 assert_eq!(
1148 chatsvc_conversation(&reply),
1149 Some(ConversationRef::channel_reply("19:c@thread.tacv2", "m1"))
1150 );
1151 }
1152
1153 #[test]
1154 fn reactions_reach_chats_regardless_of_the_feature_switch() {
1155 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
1156 assert_eq!(
1157 conversation_ref(&chat),
1158 ConversationRef::chat("19:a@thread.v2")
1159 );
1160 }
1161
1162 #[test]
1163 fn chats_follow_the_feature_switch() {
1164 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
1165 assert_eq!(chatsvc_conversation(&chat).is_some(), CHATSVC_CHAT_WRITES);
1166 }
1167}