BlueGit

ict rusty-teams

Files

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

remote.rs

remote.rs 33.7 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, Messages, PinnedMessage, Pins, PresenceService, PresenceStatus, Receipts,
6 SavedMessage, ScheduledDraft, ScheduledDrafts, StatusNote, TranslateRequest, Translation,
7 TranslationTrigger, Translator, 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_pins(&self, _chat_id: &str) -> Result<Vec<PinnedMessage>> {
304 Err(Error::Unsupported("pinned messages"))
305 }
306
307 async fn pin_message(&self, _chat_id: &str, _message_id: &str) -> Result<()> {
308 Err(Error::Unsupported("pinning messages"))
309 }
310
311 async fn unpin_message(
312 &self,
313 _chat_id: &str,
314 _message_id: &str,
315 _parent_id: Option<&str>,
316 ) -> Result<()> {
317 Err(Error::Unsupported("pinning messages"))
318 }
319
320 async fn chat_apps(&self, _chat_id: &str) -> Result<Vec<ChatApp>> {
321 Err(Error::Unsupported("chat apps"))
322 }
323
324 async fn invoke_card(&self, _invoke: InvokeRequest) -> Result<InvokeResponse> {
325 Err(Error::Unsupported("card actions"))
326 }
327
328 async fn message_links(
329 &self,
330 _conversation: &ConversationRef,
331 _page_size: usize,
332 ) -> Result<Vec<MessageLinks>> {
333 Err(Error::Unsupported("link previews"))
334 }
335
336 async fn set_message_links(&self, _target: &MessageTarget, _links_json: &str) -> Result<()> {
337 Err(Error::Unsupported("link previews"))
338 }
339
340 async fn link_info(&self, _url: &str) -> Result<LinkInfo> {
341 Err(Error::Unsupported("link previews"))
342 }
343
344 async fn message_languages(
345 &self,
346 _conversation: &ConversationRef,
347 _page_size: usize,
348 ) -> Result<Vec<MessageLanguage>> {
349 Err(Error::Unsupported("message languages"))
350 }
351
352 async fn translate_chat_messages(
353 &self,
354 _chat_id: &str,
355 _to_language: &str,
356 _messages: &[TranslateRequest],
357 _trigger: &TranslationTrigger,
358 ) -> Result<Vec<Translation>> {
359 Err(Error::Unsupported("translation"))
360 }
361
362 async fn translate_channel_messages(
363 &self,
364 _team_id: &str,
365 _channel_id: &str,
366 _root_id: &str,
367 _to_language: &str,
368 _messages: &[TranslateRequest],
369 _trigger: &TranslationTrigger,
370 ) -> Result<Vec<Translation>> {
371 Err(Error::Unsupported("translation"))
372 }
373
374 async fn translation_languages(&self, _locale: &str) -> Result<Vec<Language>> {
375 Err(Error::Unsupported("translation"))
376 }
377
378 async fn language_settings(&self) -> Result<LanguageSettings> {
379 Err(Error::Unsupported("translation settings"))
380 }
381
382 async fn patch_language_settings(&self, _patch: serde_json::Value) -> Result<()> {
383 Err(Error::Unsupported("translation settings"))
384 }
385
386 async fn search_gifs(&self, _query: &str) -> Result<Vec<Gif>> {
387 Err(Error::Unsupported("GIF search"))
388 }
389
390 async fn create_scheduled(
391 &self,
392 _inner_thread_id: &str,
393 _html: &str,
394 _send_at: DateTime<Utc>,
395 _display_name: &str,
396 ) -> Result<ScheduledDraft> {
397 Err(Error::Unsupported("schedule send"))
398 }
399
400 async fn scheduled_drafts(&self) -> Result<Vec<ScheduledDraft>> {
401 Err(Error::Unsupported("schedule send"))
402 }
403
404 async fn update_scheduled(
405 &self,
406 _draft: &ScheduledDraft,
407 _html: &str,
408 _display_name: &str,
409 ) -> Result<ScheduledDraft> {
410 Err(Error::Unsupported("schedule send"))
411 }
412
413 async fn cancel_scheduled(&self, _draft_id: &str) -> Result<()> {
414 Err(Error::Unsupported("schedule send"))
415 }
416}
417
418impl Remote for Graph {
419 async fn me(&self) -> Result<User> {
420 Ok(Graph::me(self).await?)
421 }
422
423 async fn chats_page(&self, top: usize) -> Result<ChatsPage> {
424 Ok(Graph::chats_page(self, top).await?.into())
425 }
426
427 async fn chats_at(&self, next_link: &str) -> Result<ChatsPage> {
428 Ok(Graph::chats_at(self, next_link).await?.into())
429 }
430
431 async fn chat(&self, chat_id: &str) -> Result<Chat> {
432 Ok(Graph::chat(self, chat_id).await?)
433 }
434
435 async fn chat_members(&self, chat_id: &str) -> Result<Vec<Member>> {
436 Ok(Graph::chat_members(self, chat_id).await?)
437 }
438
439 async fn joined_teams(&self) -> Result<Vec<Team>> {
440 Ok(Graph::joined_teams(self).await?)
441 }
442
443 async fn channels_for_teams(&self, team_ids: &[String]) -> Result<Vec<Result<Vec<Channel>>>> {
444 let listings = Graph::channels_for_teams(self, team_ids).await?;
445 Ok(listings
446 .into_iter()
447 .map(|listing| listing.map_err(Into::into))
448 .collect())
449 }
450
451 async fn chat_messages(
452 &self,
453 chat_id: &str,
454 before: Option<DateTime<Utc>>,
455 top: usize,
456 ) -> Result<RemotePage> {
457 Ok(Graph::chat_messages(self, chat_id, before, top)
458 .await?
459 .into())
460 }
461
462 async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result<Message> {
463 Ok(Graph::chat_message(self, chat_id, message_id).await?)
464 }
465
466 async fn channel_messages(
467 &self,
468 team_id: &str,
469 channel_id: &str,
470 top: usize,
471 ) -> Result<RemotePage> {
472 Ok(Graph::channel_messages(self, team_id, channel_id, top)
473 .await?
474 .into())
475 }
476
477 async fn channel_messages_at(&self, next_link: &str) -> Result<RemotePage> {
478 Ok(Graph::channel_messages_at(self, next_link).await?.into())
479 }
480
481 async fn channel_message(
482 &self,
483 team_id: &str,
484 channel_id: &str,
485 message_id: &str,
486 ) -> Result<Message> {
487 Ok(Graph::channel_message(self, team_id, channel_id, message_id).await?)
488 }
489
490 async fn channel_reply(
491 &self,
492 team_id: &str,
493 channel_id: &str,
494 message_id: &str,
495 reply_id: &str,
496 ) -> Result<Message> {
497 Ok(Graph::channel_reply(self, team_id, channel_id, message_id, reply_id).await?)
498 }
499
500 async fn channel_delta(
501 &self,
502 team_id: &str,
503 channel_id: &str,
504 modified_after: Option<DateTime<Utc>>,
505 ) -> Result<DeltaPage> {
506 Ok(
507 Graph::channel_delta(self, team_id, channel_id, modified_after)
508 .await?
509 .into(),
510 )
511 }
512
513 async fn channel_delta_at(&self, link: &str) -> Result<DeltaPage> {
514 Ok(Graph::channel_delta_at(self, link).await?.into())
515 }
516
517 async fn channel_replies(
518 &self,
519 team_id: &str,
520 channel_id: &str,
521 root_ids: &[String],
522 ) -> Result<Vec<Result<Vec<Message>>>> {
523 let listings = Graph::channel_replies(self, team_id, channel_id, root_ids).await?;
524 Ok(listings
525 .into_iter()
526 .map(|listing| listing.map_err(Into::into))
527 .collect())
528 }
529
530 async fn send_chat_message(
531 &self,
532 chat_id: &str,
533 html: &str,
534 mentions: &[OutgoingMention],
535 extras: &MessageExtras,
536 ) -> Result<Message> {
537 Ok(Graph::send_chat_message(self, chat_id, html, mentions, extras).await?)
538 }
539
540 async fn send_channel_message(
541 &self,
542 team_id: &str,
543 channel_id: &str,
544 html: &str,
545 subject: Option<&str>,
546 mentions: &[OutgoingMention],
547 extras: &MessageExtras,
548 ) -> Result<Message> {
549 Ok(
550 Graph::send_channel_message(self, team_id, channel_id, html, subject, mentions, extras)
551 .await?,
552 )
553 }
554
555 async fn reply_to_channel_message(
556 &self,
557 team_id: &str,
558 channel_id: &str,
559 message_id: &str,
560 html: &str,
561 mentions: &[OutgoingMention],
562 extras: &MessageExtras,
563 ) -> Result<Message> {
564 Ok(Graph::reply_to_channel_message(
565 self, team_id, channel_id, message_id, html, mentions, extras,
566 )
567 .await?)
568 }
569
570 async fn mark_chat_read(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> {
571 Ok(Graph::mark_chat_read(self, chat_id, user_id, tenant_id).await?)
572 }
573
574 async fn mark_chat_unread(
575 &self,
576 chat_id: &str,
577 user_id: &str,
578 tenant_id: &str,
579 last_read_at: DateTime<Utc>,
580 ) -> Result<()> {
581 Ok(Graph::mark_chat_unread(self, chat_id, user_id, tenant_id, last_read_at).await?)
582 }
583
584 async fn hide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> {
585 Ok(Graph::hide_chat(self, chat_id, user_id, tenant_id).await?)
586 }
587
588 async fn unhide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> {
589 Ok(Graph::unhide_chat(self, chat_id, user_id, tenant_id).await?)
590 }
591
592 async fn leave_chat(&self, chat_id: &str, user_id: &str) -> Result<()> {
593 Ok(Graph::leave_chat(self, chat_id, user_id).await?)
594 }
595
596 async fn chat_states(&self) -> Result<Vec<ConversationState>> {
597 Ok(Conversations::new(self.session())
598 .conversation_states()
599 .await?)
600 }
601
602 async fn set_chat_muted(&self, chat_id: &str, muted: bool) -> Result<()> {
603 Ok(Conversations::new(self.session())
604 .set_alerts(chat_id, !muted)
605 .await?)
606 }
607
608 async fn set_channel_notifications(
609 &self,
610 channel_id: &str,
611 notifications: store::ChannelNotifications,
612 ) -> Result<()> {
613 Ok(Conversations::new(self.session())
614 .set_channel_notifications(
615 channel_id,
616 crate::folders::chatsvc_notifications(notifications),
617 )
618 .await?)
619 }
620
621 async fn send_typing(&self, conversation: &ConversationRef, active: bool) -> Result<()> {
622 Ok(Messages::new(self.session())
623 .send_typing(conversation, active)
624 .await?)
625 }
626
627 async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
628 if let Some(key) = chatsvc::emotion_key(reaction_type) {
629 return Ok(Messages::new(self.session())
630 .set_emotion(&conversation_ref(target), target.message_id(), key)
631 .await?);
632 }
633 Ok(Graph::set_reaction(self, target, reaction_type).await?)
634 }
635
636 async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
637 let keys = chatsvc::emotion_keys(reaction_type);
638 if !keys.is_empty() {
639 return Ok(Messages::new(self.session())
640 .unset_emotions(&conversation_ref(target), target.message_id(), keys)
641 .await?);
642 }
643 Ok(Graph::unset_reaction(self, target, reaction_type).await?)
644 }
645
646 async fn edit_message(
647 &self,
648 target: &MessageTarget,
649 html: &str,
650 mentions: &[OutgoingMention],
651 extras: &MessageExtras,
652 ) -> Result<()> {
653 match chatsvc_conversation(target) {
654 Some(conversation) if mentions.is_empty() => {
655 let html = format!("{}{html}{}", extras.kept_html(), extras.files_html());
656 Ok(Messages::new(self.session())
657 .edit_message(&conversation, target.message_id(), &html)
658 .await?)
659 }
660 Some(_) => Err(Error::Unsupported(
661 "editing a channel message with mentions",
662 )),
663 None => Ok(Graph::edit_message(self, target, html, mentions, extras).await?),
664 }
665 }
666
667 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
668 match chatsvc_conversation(target) {
669 Some(conversation) => Ok(Messages::new(self.session())
670 .soft_delete_message(&conversation, target.message_id())
671 .await?),
672 None => Ok(Graph::soft_delete_message(self, user_id, target).await?),
673 }
674 }
675
676 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat> {
677 Ok(Graph::create_one_on_one(self, my_user_id, user_id).await?)
678 }
679
680 async fn create_group(
681 &self,
682 my_user_id: &str,
683 user_ids: &[String],
684 topic: Option<&str>,
685 ) -> Result<Chat> {
686 Ok(Graph::create_group(self, my_user_id, user_ids, topic).await?)
687 }
688
689 async fn search_people(&self, query: &str) -> Result<Vec<User>> {
690 Ok(Graph::search_people(self, query).await?)
691 }
692
693 async fn reply_with_quote(
694 &self,
695 chat_id: &str,
696 quoted_message_id: &str,
697 html: &str,
698 mentions: &[OutgoingMention],
699 extras: &MessageExtras,
700 ) -> Result<Message> {
701 Ok(
702 Graph::reply_with_quote(self, chat_id, quoted_message_id, html, mentions, extras)
703 .await?,
704 )
705 }
706
707 async fn channel_files_folder(&self, team_id: &str, channel_id: &str) -> Result<DriveFolder> {
708 Ok(Graph::channel_files_folder(self, team_id, channel_id).await?)
709 }
710
711 async fn channel_files_root(&self, team_id: &str, channel_id: &str) -> Result<DriveEntry> {
712 Ok(Graph::channel_files_root(self, team_id, channel_id).await?)
713 }
714
715 async fn channel_tab_web_url(
716 &self,
717 team_id: &str,
718 channel_id: &str,
719 tab_id: &str,
720 ) -> Result<Option<String>> {
721 Ok(Graph::channel_tab_web_url(self, team_id, channel_id, tab_id).await?)
722 }
723
724 async fn list_children(&self, folder: &DriveFolder) -> Result<Vec<DriveEntry>> {
725 Ok(Graph::list_children(self, folder).await?)
726 }
727
728 async fn create_folder(&self, parent: &DriveFolder, name: &str) -> Result<DriveEntry> {
729 Ok(Graph::create_folder(self, parent, name).await?)
730 }
731
732 async fn upload_file(
733 &self,
734 destination: &UploadDestination,
735 file_name: &str,
736 bytes: &[u8],
737 progress: &(dyn Fn(u8) + Send + Sync),
738 ) -> Result<UploadedFile> {
739 Ok(Graph::upload_file(self, destination, file_name, bytes, progress).await?)
740 }
741
742 async fn share_file(&self, file: &UploadedFile, user_ids: &[String]) -> Result<()> {
743 Ok(Graph::share_item(self, file, user_ids).await?)
744 }
745
746 async fn delete_file(&self, file: &UploadedFile) -> Result<()> {
747 Ok(Graph::delete_drive_item(self, &file.drive_id, &file.item_id).await?)
748 }
749
750 async fn resolve_share(&self, open_url: &str) -> Result<SharedFile> {
751 Ok(Graph::resolve_share(self, open_url).await?)
752 }
753
754 async fn resolve_drive_item(&self, drive_id: &str, item_id: &str) -> Result<SharedFile> {
755 Ok(Graph::resolve_drive_item(self, drive_id, item_id).await?)
756 }
757
758 async fn download_range(
759 &self,
760 download_url: &str,
761 start: u64,
762 end: Option<u64>,
763 ) -> Result<Vec<u8>> {
764 Ok(Graph::download_range(self, download_url, start, end).await?)
765 }
766
767 async fn hosted_content(&self, url: &str) -> Result<Photo> {
768 if chatsvc::is_link_image_url(url) {
769 let image = Messages::new(self.session()).link_image(url).await?;
770 return Ok(Photo {
771 bytes: image.bytes,
772 content_type: image.content_type,
773 });
774 }
775 Ok(Graph::download_hosted_content(self, url).await?)
776 }
777
778 async fn external_image(&self, url: &str) -> Result<Photo> {
779 Ok(Graph::download_external_image(self, url).await?)
780 }
781
782 async fn user_photos(&self, user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
783 let photos = Graph::user_photos(self, user_ids).await?;
784 Ok(photos
785 .into_iter()
786 .map(|photo| photo.map_err(Into::into))
787 .collect())
788 }
789
790 async fn presences(&self, user_ids: &[String]) -> Result<Vec<Presence>> {
791 Ok(Graph::presences(self, user_ids).await?)
792 }
793
794 async fn person_profile(&self, user_id: &str) -> Result<PersonProfile> {
795 crate::profile::load_profile(self, user_id).await
796 }
797
798 async fn subscribe_presence(
799 &self,
800 endpoint_id: &str,
801 trouter_uri: &str,
802 user_ids: &[String],
803 purge: bool,
804 ) -> Result<()> {
805 Ok(Graph::subscribe_presence(self, endpoint_id, trouter_uri, user_ids, purge).await?)
806 }
807
808 async fn own_status(&self, user_id: &str) -> Result<PresenceStatus> {
809 Ok(PresenceService::new(self.session()).status(user_id).await?)
810 }
811
812 async fn set_availability(&self, forced: Option<&ForcedAvailability>) -> Result<()> {
813 Ok(PresenceService::new(self.session())
814 .set_availability(forced)
815 .await?)
816 }
817
818 async fn set_status_note(&self, note: Option<&StatusNote>) -> Result<()> {
819 Ok(PresenceService::new(self.session()).set_note(note).await?)
820 }
821
822 async fn set_work_location(
823 &self,
824 location: Option<(WorkLocationKind, DateTime<Utc>)>,
825 ) -> Result<()> {
826 Ok(PresenceService::new(self.session())
827 .set_work_location(location)
828 .await?)
829 }
830
831 async fn consumption_horizons(&self, conversation_id: &str) -> Result<Vec<MemberHorizon>> {
832 Ok(Receipts::new(self.session())
833 .consumption_horizons(conversation_id)
834 .await?)
835 }
836
837 async fn forward_messages(
838 &self,
839 source_conversation_id: &str,
840 target_conversation_id: &str,
841 message_ids: &[String],
842 comment_html: &str,
843 ) -> Result<ForwardResult> {
844 Ok(Messages::new(self.session())
845 .forward_messages(
846 source_conversation_id,
847 target_conversation_id,
848 message_ids,
849 comment_html,
850 )
851 .await?)
852 }
853
854 async fn set_saved(
855 &self,
856 conversation_id: &str,
857 root_id: &str,
858 message_id: &str,
859 saved: bool,
860 ) -> Result<()> {
861 Ok(Messages::new(self.session())
862 .set_saved(conversation_id, root_id, message_id, saved)
863 .await?)
864 }
865
866 async fn list_saved(&self) -> Result<Vec<SavedMessage>> {
867 Ok(Messages::new(self.session()).list_saved().await?)
868 }
869
870 async fn list_call_logs(&self) -> Result<Vec<CallLogEntry>> {
871 Ok(Messages::new(self.session()).list_call_logs().await?)
872 }
873
874 async fn chat_pins(&self, chat_id: &str) -> Result<Vec<PinnedMessage>> {
875 Ok(Pins::new(self.session()).chat_pins(chat_id).await?)
876 }
877
878 async fn pin_message(&self, chat_id: &str, message_id: &str) -> Result<()> {
879 Ok(Pins::new(self.session())
880 .pin_message(chat_id, message_id)
881 .await?)
882 }
883
884 async fn unpin_message(
885 &self,
886 chat_id: &str,
887 message_id: &str,
888 parent_id: Option<&str>,
889 ) -> Result<()> {
890 Ok(Pins::new(self.session())
891 .unpin_message(chat_id, message_id, parent_id)
892 .await?)
893 }
894
895 async fn chat_apps(&self, chat_id: &str) -> Result<Vec<ChatApp>> {
896 Ok(CardActions::new(self.session()).chat_apps(chat_id).await?)
897 }
898
899 async fn invoke_card(&self, invoke: InvokeRequest) -> Result<InvokeResponse> {
900 Ok(CardActions::new(self.session()).invoke(invoke).await?)
901 }
902
903 async fn message_links(
904 &self,
905 conversation: &ConversationRef,
906 page_size: usize,
907 ) -> Result<Vec<MessageLinks>> {
908 Ok(Messages::new(self.session())
909 .list_message_links(conversation, page_size)
910 .await?)
911 }
912
913 async fn set_message_links(&self, target: &MessageTarget, links_json: &str) -> Result<()> {
914 Ok(Messages::new(self.session())
915 .set_links(&conversation_ref(target), target.message_id(), links_json)
916 .await?)
917 }
918
919 async fn link_info(&self, url: &str) -> Result<LinkInfo> {
920 Ok(Messages::new(self.session()).link_info(url).await?)
921 }
922
923 async fn message_languages(
924 &self,
925 conversation: &ConversationRef,
926 page_size: usize,
927 ) -> Result<Vec<MessageLanguage>> {
928 Ok(Messages::new(self.session())
929 .list_language_stamps(conversation, page_size)
930 .await?)
931 }
932
933 async fn translate_chat_messages(
934 &self,
935 chat_id: &str,
936 to_language: &str,
937 messages: &[TranslateRequest],
938 trigger: &TranslationTrigger,
939 ) -> Result<Vec<Translation>> {
940 Ok(Translator::new(self.session())
941 .translate_chat(chat_id, to_language, messages, trigger)
942 .await?)
943 }
944
945 async fn translate_channel_messages(
946 &self,
947 team_id: &str,
948 channel_id: &str,
949 root_id: &str,
950 to_language: &str,
951 messages: &[TranslateRequest],
952 trigger: &TranslationTrigger,
953 ) -> Result<Vec<Translation>> {
954 Ok(Translator::new(self.session())
955 .translate_channel(team_id, channel_id, root_id, to_language, messages, trigger)
956 .await?)
957 }
958
959 async fn translation_languages(&self, locale: &str) -> Result<Vec<Language>> {
960 Ok(Translator::new(self.session()).languages(locale).await?)
961 }
962
963 async fn language_settings(&self) -> Result<LanguageSettings> {
964 Ok(LanguageSettingsClient::new(self.session()).read().await?)
965 }
966
967 async fn patch_language_settings(&self, patch: serde_json::Value) -> Result<()> {
968 Ok(LanguageSettingsClient::new(self.session())
969 .patch(patch)
970 .await?)
971 }
972
973 async fn search_gifs(&self, query: &str) -> Result<Vec<Gif>> {
974 Ok(Gifs::new(self.session()).search(query).await?)
975 }
976
977 async fn create_scheduled(
978 &self,
979 inner_thread_id: &str,
980 html: &str,
981 send_at: DateTime<Utc>,
982 display_name: &str,
983 ) -> Result<ScheduledDraft> {
984 Ok(ScheduledDrafts::new(self.session())
985 .create(inner_thread_id, html, send_at, display_name)
986 .await?)
987 }
988
989 async fn scheduled_drafts(&self) -> Result<Vec<ScheduledDraft>> {
990 Ok(ScheduledDrafts::new(self.session()).list().await?)
991 }
992
993 async fn update_scheduled(
994 &self,
995 draft: &ScheduledDraft,
996 html: &str,
997 display_name: &str,
998 ) -> Result<ScheduledDraft> {
999 Ok(ScheduledDrafts::new(self.session())
1000 .update(draft, html, display_name)
1001 .await?)
1002 }
1003
1004 async fn cancel_scheduled(&self, draft_id: &str) -> Result<()> {
1005 Ok(ScheduledDrafts::new(self.session())
1006 .cancel(draft_id)
1007 .await?)
1008 }
1009}
1010
1011fn chatsvc_conversation(target: &MessageTarget) -> Option<ConversationRef> {
1012 match target {
1013 MessageTarget::Chat { .. } => CHATSVC_CHAT_WRITES.then(|| conversation_ref(target)),
1014 MessageTarget::Channel { .. } => Some(conversation_ref(target)),
1015 }
1016}
1017
1018fn conversation_ref(target: &MessageTarget) -> ConversationRef {
1019 match target {
1020 MessageTarget::Chat { chat_id, .. } => ConversationRef::chat(chat_id),
1021 MessageTarget::Channel {
1022 channel_id,
1023 root_id: None,
1024 ..
1025 } => ConversationRef::channel_root(channel_id),
1026 MessageTarget::Channel {
1027 channel_id,
1028 root_id: Some(root_id),
1029 ..
1030 } => ConversationRef::channel_reply(channel_id, root_id),
1031 }
1032}
1033
1034impl From<graph::Page<Message>> for RemotePage {
1035 fn from(page: graph::Page<Message>) -> Self {
1036 RemotePage {
1037 items: page.items,
1038 next_link: page.next_link,
1039 }
1040 }
1041}
1042
1043impl From<graph::Page<Message>> for DeltaPage {
1044 fn from(page: graph::Page<Message>) -> Self {
1045 DeltaPage {
1046 items: page.items,
1047 next_link: page.next_link,
1048 delta_link: page.delta_link,
1049 }
1050 }
1051}
1052
1053impl From<graph::Page<Chat>> for ChatsPage {
1054 fn from(page: graph::Page<Chat>) -> Self {
1055 ChatsPage {
1056 items: page.items,
1057 next_link: page.next_link,
1058 }
1059 }
1060}
1061
1062#[cfg(test)]
1063mod tests {
1064 use super::*;
1065
1066 #[test]
1067 fn channel_targets_route_to_chatsvc() {
1068 let root = MessageTarget::channel("t", "19:c@thread.tacv2", "m1", None);
1069 let reply = MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1"));
1070 assert_eq!(
1071 chatsvc_conversation(&root),
1072 Some(ConversationRef::channel_root("19:c@thread.tacv2"))
1073 );
1074 assert_eq!(
1075 chatsvc_conversation(&reply),
1076 Some(ConversationRef::channel_reply("19:c@thread.tacv2", "m1"))
1077 );
1078 }
1079
1080 #[test]
1081 fn reactions_reach_chats_regardless_of_the_feature_switch() {
1082 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
1083 assert_eq!(
1084 conversation_ref(&chat),
1085 ConversationRef::chat("19:a@thread.v2")
1086 );
1087 }
1088
1089 #[test]
1090 fn chats_follow_the_feature_switch() {
1091 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
1092 assert_eq!(chatsvc_conversation(&chat).is_some(), CHATSVC_CHAT_WRITES);
1093 }
1094}