BlueGit

ict rusty-teams

Files

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

message_actions.rs

message_actions.rs 5.3 KiB Download Raw
1use chatsvc::{EventKind, ForwardResult, MessageEvent, PinnedMessage, SavedMessage};
2
3use crate::engine::{Conversation, SyncEngine};
4use crate::error::{Error, Result};
5use crate::events::CoreEvent;
6use crate::markdown::markdown_to_html;
7use crate::message_link::{ChannelLinkInput, channel_message_link, chat_message_link};
8use crate::remote::Remote;
9use crate::scheduled::NOTES_CHAT_ID;
10
11impl<R: Remote> SyncEngine<R> {
12 /// `message_ids` holds 1 to 5 ids, oldest first.
13 pub async fn forward_messages(
14 &self,
15 source_conversation_id: &str,
16 target_conversation_id: &str,
17 message_ids: &[String],
18 comment_markdown: &str,
19 ) -> Result<ForwardResult> {
20 let comment_html = if comment_markdown.is_empty() {
21 String::new()
22 } else {
23 markdown_to_html(comment_markdown)
24 };
25 let result = self
26 .remote
27 .forward_messages(
28 source_conversation_id,
29 target_conversation_id,
30 message_ids,
31 &comment_html,
32 )
33 .await?;
34 let refreshed = [result.thread_id.as_str(), target_conversation_id]
35 .into_iter()
36 .find(|conversation_id| self.resolve(conversation_id).is_ok());
37 if let Some(conversation_id) = refreshed
38 && let Err(error) = self.fetch_newer(conversation_id).await
39 {
40 self.report(format!("cannot refresh a forward target: {error}"));
41 }
42 Ok(result)
43 }
44
45 /// `root_id` is the channel root post id, `None` in chats.
46 pub async fn set_saved(
47 &self,
48 conversation_id: &str,
49 message_id: &str,
50 root_id: Option<&str>,
51 saved: bool,
52 ) -> Result<()> {
53 self.remote
54 .set_saved(
55 conversation_id,
56 root_id.unwrap_or(message_id),
57 message_id,
58 saved,
59 )
60 .await
61 }
62
63 pub async fn list_saved(&self) -> Result<Vec<SavedMessage>> {
64 self.remote.list_saved().await
65 }
66
67 pub async fn chat_pins(&self, chat_id: &str) -> Result<Vec<PinnedMessage>> {
68 self.ensure_chat(chat_id)?;
69 self.remote.chat_pins(chat_id).await
70 }
71
72 pub async fn pin_message(&self, chat_id: &str, message_id: &str) -> Result<()> {
73 self.ensure_chat(chat_id)?;
74 self.remote.pin_message(chat_id, message_id).await?;
75 self.announce_pins(chat_id);
76 Ok(())
77 }
78
79 pub async fn unpin_message(
80 &self,
81 chat_id: &str,
82 message_id: &str,
83 parent_id: Option<&str>,
84 ) -> Result<()> {
85 self.ensure_chat(chat_id)?;
86 self.remote
87 .unpin_message(chat_id, message_id, parent_id)
88 .await?;
89 self.announce_pins(chat_id);
90 Ok(())
91 }
92
93 pub async fn message_link(&self, conversation_id: &str, message_id: &str) -> Result<String> {
94 let Conversation::Channel { team_id } = self.resolve(conversation_id)? else {
95 let notes_oid = if conversation_id == NOTES_CHAT_ID {
96 Some(self.my_user_id().await?)
97 } else {
98 None
99 };
100 return Ok(chat_message_link(
101 conversation_id,
102 message_id,
103 notes_oid.as_deref(),
104 ));
105 };
106 let channel = self
107 .store
108 .channel(conversation_id)?
109 .ok_or_else(|| Error::UnknownConversation(conversation_id.to_owned()))?;
110 let team = self
111 .store
112 .team(&team_id)?
113 .ok_or_else(|| Error::UnknownConversation(team_id.clone()))?;
114 let record = self
115 .store
116 .messages_by_id(conversation_id, &[message_id.to_owned()])?
117 .remove(message_id)
118 .ok_or(Error::Unsupported("a link to an uncached message"))?;
119 let any_chat_id = self
120 .store
121 .sidebar()?
122 .chats
123 .into_iter()
124 .next()
125 .map(|chat| chat.id)
126 .ok_or(Error::Unsupported("a channel link without a known chat"))?;
127 let tenant_id = self.my_tenant_id(&any_chat_id).await?;
128 Ok(channel_message_link(&ChannelLinkInput {
129 tenant_id,
130 group_id: team_id,
131 channel_id: conversation_id.to_owned(),
132 team_name: team.name,
133 channel_name: channel.name,
134 root_id: record
135 .reply_to_id
136 .clone()
137 .unwrap_or_else(|| record.message_id.clone()),
138 message_id: record.message_id,
139 created_ms: record.created_at.timestamp_millis(),
140 }))
141 }
142
143 pub fn handle_pin_event(&self, event: &MessageEvent) {
144 if event.kind != EventKind::PinsChanged {
145 return;
146 }
147 if let Some(conversation_id) = event.conversation_id.as_deref() {
148 self.announce_pins(conversation_id);
149 }
150 }
151
152 fn ensure_chat(&self, conversation_id: &str) -> Result<()> {
153 match self.resolve(conversation_id)? {
154 Conversation::Chat => Ok(()),
155 Conversation::Channel { .. } => Err(Error::Unsupported("pinned messages in a channel")),
156 }
157 }
158
159 fn announce_pins(&self, conversation_id: &str) {
160 let _ = self.events.send(CoreEvent::PinsChanged {
161 conversation_id: conversation_id.to_owned(),
162 });
163 }
164}