BlueGit

ict rusty-teams

Files

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

scheduled.rs

scheduled.rs 2.4 KiB Download Raw
1use chatsvc::{ConversationRef, ScheduledDraft};
2use chrono::{DateTime, Utc};
3
4use crate::engine::{Conversation, SyncEngine};
5use crate::error::{Error, Result};
6use crate::remote::Remote;
7
8pub const NOTES_CHAT_ID: &str = "48:notes";
9
10impl<R: Remote> SyncEngine<R> {
11 pub async fn schedule_message(
12 &self,
13 conversation_id: &str,
14 thread_root_id: Option<&str>,
15 html: &str,
16 send_at: DateTime<Utc>,
17 ) -> Result<ScheduledDraft> {
18 let inner_thread_id = self.inner_thread_id(conversation_id, thread_root_id)?;
19 let display_name = self.display_name().await?;
20 self.remote
21 .create_scheduled(&inner_thread_id, html, send_at, &display_name)
22 .await
23 }
24
25 pub async fn scheduled_messages(&self) -> Result<Vec<ScheduledDraft>> {
26 let mut drafts = self.remote.scheduled_drafts().await?;
27 drafts.sort_by_key(|draft| draft.send_at);
28 Ok(drafts)
29 }
30
31 pub async fn update_scheduled(
32 &self,
33 draft: &ScheduledDraft,
34 html: &str,
35 ) -> Result<ScheduledDraft> {
36 let display_name = self.display_name().await?;
37 self.remote
38 .update_scheduled(draft, html, &display_name)
39 .await
40 }
41
42 pub async fn cancel_scheduled(&self, draft_id: &str) -> Result<()> {
43 self.remote.cancel_scheduled(draft_id).await
44 }
45
46 fn inner_thread_id(
47 &self,
48 conversation_id: &str,
49 thread_root_id: Option<&str>,
50 ) -> Result<String> {
51 if conversation_id == NOTES_CHAT_ID {
52 return Err(Error::Unsupported("schedule send in the notes chat"));
53 }
54 let conversation = match (self.resolve(conversation_id)?, thread_root_id) {
55 (Conversation::Chat, None) => ConversationRef::chat(conversation_id),
56 (Conversation::Channel { .. }, None) => ConversationRef::channel_root(conversation_id),
57 (Conversation::Channel { .. }, Some(root_id)) => {
58 ConversationRef::channel_reply(conversation_id, root_id)
59 }
60 (Conversation::Chat, Some(_)) => {
61 return Err(Error::Unsupported("a thread reply in a chat"));
62 }
63 };
64 Ok(conversation.conversation_id())
65 }
66
67 async fn display_name(&self) -> Result<String> {
68 self.ensure_display_name().await?;
69 Ok(self.me().map(|me| me.display_name).unwrap_or_default())
70 }
71}