BlueGit

ict rusty-teams

Files

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

remote.rs

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