BlueGit

ict rusty-teams

Files

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

remote.rs

remote.rs 57.8 KiB Download Raw
1use std::collections::HashMap;
2
3use chatsvc::compose_copilot::{ComposeCopilot, ComposeRewrite};
4use chatsvc::copilot::{Copilot, CopilotAsk};
5use chatsvc::sound_settings::{SoundSettings, SoundSettingsClient};
6use chatsvc::substrate_search::{
7 QueryRequest, QueryResults, SearchIdentity, SearchSession, SubstrateSearch, Suggestions,
8};
9use chatsvc::{
10 ActivityFeed, ActivitySnapshot, AppConsumptionHorizon, ChatNotificationMode,
11 ChatNotificationWrite, ChatSettingChange, ChatSettingsClient, ChatSettingsDocument,
12 DesktopNotifications, UserSettings, parse_app_consumption_horizon,
13};
14use chatsvc::{
15 BotProfile, BotProfiles, CallLogEntry, CardActions, ChatApp, ConversationRef,
16 ConversationState, Conversations, ForcedAvailability, ForwardResult, Gif, Gifs, HistoryShare,
17 InvokeRequest, InvokeResponse, Language, LanguageSettings, LanguageSettingsClient, LinkInfo, CustomEmojis, OrgEmojiCatalog,
18 MemberHorizon, MessageEmotions, MessageLanguage, MessageLinks, MessageWindow, Messages,
19 PinnedMessage, Pins, PostFollowState, PresenceLookup, PresenceService, PresenceStatus,
20 Receipts, RecordingPolicy, SavedMessage, ScheduledDraft, ScheduledDrafts, StatusNote,
21 ThreadActivity, ThreadMembers, TranslateRequest, Translation, TranslationTrigger, Translator,
22 WindowDirection, WorkLocationKind,
23};
24use chrono::{DateTime, Utc};
25use graph::{
26 Channel, Chat, CloudFile, DriveEntry, DriveFolder, Graph, Member, Message, MessageExtras,
27 MessageTarget, OutgoingMention, Photo, Presence, QuickSite, Recipient, SharedFile, Tag, Team,
28 UploadDestination, UploadedFile, User,
29};
30
31use crate::error::{Error, Result};
32use crate::native::NativeChats;
33use crate::profile::PersonProfile;
34
35pub const CHATSVC_CHAT_WRITES: bool = true;
36const URGENT_ACK_EMOTION: &str = "acks";
37
38#[derive(Debug, Clone)]
39pub struct RemotePage {
40 pub items: Vec<Message>,
41 pub next_link: Option<String>,
42 pub oldest_at: Option<DateTime<Utc>>,
43}
44
45#[derive(Debug, Clone)]
46pub struct DeltaPage {
47 pub items: Vec<Message>,
48 pub next_link: Option<String>,
49 pub delta_link: Option<String>,
50}
51
52#[derive(Debug, Clone)]
53pub struct ChatsPage {
54 pub items: Vec<Chat>,
55 pub next_link: Option<String>,
56}
57
58#[allow(async_fn_in_trait)]
59pub trait Remote {
60 async fn me(&self) -> Result<User>;
61 async fn chats_page(&self, top: usize) -> Result<ChatsPage>;
62 async fn chats_at(&self, next_link: &str) -> Result<ChatsPage>;
63 async fn chat(&self, chat_id: &str) -> Result<Chat>;
64 async fn chat_members(&self, chat_id: &str) -> Result<Vec<Member>>;
65 async fn joined_teams(&self) -> Result<Vec<Team>>;
66 async fn channels_for_teams(&self, team_ids: &[String]) -> Result<Vec<Result<Vec<Channel>>>>;
67 async fn chat_messages(
68 &self,
69 chat_id: &str,
70 before: Option<DateTime<Utc>>,
71 top: usize,
72 ) -> Result<RemotePage>;
73 async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result<Message>;
74 async fn channel_messages(
75 &self,
76 team_id: &str,
77 channel_id: &str,
78 top: usize,
79 ) -> Result<RemotePage>;
80 async fn channel_messages_at(&self, next_link: &str) -> Result<RemotePage>;
81 async fn channel_message(
82 &self,
83 team_id: &str,
84 channel_id: &str,
85 message_id: &str,
86 ) -> Result<Message>;
87 async fn channel_reply(
88 &self,
89 team_id: &str,
90 channel_id: &str,
91 message_id: &str,
92 reply_id: &str,
93 ) -> Result<Message>;
94 async fn channel_delta(
95 &self,
96 team_id: &str,
97 channel_id: &str,
98 modified_after: Option<DateTime<Utc>>,
99 ) -> Result<DeltaPage>;
100 async fn channel_delta_at(&self, link: &str) -> Result<DeltaPage>;
101 async fn channel_replies(
102 &self,
103 team_id: &str,
104 channel_id: &str,
105 root_ids: &[String],
106 ) -> Result<Vec<Result<Vec<Message>>>>;
107 async fn send_channel_message(
108 &self,
109 team_id: &str,
110 channel_id: &str,
111 html: &str,
112 subject: Option<&str>,
113 mentions: &[OutgoingMention],
114 extras: &MessageExtras,
115 ) -> Result<Message>;
116 async fn reply_to_channel_message(
117 &self,
118 team_id: &str,
119 channel_id: &str,
120 message_id: &str,
121 html: &str,
122 mentions: &[OutgoingMention],
123 extras: &MessageExtras,
124 ) -> Result<Message>;
125 async fn post_chatsvc_message(
126 &self,
127 conversation: &ConversationRef,
128 html: &str,
129 display_name: &str,
130 properties: &serde_json::Value,
131 ) -> Result<String>;
132 /// Readable by the members of `conversation_id`; returns the object id and its full size url.
133 async fn upload_ams_image(
134 &self,
135 conversation_id: &str,
136 bytes: &[u8],
137 filename: &str,
138 ) -> Result<chatsvc::ams::AmsImage>;
139 async fn set_message_cards(
140 &self,
141 conversation: &ConversationRef,
142 message_id: &str,
143 cards: &str,
144 ) -> Result<()>;
145 async fn remove_chat_member(&self, chat_id: &str, user_id: &str) -> Result<()>;
146 async fn add_chat_members(
147 &self,
148 chat_id: &str,
149 user_ids: &[String],
150 share: HistoryShare,
151 ) -> Result<()>;
152 async fn chat_states(&self) -> Result<Vec<ConversationState>>;
153 async fn clear_chat_history(&self, _chat_id: &str, _cleared_until_ms: i64) -> Result<()> {
154 Err(Error::Unsupported("removing chat history"))
155 }
156 async fn set_chat_topic(&self, _chat_id: &str, _topic: &str) -> Result<()> {
157 Err(Error::Unsupported("renaming a chat"))
158 }
159 async fn set_chat_muted(&self, chat_id: &str, muted: bool) -> Result<()>;
160 async fn set_read_horizon(&self, conversation_id: &str, horizon: &str) -> Result<()>;
161 async fn set_read_bookmark(&self, _conversation_id: &str, _bookmark: &str) -> Result<()> {
162 Err(Error::Unsupported("marking the notes chat as unread"))
163 }
164 async fn set_chat_hidden_at(&self, _chat_id: &str, _hidden_at_ms: Option<i64>) -> Result<()> {
165 Err(Error::Unsupported("hiding the notes chat"))
166 }
167 async fn set_channel_notifications(
168 &self,
169 channel_id: &str,
170 notifications: store::ChannelNotifications,
171 ) -> Result<()>;
172 async fn send_typing(&self, conversation: &ConversationRef, active: bool) -> Result<()>;
173 async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()>;
174 async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()>;
175 async fn edit_message(
176 &self,
177 target: &MessageTarget,
178 html: &str,
179 mentions: &[OutgoingMention],
180 extras: &MessageExtras,
181 ) -> Result<()>;
182 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()>;
183 async fn undo_soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()>;
184 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat>;
185 async fn create_group(
186 &self,
187 my_user_id: &str,
188 user_ids: &[String],
189 topic: Option<&str>,
190 ) -> Result<Chat>;
191 async fn search_people(&self, query: &str) -> Result<Vec<User>>;
192
193 async fn team_tags(&self, _team_id: &str) -> Result<Vec<Tag>> {
194 Err(Error::Unsupported("team tags"))
195 }
196
197 async fn channel_description(&self, _team_id: &str, _channel_id: &str) -> Result<String> {
198 Err(Error::Unsupported("channel description"))
199 }
200
201 async fn channel_files_folder(&self, _team_id: &str, _channel_id: &str) -> Result<DriveFolder> {
202 Err(Error::Unsupported("a channel files folder"))
203 }
204
205 async fn channel_files_root(&self, _team_id: &str, _channel_id: &str) -> Result<DriveEntry> {
206 Err(Error::Unsupported("a channel library"))
207 }
208
209 async fn channel_tab_web_url(
210 &self,
211 _team_id: &str,
212 _channel_id: &str,
213 _tab_id: &str,
214 ) -> Result<Option<String>> {
215 Err(Error::Unsupported("a channel tab link"))
216 }
217
218 async fn channel_member_count(&self, _team_id: &str, _channel_id: &str) -> Result<usize> {
219 Err(Error::Unsupported("a channel member count"))
220 }
221
222 async fn channel_members(
223 &self,
224 _team_id: &str,
225 _channel_id: &str,
226 ) -> Result<Vec<graph::Member>> {
227 Err(Error::Unsupported("a channel member list"))
228 }
229
230 async fn list_children(&self, _folder: &DriveFolder) -> Result<Vec<DriveEntry>> {
231 Err(Error::Unsupported("a folder listing"))
232 }
233
234 async fn create_folder(&self, _parent: &DriveFolder, _name: &str) -> Result<DriveEntry> {
235 Err(Error::Unsupported("creating a folder"))
236 }
237
238 async fn upload_file(
239 &self,
240 _destination: &UploadDestination,
241 _file_name: &str,
242 _bytes: &[u8],
243 _progress: &(dyn Fn(u8) + Send + Sync),
244 ) -> Result<UploadedFile> {
245 Err(Error::Unsupported("a file upload"))
246 }
247
248 async fn share_file(
249 &self,
250 _file: &UploadedFile,
251 _recipients: &[Recipient],
252 ) -> Result<Option<String>> {
253 Err(Error::Unsupported("sharing a file"))
254 }
255
256 async fn recent_files(&self, _top: usize) -> Result<Vec<CloudFile>> {
257 Err(Error::Unsupported("recent files"))
258 }
259
260 async fn recent_files_with_owners(
261 &self,
262 _top: usize,
263 ) -> Result<(Vec<CloudFile>, Vec<(String, String)>)> {
264 Err(Error::Unsupported("recent files"))
265 }
266
267 async fn shared_with_me(&self) -> Result<Vec<DriveEntry>> {
268 Err(Error::Unsupported("shared files"))
269 }
270
271 async fn quick_access_sites(&self) -> Result<Vec<QuickSite>> {
272 Err(Error::Unsupported("sites"))
273 }
274
275 async fn drive_root(&self, _site_id: Option<&str>) -> Result<DriveFolder> {
276 Err(Error::Unsupported("drive root"))
277 }
278
279 async fn cloud_file_reference(&self, _file: &CloudFile) -> Result<UploadedFile> {
280 Err(Error::Unsupported("cloud files"))
281 }
282
283 async fn delete_file(&self, _file: &UploadedFile) -> Result<()> {
284 Err(Error::Unsupported("deleting a file"))
285 }
286
287 async fn resolve_share(&self, _open_url: &str) -> Result<SharedFile> {
288 Err(Error::Unsupported("resolving a shared file"))
289 }
290
291 async fn resolve_drive_item(&self, _drive_id: &str, _item_id: &str) -> Result<SharedFile> {
292 Err(Error::Unsupported("resolving a drive item"))
293 }
294
295 async fn download_range(
296 &self,
297 _download_url: &str,
298 _start: u64,
299 _end: Option<u64>,
300 ) -> Result<Vec<u8>> {
301 Err(Error::Unsupported("a file download"))
302 }
303
304 async fn hosted_content(&self, _url: &str) -> Result<Photo> {
305 Err(Error::Unsupported("hosted content"))
306 }
307
308 async fn external_image(&self, _url: &str) -> Result<Photo> {
309 Err(Error::Unsupported("external images"))
310 }
311
312 async fn user_photos(&self, _user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
313 Err(Error::Unsupported("profile photos"))
314 }
315
316 async fn team_photos(&self, _team_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
317 Err(Error::Unsupported("team pictures"))
318 }
319
320 async fn presences(&self, _user_ids: &[String]) -> Result<Vec<Presence>> {
321 Err(Error::Unsupported("presence"))
322 }
323
324 async fn person_profile(&self, _user_id: &str) -> Result<PersonProfile> {
325 Err(Error::Unsupported("profile cards"))
326 }
327
328 async fn bot_profiles(&self, _bot_ids: &[String]) -> Result<Vec<BotProfile>> {
329 Err(Error::Unsupported("bot profiles"))
330 }
331
332 async fn subscribe_presence(
333 &self,
334 _endpoint_id: &str,
335 _trouter_uri: &str,
336 _user_ids: &[String],
337 _purge: bool,
338 ) -> Result<()> {
339 Err(Error::Unsupported("presence subscriptions"))
340 }
341
342 async fn own_status(&self, _user_id: &str) -> Result<PresenceStatus> {
343 Err(Error::Unsupported("own status"))
344 }
345
346 async fn set_availability(&self, _forced: Option<&ForcedAvailability>) -> Result<()> {
347 Err(Error::Unsupported("setting availability"))
348 }
349
350 async fn set_status_note(&self, _note: Option<&StatusNote>) -> Result<()> {
351 Err(Error::Unsupported("setting the status message"))
352 }
353
354 async fn set_work_location(
355 &self,
356 _location: Option<(WorkLocationKind, DateTime<Utc>)>,
357 ) -> Result<()> {
358 Err(Error::Unsupported("setting the work location"))
359 }
360
361 async fn consumption_horizons(&self, _conversation_id: &str) -> Result<Vec<MemberHorizon>> {
362 Err(Error::Unsupported("read receipts"))
363 }
364
365 async fn forward_messages(
366 &self,
367 _source_conversation_id: &str,
368 _target_conversation_id: &str,
369 _message_ids: &[String],
370 _comment_html: &str,
371 ) -> Result<ForwardResult> {
372 Err(Error::Unsupported("forwarding messages"))
373 }
374
375 async fn set_saved(
376 &self,
377 _conversation_id: &str,
378 _root_id: &str,
379 _message_id: &str,
380 _saved: bool,
381 ) -> Result<()> {
382 Err(Error::Unsupported("saving messages"))
383 }
384
385 async fn list_saved(&self) -> Result<Vec<SavedMessage>> {
386 Err(Error::Unsupported("saved messages"))
387 }
388
389 async fn set_post_followed(
390 &self,
391 _user_id: &str,
392 _channel_id: &str,
393 _root_id: &str,
394 _followed: bool,
395 ) -> Result<Option<String>> {
396 Err(Error::Unsupported("following posts"))
397 }
398
399 async fn post_follow_states(
400 &self,
401 _channel_id: &str,
402 _root_ids: &[String],
403 ) -> Result<HashMap<String, PostFollowState>> {
404 Err(Error::Unsupported("post follow states"))
405 }
406
407 async fn list_call_logs(&self) -> Result<Vec<CallLogEntry>> {
408 Err(Error::Unsupported("call history"))
409 }
410
411 async fn chat_message_window(
412 &self,
413 _chat_id: &str,
414 _anchor_id: &str,
415 _direction: WindowDirection,
416 _page_size: usize,
417 ) -> Result<MessageWindow> {
418 Err(Error::Unsupported("message windows"))
419 }
420
421 async fn chat_thread_activities(&self, _chat_id: &str) -> Result<Vec<ThreadActivity>> {
422 Ok(Vec::new())
423 }
424
425 async fn chat_messages_by_id(
426 &self,
427 _chat_id: &str,
428 _message_ids: &[String],
429 ) -> Result<Vec<Result<Message>>> {
430 Err(Error::Unsupported("batched message reads"))
431 }
432
433 async fn chat_pins(&self, _chat_id: &str) -> Result<Vec<PinnedMessage>> {
434 Err(Error::Unsupported("pinned messages"))
435 }
436
437 async fn pin_message(&self, _chat_id: &str, _message_id: &str) -> Result<()> {
438 Err(Error::Unsupported("pinning messages"))
439 }
440
441 async fn unpin_message(
442 &self,
443 _chat_id: &str,
444 _message_id: &str,
445 _parent_id: Option<&str>,
446 ) -> Result<()> {
447 Err(Error::Unsupported("pinning messages"))
448 }
449
450 async fn channel_pinned_posts(
451 &self,
452 _team_thread_id: &str,
453 _channel_id: &str,
454 ) -> Result<Vec<PinnedMessage>> {
455 Err(Error::Unsupported("pinned channel posts"))
456 }
457
458 async fn set_post_pinned(
459 &self,
460 _team_thread_id: &str,
461 _channel_id: &str,
462 _post_id: &str,
463 _pinned: bool,
464 ) -> Result<()> {
465 Err(Error::Unsupported("pinning channel posts"))
466 }
467
468 async fn activity_feed(&self) -> Result<ActivitySnapshot> {
469 Err(Error::Unsupported("the activity feed"))
470 }
471
472 async fn mark_activity_read(&self, _message_id: &str) -> Result<()> {
473 Err(Error::Unsupported("marking activity as read"))
474 }
475
476 async fn mark_all_activity_read(&self, _now_ms: i64) -> Result<()> {
477 Err(Error::Unsupported("marking all activity as read"))
478 }
479
480 async fn app_consumption_horizon(&self) -> Result<AppConsumptionHorizon> {
481 Ok(AppConsumptionHorizon::default())
482 }
483
484 async fn mark_all_channels_read(&self, _now_ms: i64) -> Result<AppConsumptionHorizon> {
485 Err(Error::Unsupported("marking all channels as read"))
486 }
487
488 async fn chat_apps(&self, _chat_id: &str) -> Result<Vec<ChatApp>> {
489 Err(Error::Unsupported("chat apps"))
490 }
491
492 async fn invoke_card(&self, _invoke: InvokeRequest) -> Result<InvokeResponse> {
493 Err(Error::Unsupported("card actions"))
494 }
495
496 async fn message_links(
497 &self,
498 _conversation: &ConversationRef,
499 _page_size: usize,
500 ) -> Result<Vec<MessageLinks>> {
501 Err(Error::Unsupported("link previews"))
502 }
503
504 async fn message_emotions(
505 &self,
506 _conversation: &ConversationRef,
507 _newest_message_id: &str,
508 _page_size: usize,
509 ) -> Result<Vec<MessageEmotions>> {
510 Err(Error::Unsupported("chat service reactions"))
511 }
512
513 async fn set_message_links(&self, _target: &MessageTarget, _links_json: &str) -> Result<()> {
514 Err(Error::Unsupported("link previews"))
515 }
516
517 async fn link_info(&self, _url: &str) -> Result<LinkInfo> {
518 Err(Error::Unsupported("link previews"))
519 }
520
521 async fn message_languages(
522 &self,
523 _conversation: &ConversationRef,
524 _page_size: usize,
525 ) -> Result<Vec<MessageLanguage>> {
526 Err(Error::Unsupported("message languages"))
527 }
528
529 async fn ask_copilot(&self, _ask: &CopilotAsk) -> Result<String> {
530 Err(Error::Unsupported("Copilot"))
531 }
532
533 async fn rewrite_draft(&self, _rewrite: &ComposeRewrite) -> Result<String> {
534 Err(Error::Unsupported("Copilot"))
535 }
536
537 async fn translate_chat_messages(
538 &self,
539 _chat_id: &str,
540 _to_language: &str,
541 _messages: &[TranslateRequest],
542 _trigger: &TranslationTrigger,
543 ) -> Result<Vec<Translation>> {
544 Err(Error::Unsupported("translation"))
545 }
546
547 async fn translate_channel_messages(
548 &self,
549 _team_id: &str,
550 _channel_id: &str,
551 _root_id: &str,
552 _to_language: &str,
553 _messages: &[TranslateRequest],
554 _trigger: &TranslationTrigger,
555 ) -> Result<Vec<Translation>> {
556 Err(Error::Unsupported("translation"))
557 }
558
559 async fn translation_languages(&self, _locale: &str) -> Result<Vec<Language>> {
560 Err(Error::Unsupported("translation"))
561 }
562
563 async fn language_settings(&self) -> Result<LanguageSettings> {
564 Err(Error::Unsupported("translation settings"))
565 }
566
567 async fn patch_language_settings(&self, _patch: serde_json::Value) -> Result<()> {
568 Err(Error::Unsupported("translation settings"))
569 }
570
571 async fn recording_policy(&self) -> Result<RecordingPolicy> {
572 Err(Error::Unsupported("recording policy"))
573 }
574
575 async fn allow_priority_messages(&self) -> Result<bool> {
576 Err(Error::Unsupported("messaging policy"))
577 }
578
579 async fn ack_urgent_message(&self, _chat_id: &str, _message_id: &str) -> Result<()> {
580 Err(Error::Unsupported("urgent message acknowledgement"))
581 }
582
583 async fn sound_settings(&self) -> Result<SoundSettings> {
584 Err(Error::Unsupported("sound settings"))
585 }
586
587 async fn chat_settings(&self) -> Result<ChatSettingsDocument> {
588 Err(Error::Unsupported("chat settings"))
589 }
590
591 async fn save_chat_setting(&self, _change: ChatSettingChange) -> Result<ChatSettingsDocument> {
592 Err(Error::Unsupported("chat settings"))
593 }
594
595 async fn set_chat_notifications(
596 &self,
597 _chat_id: &str,
598 _current_settings: Option<&str>,
599 _mode: ChatNotificationMode,
600 _desktop: DesktopNotifications,
601 ) -> Result<ChatNotificationWrite> {
602 Err(Error::Unsupported("chat notifications"))
603 }
604
605 async fn save_sound_settings(
606 &self,
607 _user_mri: &str,
608 _previous: &SoundSettings,
609 _updated: &SoundSettings,
610 ) -> Result<()> {
611 Err(Error::Unsupported("sound settings"))
612 }
613
614 async fn search_gifs(&self, _query: &str) -> Result<Vec<Gif>> {
615 Err(Error::Unsupported("GIF search"))
616 }
617
618 async fn list_org_emojis(&self, _continuation: Option<&str>) -> Result<OrgEmojiCatalog> {
619 Err(Error::Unsupported("custom emoji catalogue"))
620 }
621
622 async fn server_search_init(
623 &self,
624 _identity: &SearchIdentity,
625 _search_session: &SearchSession,
626 ) -> Result<()> {
627 Err(Error::Unsupported("server search"))
628 }
629
630 async fn server_suggestions(
631 &self,
632 _identity: &SearchIdentity,
633 _search_session: &SearchSession,
634 _text: &str,
635 ) -> Result<Suggestions> {
636 Err(Error::Unsupported("server search"))
637 }
638
639 async fn server_query(
640 &self,
641 _identity: &SearchIdentity,
642 _search_session: &SearchSession,
643 _request: &QueryRequest,
644 ) -> Result<QueryResults> {
645 Err(Error::Unsupported("server search"))
646 }
647
648 async fn create_scheduled(
649 &self,
650 _inner_thread_id: &str,
651 _html: &str,
652 _send_at: DateTime<Utc>,
653 _display_name: &str,
654 ) -> Result<ScheduledDraft> {
655 Err(Error::Unsupported("schedule send"))
656 }
657
658 async fn scheduled_drafts(&self) -> Result<Vec<ScheduledDraft>> {
659 Err(Error::Unsupported("schedule send"))
660 }
661
662 async fn update_scheduled(
663 &self,
664 _draft: &ScheduledDraft,
665 _html: &str,
666 _display_name: &str,
667 ) -> Result<ScheduledDraft> {
668 Err(Error::Unsupported("schedule send"))
669 }
670
671 async fn cancel_scheduled(&self, _draft_id: &str) -> Result<()> {
672 Err(Error::Unsupported("schedule send"))
673 }
674}
675
676impl Remote for Graph {
677 async fn me(&self) -> Result<User> {
678 Ok(Graph::me(self).await?)
679 }
680
681 async fn chats_page(&self, top: usize) -> Result<ChatsPage> {
682 NativeChats::new(self.session()).first_page(top).await
683 }
684
685 async fn chats_at(&self, next_link: &str) -> Result<ChatsPage> {
686 NativeChats::new(self.session()).page(next_link).await
687 }
688
689 async fn chat(&self, chat_id: &str) -> Result<Chat> {
690 NativeChats::new(self.session()).chat(chat_id).await
691 }
692
693 async fn chat_members(&self, chat_id: &str) -> Result<Vec<Member>> {
694 NativeChats::new(self.session()).members(chat_id).await
695 }
696
697 async fn joined_teams(&self) -> Result<Vec<Team>> {
698 Ok(Graph::joined_teams(self).await?)
699 }
700
701 async fn channels_for_teams(&self, team_ids: &[String]) -> Result<Vec<Result<Vec<Channel>>>> {
702 let listings = Graph::channels_for_teams(self, team_ids).await?;
703 Ok(listings
704 .into_iter()
705 .map(|listing| listing.map_err(Into::into))
706 .collect())
707 }
708
709 async fn chat_messages(
710 &self,
711 chat_id: &str,
712 before: Option<DateTime<Utc>>,
713 top: usize,
714 ) -> Result<RemotePage> {
715 NativeChats::new(self.session())
716 .messages(chat_id, before, top)
717 .await
718 }
719
720 async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result<Message> {
721 NativeChats::new(self.session())
722 .message(chat_id, message_id)
723 .await
724 }
725
726 async fn channel_messages(
727 &self,
728 team_id: &str,
729 channel_id: &str,
730 top: usize,
731 ) -> Result<RemotePage> {
732 Ok(Graph::channel_messages(self, team_id, channel_id, top)
733 .await?
734 .into())
735 }
736
737 async fn channel_messages_at(&self, next_link: &str) -> Result<RemotePage> {
738 Ok(Graph::channel_messages_at(self, next_link).await?.into())
739 }
740
741 async fn channel_message(
742 &self,
743 team_id: &str,
744 channel_id: &str,
745 message_id: &str,
746 ) -> Result<Message> {
747 Ok(Graph::channel_message(self, team_id, channel_id, message_id).await?)
748 }
749
750 async fn channel_reply(
751 &self,
752 team_id: &str,
753 channel_id: &str,
754 message_id: &str,
755 reply_id: &str,
756 ) -> Result<Message> {
757 Ok(Graph::channel_reply(self, team_id, channel_id, message_id, reply_id).await?)
758 }
759
760 async fn channel_delta(
761 &self,
762 team_id: &str,
763 channel_id: &str,
764 modified_after: Option<DateTime<Utc>>,
765 ) -> Result<DeltaPage> {
766 Ok(
767 Graph::channel_delta(self, team_id, channel_id, modified_after)
768 .await?
769 .into(),
770 )
771 }
772
773 async fn channel_delta_at(&self, link: &str) -> Result<DeltaPage> {
774 Ok(Graph::channel_delta_at(self, link).await?.into())
775 }
776
777 async fn channel_replies(
778 &self,
779 team_id: &str,
780 channel_id: &str,
781 root_ids: &[String],
782 ) -> Result<Vec<Result<Vec<Message>>>> {
783 let listings = Graph::channel_replies(self, team_id, channel_id, root_ids).await?;
784 Ok(listings
785 .into_iter()
786 .map(|listing| listing.map_err(Into::into))
787 .collect())
788 }
789
790 async fn send_channel_message(
791 &self,
792 team_id: &str,
793 channel_id: &str,
794 html: &str,
795 subject: Option<&str>,
796 mentions: &[OutgoingMention],
797 extras: &MessageExtras,
798 ) -> Result<Message> {
799 Ok(
800 Graph::send_channel_message(self, team_id, channel_id, html, subject, mentions, extras)
801 .await?,
802 )
803 }
804
805 async fn reply_to_channel_message(
806 &self,
807 team_id: &str,
808 channel_id: &str,
809 message_id: &str,
810 html: &str,
811 mentions: &[OutgoingMention],
812 extras: &MessageExtras,
813 ) -> Result<Message> {
814 Ok(Graph::reply_to_channel_message(
815 self, team_id, channel_id, message_id, html, mentions, extras,
816 )
817 .await?)
818 }
819
820 async fn post_chatsvc_message(
821 &self,
822 conversation: &ConversationRef,
823 html: &str,
824 display_name: &str,
825 properties: &serde_json::Value,
826 ) -> Result<String> {
827 Ok(Messages::new(self.session())
828 .post_message(conversation, html, display_name, properties)
829 .await?)
830 }
831
832 async fn upload_ams_image(
833 &self,
834 conversation_id: &str,
835 bytes: &[u8],
836 filename: &str,
837 ) -> Result<chatsvc::ams::AmsImage> {
838 Ok(chatsvc::ams::Ams::new(self.session())
839 .upload_image(conversation_id, bytes, filename)
840 .await?)
841 }
842
843 async fn set_message_cards(
844 &self,
845 conversation: &ConversationRef,
846 message_id: &str,
847 cards: &str,
848 ) -> Result<()> {
849 Ok(Messages::new(self.session())
850 .set_cards(conversation, message_id, cards)
851 .await?)
852 }
853
854 async fn remove_chat_member(&self, chat_id: &str, user_id: &str) -> Result<()> {
855 Ok(ThreadMembers::new(self.session())
856 .remove(chat_id, user_id)
857 .await?)
858 }
859
860 async fn add_chat_members(
861 &self,
862 chat_id: &str,
863 user_ids: &[String],
864 share: HistoryShare,
865 ) -> Result<()> {
866 Ok(ThreadMembers::new(self.session())
867 .add(chat_id, user_ids, share, Utc::now().timestamp_millis())
868 .await?)
869 }
870
871 async fn chat_states(&self) -> Result<Vec<ConversationState>> {
872 Ok(Conversations::new(self.session())
873 .conversation_states()
874 .await?)
875 }
876
877 async fn clear_chat_history(&self, chat_id: &str, cleared_until_ms: i64) -> Result<()> {
878 Ok(Conversations::new(self.session())
879 .clear_history(chat_id, cleared_until_ms)
880 .await?)
881 }
882
883 async fn set_chat_topic(&self, chat_id: &str, topic: &str) -> Result<()> {
884 Ok(ThreadMembers::new(self.session())
885 .set_topic(chat_id, topic)
886 .await?)
887 }
888
889 async fn set_chat_muted(&self, chat_id: &str, muted: bool) -> Result<()> {
890 Ok(Conversations::new(self.session())
891 .set_alerts(chat_id, !muted)
892 .await?)
893 }
894
895 async fn set_read_horizon(&self, conversation_id: &str, horizon: &str) -> Result<()> {
896 Ok(Conversations::new(self.session())
897 .set_read_horizon(conversation_id, horizon)
898 .await?)
899 }
900
901 async fn set_read_bookmark(&self, conversation_id: &str, bookmark: &str) -> Result<()> {
902 Ok(Conversations::new(self.session())
903 .set_read_bookmark(conversation_id, bookmark)
904 .await?)
905 }
906
907 async fn set_chat_hidden_at(&self, chat_id: &str, hidden_at_ms: Option<i64>) -> Result<()> {
908 Ok(Conversations::new(self.session())
909 .set_hidden_at(chat_id, hidden_at_ms)
910 .await?)
911 }
912
913 async fn set_channel_notifications(
914 &self,
915 channel_id: &str,
916 notifications: store::ChannelNotifications,
917 ) -> Result<()> {
918 Ok(Conversations::new(self.session())
919 .set_channel_notifications(
920 channel_id,
921 crate::folders::chatsvc_notifications(notifications),
922 )
923 .await?)
924 }
925
926 async fn send_typing(&self, conversation: &ConversationRef, active: bool) -> Result<()> {
927 Ok(Messages::new(self.session())
928 .send_typing(conversation, active)
929 .await?)
930 }
931
932 /// Teams `ackUrgentMessage`: the emotion `acks` on the message stops the urgent repeat notifications.
933 async fn ack_urgent_message(&self, chat_id: &str, message_id: &str) -> Result<()> {
934 Ok(Messages::new(self.session())
935 .set_emotion(
936 &ConversationRef::chat(chat_id),
937 message_id,
938 URGENT_ACK_EMOTION,
939 )
940 .await?)
941 }
942
943 async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
944 if let Some(key) = chatsvc::reaction_key(reaction_type) {
945 return Ok(Messages::new(self.session())
946 .set_emotion(&conversation_ref(target), target.message_id(), &key)
947 .await?);
948 }
949 if matches!(target, MessageTarget::Chat { .. }) {
950 return Err(Error::Unsupported("this reaction in a chat"));
951 }
952 Ok(Graph::set_reaction(self, target, reaction_type).await?)
953 }
954
955 async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
956 let keys = chatsvc::reaction_keys(reaction_type);
957 if !keys.is_empty() {
958 let keys: Vec<&str> = keys.iter().map(String::as_str).collect();
959 return Ok(Messages::new(self.session())
960 .unset_emotions(&conversation_ref(target), target.message_id(), &keys)
961 .await?);
962 }
963 if matches!(target, MessageTarget::Chat { .. }) {
964 return Err(Error::Unsupported("this reaction in a chat"));
965 }
966 Ok(Graph::unset_reaction(self, target, reaction_type).await?)
967 }
968
969 async fn edit_message(
970 &self,
971 target: &MessageTarget,
972 html: &str,
973 mentions: &[OutgoingMention],
974 extras: &MessageExtras,
975 ) -> Result<()> {
976 match chatsvc_conversation(target) {
977 Some(conversation) if mentions.is_empty() => {
978 let html = format!("{}{html}{}", extras.kept_html(), extras.files_html());
979 Ok(Messages::new(self.session())
980 .edit_message(&conversation, target.message_id(), &html)
981 .await?)
982 }
983 Some(conversation) => {
984 let Some(entries) = crate::teams_quote::mention_entries(mentions) else {
985 if matches!(conversation, ConversationRef::Chat { .. }) {
986 return Err(Error::Unsupported("editing in a team or tag mention"));
987 }
988 return Ok(Graph::edit_message(self, target, html, mentions, extras).await?);
989 };
990 let html = format!(
991 "{}{}{}",
992 extras.kept_html(),
993 crate::teams_quote::chatsvc_mention_html(html, mentions),
994 extras.files_html()
995 );
996 Ok(Messages::new(self.session())
997 .edit_message_with_mentions(
998 &conversation,
999 target.message_id(),
1000 &html,
1001 Some(&serde_json::Value::Array(entries).to_string()),
1002 )
1003 .await?)
1004 }
1005 None => Ok(Graph::edit_message(self, target, html, mentions, extras).await?),
1006 }
1007 }
1008
1009 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
1010 match chatsvc_conversation(target) {
1011 Some(conversation) => Ok(Messages::new(self.session())
1012 .soft_delete_message(&conversation, target.message_id())
1013 .await?),
1014 None => Ok(Graph::soft_delete_message(self, user_id, target).await?),
1015 }
1016 }
1017
1018 async fn undo_soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
1019 match chatsvc_conversation(target) {
1020 Some(conversation) => Ok(Messages::new(self.session())
1021 .undelete_message(&conversation, target.message_id())
1022 .await?),
1023 None => Ok(Graph::undo_soft_delete_message(self, user_id, target).await?),
1024 }
1025 }
1026
1027 /// Teams names a 1:1 chat after its two members; the chat service creates it with the first message.
1028 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat> {
1029 let mut pair = [my_user_id.to_owned(), user_id.to_owned()];
1030 pair.sort();
1031 let sorted_id = format!("19:{}_{}@unq.gbl.spaces", pair[0], pair[1]);
1032 let reversed_id = format!("19:{}_{}@unq.gbl.spaces", pair[1], pair[0]);
1033 let listing = chatsvc::ChatListing::new(self.session());
1034 let chat_id = if listing.conversation(&sorted_id).await.is_err()
1035 && listing.conversation(&reversed_id).await.is_ok()
1036 {
1037 reversed_id
1038 } else {
1039 sorted_id
1040 };
1041 NativeChats::new(self.session())
1042 .created_chat(&chat_id, "oneOnOne", None, &pair)
1043 .await
1044 }
1045
1046 async fn create_group(
1047 &self,
1048 my_user_id: &str,
1049 user_ids: &[String],
1050 topic: Option<&str>,
1051 ) -> Result<Chat> {
1052 let mut members = vec![my_user_id.to_owned()];
1053 members.extend(
1054 user_ids
1055 .iter()
1056 .filter(|user_id| *user_id != my_user_id)
1057 .cloned(),
1058 );
1059 let chat_id = ThreadMembers::new(self.session())
1060 .create_group(&members, topic)
1061 .await?;
1062 NativeChats::new(self.session())
1063 .created_chat(&chat_id, "group", topic, &members)
1064 .await
1065 }
1066
1067 async fn search_people(&self, query: &str) -> Result<Vec<User>> {
1068 let email = crate::native::is_email_address(query);
1069 let directory = Graph::search_people(self, query).await;
1070 let (mut users, directory_error) = match directory {
1071 Ok(users) => (users, None),
1072 Err(error) if email && directory_address_unknown(&error) => (Vec::new(), None),
1073 Err(error) if email => (Vec::new(), Some(error)),
1074 Err(error) => return Err(error.into()),
1075 };
1076 let known = users.iter().any(|user| {
1077 crate::native::user_has_address(
1078 user.mail.as_deref(),
1079 user.user_principal_name.as_deref(),
1080 query,
1081 )
1082 });
1083 if email && !known {
1084 match chatsvc::UserProfiles::new(self.session())
1085 .search_external(query)
1086 .await
1087 {
1088 Ok(external) => {
1089 let found = crate::native::remember_external(external);
1090 if let Some(error) = directory_error {
1091 let matched: Vec<User> = found
1092 .into_iter()
1093 .filter(|user| {
1094 crate::native::user_has_address(
1095 user.mail.as_deref(),
1096 user.user_principal_name.as_deref(),
1097 query,
1098 )
1099 })
1100 .collect();
1101 if matched.is_empty() {
1102 return Err(error.into());
1103 }
1104 users.extend(matched);
1105 } else {
1106 users.extend(found);
1107 }
1108 }
1109 Err(_) => {
1110 if let Some(error) = directory_error {
1111 return Err(error.into());
1112 }
1113 }
1114 }
1115 }
1116 Ok(users)
1117 }
1118
1119 async fn team_tags(&self, team_id: &str) -> Result<Vec<Tag>> {
1120 Ok(Graph::team_tags(self, team_id).await?)
1121 }
1122
1123 async fn channel_description(&self, team_id: &str, channel_id: &str) -> Result<String> {
1124 Ok(Graph::channel_description(self, team_id, channel_id).await?)
1125 }
1126
1127 async fn channel_files_folder(&self, team_id: &str, channel_id: &str) -> Result<DriveFolder> {
1128 Ok(Graph::channel_files_folder(self, team_id, channel_id).await?)
1129 }
1130
1131 async fn channel_files_root(&self, team_id: &str, channel_id: &str) -> Result<DriveEntry> {
1132 Ok(Graph::channel_files_root(self, team_id, channel_id).await?)
1133 }
1134
1135 async fn channel_tab_web_url(
1136 &self,
1137 team_id: &str,
1138 channel_id: &str,
1139 tab_id: &str,
1140 ) -> Result<Option<String>> {
1141 Ok(Graph::channel_tab_web_url(self, team_id, channel_id, tab_id).await?)
1142 }
1143
1144 async fn channel_member_count(&self, team_id: &str, channel_id: &str) -> Result<usize> {
1145 Ok(Graph::channel_member_count(self, team_id, channel_id).await?)
1146 }
1147
1148 async fn channel_members(&self, team_id: &str, channel_id: &str) -> Result<Vec<graph::Member>> {
1149 Ok(Graph::channel_member_list(self, team_id, channel_id).await?)
1150 }
1151
1152 async fn list_children(&self, folder: &DriveFolder) -> Result<Vec<DriveEntry>> {
1153 Ok(Graph::list_children(self, folder).await?)
1154 }
1155
1156 async fn create_folder(&self, parent: &DriveFolder, name: &str) -> Result<DriveEntry> {
1157 Ok(Graph::create_folder(self, parent, name).await?)
1158 }
1159
1160 async fn upload_file(
1161 &self,
1162 destination: &UploadDestination,
1163 file_name: &str,
1164 bytes: &[u8],
1165 progress: &(dyn Fn(u8) + Send + Sync),
1166 ) -> Result<UploadedFile> {
1167 Ok(Graph::upload_file(self, destination, file_name, bytes, progress).await?)
1168 }
1169
1170 async fn share_file(
1171 &self,
1172 file: &UploadedFile,
1173 recipients: &[Recipient],
1174 ) -> Result<Option<String>> {
1175 Ok(Graph::share_item(self, file, recipients).await?)
1176 }
1177
1178 async fn recent_files(&self, top: usize) -> Result<Vec<CloudFile>> {
1179 Ok(Graph::recent_files(self, top).await?)
1180 }
1181
1182 async fn recent_files_with_owners(
1183 &self,
1184 top: usize,
1185 ) -> Result<(Vec<CloudFile>, Vec<(String, String)>)> {
1186 Ok(Graph::recent_files_with_owners(self, top).await?)
1187 }
1188
1189 async fn shared_with_me(&self) -> Result<Vec<DriveEntry>> {
1190 Ok(Graph::shared_with_me(self).await?)
1191 }
1192
1193 async fn quick_access_sites(&self) -> Result<Vec<QuickSite>> {
1194 Ok(Graph::quick_access_sites(self).await?)
1195 }
1196
1197 async fn drive_root(&self, site_id: Option<&str>) -> Result<DriveFolder> {
1198 Ok(Graph::drive_root(self, site_id).await?)
1199 }
1200
1201 async fn cloud_file_reference(&self, file: &CloudFile) -> Result<UploadedFile> {
1202 Ok(Graph::cloud_file_reference(self, file).await?)
1203 }
1204
1205 async fn delete_file(&self, file: &UploadedFile) -> Result<()> {
1206 Ok(Graph::delete_uploaded(self, file).await?)
1207 }
1208
1209 async fn resolve_share(&self, open_url: &str) -> Result<SharedFile> {
1210 Ok(Graph::resolve_share(self, open_url).await?)
1211 }
1212
1213 async fn resolve_drive_item(&self, drive_id: &str, item_id: &str) -> Result<SharedFile> {
1214 Ok(Graph::resolve_drive_item(self, drive_id, item_id).await?)
1215 }
1216
1217 async fn download_range(
1218 &self,
1219 download_url: &str,
1220 start: u64,
1221 end: Option<u64>,
1222 ) -> Result<Vec<u8>> {
1223 Ok(Graph::download_range(self, download_url, start, end).await?)
1224 }
1225
1226 async fn hosted_content(&self, url: &str) -> Result<Photo> {
1227 let ams_url = chatsvc::hosted_content_image_url(url);
1228 let url = ams_url.as_deref().unwrap_or(url);
1229 let image = if chatsvc::is_ams_image_url(url) {
1230 chatsvc::ams::Ams::new(self.session())
1231 .download_image(url)
1232 .await?
1233 } else if chatsvc::is_link_image_url(url) {
1234 Messages::new(self.session()).link_image(url).await?
1235 } else {
1236 return Ok(Graph::download_hosted_content(self, url).await?);
1237 };
1238 Ok(Photo {
1239 bytes: image.bytes,
1240 content_type: image.content_type,
1241 })
1242 }
1243
1244 async fn external_image(&self, url: &str) -> Result<Photo> {
1245 Ok(Graph::download_external_image(self, url).await?)
1246 }
1247
1248 async fn user_photos(&self, user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
1249 let photos = Graph::user_photos(self, user_ids).await?;
1250 Ok(photos
1251 .into_iter()
1252 .map(|photo| photo.map_err(Into::into))
1253 .collect())
1254 }
1255
1256 async fn team_photos(&self, team_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
1257 let photos = Graph::team_photos(self, team_ids).await?;
1258 Ok(photos
1259 .into_iter()
1260 .map(|photo| photo.map_err(Into::into))
1261 .collect())
1262 }
1263
1264 async fn presences(&self, user_ids: &[String]) -> Result<Vec<Presence>> {
1265 let found = PresenceLookup::new(self.session())
1266 .presences(user_ids)
1267 .await?;
1268 Ok(found
1269 .into_iter()
1270 .map(|presence| Presence {
1271 user_id: presence.user_id,
1272 availability: presence.availability,
1273 activity: presence.activity,
1274 })
1275 .collect())
1276 }
1277
1278 async fn person_profile(&self, user_id: &str) -> Result<PersonProfile> {
1279 crate::profile::load_profile(self, user_id).await
1280 }
1281
1282 async fn bot_profiles(&self, bot_ids: &[String]) -> Result<Vec<BotProfile>> {
1283 Ok(BotProfiles::new(self.session()).fetch(bot_ids).await?)
1284 }
1285
1286 async fn subscribe_presence(
1287 &self,
1288 endpoint_id: &str,
1289 trouter_uri: &str,
1290 user_ids: &[String],
1291 purge: bool,
1292 ) -> Result<()> {
1293 Ok(PresenceLookup::new(self.session())
1294 .subscribe(endpoint_id, trouter_uri, user_ids, purge)
1295 .await?)
1296 }
1297
1298 async fn own_status(&self, user_id: &str) -> Result<PresenceStatus> {
1299 Ok(PresenceService::new(self.session()).status(user_id).await?)
1300 }
1301
1302 async fn set_availability(&self, forced: Option<&ForcedAvailability>) -> Result<()> {
1303 Ok(PresenceService::new(self.session())
1304 .set_availability(forced)
1305 .await?)
1306 }
1307
1308 async fn set_status_note(&self, note: Option<&StatusNote>) -> Result<()> {
1309 Ok(PresenceService::new(self.session()).set_note(note).await?)
1310 }
1311
1312 async fn set_work_location(
1313 &self,
1314 location: Option<(WorkLocationKind, DateTime<Utc>)>,
1315 ) -> Result<()> {
1316 Ok(PresenceService::new(self.session())
1317 .set_work_location(location)
1318 .await?)
1319 }
1320
1321 async fn consumption_horizons(&self, conversation_id: &str) -> Result<Vec<MemberHorizon>> {
1322 Ok(Receipts::new(self.session())
1323 .consumption_horizons(conversation_id)
1324 .await?)
1325 }
1326
1327 async fn forward_messages(
1328 &self,
1329 source_conversation_id: &str,
1330 target_conversation_id: &str,
1331 message_ids: &[String],
1332 comment_html: &str,
1333 ) -> Result<ForwardResult> {
1334 Ok(Messages::new(self.session())
1335 .forward_messages(
1336 source_conversation_id,
1337 target_conversation_id,
1338 message_ids,
1339 comment_html,
1340 )
1341 .await?)
1342 }
1343
1344 async fn set_saved(
1345 &self,
1346 conversation_id: &str,
1347 root_id: &str,
1348 message_id: &str,
1349 saved: bool,
1350 ) -> Result<()> {
1351 Ok(Messages::new(self.session())
1352 .set_saved(conversation_id, root_id, message_id, saved)
1353 .await?)
1354 }
1355
1356 async fn list_saved(&self) -> Result<Vec<SavedMessage>> {
1357 Ok(Messages::new(self.session()).list_saved().await?)
1358 }
1359
1360 async fn set_post_followed(
1361 &self,
1362 user_id: &str,
1363 channel_id: &str,
1364 root_id: &str,
1365 followed: bool,
1366 ) -> Result<Option<String>> {
1367 Ok(Messages::new(self.session())
1368 .set_post_followed(user_id, channel_id, root_id, followed)
1369 .await?)
1370 }
1371
1372 async fn post_follow_states(
1373 &self,
1374 channel_id: &str,
1375 root_ids: &[String],
1376 ) -> Result<HashMap<String, PostFollowState>> {
1377 Ok(Messages::new(self.session())
1378 .post_follow_states(channel_id, root_ids)
1379 .await?)
1380 }
1381
1382 async fn list_call_logs(&self) -> Result<Vec<CallLogEntry>> {
1383 Ok(Messages::new(self.session()).list_call_logs().await?)
1384 }
1385
1386 async fn chat_message_window(
1387 &self,
1388 chat_id: &str,
1389 anchor_id: &str,
1390 direction: WindowDirection,
1391 page_size: usize,
1392 ) -> Result<MessageWindow> {
1393 Ok(Messages::new(self.session())
1394 .message_window(chat_id, anchor_id, direction, page_size)
1395 .await?)
1396 }
1397
1398 async fn chat_thread_activities(&self, chat_id: &str) -> Result<Vec<ThreadActivity>> {
1399 Ok(Messages::new(self.session())
1400 .thread_activities(chat_id)
1401 .await?)
1402 }
1403
1404 async fn chat_messages_by_id(
1405 &self,
1406 chat_id: &str,
1407 message_ids: &[String],
1408 ) -> Result<Vec<Result<Message>>> {
1409 let native = NativeChats::new(self.session());
1410 Ok(futures_util::future::join_all(
1411 message_ids
1412 .iter()
1413 .map(|message_id| native.message(chat_id, message_id)),
1414 )
1415 .await)
1416 }
1417
1418 async fn chat_pins(&self, chat_id: &str) -> Result<Vec<PinnedMessage>> {
1419 Ok(Pins::new(self.session()).chat_pins(chat_id).await?)
1420 }
1421
1422 async fn pin_message(&self, chat_id: &str, message_id: &str) -> Result<()> {
1423 Ok(Pins::new(self.session())
1424 .pin_message(chat_id, message_id)
1425 .await?)
1426 }
1427
1428 async fn unpin_message(
1429 &self,
1430 chat_id: &str,
1431 message_id: &str,
1432 parent_id: Option<&str>,
1433 ) -> Result<()> {
1434 Ok(Pins::new(self.session())
1435 .unpin_message(chat_id, message_id, parent_id)
1436 .await?)
1437 }
1438
1439 async fn channel_pinned_posts(
1440 &self,
1441 team_thread_id: &str,
1442 channel_id: &str,
1443 ) -> Result<Vec<PinnedMessage>> {
1444 Ok(Pins::with_skype_token(self.session())
1445 .channel_pinned_posts(team_thread_id, channel_id)
1446 .await?)
1447 }
1448
1449 async fn set_post_pinned(
1450 &self,
1451 team_thread_id: &str,
1452 channel_id: &str,
1453 post_id: &str,
1454 pinned: bool,
1455 ) -> Result<()> {
1456 Ok(Pins::with_skype_token(self.session())
1457 .set_post_pinned(team_thread_id, channel_id, post_id, pinned)
1458 .await?)
1459 }
1460
1461 async fn activity_feed(&self) -> Result<ActivitySnapshot> {
1462 Ok(ActivityFeed::new(self.session()).snapshot().await?)
1463 }
1464
1465 async fn mark_activity_read(&self, message_id: &str) -> Result<()> {
1466 Ok(ActivityFeed::new(self.session())
1467 .mark_read(message_id)
1468 .await?)
1469 }
1470
1471 async fn mark_all_activity_read(&self, now_ms: i64) -> Result<()> {
1472 Ok(ActivityFeed::new(self.session())
1473 .mark_all_read(now_ms)
1474 .await?)
1475 }
1476
1477 async fn app_consumption_horizon(&self) -> Result<AppConsumptionHorizon> {
1478 let properties = UserSettings::new(self.session()).properties().await?;
1479 Ok(parse_app_consumption_horizon(&properties))
1480 }
1481
1482 async fn mark_all_channels_read(&self, now_ms: i64) -> Result<AppConsumptionHorizon> {
1483 Ok(UserSettings::new(self.session())
1484 .mark_all_channels_read(now_ms)
1485 .await?)
1486 }
1487
1488 async fn chat_apps(&self, chat_id: &str) -> Result<Vec<ChatApp>> {
1489 Ok(CardActions::new(self.session()).chat_apps(chat_id).await?)
1490 }
1491
1492 async fn invoke_card(&self, invoke: InvokeRequest) -> Result<InvokeResponse> {
1493 Ok(CardActions::new(self.session()).invoke(invoke).await?)
1494 }
1495
1496 async fn message_links(
1497 &self,
1498 conversation: &ConversationRef,
1499 page_size: usize,
1500 ) -> Result<Vec<MessageLinks>> {
1501 Ok(Messages::new(self.session())
1502 .list_message_links(conversation, page_size)
1503 .await?)
1504 }
1505
1506 async fn message_emotions(
1507 &self,
1508 conversation: &ConversationRef,
1509 newest_message_id: &str,
1510 page_size: usize,
1511 ) -> Result<Vec<MessageEmotions>> {
1512 Ok(Messages::new(self.session())
1513 .message_emotions(
1514 &conversation.conversation_id(),
1515 newest_message_id,
1516 page_size,
1517 )
1518 .await?)
1519 }
1520
1521 async fn set_message_links(&self, target: &MessageTarget, links_json: &str) -> Result<()> {
1522 Ok(Messages::new(self.session())
1523 .set_links(&conversation_ref(target), target.message_id(), links_json)
1524 .await?)
1525 }
1526
1527 async fn link_info(&self, url: &str) -> Result<LinkInfo> {
1528 Ok(Messages::new(self.session()).link_info(url).await?)
1529 }
1530
1531 async fn message_languages(
1532 &self,
1533 conversation: &ConversationRef,
1534 page_size: usize,
1535 ) -> Result<Vec<MessageLanguage>> {
1536 Ok(Messages::new(self.session())
1537 .list_language_stamps(conversation, page_size)
1538 .await?)
1539 }
1540
1541 async fn ask_copilot(&self, ask: &CopilotAsk) -> Result<String> {
1542 Ok(Copilot::new(self.session()).ask(ask).await?)
1543 }
1544
1545 async fn rewrite_draft(&self, rewrite: &ComposeRewrite) -> Result<String> {
1546 Ok(ComposeCopilot::new(self.session()).rewrite(rewrite).await?)
1547 }
1548
1549 async fn translate_chat_messages(
1550 &self,
1551 chat_id: &str,
1552 to_language: &str,
1553 messages: &[TranslateRequest],
1554 trigger: &TranslationTrigger,
1555 ) -> Result<Vec<Translation>> {
1556 Ok(Translator::new(self.session())
1557 .translate_chat(chat_id, to_language, messages, trigger)
1558 .await?)
1559 }
1560
1561 async fn translate_channel_messages(
1562 &self,
1563 team_id: &str,
1564 channel_id: &str,
1565 root_id: &str,
1566 to_language: &str,
1567 messages: &[TranslateRequest],
1568 trigger: &TranslationTrigger,
1569 ) -> Result<Vec<Translation>> {
1570 Ok(Translator::new(self.session())
1571 .translate_channel(team_id, channel_id, root_id, to_language, messages, trigger)
1572 .await?)
1573 }
1574
1575 async fn translation_languages(&self, locale: &str) -> Result<Vec<Language>> {
1576 Ok(Translator::new(self.session()).languages(locale).await?)
1577 }
1578
1579 async fn language_settings(&self) -> Result<LanguageSettings> {
1580 Ok(LanguageSettingsClient::new(self.session()).read().await?)
1581 }
1582
1583 async fn recording_policy(&self) -> Result<RecordingPolicy> {
1584 Ok(LanguageSettingsClient::new(self.session())
1585 .read_recording_policy()
1586 .await?)
1587 }
1588
1589 async fn allow_priority_messages(&self) -> Result<bool> {
1590 Ok(LanguageSettingsClient::new(self.session())
1591 .read_allow_priority_messages()
1592 .await?)
1593 }
1594
1595 async fn patch_language_settings(&self, patch: serde_json::Value) -> Result<()> {
1596 Ok(LanguageSettingsClient::new(self.session())
1597 .patch(patch)
1598 .await?)
1599 }
1600
1601 async fn sound_settings(&self) -> Result<SoundSettings> {
1602 Ok(SoundSettingsClient::new(self.session()).read().await?)
1603 }
1604
1605 async fn chat_settings(&self) -> Result<ChatSettingsDocument> {
1606 Ok(ChatSettingsClient::new(self.session()).read().await?)
1607 }
1608
1609 async fn save_chat_setting(&self, change: ChatSettingChange) -> Result<ChatSettingsDocument> {
1610 Ok(ChatSettingsClient::new(self.session())
1611 .write(change)
1612 .await?)
1613 }
1614
1615 async fn set_chat_notifications(
1616 &self,
1617 chat_id: &str,
1618 current_settings: Option<&str>,
1619 mode: ChatNotificationMode,
1620 desktop: DesktopNotifications,
1621 ) -> Result<ChatNotificationWrite> {
1622 Ok(Conversations::new(self.session())
1623 .set_chat_notifications(chat_id, current_settings, mode, desktop)
1624 .await?)
1625 }
1626
1627 async fn save_sound_settings(
1628 &self,
1629 user_mri: &str,
1630 previous: &SoundSettings,
1631 updated: &SoundSettings,
1632 ) -> Result<()> {
1633 Ok(SoundSettingsClient::new(self.session())
1634 .write(user_mri, previous, updated)
1635 .await?)
1636 }
1637
1638 async fn search_gifs(&self, query: &str) -> Result<Vec<Gif>> {
1639 Ok(Gifs::new(self.session()).search(query).await?)
1640 }
1641
1642 async fn list_org_emojis(&self, continuation: Option<&str>) -> Result<OrgEmojiCatalog> {
1643 let client = CustomEmojis::new(self.session());
1644 match continuation.map(str::trim).filter(|token| !token.is_empty()) {
1645 Some(token) => Ok(client.list_since(token).await?),
1646 None => Ok(client.list().await?),
1647 }
1648 }
1649
1650 async fn server_search_init(
1651 &self,
1652 identity: &SearchIdentity,
1653 search_session: &SearchSession,
1654 ) -> Result<()> {
1655 Ok(SubstrateSearch::new(self.session(), identity.clone())
1656 .init(search_session)
1657 .await?)
1658 }
1659
1660 async fn server_suggestions(
1661 &self,
1662 identity: &SearchIdentity,
1663 search_session: &SearchSession,
1664 text: &str,
1665 ) -> Result<Suggestions> {
1666 Ok(SubstrateSearch::new(self.session(), identity.clone())
1667 .suggestions(search_session, text)
1668 .await?)
1669 }
1670
1671 async fn server_query(
1672 &self,
1673 identity: &SearchIdentity,
1674 search_session: &SearchSession,
1675 request: &QueryRequest,
1676 ) -> Result<QueryResults> {
1677 Ok(SubstrateSearch::new(self.session(), identity.clone())
1678 .query(search_session, request)
1679 .await?)
1680 }
1681
1682 async fn create_scheduled(
1683 &self,
1684 inner_thread_id: &str,
1685 html: &str,
1686 send_at: DateTime<Utc>,
1687 display_name: &str,
1688 ) -> Result<ScheduledDraft> {
1689 Ok(ScheduledDrafts::new(self.session())
1690 .create(inner_thread_id, html, send_at, display_name)
1691 .await?)
1692 }
1693
1694 async fn scheduled_drafts(&self) -> Result<Vec<ScheduledDraft>> {
1695 Ok(ScheduledDrafts::new(self.session()).list().await?)
1696 }
1697
1698 async fn update_scheduled(
1699 &self,
1700 draft: &ScheduledDraft,
1701 html: &str,
1702 display_name: &str,
1703 ) -> Result<ScheduledDraft> {
1704 Ok(ScheduledDrafts::new(self.session())
1705 .update(draft, html, display_name)
1706 .await?)
1707 }
1708
1709 async fn cancel_scheduled(&self, draft_id: &str) -> Result<()> {
1710 Ok(ScheduledDrafts::new(self.session())
1711 .cancel(draft_id)
1712 .await?)
1713 }
1714}
1715
1716fn chatsvc_conversation(target: &MessageTarget) -> Option<ConversationRef> {
1717 match target {
1718 MessageTarget::Chat { .. } => CHATSVC_CHAT_WRITES.then(|| conversation_ref(target)),
1719 MessageTarget::Channel { .. } => Some(conversation_ref(target)),
1720 }
1721}
1722
1723fn conversation_ref(target: &MessageTarget) -> ConversationRef {
1724 match target {
1725 MessageTarget::Chat { chat_id, .. } => ConversationRef::chat(chat_id),
1726 MessageTarget::Channel {
1727 channel_id,
1728 root_id: None,
1729 ..
1730 } => ConversationRef::channel_root(channel_id),
1731 MessageTarget::Channel {
1732 channel_id,
1733 root_id: Some(root_id),
1734 ..
1735 } => ConversationRef::channel_reply(channel_id, root_id),
1736 }
1737}
1738
1739impl From<graph::Page<Message>> for RemotePage {
1740 fn from(page: graph::Page<Message>) -> Self {
1741 RemotePage {
1742 items: page.items,
1743 next_link: page.next_link,
1744 oldest_at: None,
1745 }
1746 }
1747}
1748
1749impl From<graph::Page<Message>> for DeltaPage {
1750 fn from(page: graph::Page<Message>) -> Self {
1751 DeltaPage {
1752 items: page.items,
1753 next_link: page.next_link,
1754 delta_link: page.delta_link,
1755 }
1756 }
1757}
1758
1759impl From<graph::Page<Chat>> for ChatsPage {
1760 fn from(page: graph::Page<Chat>) -> Self {
1761 ChatsPage {
1762 items: page.items,
1763 next_link: page.next_link,
1764 }
1765 }
1766}
1767
1768/// HTTP 400 and 404 on a mail lookup mean the address is not in the directory.
1769/// A 401, a 5xx, or a transport failure is a real search error.
1770fn directory_address_unknown(error: &graph::Error) -> bool {
1771 matches!(
1772 error,
1773 graph::Error::Session(session::Error::Api {
1774 status: 400 | 404,
1775 ..
1776 })
1777 )
1778}
1779
1780#[cfg(test)]
1781mod tests {
1782 use super::*;
1783
1784 #[test]
1785 fn only_a_missing_address_is_an_unknown_directory() {
1786 let missing = graph::Error::Session(session::Error::api(
1787 404,
1788 "https://graph.test/users",
1789 serde_json::json!({}),
1790 ));
1791 let invalid = graph::Error::Session(session::Error::api(
1792 400,
1793 "https://graph.test/users",
1794 serde_json::json!({}),
1795 ));
1796 let denied = graph::Error::Session(session::Error::api(
1797 401,
1798 "https://graph.test/users",
1799 serde_json::json!({}),
1800 ));
1801 assert!(directory_address_unknown(&missing));
1802 assert!(directory_address_unknown(&invalid));
1803 assert!(!directory_address_unknown(&denied));
1804 assert!(!directory_address_unknown(&graph::Error::NotAMember));
1805 }
1806
1807 #[test]
1808 fn channel_targets_route_to_chatsvc() {
1809 let root = MessageTarget::channel("t", "19:c@thread.tacv2", "m1", None);
1810 let reply = MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1"));
1811 assert_eq!(
1812 chatsvc_conversation(&root),
1813 Some(ConversationRef::channel_root("19:c@thread.tacv2"))
1814 );
1815 assert_eq!(
1816 chatsvc_conversation(&reply),
1817 Some(ConversationRef::channel_reply("19:c@thread.tacv2", "m1"))
1818 );
1819 }
1820
1821 #[test]
1822 fn reactions_reach_chats_regardless_of_the_feature_switch() {
1823 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
1824 assert_eq!(
1825 conversation_ref(&chat),
1826 ConversationRef::chat("19:a@thread.v2")
1827 );
1828 }
1829
1830 #[test]
1831 fn chats_follow_the_feature_switch() {
1832 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
1833 assert_eq!(chatsvc_conversation(&chat).is_some(), CHATSVC_CHAT_WRITES);
1834 }
1835}