BlueGit

ict rusty-teams

Files

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

remote.rs

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