BlueGit

ict rusty-teams

Files

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

remote.rs

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