BlueGit

ict rusty-teams

Files

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

remote.rs

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