BlueGit

ict rusty-teams

Files

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

remote.rs

remote.rs 57.3 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,
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 server_search_init(
619 &self,
620 _identity: &SearchIdentity,
621 _search_session: &SearchSession,
622 ) -> Result<()> {
623 Err(Error::Unsupported("server search"))
624 }
625
626 async fn server_suggestions(
627 &self,
628 _identity: &SearchIdentity,
629 _search_session: &SearchSession,
630 _text: &str,
631 ) -> Result<Suggestions> {
632 Err(Error::Unsupported("server search"))
633 }
634
635 async fn server_query(
636 &self,
637 _identity: &SearchIdentity,
638 _search_session: &SearchSession,
639 _request: &QueryRequest,
640 ) -> Result<QueryResults> {
641 Err(Error::Unsupported("server search"))
642 }
643
644 async fn create_scheduled(
645 &self,
646 _inner_thread_id: &str,
647 _html: &str,
648 _send_at: DateTime<Utc>,
649 _display_name: &str,
650 ) -> Result<ScheduledDraft> {
651 Err(Error::Unsupported("schedule send"))
652 }
653
654 async fn scheduled_drafts(&self) -> Result<Vec<ScheduledDraft>> {
655 Err(Error::Unsupported("schedule send"))
656 }
657
658 async fn update_scheduled(
659 &self,
660 _draft: &ScheduledDraft,
661 _html: &str,
662 _display_name: &str,
663 ) -> Result<ScheduledDraft> {
664 Err(Error::Unsupported("schedule send"))
665 }
666
667 async fn cancel_scheduled(&self, _draft_id: &str) -> Result<()> {
668 Err(Error::Unsupported("schedule send"))
669 }
670}
671
672impl Remote for Graph {
673 async fn me(&self) -> Result<User> {
674 Ok(Graph::me(self).await?)
675 }
676
677 async fn chats_page(&self, top: usize) -> Result<ChatsPage> {
678 NativeChats::new(self.session()).first_page(top).await
679 }
680
681 async fn chats_at(&self, next_link: &str) -> Result<ChatsPage> {
682 NativeChats::new(self.session()).page(next_link).await
683 }
684
685 async fn chat(&self, chat_id: &str) -> Result<Chat> {
686 NativeChats::new(self.session()).chat(chat_id).await
687 }
688
689 async fn chat_members(&self, chat_id: &str) -> Result<Vec<Member>> {
690 NativeChats::new(self.session()).members(chat_id).await
691 }
692
693 async fn joined_teams(&self) -> Result<Vec<Team>> {
694 Ok(Graph::joined_teams(self).await?)
695 }
696
697 async fn channels_for_teams(&self, team_ids: &[String]) -> Result<Vec<Result<Vec<Channel>>>> {
698 let listings = Graph::channels_for_teams(self, team_ids).await?;
699 Ok(listings
700 .into_iter()
701 .map(|listing| listing.map_err(Into::into))
702 .collect())
703 }
704
705 async fn chat_messages(
706 &self,
707 chat_id: &str,
708 before: Option<DateTime<Utc>>,
709 top: usize,
710 ) -> Result<RemotePage> {
711 NativeChats::new(self.session())
712 .messages(chat_id, before, top)
713 .await
714 }
715
716 async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result<Message> {
717 NativeChats::new(self.session())
718 .message(chat_id, message_id)
719 .await
720 }
721
722 async fn channel_messages(
723 &self,
724 team_id: &str,
725 channel_id: &str,
726 top: usize,
727 ) -> Result<RemotePage> {
728 Ok(Graph::channel_messages(self, team_id, channel_id, top)
729 .await?
730 .into())
731 }
732
733 async fn channel_messages_at(&self, next_link: &str) -> Result<RemotePage> {
734 Ok(Graph::channel_messages_at(self, next_link).await?.into())
735 }
736
737 async fn channel_message(
738 &self,
739 team_id: &str,
740 channel_id: &str,
741 message_id: &str,
742 ) -> Result<Message> {
743 Ok(Graph::channel_message(self, team_id, channel_id, message_id).await?)
744 }
745
746 async fn channel_reply(
747 &self,
748 team_id: &str,
749 channel_id: &str,
750 message_id: &str,
751 reply_id: &str,
752 ) -> Result<Message> {
753 Ok(Graph::channel_reply(self, team_id, channel_id, message_id, reply_id).await?)
754 }
755
756 async fn channel_delta(
757 &self,
758 team_id: &str,
759 channel_id: &str,
760 modified_after: Option<DateTime<Utc>>,
761 ) -> Result<DeltaPage> {
762 Ok(
763 Graph::channel_delta(self, team_id, channel_id, modified_after)
764 .await?
765 .into(),
766 )
767 }
768
769 async fn channel_delta_at(&self, link: &str) -> Result<DeltaPage> {
770 Ok(Graph::channel_delta_at(self, link).await?.into())
771 }
772
773 async fn channel_replies(
774 &self,
775 team_id: &str,
776 channel_id: &str,
777 root_ids: &[String],
778 ) -> Result<Vec<Result<Vec<Message>>>> {
779 let listings = Graph::channel_replies(self, team_id, channel_id, root_ids).await?;
780 Ok(listings
781 .into_iter()
782 .map(|listing| listing.map_err(Into::into))
783 .collect())
784 }
785
786 async fn send_channel_message(
787 &self,
788 team_id: &str,
789 channel_id: &str,
790 html: &str,
791 subject: Option<&str>,
792 mentions: &[OutgoingMention],
793 extras: &MessageExtras,
794 ) -> Result<Message> {
795 Ok(
796 Graph::send_channel_message(self, team_id, channel_id, html, subject, mentions, extras)
797 .await?,
798 )
799 }
800
801 async fn reply_to_channel_message(
802 &self,
803 team_id: &str,
804 channel_id: &str,
805 message_id: &str,
806 html: &str,
807 mentions: &[OutgoingMention],
808 extras: &MessageExtras,
809 ) -> Result<Message> {
810 Ok(Graph::reply_to_channel_message(
811 self, team_id, channel_id, message_id, html, mentions, extras,
812 )
813 .await?)
814 }
815
816 async fn post_chatsvc_message(
817 &self,
818 conversation: &ConversationRef,
819 html: &str,
820 display_name: &str,
821 properties: &serde_json::Value,
822 ) -> Result<String> {
823 Ok(Messages::new(self.session())
824 .post_message(conversation, html, display_name, properties)
825 .await?)
826 }
827
828 async fn upload_ams_image(
829 &self,
830 conversation_id: &str,
831 bytes: &[u8],
832 filename: &str,
833 ) -> Result<chatsvc::ams::AmsImage> {
834 Ok(chatsvc::ams::Ams::new(self.session())
835 .upload_image(conversation_id, bytes, filename)
836 .await?)
837 }
838
839 async fn set_message_cards(
840 &self,
841 conversation: &ConversationRef,
842 message_id: &str,
843 cards: &str,
844 ) -> Result<()> {
845 Ok(Messages::new(self.session())
846 .set_cards(conversation, message_id, cards)
847 .await?)
848 }
849
850 async fn remove_chat_member(&self, chat_id: &str, user_id: &str) -> Result<()> {
851 Ok(ThreadMembers::new(self.session())
852 .remove(chat_id, user_id)
853 .await?)
854 }
855
856 async fn add_chat_members(
857 &self,
858 chat_id: &str,
859 user_ids: &[String],
860 share: HistoryShare,
861 ) -> Result<()> {
862 Ok(ThreadMembers::new(self.session())
863 .add(chat_id, user_ids, share, Utc::now().timestamp_millis())
864 .await?)
865 }
866
867 async fn chat_states(&self) -> Result<Vec<ConversationState>> {
868 Ok(Conversations::new(self.session())
869 .conversation_states()
870 .await?)
871 }
872
873 async fn clear_chat_history(&self, chat_id: &str, cleared_until_ms: i64) -> Result<()> {
874 Ok(Conversations::new(self.session())
875 .clear_history(chat_id, cleared_until_ms)
876 .await?)
877 }
878
879 async fn set_chat_topic(&self, chat_id: &str, topic: &str) -> Result<()> {
880 Ok(ThreadMembers::new(self.session())
881 .set_topic(chat_id, topic)
882 .await?)
883 }
884
885 async fn set_chat_muted(&self, chat_id: &str, muted: bool) -> Result<()> {
886 Ok(Conversations::new(self.session())
887 .set_alerts(chat_id, !muted)
888 .await?)
889 }
890
891 async fn set_read_horizon(&self, conversation_id: &str, horizon: &str) -> Result<()> {
892 Ok(Conversations::new(self.session())
893 .set_read_horizon(conversation_id, horizon)
894 .await?)
895 }
896
897 async fn set_read_bookmark(&self, conversation_id: &str, bookmark: &str) -> Result<()> {
898 Ok(Conversations::new(self.session())
899 .set_read_bookmark(conversation_id, bookmark)
900 .await?)
901 }
902
903 async fn set_chat_hidden_at(&self, chat_id: &str, hidden_at_ms: Option<i64>) -> Result<()> {
904 Ok(Conversations::new(self.session())
905 .set_hidden_at(chat_id, hidden_at_ms)
906 .await?)
907 }
908
909 async fn set_channel_notifications(
910 &self,
911 channel_id: &str,
912 notifications: store::ChannelNotifications,
913 ) -> Result<()> {
914 Ok(Conversations::new(self.session())
915 .set_channel_notifications(
916 channel_id,
917 crate::folders::chatsvc_notifications(notifications),
918 )
919 .await?)
920 }
921
922 async fn send_typing(&self, conversation: &ConversationRef, active: bool) -> Result<()> {
923 Ok(Messages::new(self.session())
924 .send_typing(conversation, active)
925 .await?)
926 }
927
928 /// Teams `ackUrgentMessage`: the emotion `acks` on the message stops the urgent repeat notifications.
929 async fn ack_urgent_message(&self, chat_id: &str, message_id: &str) -> Result<()> {
930 Ok(Messages::new(self.session())
931 .set_emotion(
932 &ConversationRef::chat(chat_id),
933 message_id,
934 URGENT_ACK_EMOTION,
935 )
936 .await?)
937 }
938
939 async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
940 if let Some(key) = chatsvc::reaction_key(reaction_type) {
941 return Ok(Messages::new(self.session())
942 .set_emotion(&conversation_ref(target), target.message_id(), &key)
943 .await?);
944 }
945 if matches!(target, MessageTarget::Chat { .. }) {
946 return Err(Error::Unsupported("this reaction in a chat"));
947 }
948 Ok(Graph::set_reaction(self, target, reaction_type).await?)
949 }
950
951 async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
952 let keys = chatsvc::reaction_keys(reaction_type);
953 if !keys.is_empty() {
954 let keys: Vec<&str> = keys.iter().map(String::as_str).collect();
955 return Ok(Messages::new(self.session())
956 .unset_emotions(&conversation_ref(target), target.message_id(), &keys)
957 .await?);
958 }
959 if matches!(target, MessageTarget::Chat { .. }) {
960 return Err(Error::Unsupported("this reaction in a chat"));
961 }
962 Ok(Graph::unset_reaction(self, target, reaction_type).await?)
963 }
964
965 async fn edit_message(
966 &self,
967 target: &MessageTarget,
968 html: &str,
969 mentions: &[OutgoingMention],
970 extras: &MessageExtras,
971 ) -> Result<()> {
972 match chatsvc_conversation(target) {
973 Some(conversation) if mentions.is_empty() => {
974 let html = format!("{}{html}{}", extras.kept_html(), extras.files_html());
975 Ok(Messages::new(self.session())
976 .edit_message(&conversation, target.message_id(), &html)
977 .await?)
978 }
979 Some(conversation) => {
980 let Some(entries) = crate::teams_quote::mention_entries(mentions) else {
981 if matches!(conversation, ConversationRef::Chat { .. }) {
982 return Err(Error::Unsupported("editing in a team or tag mention"));
983 }
984 return Ok(Graph::edit_message(self, target, html, mentions, extras).await?);
985 };
986 let html = format!(
987 "{}{}{}",
988 extras.kept_html(),
989 crate::teams_quote::chatsvc_mention_html(html, mentions),
990 extras.files_html()
991 );
992 Ok(Messages::new(self.session())
993 .edit_message_with_mentions(
994 &conversation,
995 target.message_id(),
996 &html,
997 Some(&serde_json::Value::Array(entries).to_string()),
998 )
999 .await?)
1000 }
1001 None => Ok(Graph::edit_message(self, target, html, mentions, extras).await?),
1002 }
1003 }
1004
1005 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
1006 match chatsvc_conversation(target) {
1007 Some(conversation) => Ok(Messages::new(self.session())
1008 .soft_delete_message(&conversation, target.message_id())
1009 .await?),
1010 None => Ok(Graph::soft_delete_message(self, user_id, target).await?),
1011 }
1012 }
1013
1014 async fn undo_soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
1015 match chatsvc_conversation(target) {
1016 Some(conversation) => Ok(Messages::new(self.session())
1017 .undelete_message(&conversation, target.message_id())
1018 .await?),
1019 None => Ok(Graph::undo_soft_delete_message(self, user_id, target).await?),
1020 }
1021 }
1022
1023 /// Teams names a 1:1 chat after its two members; the chat service creates it with the first message.
1024 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat> {
1025 let mut pair = [my_user_id.to_owned(), user_id.to_owned()];
1026 pair.sort();
1027 let sorted_id = format!("19:{}_{}@unq.gbl.spaces", pair[0], pair[1]);
1028 let reversed_id = format!("19:{}_{}@unq.gbl.spaces", pair[1], pair[0]);
1029 let listing = chatsvc::ChatListing::new(self.session());
1030 let chat_id = if listing.conversation(&sorted_id).await.is_err()
1031 && listing.conversation(&reversed_id).await.is_ok()
1032 {
1033 reversed_id
1034 } else {
1035 sorted_id
1036 };
1037 NativeChats::new(self.session())
1038 .created_chat(&chat_id, "oneOnOne", None, &pair)
1039 .await
1040 }
1041
1042 async fn create_group(
1043 &self,
1044 my_user_id: &str,
1045 user_ids: &[String],
1046 topic: Option<&str>,
1047 ) -> Result<Chat> {
1048 let mut members = vec![my_user_id.to_owned()];
1049 members.extend(
1050 user_ids
1051 .iter()
1052 .filter(|user_id| *user_id != my_user_id)
1053 .cloned(),
1054 );
1055 let chat_id = ThreadMembers::new(self.session())
1056 .create_group(&members, topic)
1057 .await?;
1058 NativeChats::new(self.session())
1059 .created_chat(&chat_id, "group", topic, &members)
1060 .await
1061 }
1062
1063 async fn search_people(&self, query: &str) -> Result<Vec<User>> {
1064 let email = crate::native::is_email_address(query);
1065 let directory = Graph::search_people(self, query).await;
1066 let (mut users, directory_error) = match directory {
1067 Ok(users) => (users, None),
1068 Err(error) if email && directory_address_unknown(&error) => (Vec::new(), None),
1069 Err(error) if email => (Vec::new(), Some(error)),
1070 Err(error) => return Err(error.into()),
1071 };
1072 let known = users.iter().any(|user| {
1073 crate::native::user_has_address(
1074 user.mail.as_deref(),
1075 user.user_principal_name.as_deref(),
1076 query,
1077 )
1078 });
1079 if email && !known {
1080 match chatsvc::UserProfiles::new(self.session())
1081 .search_external(query)
1082 .await
1083 {
1084 Ok(external) => {
1085 let found = crate::native::remember_external(external);
1086 if let Some(error) = directory_error {
1087 let matched: Vec<User> = found
1088 .into_iter()
1089 .filter(|user| {
1090 crate::native::user_has_address(
1091 user.mail.as_deref(),
1092 user.user_principal_name.as_deref(),
1093 query,
1094 )
1095 })
1096 .collect();
1097 if matched.is_empty() {
1098 return Err(error.into());
1099 }
1100 users.extend(matched);
1101 } else {
1102 users.extend(found);
1103 }
1104 }
1105 Err(_) => {
1106 if let Some(error) = directory_error {
1107 return Err(error.into());
1108 }
1109 }
1110 }
1111 }
1112 Ok(users)
1113 }
1114
1115 async fn team_tags(&self, team_id: &str) -> Result<Vec<Tag>> {
1116 Ok(Graph::team_tags(self, team_id).await?)
1117 }
1118
1119 async fn channel_description(&self, team_id: &str, channel_id: &str) -> Result<String> {
1120 Ok(Graph::channel_description(self, team_id, channel_id).await?)
1121 }
1122
1123 async fn channel_files_folder(&self, team_id: &str, channel_id: &str) -> Result<DriveFolder> {
1124 Ok(Graph::channel_files_folder(self, team_id, channel_id).await?)
1125 }
1126
1127 async fn channel_files_root(&self, team_id: &str, channel_id: &str) -> Result<DriveEntry> {
1128 Ok(Graph::channel_files_root(self, team_id, channel_id).await?)
1129 }
1130
1131 async fn channel_tab_web_url(
1132 &self,
1133 team_id: &str,
1134 channel_id: &str,
1135 tab_id: &str,
1136 ) -> Result<Option<String>> {
1137 Ok(Graph::channel_tab_web_url(self, team_id, channel_id, tab_id).await?)
1138 }
1139
1140 async fn channel_member_count(&self, team_id: &str, channel_id: &str) -> Result<usize> {
1141 Ok(Graph::channel_member_count(self, team_id, channel_id).await?)
1142 }
1143
1144 async fn channel_members(&self, team_id: &str, channel_id: &str) -> Result<Vec<graph::Member>> {
1145 Ok(Graph::channel_member_list(self, team_id, channel_id).await?)
1146 }
1147
1148 async fn list_children(&self, folder: &DriveFolder) -> Result<Vec<DriveEntry>> {
1149 Ok(Graph::list_children(self, folder).await?)
1150 }
1151
1152 async fn create_folder(&self, parent: &DriveFolder, name: &str) -> Result<DriveEntry> {
1153 Ok(Graph::create_folder(self, parent, name).await?)
1154 }
1155
1156 async fn upload_file(
1157 &self,
1158 destination: &UploadDestination,
1159 file_name: &str,
1160 bytes: &[u8],
1161 progress: &(dyn Fn(u8) + Send + Sync),
1162 ) -> Result<UploadedFile> {
1163 Ok(Graph::upload_file(self, destination, file_name, bytes, progress).await?)
1164 }
1165
1166 async fn share_file(
1167 &self,
1168 file: &UploadedFile,
1169 recipients: &[Recipient],
1170 ) -> Result<Option<String>> {
1171 Ok(Graph::share_item(self, file, recipients).await?)
1172 }
1173
1174 async fn recent_files(&self, top: usize) -> Result<Vec<CloudFile>> {
1175 Ok(Graph::recent_files(self, top).await?)
1176 }
1177
1178 async fn recent_files_with_owners(
1179 &self,
1180 top: usize,
1181 ) -> Result<(Vec<CloudFile>, Vec<(String, String)>)> {
1182 Ok(Graph::recent_files_with_owners(self, top).await?)
1183 }
1184
1185 async fn shared_with_me(&self) -> Result<Vec<DriveEntry>> {
1186 Ok(Graph::shared_with_me(self).await?)
1187 }
1188
1189 async fn quick_access_sites(&self) -> Result<Vec<QuickSite>> {
1190 Ok(Graph::quick_access_sites(self).await?)
1191 }
1192
1193 async fn drive_root(&self, site_id: Option<&str>) -> Result<DriveFolder> {
1194 Ok(Graph::drive_root(self, site_id).await?)
1195 }
1196
1197 async fn cloud_file_reference(&self, file: &CloudFile) -> Result<UploadedFile> {
1198 Ok(Graph::cloud_file_reference(self, file).await?)
1199 }
1200
1201 async fn delete_file(&self, file: &UploadedFile) -> Result<()> {
1202 Ok(Graph::delete_uploaded(self, file).await?)
1203 }
1204
1205 async fn resolve_share(&self, open_url: &str) -> Result<SharedFile> {
1206 Ok(Graph::resolve_share(self, open_url).await?)
1207 }
1208
1209 async fn resolve_drive_item(&self, drive_id: &str, item_id: &str) -> Result<SharedFile> {
1210 Ok(Graph::resolve_drive_item(self, drive_id, item_id).await?)
1211 }
1212
1213 async fn download_range(
1214 &self,
1215 download_url: &str,
1216 start: u64,
1217 end: Option<u64>,
1218 ) -> Result<Vec<u8>> {
1219 Ok(Graph::download_range(self, download_url, start, end).await?)
1220 }
1221
1222 async fn hosted_content(&self, url: &str) -> Result<Photo> {
1223 let ams_url = chatsvc::hosted_content_image_url(url);
1224 let url = ams_url.as_deref().unwrap_or(url);
1225 let image = if chatsvc::is_ams_image_url(url) {
1226 chatsvc::ams::Ams::new(self.session())
1227 .download_image(url)
1228 .await?
1229 } else if chatsvc::is_link_image_url(url) {
1230 Messages::new(self.session()).link_image(url).await?
1231 } else {
1232 return Ok(Graph::download_hosted_content(self, url).await?);
1233 };
1234 Ok(Photo {
1235 bytes: image.bytes,
1236 content_type: image.content_type,
1237 })
1238 }
1239
1240 async fn external_image(&self, url: &str) -> Result<Photo> {
1241 Ok(Graph::download_external_image(self, url).await?)
1242 }
1243
1244 async fn user_photos(&self, user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
1245 let photos = Graph::user_photos(self, user_ids).await?;
1246 Ok(photos
1247 .into_iter()
1248 .map(|photo| photo.map_err(Into::into))
1249 .collect())
1250 }
1251
1252 async fn team_photos(&self, team_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
1253 let photos = Graph::team_photos(self, team_ids).await?;
1254 Ok(photos
1255 .into_iter()
1256 .map(|photo| photo.map_err(Into::into))
1257 .collect())
1258 }
1259
1260 async fn presences(&self, user_ids: &[String]) -> Result<Vec<Presence>> {
1261 let found = PresenceLookup::new(self.session())
1262 .presences(user_ids)
1263 .await?;
1264 Ok(found
1265 .into_iter()
1266 .map(|presence| Presence {
1267 user_id: presence.user_id,
1268 availability: presence.availability,
1269 activity: presence.activity,
1270 })
1271 .collect())
1272 }
1273
1274 async fn person_profile(&self, user_id: &str) -> Result<PersonProfile> {
1275 crate::profile::load_profile(self, user_id).await
1276 }
1277
1278 async fn bot_profiles(&self, bot_ids: &[String]) -> Result<Vec<BotProfile>> {
1279 Ok(BotProfiles::new(self.session()).fetch(bot_ids).await?)
1280 }
1281
1282 async fn subscribe_presence(
1283 &self,
1284 endpoint_id: &str,
1285 trouter_uri: &str,
1286 user_ids: &[String],
1287 purge: bool,
1288 ) -> Result<()> {
1289 Ok(PresenceLookup::new(self.session())
1290 .subscribe(endpoint_id, trouter_uri, user_ids, purge)
1291 .await?)
1292 }
1293
1294 async fn own_status(&self, user_id: &str) -> Result<PresenceStatus> {
1295 Ok(PresenceService::new(self.session()).status(user_id).await?)
1296 }
1297
1298 async fn set_availability(&self, forced: Option<&ForcedAvailability>) -> Result<()> {
1299 Ok(PresenceService::new(self.session())
1300 .set_availability(forced)
1301 .await?)
1302 }
1303
1304 async fn set_status_note(&self, note: Option<&StatusNote>) -> Result<()> {
1305 Ok(PresenceService::new(self.session()).set_note(note).await?)
1306 }
1307
1308 async fn set_work_location(
1309 &self,
1310 location: Option<(WorkLocationKind, DateTime<Utc>)>,
1311 ) -> Result<()> {
1312 Ok(PresenceService::new(self.session())
1313 .set_work_location(location)
1314 .await?)
1315 }
1316
1317 async fn consumption_horizons(&self, conversation_id: &str) -> Result<Vec<MemberHorizon>> {
1318 Ok(Receipts::new(self.session())
1319 .consumption_horizons(conversation_id)
1320 .await?)
1321 }
1322
1323 async fn forward_messages(
1324 &self,
1325 source_conversation_id: &str,
1326 target_conversation_id: &str,
1327 message_ids: &[String],
1328 comment_html: &str,
1329 ) -> Result<ForwardResult> {
1330 Ok(Messages::new(self.session())
1331 .forward_messages(
1332 source_conversation_id,
1333 target_conversation_id,
1334 message_ids,
1335 comment_html,
1336 )
1337 .await?)
1338 }
1339
1340 async fn set_saved(
1341 &self,
1342 conversation_id: &str,
1343 root_id: &str,
1344 message_id: &str,
1345 saved: bool,
1346 ) -> Result<()> {
1347 Ok(Messages::new(self.session())
1348 .set_saved(conversation_id, root_id, message_id, saved)
1349 .await?)
1350 }
1351
1352 async fn list_saved(&self) -> Result<Vec<SavedMessage>> {
1353 Ok(Messages::new(self.session()).list_saved().await?)
1354 }
1355
1356 async fn set_post_followed(
1357 &self,
1358 user_id: &str,
1359 channel_id: &str,
1360 root_id: &str,
1361 followed: bool,
1362 ) -> Result<Option<String>> {
1363 Ok(Messages::new(self.session())
1364 .set_post_followed(user_id, channel_id, root_id, followed)
1365 .await?)
1366 }
1367
1368 async fn post_follow_states(
1369 &self,
1370 channel_id: &str,
1371 root_ids: &[String],
1372 ) -> Result<HashMap<String, PostFollowState>> {
1373 Ok(Messages::new(self.session())
1374 .post_follow_states(channel_id, root_ids)
1375 .await?)
1376 }
1377
1378 async fn list_call_logs(&self) -> Result<Vec<CallLogEntry>> {
1379 Ok(Messages::new(self.session()).list_call_logs().await?)
1380 }
1381
1382 async fn chat_message_window(
1383 &self,
1384 chat_id: &str,
1385 anchor_id: &str,
1386 direction: WindowDirection,
1387 page_size: usize,
1388 ) -> Result<MessageWindow> {
1389 Ok(Messages::new(self.session())
1390 .message_window(chat_id, anchor_id, direction, page_size)
1391 .await?)
1392 }
1393
1394 async fn chat_thread_activities(&self, chat_id: &str) -> Result<Vec<ThreadActivity>> {
1395 Ok(Messages::new(self.session())
1396 .thread_activities(chat_id)
1397 .await?)
1398 }
1399
1400 async fn chat_messages_by_id(
1401 &self,
1402 chat_id: &str,
1403 message_ids: &[String],
1404 ) -> Result<Vec<Result<Message>>> {
1405 let native = NativeChats::new(self.session());
1406 Ok(futures_util::future::join_all(
1407 message_ids
1408 .iter()
1409 .map(|message_id| native.message(chat_id, message_id)),
1410 )
1411 .await)
1412 }
1413
1414 async fn chat_pins(&self, chat_id: &str) -> Result<Vec<PinnedMessage>> {
1415 Ok(Pins::new(self.session()).chat_pins(chat_id).await?)
1416 }
1417
1418 async fn pin_message(&self, chat_id: &str, message_id: &str) -> Result<()> {
1419 Ok(Pins::new(self.session())
1420 .pin_message(chat_id, message_id)
1421 .await?)
1422 }
1423
1424 async fn unpin_message(
1425 &self,
1426 chat_id: &str,
1427 message_id: &str,
1428 parent_id: Option<&str>,
1429 ) -> Result<()> {
1430 Ok(Pins::new(self.session())
1431 .unpin_message(chat_id, message_id, parent_id)
1432 .await?)
1433 }
1434
1435 async fn channel_pinned_posts(
1436 &self,
1437 team_thread_id: &str,
1438 channel_id: &str,
1439 ) -> Result<Vec<PinnedMessage>> {
1440 Ok(Pins::with_skype_token(self.session())
1441 .channel_pinned_posts(team_thread_id, channel_id)
1442 .await?)
1443 }
1444
1445 async fn set_post_pinned(
1446 &self,
1447 team_thread_id: &str,
1448 channel_id: &str,
1449 post_id: &str,
1450 pinned: bool,
1451 ) -> Result<()> {
1452 Ok(Pins::with_skype_token(self.session())
1453 .set_post_pinned(team_thread_id, channel_id, post_id, pinned)
1454 .await?)
1455 }
1456
1457 async fn activity_feed(&self) -> Result<ActivitySnapshot> {
1458 Ok(ActivityFeed::new(self.session()).snapshot().await?)
1459 }
1460
1461 async fn mark_activity_read(&self, message_id: &str) -> Result<()> {
1462 Ok(ActivityFeed::new(self.session())
1463 .mark_read(message_id)
1464 .await?)
1465 }
1466
1467 async fn mark_all_activity_read(&self, now_ms: i64) -> Result<()> {
1468 Ok(ActivityFeed::new(self.session())
1469 .mark_all_read(now_ms)
1470 .await?)
1471 }
1472
1473 async fn app_consumption_horizon(&self) -> Result<AppConsumptionHorizon> {
1474 let properties = UserSettings::new(self.session()).properties().await?;
1475 Ok(parse_app_consumption_horizon(&properties))
1476 }
1477
1478 async fn mark_all_channels_read(&self, now_ms: i64) -> Result<AppConsumptionHorizon> {
1479 Ok(UserSettings::new(self.session())
1480 .mark_all_channels_read(now_ms)
1481 .await?)
1482 }
1483
1484 async fn chat_apps(&self, chat_id: &str) -> Result<Vec<ChatApp>> {
1485 Ok(CardActions::new(self.session()).chat_apps(chat_id).await?)
1486 }
1487
1488 async fn invoke_card(&self, invoke: InvokeRequest) -> Result<InvokeResponse> {
1489 Ok(CardActions::new(self.session()).invoke(invoke).await?)
1490 }
1491
1492 async fn message_links(
1493 &self,
1494 conversation: &ConversationRef,
1495 page_size: usize,
1496 ) -> Result<Vec<MessageLinks>> {
1497 Ok(Messages::new(self.session())
1498 .list_message_links(conversation, page_size)
1499 .await?)
1500 }
1501
1502 async fn message_emotions(
1503 &self,
1504 conversation: &ConversationRef,
1505 newest_message_id: &str,
1506 page_size: usize,
1507 ) -> Result<Vec<MessageEmotions>> {
1508 Ok(Messages::new(self.session())
1509 .message_emotions(
1510 &conversation.conversation_id(),
1511 newest_message_id,
1512 page_size,
1513 )
1514 .await?)
1515 }
1516
1517 async fn set_message_links(&self, target: &MessageTarget, links_json: &str) -> Result<()> {
1518 Ok(Messages::new(self.session())
1519 .set_links(&conversation_ref(target), target.message_id(), links_json)
1520 .await?)
1521 }
1522
1523 async fn link_info(&self, url: &str) -> Result<LinkInfo> {
1524 Ok(Messages::new(self.session()).link_info(url).await?)
1525 }
1526
1527 async fn message_languages(
1528 &self,
1529 conversation: &ConversationRef,
1530 page_size: usize,
1531 ) -> Result<Vec<MessageLanguage>> {
1532 Ok(Messages::new(self.session())
1533 .list_language_stamps(conversation, page_size)
1534 .await?)
1535 }
1536
1537 async fn ask_copilot(&self, ask: &CopilotAsk) -> Result<String> {
1538 Ok(Copilot::new(self.session()).ask(ask).await?)
1539 }
1540
1541 async fn rewrite_draft(&self, rewrite: &ComposeRewrite) -> Result<String> {
1542 Ok(ComposeCopilot::new(self.session()).rewrite(rewrite).await?)
1543 }
1544
1545 async fn translate_chat_messages(
1546 &self,
1547 chat_id: &str,
1548 to_language: &str,
1549 messages: &[TranslateRequest],
1550 trigger: &TranslationTrigger,
1551 ) -> Result<Vec<Translation>> {
1552 Ok(Translator::new(self.session())
1553 .translate_chat(chat_id, to_language, messages, trigger)
1554 .await?)
1555 }
1556
1557 async fn translate_channel_messages(
1558 &self,
1559 team_id: &str,
1560 channel_id: &str,
1561 root_id: &str,
1562 to_language: &str,
1563 messages: &[TranslateRequest],
1564 trigger: &TranslationTrigger,
1565 ) -> Result<Vec<Translation>> {
1566 Ok(Translator::new(self.session())
1567 .translate_channel(team_id, channel_id, root_id, to_language, messages, trigger)
1568 .await?)
1569 }
1570
1571 async fn translation_languages(&self, locale: &str) -> Result<Vec<Language>> {
1572 Ok(Translator::new(self.session()).languages(locale).await?)
1573 }
1574
1575 async fn language_settings(&self) -> Result<LanguageSettings> {
1576 Ok(LanguageSettingsClient::new(self.session()).read().await?)
1577 }
1578
1579 async fn recording_policy(&self) -> Result<RecordingPolicy> {
1580 Ok(LanguageSettingsClient::new(self.session())
1581 .read_recording_policy()
1582 .await?)
1583 }
1584
1585 async fn allow_priority_messages(&self) -> Result<bool> {
1586 Ok(LanguageSettingsClient::new(self.session())
1587 .read_allow_priority_messages()
1588 .await?)
1589 }
1590
1591 async fn patch_language_settings(&self, patch: serde_json::Value) -> Result<()> {
1592 Ok(LanguageSettingsClient::new(self.session())
1593 .patch(patch)
1594 .await?)
1595 }
1596
1597 async fn sound_settings(&self) -> Result<SoundSettings> {
1598 Ok(SoundSettingsClient::new(self.session()).read().await?)
1599 }
1600
1601 async fn chat_settings(&self) -> Result<ChatSettingsDocument> {
1602 Ok(ChatSettingsClient::new(self.session()).read().await?)
1603 }
1604
1605 async fn save_chat_setting(&self, change: ChatSettingChange) -> Result<ChatSettingsDocument> {
1606 Ok(ChatSettingsClient::new(self.session())
1607 .write(change)
1608 .await?)
1609 }
1610
1611 async fn set_chat_notifications(
1612 &self,
1613 chat_id: &str,
1614 current_settings: Option<&str>,
1615 mode: ChatNotificationMode,
1616 desktop: DesktopNotifications,
1617 ) -> Result<ChatNotificationWrite> {
1618 Ok(Conversations::new(self.session())
1619 .set_chat_notifications(chat_id, current_settings, mode, desktop)
1620 .await?)
1621 }
1622
1623 async fn save_sound_settings(
1624 &self,
1625 user_mri: &str,
1626 previous: &SoundSettings,
1627 updated: &SoundSettings,
1628 ) -> Result<()> {
1629 Ok(SoundSettingsClient::new(self.session())
1630 .write(user_mri, previous, updated)
1631 .await?)
1632 }
1633
1634 async fn search_gifs(&self, query: &str) -> Result<Vec<Gif>> {
1635 Ok(Gifs::new(self.session()).search(query).await?)
1636 }
1637
1638 async fn server_search_init(
1639 &self,
1640 identity: &SearchIdentity,
1641 search_session: &SearchSession,
1642 ) -> Result<()> {
1643 Ok(SubstrateSearch::new(self.session(), identity.clone())
1644 .init(search_session)
1645 .await?)
1646 }
1647
1648 async fn server_suggestions(
1649 &self,
1650 identity: &SearchIdentity,
1651 search_session: &SearchSession,
1652 text: &str,
1653 ) -> Result<Suggestions> {
1654 Ok(SubstrateSearch::new(self.session(), identity.clone())
1655 .suggestions(search_session, text)
1656 .await?)
1657 }
1658
1659 async fn server_query(
1660 &self,
1661 identity: &SearchIdentity,
1662 search_session: &SearchSession,
1663 request: &QueryRequest,
1664 ) -> Result<QueryResults> {
1665 Ok(SubstrateSearch::new(self.session(), identity.clone())
1666 .query(search_session, request)
1667 .await?)
1668 }
1669
1670 async fn create_scheduled(
1671 &self,
1672 inner_thread_id: &str,
1673 html: &str,
1674 send_at: DateTime<Utc>,
1675 display_name: &str,
1676 ) -> Result<ScheduledDraft> {
1677 Ok(ScheduledDrafts::new(self.session())
1678 .create(inner_thread_id, html, send_at, display_name)
1679 .await?)
1680 }
1681
1682 async fn scheduled_drafts(&self) -> Result<Vec<ScheduledDraft>> {
1683 Ok(ScheduledDrafts::new(self.session()).list().await?)
1684 }
1685
1686 async fn update_scheduled(
1687 &self,
1688 draft: &ScheduledDraft,
1689 html: &str,
1690 display_name: &str,
1691 ) -> Result<ScheduledDraft> {
1692 Ok(ScheduledDrafts::new(self.session())
1693 .update(draft, html, display_name)
1694 .await?)
1695 }
1696
1697 async fn cancel_scheduled(&self, draft_id: &str) -> Result<()> {
1698 Ok(ScheduledDrafts::new(self.session())
1699 .cancel(draft_id)
1700 .await?)
1701 }
1702}
1703
1704fn chatsvc_conversation(target: &MessageTarget) -> Option<ConversationRef> {
1705 match target {
1706 MessageTarget::Chat { .. } => CHATSVC_CHAT_WRITES.then(|| conversation_ref(target)),
1707 MessageTarget::Channel { .. } => Some(conversation_ref(target)),
1708 }
1709}
1710
1711fn conversation_ref(target: &MessageTarget) -> ConversationRef {
1712 match target {
1713 MessageTarget::Chat { chat_id, .. } => ConversationRef::chat(chat_id),
1714 MessageTarget::Channel {
1715 channel_id,
1716 root_id: None,
1717 ..
1718 } => ConversationRef::channel_root(channel_id),
1719 MessageTarget::Channel {
1720 channel_id,
1721 root_id: Some(root_id),
1722 ..
1723 } => ConversationRef::channel_reply(channel_id, root_id),
1724 }
1725}
1726
1727impl From<graph::Page<Message>> for RemotePage {
1728 fn from(page: graph::Page<Message>) -> Self {
1729 RemotePage {
1730 items: page.items,
1731 next_link: page.next_link,
1732 oldest_at: None,
1733 }
1734 }
1735}
1736
1737impl From<graph::Page<Message>> for DeltaPage {
1738 fn from(page: graph::Page<Message>) -> Self {
1739 DeltaPage {
1740 items: page.items,
1741 next_link: page.next_link,
1742 delta_link: page.delta_link,
1743 }
1744 }
1745}
1746
1747impl From<graph::Page<Chat>> for ChatsPage {
1748 fn from(page: graph::Page<Chat>) -> Self {
1749 ChatsPage {
1750 items: page.items,
1751 next_link: page.next_link,
1752 }
1753 }
1754}
1755
1756/// HTTP 400 and 404 on a mail lookup mean the address is not in the directory.
1757/// A 401, a 5xx, or a transport failure is a real search error.
1758fn directory_address_unknown(error: &graph::Error) -> bool {
1759 matches!(
1760 error,
1761 graph::Error::Session(session::Error::Api {
1762 status: 400 | 404,
1763 ..
1764 })
1765 )
1766}
1767
1768#[cfg(test)]
1769mod tests {
1770 use super::*;
1771
1772 #[test]
1773 fn only_a_missing_address_is_an_unknown_directory() {
1774 let missing = graph::Error::Session(session::Error::api(
1775 404,
1776 "https://graph.test/users",
1777 serde_json::json!({}),
1778 ));
1779 let invalid = graph::Error::Session(session::Error::api(
1780 400,
1781 "https://graph.test/users",
1782 serde_json::json!({}),
1783 ));
1784 let denied = graph::Error::Session(session::Error::api(
1785 401,
1786 "https://graph.test/users",
1787 serde_json::json!({}),
1788 ));
1789 assert!(directory_address_unknown(&missing));
1790 assert!(directory_address_unknown(&invalid));
1791 assert!(!directory_address_unknown(&denied));
1792 assert!(!directory_address_unknown(&graph::Error::NotAMember));
1793 }
1794
1795 #[test]
1796 fn channel_targets_route_to_chatsvc() {
1797 let root = MessageTarget::channel("t", "19:c@thread.tacv2", "m1", None);
1798 let reply = MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1"));
1799 assert_eq!(
1800 chatsvc_conversation(&root),
1801 Some(ConversationRef::channel_root("19:c@thread.tacv2"))
1802 );
1803 assert_eq!(
1804 chatsvc_conversation(&reply),
1805 Some(ConversationRef::channel_reply("19:c@thread.tacv2", "m1"))
1806 );
1807 }
1808
1809 #[test]
1810 fn reactions_reach_chats_regardless_of_the_feature_switch() {
1811 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
1812 assert_eq!(
1813 conversation_ref(&chat),
1814 ConversationRef::chat("19:a@thread.v2")
1815 );
1816 }
1817
1818 #[test]
1819 fn chats_follow_the_feature_switch() {
1820 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
1821 assert_eq!(chatsvc_conversation(&chat).is_some(), CHATSVC_CHAT_WRITES);
1822 }
1823}