BlueGit

ict rusty-teams

Files

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

remote.rs

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