BlueGit

ict rusty-teams

Files

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

remote.rs

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