BlueGit

ict rusty-teams

Files

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

remote.rs

remote.rs 35.2 KiB Download Raw
1use chatsvc::{
2 CallLogEntry, CardActions, ChatApp, ConversationRef, ConversationState, Conversations,
3 ForcedAvailability, ForwardResult, Gif, Gifs, InvokeRequest, InvokeResponse, Language,
4 LanguageSettings, LanguageSettingsClient, LinkInfo, MemberHorizon, MessageLanguage,
5 MessageLinks, MessageWindow, Messages, PinnedMessage, Pins, PresenceService, PresenceStatus,
6 Receipts, RecordingPolicy, SavedMessage, ScheduledDraft, ScheduledDrafts, StatusNote, TranslateRequest,
7 Translation, TranslationTrigger, Translator, WindowDirection, WorkLocationKind,
8};
9use chrono::{DateTime, Utc};
10use graph::{
11 Channel, Chat, DriveEntry, DriveFolder, Graph, Member, Message, MessageExtras, MessageTarget,
12 OutgoingMention, Photo, Presence, SharedFile, Team, UploadDestination, UploadedFile, User,
13};
14
15use crate::error::{Error, Result};
16use crate::profile::PersonProfile;
17
18pub const CHATSVC_CHAT_WRITES: bool = false;
19
20#[derive(Debug, Clone)]
21pub struct RemotePage {
22 pub items: Vec<Message>,
23 pub next_link: Option<String>,
24}
25
26#[derive(Debug, Clone)]
27pub struct DeltaPage {
28 pub items: Vec<Message>,
29 pub next_link: Option<String>,
30 pub delta_link: Option<String>,
31}
32
33#[derive(Debug, Clone)]
34pub struct ChatsPage {
35 pub items: Vec<Chat>,
36 pub next_link: Option<String>,
37}
38
39#[allow(async_fn_in_trait)]
40pub trait Remote {
41 async fn me(&self) -> Result<User>;
42 async fn chats_page(&self, top: usize) -> Result<ChatsPage>;
43 async fn chats_at(&self, next_link: &str) -> Result<ChatsPage>;
44 async fn chat(&self, chat_id: &str) -> Result<Chat>;
45 async fn chat_members(&self, chat_id: &str) -> Result<Vec<Member>>;
46 async fn joined_teams(&self) -> Result<Vec<Team>>;
47 async fn channels_for_teams(&self, team_ids: &[String]) -> Result<Vec<Result<Vec<Channel>>>>;
48 async fn chat_messages(
49 &self,
50 chat_id: &str,
51 before: Option<DateTime<Utc>>,
52 top: usize,
53 ) -> Result<RemotePage>;
54 async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result<Message>;
55 async fn channel_messages(
56 &self,
57 team_id: &str,
58 channel_id: &str,
59 top: usize,
60 ) -> Result<RemotePage>;
61 async fn channel_messages_at(&self, next_link: &str) -> Result<RemotePage>;
62 async fn channel_message(
63 &self,
64 team_id: &str,
65 channel_id: &str,
66 message_id: &str,
67 ) -> Result<Message>;
68 async fn channel_reply(
69 &self,
70 team_id: &str,
71 channel_id: &str,
72 message_id: &str,
73 reply_id: &str,
74 ) -> Result<Message>;
75 async fn channel_delta(
76 &self,
77 team_id: &str,
78 channel_id: &str,
79 modified_after: Option<DateTime<Utc>>,
80 ) -> Result<DeltaPage>;
81 async fn channel_delta_at(&self, link: &str) -> Result<DeltaPage>;
82 async fn channel_replies(
83 &self,
84 team_id: &str,
85 channel_id: &str,
86 root_ids: &[String],
87 ) -> Result<Vec<Result<Vec<Message>>>>;
88 async fn send_chat_message(
89 &self,
90 chat_id: &str,
91 html: &str,
92 mentions: &[OutgoingMention],
93 extras: &MessageExtras,
94 ) -> Result<Message>;
95 async fn send_channel_message(
96 &self,
97 team_id: &str,
98 channel_id: &str,
99 html: &str,
100 subject: Option<&str>,
101 mentions: &[OutgoingMention],
102 extras: &MessageExtras,
103 ) -> Result<Message>;
104 async fn reply_to_channel_message(
105 &self,
106 team_id: &str,
107 channel_id: &str,
108 message_id: &str,
109 html: &str,
110 mentions: &[OutgoingMention],
111 extras: &MessageExtras,
112 ) -> Result<Message>;
113 async fn mark_chat_read(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()>;
114 async fn mark_chat_unread(
115 &self,
116 chat_id: &str,
117 user_id: &str,
118 tenant_id: &str,
119 last_read_at: DateTime<Utc>,
120 ) -> Result<()>;
121 async fn hide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()>;
122 async fn unhide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()>;
123 async fn leave_chat(&self, chat_id: &str, user_id: &str) -> Result<()>;
124 async fn chat_states(&self) -> Result<Vec<ConversationState>>;
125 async fn set_chat_muted(&self, chat_id: &str, muted: bool) -> Result<()>;
126 async fn set_channel_notifications(
127 &self,
128 channel_id: &str,
129 notifications: store::ChannelNotifications,
130 ) -> Result<()>;
131 async fn send_typing(&self, conversation: &ConversationRef, active: bool) -> Result<()>;
132 async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()>;
133 async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()>;
134 async fn edit_message(
135 &self,
136 target: &MessageTarget,
137 html: &str,
138 mentions: &[OutgoingMention],
139 extras: &MessageExtras,
140 ) -> Result<()>;
141 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()>;
142 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat>;
143 async fn create_group(
144 &self,
145 my_user_id: &str,
146 user_ids: &[String],
147 topic: Option<&str>,
148 ) -> Result<Chat>;
149 async fn search_people(&self, query: &str) -> Result<Vec<User>>;
150
151 async fn reply_with_quote(
152 &self,
153 _chat_id: &str,
154 _quoted_message_id: &str,
155 _html: &str,
156 _mentions: &[OutgoingMention],
157 _extras: &MessageExtras,
158 ) -> Result<Message> {
159 Err(Error::Unsupported("a quoted reply"))
160 }
161
162 async fn channel_files_folder(&self, _team_id: &str, _channel_id: &str) -> Result<DriveFolder> {
163 Err(Error::Unsupported("a channel files folder"))
164 }
165
166 async fn channel_files_root(&self, _team_id: &str, _channel_id: &str) -> Result<DriveEntry> {
167 Err(Error::Unsupported("a channel library"))
168 }
169
170 async fn channel_tab_web_url(
171 &self,
172 _team_id: &str,
173 _channel_id: &str,
174 _tab_id: &str,
175 ) -> Result<Option<String>> {
176 Err(Error::Unsupported("a channel tab link"))
177 }
178
179 async fn list_children(&self, _folder: &DriveFolder) -> Result<Vec<DriveEntry>> {
180 Err(Error::Unsupported("a folder listing"))
181 }
182
183 async fn create_folder(&self, _parent: &DriveFolder, _name: &str) -> Result<DriveEntry> {
184 Err(Error::Unsupported("creating a folder"))
185 }
186
187 async fn upload_file(
188 &self,
189 _destination: &UploadDestination,
190 _file_name: &str,
191 _bytes: &[u8],
192 _progress: &(dyn Fn(u8) + Send + Sync),
193 ) -> Result<UploadedFile> {
194 Err(Error::Unsupported("a file upload"))
195 }
196
197 async fn share_file(&self, _file: &UploadedFile, _user_ids: &[String]) -> Result<()> {
198 Err(Error::Unsupported("sharing a file"))
199 }
200
201 async fn delete_file(&self, _file: &UploadedFile) -> Result<()> {
202 Err(Error::Unsupported("deleting a file"))
203 }
204
205 async fn resolve_share(&self, _open_url: &str) -> Result<SharedFile> {
206 Err(Error::Unsupported("resolving a shared file"))
207 }
208
209 async fn resolve_drive_item(&self, _drive_id: &str, _item_id: &str) -> Result<SharedFile> {
210 Err(Error::Unsupported("resolving a drive item"))
211 }
212
213 async fn download_range(
214 &self,
215 _download_url: &str,
216 _start: u64,
217 _end: Option<u64>,
218 ) -> Result<Vec<u8>> {
219 Err(Error::Unsupported("a file download"))
220 }
221
222 async fn hosted_content(&self, _url: &str) -> Result<Photo> {
223 Err(Error::Unsupported("hosted content"))
224 }
225
226 async fn external_image(&self, _url: &str) -> Result<Photo> {
227 Err(Error::Unsupported("external images"))
228 }
229
230 async fn user_photos(&self, _user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
231 Err(Error::Unsupported("profile photos"))
232 }
233
234 async fn presences(&self, _user_ids: &[String]) -> Result<Vec<Presence>> {
235 Err(Error::Unsupported("presence"))
236 }
237
238 async fn person_profile(&self, _user_id: &str) -> Result<PersonProfile> {
239 Err(Error::Unsupported("profile cards"))
240 }
241
242 async fn subscribe_presence(
243 &self,
244 _endpoint_id: &str,
245 _trouter_uri: &str,
246 _user_ids: &[String],
247 _purge: bool,
248 ) -> Result<()> {
249 Err(Error::Unsupported("presence subscriptions"))
250 }
251
252 async fn own_status(&self, _user_id: &str) -> Result<PresenceStatus> {
253 Err(Error::Unsupported("own status"))
254 }
255
256 async fn set_availability(&self, _forced: Option<&ForcedAvailability>) -> Result<()> {
257 Err(Error::Unsupported("setting availability"))
258 }
259
260 async fn set_status_note(&self, _note: Option<&StatusNote>) -> Result<()> {
261 Err(Error::Unsupported("setting the status message"))
262 }
263
264 async fn set_work_location(
265 &self,
266 _location: Option<(WorkLocationKind, DateTime<Utc>)>,
267 ) -> Result<()> {
268 Err(Error::Unsupported("setting the work location"))
269 }
270
271 async fn consumption_horizons(&self, _conversation_id: &str) -> Result<Vec<MemberHorizon>> {
272 Err(Error::Unsupported("read receipts"))
273 }
274
275 async fn forward_messages(
276 &self,
277 _source_conversation_id: &str,
278 _target_conversation_id: &str,
279 _message_ids: &[String],
280 _comment_html: &str,
281 ) -> Result<ForwardResult> {
282 Err(Error::Unsupported("forwarding messages"))
283 }
284
285 async fn set_saved(
286 &self,
287 _conversation_id: &str,
288 _root_id: &str,
289 _message_id: &str,
290 _saved: bool,
291 ) -> Result<()> {
292 Err(Error::Unsupported("saving messages"))
293 }
294
295 async fn list_saved(&self) -> Result<Vec<SavedMessage>> {
296 Err(Error::Unsupported("saved messages"))
297 }
298
299 async fn list_call_logs(&self) -> Result<Vec<CallLogEntry>> {
300 Err(Error::Unsupported("call history"))
301 }
302
303 async fn chat_message_window(
304 &self,
305 _chat_id: &str,
306 _anchor_id: &str,
307 _direction: WindowDirection,
308 _page_size: usize,
309 ) -> Result<MessageWindow> {
310 Err(Error::Unsupported("message windows"))
311 }
312
313 async fn chat_messages_by_id(
314 &self,
315 _chat_id: &str,
316 _message_ids: &[String],
317 ) -> Result<Vec<Result<Message>>> {
318 Err(Error::Unsupported("batched message reads"))
319 }
320
321 async fn chat_pins(&self, _chat_id: &str) -> Result<Vec<PinnedMessage>> {
322 Err(Error::Unsupported("pinned messages"))
323 }
324
325 async fn pin_message(&self, _chat_id: &str, _message_id: &str) -> Result<()> {
326 Err(Error::Unsupported("pinning messages"))
327 }
328
329 async fn unpin_message(
330 &self,
331 _chat_id: &str,
332 _message_id: &str,
333 _parent_id: Option<&str>,
334 ) -> Result<()> {
335 Err(Error::Unsupported("pinning messages"))
336 }
337
338 async fn chat_apps(&self, _chat_id: &str) -> Result<Vec<ChatApp>> {
339 Err(Error::Unsupported("chat apps"))
340 }
341
342 async fn invoke_card(&self, _invoke: InvokeRequest) -> Result<InvokeResponse> {
343 Err(Error::Unsupported("card actions"))
344 }
345
346 async fn message_links(
347 &self,
348 _conversation: &ConversationRef,
349 _page_size: usize,
350 ) -> Result<Vec<MessageLinks>> {
351 Err(Error::Unsupported("link previews"))
352 }
353
354 async fn set_message_links(&self, _target: &MessageTarget, _links_json: &str) -> Result<()> {
355 Err(Error::Unsupported("link previews"))
356 }
357
358 async fn link_info(&self, _url: &str) -> Result<LinkInfo> {
359 Err(Error::Unsupported("link previews"))
360 }
361
362 async fn message_languages(
363 &self,
364 _conversation: &ConversationRef,
365 _page_size: usize,
366 ) -> Result<Vec<MessageLanguage>> {
367 Err(Error::Unsupported("message languages"))
368 }
369
370 async fn translate_chat_messages(
371 &self,
372 _chat_id: &str,
373 _to_language: &str,
374 _messages: &[TranslateRequest],
375 _trigger: &TranslationTrigger,
376 ) -> Result<Vec<Translation>> {
377 Err(Error::Unsupported("translation"))
378 }
379
380 async fn translate_channel_messages(
381 &self,
382 _team_id: &str,
383 _channel_id: &str,
384 _root_id: &str,
385 _to_language: &str,
386 _messages: &[TranslateRequest],
387 _trigger: &TranslationTrigger,
388 ) -> Result<Vec<Translation>> {
389 Err(Error::Unsupported("translation"))
390 }
391
392 async fn translation_languages(&self, _locale: &str) -> Result<Vec<Language>> {
393 Err(Error::Unsupported("translation"))
394 }
395
396 async fn language_settings(&self) -> Result<LanguageSettings> {
397 Err(Error::Unsupported("translation settings"))
398 }
399
400 async fn patch_language_settings(&self, _patch: serde_json::Value) -> Result<()> {
401 Err(Error::Unsupported("translation settings"))
402 }
403
404 async fn recording_policy(&self) -> Result<RecordingPolicy> {
405 Err(Error::Unsupported("recording policy"))
406 }
407
408 async fn search_gifs(&self, _query: &str) -> Result<Vec<Gif>> {
409 Err(Error::Unsupported("GIF search"))
410 }
411
412 async fn create_scheduled(
413 &self,
414 _inner_thread_id: &str,
415 _html: &str,
416 _send_at: DateTime<Utc>,
417 _display_name: &str,
418 ) -> Result<ScheduledDraft> {
419 Err(Error::Unsupported("schedule send"))
420 }
421
422 async fn scheduled_drafts(&self) -> Result<Vec<ScheduledDraft>> {
423 Err(Error::Unsupported("schedule send"))
424 }
425
426 async fn update_scheduled(
427 &self,
428 _draft: &ScheduledDraft,
429 _html: &str,
430 _display_name: &str,
431 ) -> Result<ScheduledDraft> {
432 Err(Error::Unsupported("schedule send"))
433 }
434
435 async fn cancel_scheduled(&self, _draft_id: &str) -> Result<()> {
436 Err(Error::Unsupported("schedule send"))
437 }
438}
439
440impl Remote for Graph {
441 async fn me(&self) -> Result<User> {
442 Ok(Graph::me(self).await?)
443 }
444
445 async fn chats_page(&self, top: usize) -> Result<ChatsPage> {
446 Ok(Graph::chats_page(self, top).await?.into())
447 }
448
449 async fn chats_at(&self, next_link: &str) -> Result<ChatsPage> {
450 Ok(Graph::chats_at(self, next_link).await?.into())
451 }
452
453 async fn chat(&self, chat_id: &str) -> Result<Chat> {
454 Ok(Graph::chat(self, chat_id).await?)
455 }
456
457 async fn chat_members(&self, chat_id: &str) -> Result<Vec<Member>> {
458 Ok(Graph::chat_members(self, chat_id).await?)
459 }
460
461 async fn joined_teams(&self) -> Result<Vec<Team>> {
462 Ok(Graph::joined_teams(self).await?)
463 }
464
465 async fn channels_for_teams(&self, team_ids: &[String]) -> Result<Vec<Result<Vec<Channel>>>> {
466 let listings = Graph::channels_for_teams(self, team_ids).await?;
467 Ok(listings
468 .into_iter()
469 .map(|listing| listing.map_err(Into::into))
470 .collect())
471 }
472
473 async fn chat_messages(
474 &self,
475 chat_id: &str,
476 before: Option<DateTime<Utc>>,
477 top: usize,
478 ) -> Result<RemotePage> {
479 Ok(Graph::chat_messages(self, chat_id, before, top)
480 .await?
481 .into())
482 }
483
484 async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result<Message> {
485 Ok(Graph::chat_message(self, chat_id, message_id).await?)
486 }
487
488 async fn channel_messages(
489 &self,
490 team_id: &str,
491 channel_id: &str,
492 top: usize,
493 ) -> Result<RemotePage> {
494 Ok(Graph::channel_messages(self, team_id, channel_id, top)
495 .await?
496 .into())
497 }
498
499 async fn channel_messages_at(&self, next_link: &str) -> Result<RemotePage> {
500 Ok(Graph::channel_messages_at(self, next_link).await?.into())
501 }
502
503 async fn channel_message(
504 &self,
505 team_id: &str,
506 channel_id: &str,
507 message_id: &str,
508 ) -> Result<Message> {
509 Ok(Graph::channel_message(self, team_id, channel_id, message_id).await?)
510 }
511
512 async fn channel_reply(
513 &self,
514 team_id: &str,
515 channel_id: &str,
516 message_id: &str,
517 reply_id: &str,
518 ) -> Result<Message> {
519 Ok(Graph::channel_reply(self, team_id, channel_id, message_id, reply_id).await?)
520 }
521
522 async fn channel_delta(
523 &self,
524 team_id: &str,
525 channel_id: &str,
526 modified_after: Option<DateTime<Utc>>,
527 ) -> Result<DeltaPage> {
528 Ok(
529 Graph::channel_delta(self, team_id, channel_id, modified_after)
530 .await?
531 .into(),
532 )
533 }
534
535 async fn channel_delta_at(&self, link: &str) -> Result<DeltaPage> {
536 Ok(Graph::channel_delta_at(self, link).await?.into())
537 }
538
539 async fn channel_replies(
540 &self,
541 team_id: &str,
542 channel_id: &str,
543 root_ids: &[String],
544 ) -> Result<Vec<Result<Vec<Message>>>> {
545 let listings = Graph::channel_replies(self, team_id, channel_id, root_ids).await?;
546 Ok(listings
547 .into_iter()
548 .map(|listing| listing.map_err(Into::into))
549 .collect())
550 }
551
552 async fn send_chat_message(
553 &self,
554 chat_id: &str,
555 html: &str,
556 mentions: &[OutgoingMention],
557 extras: &MessageExtras,
558 ) -> Result<Message> {
559 Ok(Graph::send_chat_message(self, chat_id, html, mentions, extras).await?)
560 }
561
562 async fn send_channel_message(
563 &self,
564 team_id: &str,
565 channel_id: &str,
566 html: &str,
567 subject: Option<&str>,
568 mentions: &[OutgoingMention],
569 extras: &MessageExtras,
570 ) -> Result<Message> {
571 Ok(
572 Graph::send_channel_message(self, team_id, channel_id, html, subject, mentions, extras)
573 .await?,
574 )
575 }
576
577 async fn reply_to_channel_message(
578 &self,
579 team_id: &str,
580 channel_id: &str,
581 message_id: &str,
582 html: &str,
583 mentions: &[OutgoingMention],
584 extras: &MessageExtras,
585 ) -> Result<Message> {
586 Ok(Graph::reply_to_channel_message(
587 self, team_id, channel_id, message_id, html, mentions, extras,
588 )
589 .await?)
590 }
591
592 async fn mark_chat_read(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> {
593 Ok(Graph::mark_chat_read(self, chat_id, user_id, tenant_id).await?)
594 }
595
596 async fn mark_chat_unread(
597 &self,
598 chat_id: &str,
599 user_id: &str,
600 tenant_id: &str,
601 last_read_at: DateTime<Utc>,
602 ) -> Result<()> {
603 Ok(Graph::mark_chat_unread(self, chat_id, user_id, tenant_id, last_read_at).await?)
604 }
605
606 async fn hide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> {
607 Ok(Graph::hide_chat(self, chat_id, user_id, tenant_id).await?)
608 }
609
610 async fn unhide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> {
611 Ok(Graph::unhide_chat(self, chat_id, user_id, tenant_id).await?)
612 }
613
614 async fn leave_chat(&self, chat_id: &str, user_id: &str) -> Result<()> {
615 Ok(Graph::leave_chat(self, chat_id, user_id).await?)
616 }
617
618 async fn chat_states(&self) -> Result<Vec<ConversationState>> {
619 Ok(Conversations::new(self.session())
620 .conversation_states()
621 .await?)
622 }
623
624 async fn set_chat_muted(&self, chat_id: &str, muted: bool) -> Result<()> {
625 Ok(Conversations::new(self.session())
626 .set_alerts(chat_id, !muted)
627 .await?)
628 }
629
630 async fn set_channel_notifications(
631 &self,
632 channel_id: &str,
633 notifications: store::ChannelNotifications,
634 ) -> Result<()> {
635 Ok(Conversations::new(self.session())
636 .set_channel_notifications(
637 channel_id,
638 crate::folders::chatsvc_notifications(notifications),
639 )
640 .await?)
641 }
642
643 async fn send_typing(&self, conversation: &ConversationRef, active: bool) -> Result<()> {
644 Ok(Messages::new(self.session())
645 .send_typing(conversation, active)
646 .await?)
647 }
648
649 async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
650 if let Some(key) = chatsvc::reaction_key(reaction_type) {
651 return Ok(Messages::new(self.session())
652 .set_emotion(&conversation_ref(target), target.message_id(), &key)
653 .await?);
654 }
655 Ok(Graph::set_reaction(self, target, reaction_type).await?)
656 }
657
658 async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
659 let keys = chatsvc::reaction_keys(reaction_type);
660 if !keys.is_empty() {
661 let keys: Vec<&str> = keys.iter().map(String::as_str).collect();
662 return Ok(Messages::new(self.session())
663 .unset_emotions(&conversation_ref(target), target.message_id(), &keys)
664 .await?);
665 }
666 Ok(Graph::unset_reaction(self, target, reaction_type).await?)
667 }
668
669 async fn edit_message(
670 &self,
671 target: &MessageTarget,
672 html: &str,
673 mentions: &[OutgoingMention],
674 extras: &MessageExtras,
675 ) -> Result<()> {
676 match chatsvc_conversation(target) {
677 Some(conversation) if mentions.is_empty() => {
678 let html = format!("{}{html}{}", extras.kept_html(), extras.files_html());
679 Ok(Messages::new(self.session())
680 .edit_message(&conversation, target.message_id(), &html)
681 .await?)
682 }
683 Some(_) => Err(Error::Unsupported(
684 "editing a channel message with mentions",
685 )),
686 None => Ok(Graph::edit_message(self, target, html, mentions, extras).await?),
687 }
688 }
689
690 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
691 match chatsvc_conversation(target) {
692 Some(conversation) => Ok(Messages::new(self.session())
693 .soft_delete_message(&conversation, target.message_id())
694 .await?),
695 None => Ok(Graph::soft_delete_message(self, user_id, target).await?),
696 }
697 }
698
699 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat> {
700 Ok(Graph::create_one_on_one(self, my_user_id, user_id).await?)
701 }
702
703 async fn create_group(
704 &self,
705 my_user_id: &str,
706 user_ids: &[String],
707 topic: Option<&str>,
708 ) -> Result<Chat> {
709 Ok(Graph::create_group(self, my_user_id, user_ids, topic).await?)
710 }
711
712 async fn search_people(&self, query: &str) -> Result<Vec<User>> {
713 Ok(Graph::search_people(self, query).await?)
714 }
715
716 async fn reply_with_quote(
717 &self,
718 chat_id: &str,
719 quoted_message_id: &str,
720 html: &str,
721 mentions: &[OutgoingMention],
722 extras: &MessageExtras,
723 ) -> Result<Message> {
724 Ok(
725 Graph::reply_with_quote(self, chat_id, quoted_message_id, html, mentions, extras)
726 .await?,
727 )
728 }
729
730 async fn channel_files_folder(&self, team_id: &str, channel_id: &str) -> Result<DriveFolder> {
731 Ok(Graph::channel_files_folder(self, team_id, channel_id).await?)
732 }
733
734 async fn channel_files_root(&self, team_id: &str, channel_id: &str) -> Result<DriveEntry> {
735 Ok(Graph::channel_files_root(self, team_id, channel_id).await?)
736 }
737
738 async fn channel_tab_web_url(
739 &self,
740 team_id: &str,
741 channel_id: &str,
742 tab_id: &str,
743 ) -> Result<Option<String>> {
744 Ok(Graph::channel_tab_web_url(self, team_id, channel_id, tab_id).await?)
745 }
746
747 async fn list_children(&self, folder: &DriveFolder) -> Result<Vec<DriveEntry>> {
748 Ok(Graph::list_children(self, folder).await?)
749 }
750
751 async fn create_folder(&self, parent: &DriveFolder, name: &str) -> Result<DriveEntry> {
752 Ok(Graph::create_folder(self, parent, name).await?)
753 }
754
755 async fn upload_file(
756 &self,
757 destination: &UploadDestination,
758 file_name: &str,
759 bytes: &[u8],
760 progress: &(dyn Fn(u8) + Send + Sync),
761 ) -> Result<UploadedFile> {
762 Ok(Graph::upload_file(self, destination, file_name, bytes, progress).await?)
763 }
764
765 async fn share_file(&self, file: &UploadedFile, user_ids: &[String]) -> Result<()> {
766 Ok(Graph::share_item(self, file, user_ids).await?)
767 }
768
769 async fn delete_file(&self, file: &UploadedFile) -> Result<()> {
770 Ok(Graph::delete_drive_item(self, &file.drive_id, &file.item_id).await?)
771 }
772
773 async fn resolve_share(&self, open_url: &str) -> Result<SharedFile> {
774 Ok(Graph::resolve_share(self, open_url).await?)
775 }
776
777 async fn resolve_drive_item(&self, drive_id: &str, item_id: &str) -> Result<SharedFile> {
778 Ok(Graph::resolve_drive_item(self, drive_id, item_id).await?)
779 }
780
781 async fn download_range(
782 &self,
783 download_url: &str,
784 start: u64,
785 end: Option<u64>,
786 ) -> Result<Vec<u8>> {
787 Ok(Graph::download_range(self, download_url, start, end).await?)
788 }
789
790 async fn hosted_content(&self, url: &str) -> Result<Photo> {
791 if chatsvc::is_link_image_url(url) {
792 let image = Messages::new(self.session()).link_image(url).await?;
793 return Ok(Photo {
794 bytes: image.bytes,
795 content_type: image.content_type,
796 });
797 }
798 Ok(Graph::download_hosted_content(self, url).await?)
799 }
800
801 async fn external_image(&self, url: &str) -> Result<Photo> {
802 Ok(Graph::download_external_image(self, url).await?)
803 }
804
805 async fn user_photos(&self, user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
806 let photos = Graph::user_photos(self, user_ids).await?;
807 Ok(photos
808 .into_iter()
809 .map(|photo| photo.map_err(Into::into))
810 .collect())
811 }
812
813 async fn presences(&self, user_ids: &[String]) -> Result<Vec<Presence>> {
814 Ok(Graph::presences(self, user_ids).await?)
815 }
816
817 async fn person_profile(&self, user_id: &str) -> Result<PersonProfile> {
818 crate::profile::load_profile(self, user_id).await
819 }
820
821 async fn subscribe_presence(
822 &self,
823 endpoint_id: &str,
824 trouter_uri: &str,
825 user_ids: &[String],
826 purge: bool,
827 ) -> Result<()> {
828 Ok(Graph::subscribe_presence(self, endpoint_id, trouter_uri, user_ids, purge).await?)
829 }
830
831 async fn own_status(&self, user_id: &str) -> Result<PresenceStatus> {
832 Ok(PresenceService::new(self.session()).status(user_id).await?)
833 }
834
835 async fn set_availability(&self, forced: Option<&ForcedAvailability>) -> Result<()> {
836 Ok(PresenceService::new(self.session())
837 .set_availability(forced)
838 .await?)
839 }
840
841 async fn set_status_note(&self, note: Option<&StatusNote>) -> Result<()> {
842 Ok(PresenceService::new(self.session()).set_note(note).await?)
843 }
844
845 async fn set_work_location(
846 &self,
847 location: Option<(WorkLocationKind, DateTime<Utc>)>,
848 ) -> Result<()> {
849 Ok(PresenceService::new(self.session())
850 .set_work_location(location)
851 .await?)
852 }
853
854 async fn consumption_horizons(&self, conversation_id: &str) -> Result<Vec<MemberHorizon>> {
855 Ok(Receipts::new(self.session())
856 .consumption_horizons(conversation_id)
857 .await?)
858 }
859
860 async fn forward_messages(
861 &self,
862 source_conversation_id: &str,
863 target_conversation_id: &str,
864 message_ids: &[String],
865 comment_html: &str,
866 ) -> Result<ForwardResult> {
867 Ok(Messages::new(self.session())
868 .forward_messages(
869 source_conversation_id,
870 target_conversation_id,
871 message_ids,
872 comment_html,
873 )
874 .await?)
875 }
876
877 async fn set_saved(
878 &self,
879 conversation_id: &str,
880 root_id: &str,
881 message_id: &str,
882 saved: bool,
883 ) -> Result<()> {
884 Ok(Messages::new(self.session())
885 .set_saved(conversation_id, root_id, message_id, saved)
886 .await?)
887 }
888
889 async fn list_saved(&self) -> Result<Vec<SavedMessage>> {
890 Ok(Messages::new(self.session()).list_saved().await?)
891 }
892
893 async fn list_call_logs(&self) -> Result<Vec<CallLogEntry>> {
894 Ok(Messages::new(self.session()).list_call_logs().await?)
895 }
896
897 async fn chat_message_window(
898 &self,
899 chat_id: &str,
900 anchor_id: &str,
901 direction: WindowDirection,
902 page_size: usize,
903 ) -> Result<MessageWindow> {
904 Ok(Messages::new(self.session())
905 .message_window(chat_id, anchor_id, direction, page_size)
906 .await?)
907 }
908
909 async fn chat_messages_by_id(
910 &self,
911 chat_id: &str,
912 message_ids: &[String],
913 ) -> Result<Vec<Result<Message>>> {
914 let messages = Graph::chat_messages_by_id(self, chat_id, message_ids).await?;
915 Ok(messages
916 .into_iter()
917 .map(|message| message.map_err(Into::into))
918 .collect())
919 }
920
921 async fn chat_pins(&self, chat_id: &str) -> Result<Vec<PinnedMessage>> {
922 Ok(Pins::new(self.session()).chat_pins(chat_id).await?)
923 }
924
925 async fn pin_message(&self, chat_id: &str, message_id: &str) -> Result<()> {
926 Ok(Pins::new(self.session())
927 .pin_message(chat_id, message_id)
928 .await?)
929 }
930
931 async fn unpin_message(
932 &self,
933 chat_id: &str,
934 message_id: &str,
935 parent_id: Option<&str>,
936 ) -> Result<()> {
937 Ok(Pins::new(self.session())
938 .unpin_message(chat_id, message_id, parent_id)
939 .await?)
940 }
941
942 async fn chat_apps(&self, chat_id: &str) -> Result<Vec<ChatApp>> {
943 Ok(CardActions::new(self.session()).chat_apps(chat_id).await?)
944 }
945
946 async fn invoke_card(&self, invoke: InvokeRequest) -> Result<InvokeResponse> {
947 Ok(CardActions::new(self.session()).invoke(invoke).await?)
948 }
949
950 async fn message_links(
951 &self,
952 conversation: &ConversationRef,
953 page_size: usize,
954 ) -> Result<Vec<MessageLinks>> {
955 Ok(Messages::new(self.session())
956 .list_message_links(conversation, page_size)
957 .await?)
958 }
959
960 async fn set_message_links(&self, target: &MessageTarget, links_json: &str) -> Result<()> {
961 Ok(Messages::new(self.session())
962 .set_links(&conversation_ref(target), target.message_id(), links_json)
963 .await?)
964 }
965
966 async fn link_info(&self, url: &str) -> Result<LinkInfo> {
967 Ok(Messages::new(self.session()).link_info(url).await?)
968 }
969
970 async fn message_languages(
971 &self,
972 conversation: &ConversationRef,
973 page_size: usize,
974 ) -> Result<Vec<MessageLanguage>> {
975 Ok(Messages::new(self.session())
976 .list_language_stamps(conversation, page_size)
977 .await?)
978 }
979
980 async fn translate_chat_messages(
981 &self,
982 chat_id: &str,
983 to_language: &str,
984 messages: &[TranslateRequest],
985 trigger: &TranslationTrigger,
986 ) -> Result<Vec<Translation>> {
987 Ok(Translator::new(self.session())
988 .translate_chat(chat_id, to_language, messages, trigger)
989 .await?)
990 }
991
992 async fn translate_channel_messages(
993 &self,
994 team_id: &str,
995 channel_id: &str,
996 root_id: &str,
997 to_language: &str,
998 messages: &[TranslateRequest],
999 trigger: &TranslationTrigger,
1000 ) -> Result<Vec<Translation>> {
1001 Ok(Translator::new(self.session())
1002 .translate_channel(team_id, channel_id, root_id, to_language, messages, trigger)
1003 .await?)
1004 }
1005
1006 async fn translation_languages(&self, locale: &str) -> Result<Vec<Language>> {
1007 Ok(Translator::new(self.session()).languages(locale).await?)
1008 }
1009
1010 async fn language_settings(&self) -> Result<LanguageSettings> {
1011 Ok(LanguageSettingsClient::new(self.session()).read().await?)
1012 }
1013
1014 async fn recording_policy(&self) -> Result<RecordingPolicy> {
1015 Ok(LanguageSettingsClient::new(self.session())
1016 .read_recording_policy()
1017 .await?)
1018 }
1019
1020 async fn patch_language_settings(&self, patch: serde_json::Value) -> Result<()> {
1021 Ok(LanguageSettingsClient::new(self.session())
1022 .patch(patch)
1023 .await?)
1024 }
1025
1026 async fn search_gifs(&self, query: &str) -> Result<Vec<Gif>> {
1027 Ok(Gifs::new(self.session()).search(query).await?)
1028 }
1029
1030 async fn create_scheduled(
1031 &self,
1032 inner_thread_id: &str,
1033 html: &str,
1034 send_at: DateTime<Utc>,
1035 display_name: &str,
1036 ) -> Result<ScheduledDraft> {
1037 Ok(ScheduledDrafts::new(self.session())
1038 .create(inner_thread_id, html, send_at, display_name)
1039 .await?)
1040 }
1041
1042 async fn scheduled_drafts(&self) -> Result<Vec<ScheduledDraft>> {
1043 Ok(ScheduledDrafts::new(self.session()).list().await?)
1044 }
1045
1046 async fn update_scheduled(
1047 &self,
1048 draft: &ScheduledDraft,
1049 html: &str,
1050 display_name: &str,
1051 ) -> Result<ScheduledDraft> {
1052 Ok(ScheduledDrafts::new(self.session())
1053 .update(draft, html, display_name)
1054 .await?)
1055 }
1056
1057 async fn cancel_scheduled(&self, draft_id: &str) -> Result<()> {
1058 Ok(ScheduledDrafts::new(self.session())
1059 .cancel(draft_id)
1060 .await?)
1061 }
1062}
1063
1064fn chatsvc_conversation(target: &MessageTarget) -> Option<ConversationRef> {
1065 match target {
1066 MessageTarget::Chat { .. } => CHATSVC_CHAT_WRITES.then(|| conversation_ref(target)),
1067 MessageTarget::Channel { .. } => Some(conversation_ref(target)),
1068 }
1069}
1070
1071fn conversation_ref(target: &MessageTarget) -> ConversationRef {
1072 match target {
1073 MessageTarget::Chat { chat_id, .. } => ConversationRef::chat(chat_id),
1074 MessageTarget::Channel {
1075 channel_id,
1076 root_id: None,
1077 ..
1078 } => ConversationRef::channel_root(channel_id),
1079 MessageTarget::Channel {
1080 channel_id,
1081 root_id: Some(root_id),
1082 ..
1083 } => ConversationRef::channel_reply(channel_id, root_id),
1084 }
1085}
1086
1087impl From<graph::Page<Message>> for RemotePage {
1088 fn from(page: graph::Page<Message>) -> Self {
1089 RemotePage {
1090 items: page.items,
1091 next_link: page.next_link,
1092 }
1093 }
1094}
1095
1096impl From<graph::Page<Message>> for DeltaPage {
1097 fn from(page: graph::Page<Message>) -> Self {
1098 DeltaPage {
1099 items: page.items,
1100 next_link: page.next_link,
1101 delta_link: page.delta_link,
1102 }
1103 }
1104}
1105
1106impl From<graph::Page<Chat>> for ChatsPage {
1107 fn from(page: graph::Page<Chat>) -> Self {
1108 ChatsPage {
1109 items: page.items,
1110 next_link: page.next_link,
1111 }
1112 }
1113}
1114
1115#[cfg(test)]
1116mod tests {
1117 use super::*;
1118
1119 #[test]
1120 fn channel_targets_route_to_chatsvc() {
1121 let root = MessageTarget::channel("t", "19:c@thread.tacv2", "m1", None);
1122 let reply = MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1"));
1123 assert_eq!(
1124 chatsvc_conversation(&root),
1125 Some(ConversationRef::channel_root("19:c@thread.tacv2"))
1126 );
1127 assert_eq!(
1128 chatsvc_conversation(&reply),
1129 Some(ConversationRef::channel_reply("19:c@thread.tacv2", "m1"))
1130 );
1131 }
1132
1133 #[test]
1134 fn reactions_reach_chats_regardless_of_the_feature_switch() {
1135 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
1136 assert_eq!(
1137 conversation_ref(&chat),
1138 ConversationRef::chat("19:a@thread.v2")
1139 );
1140 }
1141
1142 #[test]
1143 fn chats_follow_the_feature_switch() {
1144 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
1145 assert_eq!(chatsvc_conversation(&chat).is_some(), CHATSVC_CHAT_WRITES);
1146 }
1147}