BlueGit

ict rusty-teams

Files

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

remote.rs

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