BlueGit

ict rusty-teams

Files

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

message_actions.rs

message_actions.rs 6.9 KiB Download Raw
1use chatsvc::{CallLogEntry, 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_stored(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 list_call_logs(&self) -> Result<Vec<CallLogEntry>> {
68 self.remote.list_call_logs().await
69 }
70
71 pub async fn chat_pins(&self, conversation_id: &str) -> Result<Vec<PinnedMessage>> {
72 match self.resolve_stored(conversation_id)? {
73 Conversation::Chat => self.remote.chat_pins(conversation_id).await,
74 Conversation::Channel { team_id } => {
75 let team_thread_id = self.team_thread_id(&team_id)?;
76 self.remote
77 .channel_pinned_posts(&team_thread_id, conversation_id)
78 .await
79 }
80 }
81 }
82
83 pub async fn pin_message(&self, conversation_id: &str, message_id: &str) -> Result<()> {
84 match self.resolve_stored(conversation_id)? {
85 Conversation::Chat => self.remote.pin_message(conversation_id, message_id).await?,
86 Conversation::Channel { team_id } => {
87 self.set_post_pinned(&team_id, conversation_id, message_id, true)
88 .await?
89 }
90 }
91 self.announce_pins(conversation_id);
92 Ok(())
93 }
94
95 pub async fn unpin_message(
96 &self,
97 conversation_id: &str,
98 message_id: &str,
99 parent_id: Option<&str>,
100 ) -> Result<()> {
101 match self.resolve_stored(conversation_id)? {
102 Conversation::Chat => {
103 self.remote
104 .unpin_message(conversation_id, message_id, parent_id)
105 .await?
106 }
107 Conversation::Channel { team_id } => {
108 self.set_post_pinned(&team_id, conversation_id, message_id, false)
109 .await?
110 }
111 }
112 self.announce_pins(conversation_id);
113 Ok(())
114 }
115
116 async fn set_post_pinned(
117 &self,
118 team_id: &str,
119 channel_id: &str,
120 post_id: &str,
121 pinned: bool,
122 ) -> Result<()> {
123 let is_reply = self
124 .store
125 .messages_by_id(channel_id, &[post_id.to_owned()])?
126 .get(post_id)
127 .is_some_and(|record| record.reply_to_id.is_some());
128 if is_reply {
129 return Err(Error::Unsupported("pinning a channel reply"));
130 }
131 let team_thread_id = self.team_thread_id(team_id)?;
132 self.remote
133 .set_post_pinned(&team_thread_id, channel_id, post_id, pinned)
134 .await
135 }
136
137 fn team_thread_id(&self, team_id: &str) -> Result<String> {
138 self.store
139 .team_thread_id(team_id)?
140 .ok_or_else(|| Error::UnknownConversation(team_id.to_owned()))
141 }
142
143 pub async fn message_link(&self, conversation_id: &str, message_id: &str) -> Result<String> {
144 let Conversation::Channel { team_id } = self.resolve(conversation_id).await? else {
145 let notes_oid = if conversation_id == NOTES_CHAT_ID {
146 Some(self.my_user_id().await?)
147 } else {
148 None
149 };
150 return Ok(chat_message_link(
151 conversation_id,
152 message_id,
153 notes_oid.as_deref(),
154 ));
155 };
156 let channel = self
157 .store
158 .channel(conversation_id)?
159 .ok_or_else(|| Error::UnknownConversation(conversation_id.to_owned()))?;
160 let team = self
161 .store
162 .team(&team_id)?
163 .ok_or_else(|| Error::UnknownConversation(team_id.clone()))?;
164 let record = self
165 .store
166 .messages_by_id(conversation_id, &[message_id.to_owned()])?
167 .remove(message_id)
168 .ok_or(Error::Unsupported("a link to an uncached message"))?;
169 let any_chat_id = self
170 .store
171 .sidebar()?
172 .chats
173 .into_iter()
174 .next()
175 .map(|chat| chat.id)
176 .ok_or(Error::Unsupported("a channel link without a known chat"))?;
177 let tenant_id = self.my_tenant_id(&any_chat_id).await?;
178 Ok(channel_message_link(&ChannelLinkInput {
179 tenant_id,
180 group_id: team_id,
181 channel_id: conversation_id.to_owned(),
182 team_name: team.name,
183 channel_name: channel.name,
184 root_id: record
185 .reply_to_id
186 .clone()
187 .unwrap_or_else(|| record.message_id.clone()),
188 message_id: record.message_id,
189 created_ms: record.created_at.timestamp_millis(),
190 }))
191 }
192
193 pub fn handle_pin_event(&self, event: &MessageEvent) {
194 if event.kind != EventKind::PinsChanged {
195 return;
196 }
197 if let Some(conversation_id) = event.conversation_id.as_deref() {
198 self.announce_pins(conversation_id);
199 }
200 }
201
202 fn announce_pins(&self, conversation_id: &str) {
203 let _ = self.events.send(CoreEvent::PinsChanged {
204 conversation_id: conversation_id.to_owned(),
205 });
206 }
207}