BlueGit

ict rusty-teams

Files

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

remote.rs

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