BlueGit

ict rusty-teams

Files

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

remote.rs

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