BlueGit

ict rusty-teams

Files

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

remote.rs

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