BlueGit

ict rusty-teams

Files

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

remote.rs

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