BlueGit

ict rusty-teams

Files

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

remote.rs

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