BlueGit

ict rusty-teams

Files

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

remote.rs

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