BlueGit

ict rusty-teams

Files

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

remote.rs

remote.rs 57.1 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 parse_app_consumption_horizon, ActivityFeed, ActivitySnapshot, AppConsumptionHorizon,
11 ChatNotificationMode, ChatNotificationWrite, ChatSettingChange, ChatSettingsClient,
12 ChatSettingsDocument, DesktopNotifications, UserSettings,
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 @ ConversationRef::Chat { .. }) => {
980 let entries = crate::teams_quote::mention_entries(mentions).ok_or(
981 Error::Unsupported("editing in a team, channel or tag mention"),
982 )?;
983 let html = format!(
984 "{}{}{}",
985 extras.kept_html(),
986 crate::teams_quote::chatsvc_mention_html(html, mentions),
987 extras.files_html()
988 );
989 Ok(Messages::new(self.session())
990 .edit_message_with_mentions(
991 &conversation,
992 target.message_id(),
993 &html,
994 Some(&serde_json::Value::Array(entries).to_string()),
995 )
996 .await?)
997 }
998 Some(_) | None => Ok(Graph::edit_message(self, target, html, mentions, extras).await?),
999 }
1000 }
1001
1002 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
1003 match chatsvc_conversation(target) {
1004 Some(conversation) => Ok(Messages::new(self.session())
1005 .soft_delete_message(&conversation, target.message_id())
1006 .await?),
1007 None => Ok(Graph::soft_delete_message(self, user_id, target).await?),
1008 }
1009 }
1010
1011 async fn undo_soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
1012 match chatsvc_conversation(target) {
1013 Some(conversation) => Ok(Messages::new(self.session())
1014 .undelete_message(&conversation, target.message_id())
1015 .await?),
1016 None => Ok(Graph::undo_soft_delete_message(self, user_id, target).await?),
1017 }
1018 }
1019
1020 /// Teams names a 1:1 chat after its two members; the chat service creates it with the first message.
1021 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat> {
1022 let mut pair = [my_user_id.to_owned(), user_id.to_owned()];
1023 pair.sort();
1024 let sorted_id = format!("19:{}_{}@unq.gbl.spaces", pair[0], pair[1]);
1025 let reversed_id = format!("19:{}_{}@unq.gbl.spaces", pair[1], pair[0]);
1026 let listing = chatsvc::ChatListing::new(self.session());
1027 let chat_id = if listing.conversation(&sorted_id).await.is_err()
1028 && listing.conversation(&reversed_id).await.is_ok()
1029 {
1030 reversed_id
1031 } else {
1032 sorted_id
1033 };
1034 NativeChats::new(self.session())
1035 .created_chat(&chat_id, "oneOnOne", None, &pair)
1036 .await
1037 }
1038
1039 async fn create_group(
1040 &self,
1041 my_user_id: &str,
1042 user_ids: &[String],
1043 topic: Option<&str>,
1044 ) -> Result<Chat> {
1045 let mut members = vec![my_user_id.to_owned()];
1046 members.extend(
1047 user_ids
1048 .iter()
1049 .filter(|user_id| *user_id != my_user_id)
1050 .cloned(),
1051 );
1052 let chat_id = ThreadMembers::new(self.session())
1053 .create_group(&members, topic)
1054 .await?;
1055 NativeChats::new(self.session())
1056 .created_chat(&chat_id, "group", topic, &members)
1057 .await
1058 }
1059
1060 async fn search_people(&self, query: &str) -> Result<Vec<User>> {
1061 let email = crate::native::is_email_address(query);
1062 let directory = Graph::search_people(self, query).await;
1063 let (mut users, directory_error) = match directory {
1064 Ok(users) => (users, None),
1065 Err(error) if email && directory_address_unknown(&error) => (Vec::new(), None),
1066 Err(error) if email => (Vec::new(), Some(error)),
1067 Err(error) => return Err(error.into()),
1068 };
1069 let known = users.iter().any(|user| {
1070 crate::native::user_has_address(
1071 user.mail.as_deref(),
1072 user.user_principal_name.as_deref(),
1073 query,
1074 )
1075 });
1076 if email && !known {
1077 match chatsvc::UserProfiles::new(self.session())
1078 .search_external(query)
1079 .await
1080 {
1081 Ok(external) => {
1082 let found = crate::native::remember_external(external);
1083 if let Some(error) = directory_error {
1084 let matched: Vec<User> = found
1085 .into_iter()
1086 .filter(|user| {
1087 crate::native::user_has_address(
1088 user.mail.as_deref(),
1089 user.user_principal_name.as_deref(),
1090 query,
1091 )
1092 })
1093 .collect();
1094 if matched.is_empty() {
1095 return Err(error.into());
1096 }
1097 users.extend(matched);
1098 } else {
1099 users.extend(found);
1100 }
1101 }
1102 Err(_) => {
1103 if let Some(error) = directory_error {
1104 return Err(error.into());
1105 }
1106 }
1107 }
1108 }
1109 Ok(users)
1110 }
1111
1112 async fn team_tags(&self, team_id: &str) -> Result<Vec<Tag>> {
1113 Ok(Graph::team_tags(self, team_id).await?)
1114 }
1115
1116 async fn channel_description(&self, team_id: &str, channel_id: &str) -> Result<String> {
1117 Ok(Graph::channel_description(self, team_id, channel_id).await?)
1118 }
1119
1120 async fn channel_files_folder(&self, team_id: &str, channel_id: &str) -> Result<DriveFolder> {
1121 Ok(Graph::channel_files_folder(self, team_id, channel_id).await?)
1122 }
1123
1124 async fn channel_files_root(&self, team_id: &str, channel_id: &str) -> Result<DriveEntry> {
1125 Ok(Graph::channel_files_root(self, team_id, channel_id).await?)
1126 }
1127
1128 async fn channel_tab_web_url(
1129 &self,
1130 team_id: &str,
1131 channel_id: &str,
1132 tab_id: &str,
1133 ) -> Result<Option<String>> {
1134 Ok(Graph::channel_tab_web_url(self, team_id, channel_id, tab_id).await?)
1135 }
1136
1137 async fn channel_member_count(&self, team_id: &str, channel_id: &str) -> Result<usize> {
1138 Ok(Graph::channel_member_count(self, team_id, channel_id).await?)
1139 }
1140
1141 async fn channel_members(&self, team_id: &str, channel_id: &str) -> Result<Vec<graph::Member>> {
1142 Ok(Graph::channel_member_list(self, team_id, channel_id).await?)
1143 }
1144
1145 async fn list_children(&self, folder: &DriveFolder) -> Result<Vec<DriveEntry>> {
1146 Ok(Graph::list_children(self, folder).await?)
1147 }
1148
1149 async fn create_folder(&self, parent: &DriveFolder, name: &str) -> Result<DriveEntry> {
1150 Ok(Graph::create_folder(self, parent, name).await?)
1151 }
1152
1153 async fn upload_file(
1154 &self,
1155 destination: &UploadDestination,
1156 file_name: &str,
1157 bytes: &[u8],
1158 progress: &(dyn Fn(u8) + Send + Sync),
1159 ) -> Result<UploadedFile> {
1160 Ok(Graph::upload_file(self, destination, file_name, bytes, progress).await?)
1161 }
1162
1163 async fn share_file(
1164 &self,
1165 file: &UploadedFile,
1166 recipients: &[Recipient],
1167 ) -> Result<Option<String>> {
1168 Ok(Graph::share_item(self, file, recipients).await?)
1169 }
1170
1171 async fn recent_files(&self, top: usize) -> Result<Vec<CloudFile>> {
1172 Ok(Graph::recent_files(self, top).await?)
1173 }
1174
1175 async fn recent_files_with_owners(
1176 &self,
1177 top: usize,
1178 ) -> Result<(Vec<CloudFile>, Vec<(String, String)>)> {
1179 Ok(Graph::recent_files_with_owners(self, top).await?)
1180 }
1181
1182 async fn shared_with_me(&self) -> Result<Vec<DriveEntry>> {
1183 Ok(Graph::shared_with_me(self).await?)
1184 }
1185
1186 async fn quick_access_sites(&self) -> Result<Vec<QuickSite>> {
1187 Ok(Graph::quick_access_sites(self).await?)
1188 }
1189
1190 async fn drive_root(&self, site_id: Option<&str>) -> Result<DriveFolder> {
1191 Ok(Graph::drive_root(self, site_id).await?)
1192 }
1193
1194 async fn cloud_file_reference(&self, file: &CloudFile) -> Result<UploadedFile> {
1195 Ok(Graph::cloud_file_reference(self, file).await?)
1196 }
1197
1198 async fn delete_file(&self, file: &UploadedFile) -> Result<()> {
1199 Ok(Graph::delete_uploaded(self, file).await?)
1200 }
1201
1202 async fn resolve_share(&self, open_url: &str) -> Result<SharedFile> {
1203 Ok(Graph::resolve_share(self, open_url).await?)
1204 }
1205
1206 async fn resolve_drive_item(&self, drive_id: &str, item_id: &str) -> Result<SharedFile> {
1207 Ok(Graph::resolve_drive_item(self, drive_id, item_id).await?)
1208 }
1209
1210 async fn download_range(
1211 &self,
1212 download_url: &str,
1213 start: u64,
1214 end: Option<u64>,
1215 ) -> Result<Vec<u8>> {
1216 Ok(Graph::download_range(self, download_url, start, end).await?)
1217 }
1218
1219 async fn hosted_content(&self, url: &str) -> Result<Photo> {
1220 let ams_url = chatsvc::hosted_content_image_url(url);
1221 let url = ams_url.as_deref().unwrap_or(url);
1222 let image = if chatsvc::is_ams_image_url(url) {
1223 chatsvc::ams::Ams::new(self.session())
1224 .download_image(url)
1225 .await?
1226 } else if chatsvc::is_link_image_url(url) {
1227 Messages::new(self.session()).link_image(url).await?
1228 } else {
1229 return Ok(Graph::download_hosted_content(self, url).await?);
1230 };
1231 Ok(Photo {
1232 bytes: image.bytes,
1233 content_type: image.content_type,
1234 })
1235 }
1236
1237 async fn external_image(&self, url: &str) -> Result<Photo> {
1238 Ok(Graph::download_external_image(self, url).await?)
1239 }
1240
1241 async fn user_photos(&self, user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
1242 let photos = Graph::user_photos(self, user_ids).await?;
1243 Ok(photos
1244 .into_iter()
1245 .map(|photo| photo.map_err(Into::into))
1246 .collect())
1247 }
1248
1249 async fn team_photos(&self, team_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
1250 let photos = Graph::team_photos(self, team_ids).await?;
1251 Ok(photos
1252 .into_iter()
1253 .map(|photo| photo.map_err(Into::into))
1254 .collect())
1255 }
1256
1257 async fn presences(&self, user_ids: &[String]) -> Result<Vec<Presence>> {
1258 let found = PresenceLookup::new(self.session())
1259 .presences(user_ids)
1260 .await?;
1261 Ok(found
1262 .into_iter()
1263 .map(|presence| Presence {
1264 user_id: presence.user_id,
1265 availability: presence.availability,
1266 activity: presence.activity,
1267 })
1268 .collect())
1269 }
1270
1271 async fn person_profile(&self, user_id: &str) -> Result<PersonProfile> {
1272 crate::profile::load_profile(self, user_id).await
1273 }
1274
1275 async fn bot_profiles(&self, bot_ids: &[String]) -> Result<Vec<BotProfile>> {
1276 Ok(BotProfiles::new(self.session()).fetch(bot_ids).await?)
1277 }
1278
1279 async fn subscribe_presence(
1280 &self,
1281 endpoint_id: &str,
1282 trouter_uri: &str,
1283 user_ids: &[String],
1284 purge: bool,
1285 ) -> Result<()> {
1286 Ok(PresenceLookup::new(self.session())
1287 .subscribe(endpoint_id, trouter_uri, user_ids, purge)
1288 .await?)
1289 }
1290
1291 async fn own_status(&self, user_id: &str) -> Result<PresenceStatus> {
1292 Ok(PresenceService::new(self.session()).status(user_id).await?)
1293 }
1294
1295 async fn set_availability(&self, forced: Option<&ForcedAvailability>) -> Result<()> {
1296 Ok(PresenceService::new(self.session())
1297 .set_availability(forced)
1298 .await?)
1299 }
1300
1301 async fn set_status_note(&self, note: Option<&StatusNote>) -> Result<()> {
1302 Ok(PresenceService::new(self.session()).set_note(note).await?)
1303 }
1304
1305 async fn set_work_location(
1306 &self,
1307 location: Option<(WorkLocationKind, DateTime<Utc>)>,
1308 ) -> Result<()> {
1309 Ok(PresenceService::new(self.session())
1310 .set_work_location(location)
1311 .await?)
1312 }
1313
1314 async fn consumption_horizons(&self, conversation_id: &str) -> Result<Vec<MemberHorizon>> {
1315 Ok(Receipts::new(self.session())
1316 .consumption_horizons(conversation_id)
1317 .await?)
1318 }
1319
1320 async fn forward_messages(
1321 &self,
1322 source_conversation_id: &str,
1323 target_conversation_id: &str,
1324 message_ids: &[String],
1325 comment_html: &str,
1326 ) -> Result<ForwardResult> {
1327 Ok(Messages::new(self.session())
1328 .forward_messages(
1329 source_conversation_id,
1330 target_conversation_id,
1331 message_ids,
1332 comment_html,
1333 )
1334 .await?)
1335 }
1336
1337 async fn set_saved(
1338 &self,
1339 conversation_id: &str,
1340 root_id: &str,
1341 message_id: &str,
1342 saved: bool,
1343 ) -> Result<()> {
1344 Ok(Messages::new(self.session())
1345 .set_saved(conversation_id, root_id, message_id, saved)
1346 .await?)
1347 }
1348
1349 async fn list_saved(&self) -> Result<Vec<SavedMessage>> {
1350 Ok(Messages::new(self.session()).list_saved().await?)
1351 }
1352
1353 async fn set_post_followed(
1354 &self,
1355 user_id: &str,
1356 channel_id: &str,
1357 root_id: &str,
1358 followed: bool,
1359 ) -> Result<Option<String>> {
1360 Ok(Messages::new(self.session())
1361 .set_post_followed(user_id, channel_id, root_id, followed)
1362 .await?)
1363 }
1364
1365 async fn post_follow_states(
1366 &self,
1367 channel_id: &str,
1368 root_ids: &[String],
1369 ) -> Result<HashMap<String, PostFollowState>> {
1370 Ok(Messages::new(self.session())
1371 .post_follow_states(channel_id, root_ids)
1372 .await?)
1373 }
1374
1375 async fn list_call_logs(&self) -> Result<Vec<CallLogEntry>> {
1376 Ok(Messages::new(self.session()).list_call_logs().await?)
1377 }
1378
1379 async fn chat_message_window(
1380 &self,
1381 chat_id: &str,
1382 anchor_id: &str,
1383 direction: WindowDirection,
1384 page_size: usize,
1385 ) -> Result<MessageWindow> {
1386 Ok(Messages::new(self.session())
1387 .message_window(chat_id, anchor_id, direction, page_size)
1388 .await?)
1389 }
1390
1391 async fn chat_thread_activities(&self, chat_id: &str) -> Result<Vec<ThreadActivity>> {
1392 Ok(Messages::new(self.session())
1393 .thread_activities(chat_id)
1394 .await?)
1395 }
1396
1397 async fn chat_messages_by_id(
1398 &self,
1399 chat_id: &str,
1400 message_ids: &[String],
1401 ) -> Result<Vec<Result<Message>>> {
1402 let native = NativeChats::new(self.session());
1403 Ok(futures_util::future::join_all(
1404 message_ids
1405 .iter()
1406 .map(|message_id| native.message(chat_id, message_id)),
1407 )
1408 .await)
1409 }
1410
1411 async fn chat_pins(&self, chat_id: &str) -> Result<Vec<PinnedMessage>> {
1412 Ok(Pins::new(self.session()).chat_pins(chat_id).await?)
1413 }
1414
1415 async fn pin_message(&self, chat_id: &str, message_id: &str) -> Result<()> {
1416 Ok(Pins::new(self.session())
1417 .pin_message(chat_id, message_id)
1418 .await?)
1419 }
1420
1421 async fn unpin_message(
1422 &self,
1423 chat_id: &str,
1424 message_id: &str,
1425 parent_id: Option<&str>,
1426 ) -> Result<()> {
1427 Ok(Pins::new(self.session())
1428 .unpin_message(chat_id, message_id, parent_id)
1429 .await?)
1430 }
1431
1432 async fn channel_pinned_posts(
1433 &self,
1434 team_thread_id: &str,
1435 channel_id: &str,
1436 ) -> Result<Vec<PinnedMessage>> {
1437 Ok(Pins::with_skype_token(self.session())
1438 .channel_pinned_posts(team_thread_id, channel_id)
1439 .await?)
1440 }
1441
1442 async fn set_post_pinned(
1443 &self,
1444 team_thread_id: &str,
1445 channel_id: &str,
1446 post_id: &str,
1447 pinned: bool,
1448 ) -> Result<()> {
1449 Ok(Pins::with_skype_token(self.session())
1450 .set_post_pinned(team_thread_id, channel_id, post_id, pinned)
1451 .await?)
1452 }
1453
1454 async fn activity_feed(&self) -> Result<ActivitySnapshot> {
1455 Ok(ActivityFeed::new(self.session()).snapshot().await?)
1456 }
1457
1458 async fn mark_activity_read(&self, message_id: &str) -> Result<()> {
1459 Ok(ActivityFeed::new(self.session())
1460 .mark_read(message_id)
1461 .await?)
1462 }
1463
1464 async fn mark_all_activity_read(&self, now_ms: i64) -> Result<()> {
1465 Ok(ActivityFeed::new(self.session())
1466 .mark_all_read(now_ms)
1467 .await?)
1468 }
1469
1470 async fn app_consumption_horizon(&self) -> Result<AppConsumptionHorizon> {
1471 let properties = UserSettings::new(self.session()).properties().await?;
1472 Ok(parse_app_consumption_horizon(&properties))
1473 }
1474
1475 async fn mark_all_channels_read(&self, now_ms: i64) -> Result<AppConsumptionHorizon> {
1476 Ok(UserSettings::new(self.session())
1477 .mark_all_channels_read(now_ms)
1478 .await?)
1479 }
1480
1481 async fn chat_apps(&self, chat_id: &str) -> Result<Vec<ChatApp>> {
1482 Ok(CardActions::new(self.session()).chat_apps(chat_id).await?)
1483 }
1484
1485 async fn invoke_card(&self, invoke: InvokeRequest) -> Result<InvokeResponse> {
1486 Ok(CardActions::new(self.session()).invoke(invoke).await?)
1487 }
1488
1489 async fn message_links(
1490 &self,
1491 conversation: &ConversationRef,
1492 page_size: usize,
1493 ) -> Result<Vec<MessageLinks>> {
1494 Ok(Messages::new(self.session())
1495 .list_message_links(conversation, page_size)
1496 .await?)
1497 }
1498
1499 async fn message_emotions(
1500 &self,
1501 conversation: &ConversationRef,
1502 newest_message_id: &str,
1503 page_size: usize,
1504 ) -> Result<Vec<MessageEmotions>> {
1505 Ok(Messages::new(self.session())
1506 .message_emotions(
1507 &conversation.conversation_id(),
1508 newest_message_id,
1509 page_size,
1510 )
1511 .await?)
1512 }
1513
1514 async fn set_message_links(&self, target: &MessageTarget, links_json: &str) -> Result<()> {
1515 Ok(Messages::new(self.session())
1516 .set_links(&conversation_ref(target), target.message_id(), links_json)
1517 .await?)
1518 }
1519
1520 async fn link_info(&self, url: &str) -> Result<LinkInfo> {
1521 Ok(Messages::new(self.session()).link_info(url).await?)
1522 }
1523
1524 async fn message_languages(
1525 &self,
1526 conversation: &ConversationRef,
1527 page_size: usize,
1528 ) -> Result<Vec<MessageLanguage>> {
1529 Ok(Messages::new(self.session())
1530 .list_language_stamps(conversation, page_size)
1531 .await?)
1532 }
1533
1534 async fn ask_copilot(&self, ask: &CopilotAsk) -> Result<String> {
1535 Ok(Copilot::new(self.session()).ask(ask).await?)
1536 }
1537
1538 async fn rewrite_draft(&self, rewrite: &ComposeRewrite) -> Result<String> {
1539 Ok(ComposeCopilot::new(self.session()).rewrite(rewrite).await?)
1540 }
1541
1542 async fn translate_chat_messages(
1543 &self,
1544 chat_id: &str,
1545 to_language: &str,
1546 messages: &[TranslateRequest],
1547 trigger: &TranslationTrigger,
1548 ) -> Result<Vec<Translation>> {
1549 Ok(Translator::new(self.session())
1550 .translate_chat(chat_id, to_language, messages, trigger)
1551 .await?)
1552 }
1553
1554 async fn translate_channel_messages(
1555 &self,
1556 team_id: &str,
1557 channel_id: &str,
1558 root_id: &str,
1559 to_language: &str,
1560 messages: &[TranslateRequest],
1561 trigger: &TranslationTrigger,
1562 ) -> Result<Vec<Translation>> {
1563 Ok(Translator::new(self.session())
1564 .translate_channel(team_id, channel_id, root_id, to_language, messages, trigger)
1565 .await?)
1566 }
1567
1568 async fn translation_languages(&self, locale: &str) -> Result<Vec<Language>> {
1569 Ok(Translator::new(self.session()).languages(locale).await?)
1570 }
1571
1572 async fn language_settings(&self) -> Result<LanguageSettings> {
1573 Ok(LanguageSettingsClient::new(self.session()).read().await?)
1574 }
1575
1576 async fn recording_policy(&self) -> Result<RecordingPolicy> {
1577 Ok(LanguageSettingsClient::new(self.session())
1578 .read_recording_policy()
1579 .await?)
1580 }
1581
1582 async fn allow_priority_messages(&self) -> Result<bool> {
1583 Ok(LanguageSettingsClient::new(self.session())
1584 .read_allow_priority_messages()
1585 .await?)
1586 }
1587
1588 async fn patch_language_settings(&self, patch: serde_json::Value) -> Result<()> {
1589 Ok(LanguageSettingsClient::new(self.session())
1590 .patch(patch)
1591 .await?)
1592 }
1593
1594 async fn sound_settings(&self) -> Result<SoundSettings> {
1595 Ok(SoundSettingsClient::new(self.session()).read().await?)
1596 }
1597
1598 async fn chat_settings(&self) -> Result<ChatSettingsDocument> {
1599 Ok(ChatSettingsClient::new(self.session()).read().await?)
1600 }
1601
1602 async fn save_chat_setting(&self, change: ChatSettingChange) -> Result<ChatSettingsDocument> {
1603 Ok(ChatSettingsClient::new(self.session())
1604 .write(change)
1605 .await?)
1606 }
1607
1608 async fn set_chat_notifications(
1609 &self,
1610 chat_id: &str,
1611 current_settings: Option<&str>,
1612 mode: ChatNotificationMode,
1613 desktop: DesktopNotifications,
1614 ) -> Result<ChatNotificationWrite> {
1615 Ok(Conversations::new(self.session())
1616 .set_chat_notifications(chat_id, current_settings, mode, desktop)
1617 .await?)
1618 }
1619
1620 async fn save_sound_settings(
1621 &self,
1622 user_mri: &str,
1623 previous: &SoundSettings,
1624 updated: &SoundSettings,
1625 ) -> Result<()> {
1626 Ok(SoundSettingsClient::new(self.session())
1627 .write(user_mri, previous, updated)
1628 .await?)
1629 }
1630
1631 async fn search_gifs(&self, query: &str) -> Result<Vec<Gif>> {
1632 Ok(Gifs::new(self.session()).search(query).await?)
1633 }
1634
1635 async fn server_search_init(
1636 &self,
1637 identity: &SearchIdentity,
1638 search_session: &SearchSession,
1639 ) -> Result<()> {
1640 Ok(SubstrateSearch::new(self.session(), identity.clone())
1641 .init(search_session)
1642 .await?)
1643 }
1644
1645 async fn server_suggestions(
1646 &self,
1647 identity: &SearchIdentity,
1648 search_session: &SearchSession,
1649 text: &str,
1650 ) -> Result<Suggestions> {
1651 Ok(SubstrateSearch::new(self.session(), identity.clone())
1652 .suggestions(search_session, text)
1653 .await?)
1654 }
1655
1656 async fn server_query(
1657 &self,
1658 identity: &SearchIdentity,
1659 search_session: &SearchSession,
1660 request: &QueryRequest,
1661 ) -> Result<QueryResults> {
1662 Ok(SubstrateSearch::new(self.session(), identity.clone())
1663 .query(search_session, request)
1664 .await?)
1665 }
1666
1667 async fn create_scheduled(
1668 &self,
1669 inner_thread_id: &str,
1670 html: &str,
1671 send_at: DateTime<Utc>,
1672 display_name: &str,
1673 ) -> Result<ScheduledDraft> {
1674 Ok(ScheduledDrafts::new(self.session())
1675 .create(inner_thread_id, html, send_at, display_name)
1676 .await?)
1677 }
1678
1679 async fn scheduled_drafts(&self) -> Result<Vec<ScheduledDraft>> {
1680 Ok(ScheduledDrafts::new(self.session()).list().await?)
1681 }
1682
1683 async fn update_scheduled(
1684 &self,
1685 draft: &ScheduledDraft,
1686 html: &str,
1687 display_name: &str,
1688 ) -> Result<ScheduledDraft> {
1689 Ok(ScheduledDrafts::new(self.session())
1690 .update(draft, html, display_name)
1691 .await?)
1692 }
1693
1694 async fn cancel_scheduled(&self, draft_id: &str) -> Result<()> {
1695 Ok(ScheduledDrafts::new(self.session())
1696 .cancel(draft_id)
1697 .await?)
1698 }
1699}
1700
1701fn chatsvc_conversation(target: &MessageTarget) -> Option<ConversationRef> {
1702 match target {
1703 MessageTarget::Chat { .. } => CHATSVC_CHAT_WRITES.then(|| conversation_ref(target)),
1704 MessageTarget::Channel { .. } => Some(conversation_ref(target)),
1705 }
1706}
1707
1708fn conversation_ref(target: &MessageTarget) -> ConversationRef {
1709 match target {
1710 MessageTarget::Chat { chat_id, .. } => ConversationRef::chat(chat_id),
1711 MessageTarget::Channel {
1712 channel_id,
1713 root_id: None,
1714 ..
1715 } => ConversationRef::channel_root(channel_id),
1716 MessageTarget::Channel {
1717 channel_id,
1718 root_id: Some(root_id),
1719 ..
1720 } => ConversationRef::channel_reply(channel_id, root_id),
1721 }
1722}
1723
1724impl From<graph::Page<Message>> for RemotePage {
1725 fn from(page: graph::Page<Message>) -> Self {
1726 RemotePage {
1727 items: page.items,
1728 next_link: page.next_link,
1729 oldest_at: None,
1730 }
1731 }
1732}
1733
1734impl From<graph::Page<Message>> for DeltaPage {
1735 fn from(page: graph::Page<Message>) -> Self {
1736 DeltaPage {
1737 items: page.items,
1738 next_link: page.next_link,
1739 delta_link: page.delta_link,
1740 }
1741 }
1742}
1743
1744impl From<graph::Page<Chat>> for ChatsPage {
1745 fn from(page: graph::Page<Chat>) -> Self {
1746 ChatsPage {
1747 items: page.items,
1748 next_link: page.next_link,
1749 }
1750 }
1751}
1752
1753/// HTTP 400 and 404 on a mail lookup mean the address is not in the directory.
1754/// A 401, a 5xx, or a transport failure is a real search error.
1755fn directory_address_unknown(error: &graph::Error) -> bool {
1756 matches!(
1757 error,
1758 graph::Error::Session(session::Error::Api {
1759 status: 400 | 404,
1760 ..
1761 })
1762 )
1763}
1764
1765#[cfg(test)]
1766mod tests {
1767 use super::*;
1768
1769 #[test]
1770 fn only_a_missing_address_is_an_unknown_directory() {
1771 let missing = graph::Error::Session(session::Error::api(
1772 404,
1773 "https://graph.test/users",
1774 serde_json::json!({}),
1775 ));
1776 let invalid = graph::Error::Session(session::Error::api(
1777 400,
1778 "https://graph.test/users",
1779 serde_json::json!({}),
1780 ));
1781 let denied = graph::Error::Session(session::Error::api(
1782 401,
1783 "https://graph.test/users",
1784 serde_json::json!({}),
1785 ));
1786 assert!(directory_address_unknown(&missing));
1787 assert!(directory_address_unknown(&invalid));
1788 assert!(!directory_address_unknown(&denied));
1789 assert!(!directory_address_unknown(&graph::Error::NotAMember));
1790 }
1791
1792 #[test]
1793 fn channel_targets_route_to_chatsvc() {
1794 let root = MessageTarget::channel("t", "19:c@thread.tacv2", "m1", None);
1795 let reply = MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1"));
1796 assert_eq!(
1797 chatsvc_conversation(&root),
1798 Some(ConversationRef::channel_root("19:c@thread.tacv2"))
1799 );
1800 assert_eq!(
1801 chatsvc_conversation(&reply),
1802 Some(ConversationRef::channel_reply("19:c@thread.tacv2", "m1"))
1803 );
1804 }
1805
1806 #[test]
1807 fn reactions_reach_chats_regardless_of_the_feature_switch() {
1808 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
1809 assert_eq!(
1810 conversation_ref(&chat),
1811 ConversationRef::chat("19:a@thread.v2")
1812 );
1813 }
1814
1815 #[test]
1816 fn chats_follow_the_feature_switch() {
1817 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
1818 assert_eq!(chatsvc_conversation(&chat).is_some(), CHATSVC_CHAT_WRITES);
1819 }
1820}