BlueGit

ict rusty-teams

Files

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

remote.rs

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