| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use chatsvc::{ |
| 4 | CallLogEntry, EventKind, ForwardResult, MessageEvent, PinnedMessage, PostFollowState, |
| 5 | SavedMessage, |
| 6 | }; |
| 7 | |
| 8 | use crate::engine::{Conversation, SyncEngine}; |
| 9 | use crate::error::{Error, Result}; |
| 10 | use crate::events::CoreEvent; |
| 11 | use crate::markdown::markdown_to_html; |
| 12 | use crate::message_link::{ChannelLinkInput, channel_message_link, chat_message_link}; |
| 13 | use crate::remote::Remote; |
| 14 | use crate::scheduled::NOTES_CHAT_ID; |
| 15 | |
| 16 | impl<R: Remote> SyncEngine<R> { |
| 17 | /// `message_ids` holds 1 to 5 ids, oldest first. |
| 18 | pub async fn forward_messages( |
| 19 | &self, |
| 20 | source_conversation_id: &str, |
| 21 | target_conversation_id: &str, |
| 22 | message_ids: &[String], |
| 23 | comment_markdown: &str, |
| 24 | ) -> Result<ForwardResult> { |
| 25 | let comment_html = if comment_markdown.is_empty() { |
| 26 | String::new() |
| 27 | } else { |
| 28 | markdown_to_html(comment_markdown) |
| 29 | }; |
| 30 | let result = self |
| 31 | .remote |
| 32 | .forward_messages( |
| 33 | source_conversation_id, |
| 34 | target_conversation_id, |
| 35 | message_ids, |
| 36 | &comment_html, |
| 37 | ) |
| 38 | .await?; |
| 39 | let refreshed = [result.thread_id.as_str(), target_conversation_id] |
| 40 | .into_iter() |
| 41 | .find(|conversation_id| self.resolve_stored(conversation_id).is_ok()); |
| 42 | if let Some(conversation_id) = refreshed |
| 43 | && let Err(error) = self.fetch_newer(conversation_id).await |
| 44 | { |
| 45 | self.report(format!("cannot refresh a forward target: {error}")); |
| 46 | } |
| 47 | Ok(result) |
| 48 | } |
| 49 | |
| 50 | /// `root_id` is the channel root post id, `None` in chats. |
| 51 | pub async fn set_saved( |
| 52 | &self, |
| 53 | conversation_id: &str, |
| 54 | message_id: &str, |
| 55 | root_id: Option<&str>, |
| 56 | saved: bool, |
| 57 | ) -> Result<()> { |
| 58 | self.remote |
| 59 | .set_saved( |
| 60 | conversation_id, |
| 61 | root_id.unwrap_or(message_id), |
| 62 | message_id, |
| 63 | saved, |
| 64 | ) |
| 65 | .await |
| 66 | } |
| 67 | |
| 68 | pub async fn set_post_followed( |
| 69 | &self, |
| 70 | channel_id: &str, |
| 71 | root_id: &str, |
| 72 | followed: bool, |
| 73 | ) -> Result<Option<String>> { |
| 74 | let my_user_id = self.my_user_id().await?; |
| 75 | self.remote |
| 76 | .set_post_followed(&my_user_id, channel_id, root_id, followed) |
| 77 | .await |
| 78 | } |
| 79 | |
| 80 | pub async fn post_follow_states( |
| 81 | &self, |
| 82 | channel_id: &str, |
| 83 | root_ids: &[String], |
| 84 | ) -> Result<HashMap<String, PostFollowState>> { |
| 85 | self.remote.post_follow_states(channel_id, root_ids).await |
| 86 | } |
| 87 | |
| 88 | pub async fn list_saved(&self) -> Result<Vec<SavedMessage>> { |
| 89 | self.remote.list_saved().await |
| 90 | } |
| 91 | |
| 92 | pub async fn list_call_logs(&self) -> Result<Vec<CallLogEntry>> { |
| 93 | self.remote.list_call_logs().await |
| 94 | } |
| 95 | |
| 96 | pub async fn chat_pins(&self, conversation_id: &str) -> Result<Vec<PinnedMessage>> { |
| 97 | match self.resolve_stored(conversation_id)? { |
| 98 | Conversation::Chat => self.remote.chat_pins(conversation_id).await, |
| 99 | Conversation::Channel { team_id } => { |
| 100 | let team_thread_id = self.team_thread_id(&team_id)?; |
| 101 | self.remote |
| 102 | .channel_pinned_posts(&team_thread_id, conversation_id) |
| 103 | .await |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | pub async fn pin_message(&self, conversation_id: &str, message_id: &str) -> Result<()> { |
| 109 | match self.resolve_stored(conversation_id)? { |
| 110 | Conversation::Chat => self.remote.pin_message(conversation_id, message_id).await?, |
| 111 | Conversation::Channel { team_id } => { |
| 112 | self.set_post_pinned(&team_id, conversation_id, message_id, true) |
| 113 | .await? |
| 114 | } |
| 115 | } |
| 116 | self.announce_pins(conversation_id); |
| 117 | Ok(()) |
| 118 | } |
| 119 | |
| 120 | pub async fn unpin_message( |
| 121 | &self, |
| 122 | conversation_id: &str, |
| 123 | message_id: &str, |
| 124 | parent_id: Option<&str>, |
| 125 | ) -> Result<()> { |
| 126 | match self.resolve_stored(conversation_id)? { |
| 127 | Conversation::Chat => { |
| 128 | self.remote |
| 129 | .unpin_message(conversation_id, message_id, parent_id) |
| 130 | .await? |
| 131 | } |
| 132 | Conversation::Channel { team_id } => { |
| 133 | self.set_post_pinned(&team_id, conversation_id, message_id, false) |
| 134 | .await? |
| 135 | } |
| 136 | } |
| 137 | self.announce_pins(conversation_id); |
| 138 | Ok(()) |
| 139 | } |
| 140 | |
| 141 | async fn set_post_pinned( |
| 142 | &self, |
| 143 | team_id: &str, |
| 144 | channel_id: &str, |
| 145 | post_id: &str, |
| 146 | pinned: bool, |
| 147 | ) -> Result<()> { |
| 148 | let is_reply = self |
| 149 | .store |
| 150 | .messages_by_id(channel_id, &[post_id.to_owned()])? |
| 151 | .get(post_id) |
| 152 | .is_some_and(|record| record.reply_to_id.is_some()); |
| 153 | if is_reply { |
| 154 | return Err(Error::Unsupported("pinning a channel reply")); |
| 155 | } |
| 156 | let team_thread_id = self.team_thread_id(team_id)?; |
| 157 | self.remote |
| 158 | .set_post_pinned(&team_thread_id, channel_id, post_id, pinned) |
| 159 | .await |
| 160 | } |
| 161 | |
| 162 | fn team_thread_id(&self, team_id: &str) -> Result<String> { |
| 163 | self.store |
| 164 | .team_thread_id(team_id)? |
| 165 | .ok_or_else(|| Error::UnknownConversation(team_id.to_owned())) |
| 166 | } |
| 167 | |
| 168 | pub async fn message_link(&self, conversation_id: &str, message_id: &str) -> Result<String> { |
| 169 | let Conversation::Channel { team_id } = self.resolve(conversation_id).await? else { |
| 170 | let notes_oid = if conversation_id == NOTES_CHAT_ID { |
| 171 | Some(self.my_user_id().await?) |
| 172 | } else { |
| 173 | None |
| 174 | }; |
| 175 | return Ok(chat_message_link( |
| 176 | conversation_id, |
| 177 | message_id, |
| 178 | notes_oid.as_deref(), |
| 179 | )); |
| 180 | }; |
| 181 | let channel = self |
| 182 | .store |
| 183 | .channel(conversation_id)? |
| 184 | .ok_or_else(|| Error::UnknownConversation(conversation_id.to_owned()))?; |
| 185 | let team = self |
| 186 | .store |
| 187 | .team(&team_id)? |
| 188 | .ok_or_else(|| Error::UnknownConversation(team_id.clone()))?; |
| 189 | let record = self |
| 190 | .store |
| 191 | .messages_by_id(conversation_id, &[message_id.to_owned()])? |
| 192 | .remove(message_id) |
| 193 | .ok_or(Error::Unsupported("a link to an uncached message"))?; |
| 194 | let any_chat_id = self |
| 195 | .store |
| 196 | .sidebar()? |
| 197 | .chats |
| 198 | .into_iter() |
| 199 | .next() |
| 200 | .map(|chat| chat.id) |
| 201 | .ok_or(Error::Unsupported("a channel link without a known chat"))?; |
| 202 | let tenant_id = self.my_tenant_id(&any_chat_id).await?; |
| 203 | Ok(channel_message_link(&ChannelLinkInput { |
| 204 | tenant_id, |
| 205 | group_id: team_id, |
| 206 | channel_id: conversation_id.to_owned(), |
| 207 | team_name: team.name, |
| 208 | channel_name: channel.name, |
| 209 | root_id: record |
| 210 | .reply_to_id |
| 211 | .clone() |
| 212 | .unwrap_or_else(|| record.message_id.clone()), |
| 213 | message_id: record.message_id, |
| 214 | created_ms: record.created_at.timestamp_millis(), |
| 215 | })) |
| 216 | } |
| 217 | |
| 218 | pub fn handle_pin_event(&self, event: &MessageEvent) { |
| 219 | if event.kind != EventKind::PinsChanged { |
| 220 | return; |
| 221 | } |
| 222 | if let Some(conversation_id) = event.conversation_id.as_deref() { |
| 223 | self.announce_pins(conversation_id); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | fn announce_pins(&self, conversation_id: &str) { |
| 228 | let _ = self.events.send(CoreEvent::PinsChanged { |
| 229 | conversation_id: conversation_id.to_owned(), |
| 230 | }); |
| 231 | } |
| 232 | } |