BlueGit

ict rusty-teams

Files

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

remote.rs

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