| 1 | use chrono::Utc; |
| 2 | use graph::{MessageTarget, OutgoingMention}; |
| 3 | use store::MessageRecord; |
| 4 | |
| 5 | use crate::engine::{Conversation, META_USER_ID, SyncEngine}; |
| 6 | use crate::error::{Error, Result}; |
| 7 | use crate::mapping::{chat_record, message_record}; |
| 8 | use crate::markdown::markdown_to_html; |
| 9 | use crate::mentions::{MentionInput, apply_mentions, ensure_allowed_in_chat}; |
| 10 | use crate::remote::Remote; |
| 11 | |
| 12 | impl<R: Remote> SyncEngine<R> { |
| 13 | pub async fn send_message( |
| 14 | &self, |
| 15 | conversation_id: &str, |
| 16 | markdown: &str, |
| 17 | thread_root_id: Option<&str>, |
| 18 | ) -> Result<MessageRecord> { |
| 19 | self.send_message_with_mentions(conversation_id, markdown, thread_root_id, &[]) |
| 20 | .await |
| 21 | } |
| 22 | |
| 23 | pub async fn send_message_with_mentions( |
| 24 | &self, |
| 25 | conversation_id: &str, |
| 26 | markdown: &str, |
| 27 | thread_root_id: Option<&str>, |
| 28 | mentions: &[MentionInput], |
| 29 | ) -> Result<MessageRecord> { |
| 30 | let conversation = self.resolve(conversation_id)?; |
| 31 | let (html, mentions) = outgoing(&conversation, markdown, mentions)?; |
| 32 | let sent = match (conversation, thread_root_id) { |
| 33 | (Conversation::Chat, None) => { |
| 34 | self.remote |
| 35 | .send_chat_message(conversation_id, &html, &mentions) |
| 36 | .await? |
| 37 | } |
| 38 | (Conversation::Channel { team_id }, Some(root_id)) => { |
| 39 | self.remote |
| 40 | .reply_to_channel_message(&team_id, conversation_id, root_id, &html, &mentions) |
| 41 | .await? |
| 42 | } |
| 43 | (Conversation::Chat, Some(_)) => { |
| 44 | return Err(Error::Unsupported("a thread reply in a chat")); |
| 45 | } |
| 46 | (Conversation::Channel { .. }, None) => { |
| 47 | return Err(Error::Unsupported( |
| 48 | "a channel send without a thread root; use post_to_channel", |
| 49 | )); |
| 50 | } |
| 51 | }; |
| 52 | self.cache_sent(conversation_id, &sent) |
| 53 | } |
| 54 | |
| 55 | /// Chat: quoted reply (Graph beta `replyWithQuote`). Channel: reply in the thread of `message_id`. |
| 56 | pub async fn reply_to( |
| 57 | &self, |
| 58 | conversation_id: &str, |
| 59 | message_id: &str, |
| 60 | markdown: &str, |
| 61 | ) -> Result<MessageRecord> { |
| 62 | self.reply_to_with_mentions(conversation_id, message_id, markdown, &[]) |
| 63 | .await |
| 64 | } |
| 65 | |
| 66 | pub async fn reply_to_with_mentions( |
| 67 | &self, |
| 68 | conversation_id: &str, |
| 69 | message_id: &str, |
| 70 | markdown: &str, |
| 71 | mentions: &[MentionInput], |
| 72 | ) -> Result<MessageRecord> { |
| 73 | let conversation = self.resolve(conversation_id)?; |
| 74 | let (html, mentions) = outgoing(&conversation, markdown, mentions)?; |
| 75 | let sent = match conversation { |
| 76 | Conversation::Chat => { |
| 77 | self.remote |
| 78 | .reply_with_quote(conversation_id, message_id, &html, &mentions) |
| 79 | .await? |
| 80 | } |
| 81 | Conversation::Channel { team_id } => { |
| 82 | let root_id = self |
| 83 | .store |
| 84 | .messages_by_id(conversation_id, &[message_id.to_owned()])? |
| 85 | .remove(message_id) |
| 86 | .and_then(|record| record.reply_to_id) |
| 87 | .unwrap_or_else(|| message_id.to_owned()); |
| 88 | self.remote |
| 89 | .reply_to_channel_message(&team_id, conversation_id, &root_id, &html, &mentions) |
| 90 | .await? |
| 91 | } |
| 92 | }; |
| 93 | self.cache_sent(conversation_id, &sent) |
| 94 | } |
| 95 | |
| 96 | /// Own and not deleted. Chat messages, channel posts and channel replies alike. |
| 97 | pub fn can_edit(&self, record: &MessageRecord) -> bool { |
| 98 | self.is_own(record) |
| 99 | } |
| 100 | |
| 101 | pub fn can_delete(&self, record: &MessageRecord) -> bool { |
| 102 | self.is_own(record) |
| 103 | } |
| 104 | |
| 105 | fn is_own(&self, record: &MessageRecord) -> bool { |
| 106 | let my_user_id = self |
| 107 | .store |
| 108 | .meta(META_USER_ID) |
| 109 | .ok() |
| 110 | .flatten() |
| 111 | .unwrap_or_default(); |
| 112 | crate::stored::can_edit(record, &my_user_id) |
| 113 | } |
| 114 | |
| 115 | pub async fn post_to_channel( |
| 116 | &self, |
| 117 | channel_id: &str, |
| 118 | markdown: &str, |
| 119 | subject: Option<&str>, |
| 120 | ) -> Result<MessageRecord> { |
| 121 | self.post_to_channel_with_mentions(channel_id, markdown, subject, &[]) |
| 122 | .await |
| 123 | } |
| 124 | |
| 125 | pub async fn post_to_channel_with_mentions( |
| 126 | &self, |
| 127 | channel_id: &str, |
| 128 | markdown: &str, |
| 129 | subject: Option<&str>, |
| 130 | mentions: &[MentionInput], |
| 131 | ) -> Result<MessageRecord> { |
| 132 | let conversation = self.resolve(channel_id)?; |
| 133 | let Conversation::Channel { team_id } = &conversation else { |
| 134 | return Err(Error::Unsupported("posting a root message to a chat")); |
| 135 | }; |
| 136 | let (html, mentions) = outgoing(&conversation, markdown, mentions)?; |
| 137 | let sent = self |
| 138 | .remote |
| 139 | .send_channel_message(team_id, channel_id, &html, subject, &mentions) |
| 140 | .await?; |
| 141 | self.cache_sent(channel_id, &sent) |
| 142 | } |
| 143 | |
| 144 | pub async fn refresh_message( |
| 145 | &self, |
| 146 | conversation_id: &str, |
| 147 | message_id: &str, |
| 148 | ) -> Result<Option<MessageRecord>> { |
| 149 | let fetched = match self.resolve(conversation_id)? { |
| 150 | Conversation::Chat => self.remote.chat_message(conversation_id, message_id).await, |
| 151 | Conversation::Channel { team_id } => { |
| 152 | self.fetch_channel_message(&team_id, conversation_id, message_id) |
| 153 | .await |
| 154 | } |
| 155 | }; |
| 156 | let message = match fetched { |
| 157 | Ok(message) => message, |
| 158 | Err(error) => { |
| 159 | self.report(format!("cannot refresh a message: {error}")); |
| 160 | return Err(error); |
| 161 | } |
| 162 | }; |
| 163 | let Some(record) = message_record(conversation_id, &message) else { |
| 164 | return Ok(None); |
| 165 | }; |
| 166 | let delta = self.ingest(conversation_id, vec![record.clone()])?; |
| 167 | self.announce(conversation_id, !delta.is_empty()); |
| 168 | Ok(Some(record)) |
| 169 | } |
| 170 | |
| 171 | pub async fn mark_read(&self, conversation_id: &str) -> Result<()> { |
| 172 | if !matches!(self.resolve(conversation_id)?, Conversation::Chat) { |
| 173 | return Err(Error::Unsupported("marking a channel as read")); |
| 174 | } |
| 175 | let user_id = self.my_user_id().await?; |
| 176 | let tenant_id = self.my_tenant_id(conversation_id).await?; |
| 177 | self.remote |
| 178 | .mark_chat_read(conversation_id, &user_id, &tenant_id) |
| 179 | .await?; |
| 180 | self.store.mark_chat_read(conversation_id, Utc::now())?; |
| 181 | let _ = self.events.send(crate::events::CoreEvent::SidebarChanged); |
| 182 | Ok(()) |
| 183 | } |
| 184 | |
| 185 | pub async fn set_reaction( |
| 186 | &self, |
| 187 | conversation_id: &str, |
| 188 | message_id: &str, |
| 189 | reaction_type: &str, |
| 190 | ) -> Result<()> { |
| 191 | let target = self.message_target(conversation_id, message_id)?; |
| 192 | self.remote.set_reaction(&target, reaction_type).await?; |
| 193 | self.refresh_message(conversation_id, message_id).await?; |
| 194 | Ok(()) |
| 195 | } |
| 196 | |
| 197 | pub async fn unset_reaction( |
| 198 | &self, |
| 199 | conversation_id: &str, |
| 200 | message_id: &str, |
| 201 | reaction_type: &str, |
| 202 | ) -> Result<()> { |
| 203 | let target = self.message_target(conversation_id, message_id)?; |
| 204 | self.remote.unset_reaction(&target, reaction_type).await?; |
| 205 | self.refresh_message(conversation_id, message_id).await?; |
| 206 | Ok(()) |
| 207 | } |
| 208 | |
| 209 | pub async fn edit_message( |
| 210 | &self, |
| 211 | conversation_id: &str, |
| 212 | message_id: &str, |
| 213 | markdown: &str, |
| 214 | ) -> Result<()> { |
| 215 | self.edit_message_with_mentions(conversation_id, message_id, markdown, &[]) |
| 216 | .await |
| 217 | } |
| 218 | |
| 219 | pub async fn edit_message_with_mentions( |
| 220 | &self, |
| 221 | conversation_id: &str, |
| 222 | message_id: &str, |
| 223 | markdown: &str, |
| 224 | mentions: &[MentionInput], |
| 225 | ) -> Result<()> { |
| 226 | let conversation = self.resolve(conversation_id)?; |
| 227 | let (html, mentions) = outgoing(&conversation, markdown, mentions)?; |
| 228 | let target = self.message_target(conversation_id, message_id)?; |
| 229 | self.remote.edit_message(&target, &html, &mentions).await?; |
| 230 | self.refresh_message(conversation_id, message_id).await?; |
| 231 | Ok(()) |
| 232 | } |
| 233 | |
| 234 | pub async fn soft_delete_message(&self, conversation_id: &str, message_id: &str) -> Result<()> { |
| 235 | let target = self.message_target(conversation_id, message_id)?; |
| 236 | let user_id = self.my_user_id().await?; |
| 237 | self.remote.soft_delete_message(&user_id, &target).await?; |
| 238 | self.refresh_message(conversation_id, message_id).await?; |
| 239 | Ok(()) |
| 240 | } |
| 241 | |
| 242 | fn message_target(&self, conversation_id: &str, message_id: &str) -> Result<MessageTarget> { |
| 243 | Ok(match self.resolve(conversation_id)? { |
| 244 | Conversation::Chat => MessageTarget::chat(conversation_id, message_id), |
| 245 | Conversation::Channel { team_id } => { |
| 246 | let root_id = self |
| 247 | .store |
| 248 | .messages_by_id(conversation_id, &[message_id.to_owned()])? |
| 249 | .remove(message_id) |
| 250 | .and_then(|record| record.reply_to_id); |
| 251 | MessageTarget::channel(&team_id, conversation_id, message_id, root_id.as_deref()) |
| 252 | } |
| 253 | }) |
| 254 | } |
| 255 | |
| 256 | pub async fn create_one_on_one(&self, user_id: &str) -> Result<String> { |
| 257 | let my_user_id = self.my_user_id().await?; |
| 258 | let chat = self.remote.create_one_on_one(&my_user_id, user_id).await?; |
| 259 | self.store_new_chat(&chat, &my_user_id) |
| 260 | } |
| 261 | |
| 262 | pub async fn create_group(&self, user_ids: &[String], topic: Option<&str>) -> Result<String> { |
| 263 | let my_user_id = self.my_user_id().await?; |
| 264 | let chat = self |
| 265 | .remote |
| 266 | .create_group(&my_user_id, user_ids, topic) |
| 267 | .await?; |
| 268 | self.store_new_chat(&chat, &my_user_id) |
| 269 | } |
| 270 | |
| 271 | fn store_new_chat(&self, chat: &graph::Chat, my_user_id: &str) -> Result<String> { |
| 272 | if let Some(record) = chat_record(chat, my_user_id) { |
| 273 | self.store.upsert_chats(&[record])?; |
| 274 | let _ = self.events.send(crate::events::CoreEvent::SidebarChanged); |
| 275 | } |
| 276 | Ok(chat.id.clone()) |
| 277 | } |
| 278 | |
| 279 | fn cache_sent(&self, conversation_id: &str, sent: &graph::Message) -> Result<MessageRecord> { |
| 280 | let record = message_record(conversation_id, sent) |
| 281 | .ok_or(Error::Unsupported("a send answer without a creation time"))?; |
| 282 | self.store.upsert_messages(std::slice::from_ref(&record))?; |
| 283 | self.update_chat_preview(std::slice::from_ref(&record))?; |
| 284 | let mut state = self.store.sync_state(conversation_id)?.unwrap_or_default(); |
| 285 | if state |
| 286 | .newest_seen |
| 287 | .is_some_and(|newest| newest < record.created_at) |
| 288 | { |
| 289 | state.newest_seen = Some(record.created_at); |
| 290 | self.store.set_sync_state(conversation_id, &state)?; |
| 291 | } |
| 292 | self.announce(conversation_id, true); |
| 293 | Ok(record) |
| 294 | } |
| 295 | |
| 296 | async fn fetch_channel_message( |
| 297 | &self, |
| 298 | team_id: &str, |
| 299 | channel_id: &str, |
| 300 | message_id: &str, |
| 301 | ) -> Result<graph::Message> { |
| 302 | let known = self |
| 303 | .store |
| 304 | .messages_by_id(channel_id, &[message_id.to_owned()])?; |
| 305 | match known |
| 306 | .get(message_id) |
| 307 | .and_then(|record| record.reply_to_id.clone()) |
| 308 | { |
| 309 | Some(root_id) => { |
| 310 | self.remote |
| 311 | .channel_reply(team_id, channel_id, &root_id, message_id) |
| 312 | .await |
| 313 | } |
| 314 | None => { |
| 315 | self.remote |
| 316 | .channel_message(team_id, channel_id, message_id) |
| 317 | .await |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | fn outgoing( |
| 324 | conversation: &Conversation, |
| 325 | markdown: &str, |
| 326 | mentions: &[MentionInput], |
| 327 | ) -> Result<(String, Vec<OutgoingMention>)> { |
| 328 | if matches!(conversation, Conversation::Chat) { |
| 329 | ensure_allowed_in_chat(mentions)?; |
| 330 | } |
| 331 | Ok(apply_mentions(&markdown_to_html(markdown), mentions)) |
| 332 | } |