| 1 | use chatsvc::ConversationRef; |
| 2 | use chrono::{DateTime, Duration, Utc}; |
| 3 | use graph::{ |
| 4 | FileReference, MessageExtras, MessageTarget, OutgoingMention, UploadDestination, UploadedFile, |
| 5 | hosted_image_order, |
| 6 | }; |
| 7 | use store::MessageRecord; |
| 8 | |
| 9 | use crate::engine::{Conversation, META_USER_ID, SyncEngine}; |
| 10 | use crate::error::{Error, Result}; |
| 11 | use crate::external_image::external_image_url; |
| 12 | use crate::hidden_chats::cleared_until_ms; |
| 13 | use crate::links::LinkPreview; |
| 14 | use crate::mapping::message_record; |
| 15 | use crate::markdown::markdown_to_html; |
| 16 | use crate::mentions::{MentionInput, apply_mentions, ensure_allowed_in_chat}; |
| 17 | use crate::remote::Remote; |
| 18 | use crate::scheduled::NOTES_CHAT_ID; |
| 19 | |
| 20 | impl<R: Remote> SyncEngine<R> { |
| 21 | pub async fn send_message( |
| 22 | &self, |
| 23 | conversation_id: &str, |
| 24 | markdown: &str, |
| 25 | thread_root_id: Option<&str>, |
| 26 | ) -> Result<MessageRecord> { |
| 27 | self.send_message_with_mentions(conversation_id, markdown, thread_root_id, &[]) |
| 28 | .await |
| 29 | } |
| 30 | |
| 31 | pub async fn send_message_with_mentions( |
| 32 | &self, |
| 33 | conversation_id: &str, |
| 34 | markdown: &str, |
| 35 | thread_root_id: Option<&str>, |
| 36 | mentions: &[MentionInput], |
| 37 | ) -> Result<MessageRecord> { |
| 38 | self.send_message_with_extras( |
| 39 | conversation_id, |
| 40 | &markdown_to_html(markdown), |
| 41 | thread_root_id, |
| 42 | mentions, |
| 43 | &MessageExtras::default(), |
| 44 | ) |
| 45 | .await |
| 46 | } |
| 47 | |
| 48 | /// `html` is the message body; mentions are matched as `@name` in it. |
| 49 | pub async fn send_message_with_extras( |
| 50 | &self, |
| 51 | conversation_id: &str, |
| 52 | html: &str, |
| 53 | thread_root_id: Option<&str>, |
| 54 | mentions: &[MentionInput], |
| 55 | extras: &MessageExtras, |
| 56 | ) -> Result<MessageRecord> { |
| 57 | let conversation = self.resolve(conversation_id).await?; |
| 58 | let (html, mentions) = outgoing(&conversation, html, mentions)?; |
| 59 | let sent = match (conversation, thread_root_id) { |
| 60 | (Conversation::Chat, None) => { |
| 61 | self.remote |
| 62 | .send_chat_message(conversation_id, &html, &mentions, extras) |
| 63 | .await? |
| 64 | } |
| 65 | (Conversation::Channel { team_id }, Some(root_id)) => { |
| 66 | self.remote |
| 67 | .reply_to_channel_message( |
| 68 | &team_id, |
| 69 | conversation_id, |
| 70 | root_id, |
| 71 | &html, |
| 72 | &mentions, |
| 73 | extras, |
| 74 | ) |
| 75 | .await? |
| 76 | } |
| 77 | (Conversation::Chat, Some(_)) => { |
| 78 | return Err(Error::Unsupported("a thread reply in a chat")); |
| 79 | } |
| 80 | (Conversation::Channel { .. }, None) => { |
| 81 | return Err(Error::Unsupported( |
| 82 | "a channel send without a thread root; use post_to_channel", |
| 83 | )); |
| 84 | } |
| 85 | }; |
| 86 | self.cache_sent(conversation_id, &sent, &html, extras) |
| 87 | } |
| 88 | |
| 89 | /// Chat: quoted reply (Graph beta `replyWithQuote`). Channel: reply in the thread of `message_id`. |
| 90 | pub async fn reply_to( |
| 91 | &self, |
| 92 | conversation_id: &str, |
| 93 | message_id: &str, |
| 94 | markdown: &str, |
| 95 | ) -> Result<MessageRecord> { |
| 96 | self.reply_to_with_mentions(conversation_id, message_id, markdown, &[]) |
| 97 | .await |
| 98 | } |
| 99 | |
| 100 | pub async fn reply_to_with_mentions( |
| 101 | &self, |
| 102 | conversation_id: &str, |
| 103 | message_id: &str, |
| 104 | markdown: &str, |
| 105 | mentions: &[MentionInput], |
| 106 | ) -> Result<MessageRecord> { |
| 107 | self.reply_to_with_extras( |
| 108 | conversation_id, |
| 109 | message_id, |
| 110 | &markdown_to_html(markdown), |
| 111 | mentions, |
| 112 | &MessageExtras::default(), |
| 113 | ) |
| 114 | .await |
| 115 | } |
| 116 | |
| 117 | pub async fn reply_to_with_extras( |
| 118 | &self, |
| 119 | conversation_id: &str, |
| 120 | message_id: &str, |
| 121 | html: &str, |
| 122 | mentions: &[MentionInput], |
| 123 | extras: &MessageExtras, |
| 124 | ) -> Result<MessageRecord> { |
| 125 | let conversation = self.resolve(conversation_id).await?; |
| 126 | let (html, mentions) = outgoing(&conversation, html, mentions)?; |
| 127 | let sent = match conversation { |
| 128 | Conversation::Chat => { |
| 129 | self.remote |
| 130 | .reply_with_quote(conversation_id, message_id, &html, &mentions, extras) |
| 131 | .await? |
| 132 | } |
| 133 | Conversation::Channel { team_id } => { |
| 134 | let root_id = self |
| 135 | .store |
| 136 | .messages_by_id(conversation_id, &[message_id.to_owned()])? |
| 137 | .remove(message_id) |
| 138 | .and_then(|record| record.reply_to_id) |
| 139 | .unwrap_or_else(|| message_id.to_owned()); |
| 140 | self.remote |
| 141 | .reply_to_channel_message( |
| 142 | &team_id, |
| 143 | conversation_id, |
| 144 | &root_id, |
| 145 | &html, |
| 146 | &mentions, |
| 147 | extras, |
| 148 | ) |
| 149 | .await? |
| 150 | } |
| 151 | }; |
| 152 | self.cache_sent(conversation_id, &sent, &html, extras) |
| 153 | } |
| 154 | |
| 155 | /// Own and not deleted. Chat messages, channel posts and channel replies alike. |
| 156 | pub fn can_edit(&self, record: &MessageRecord) -> bool { |
| 157 | self.is_own(record) |
| 158 | } |
| 159 | |
| 160 | pub fn can_delete(&self, record: &MessageRecord) -> bool { |
| 161 | self.is_own(record) |
| 162 | } |
| 163 | |
| 164 | fn is_own(&self, record: &MessageRecord) -> bool { |
| 165 | let my_user_id = self |
| 166 | .store |
| 167 | .meta(META_USER_ID) |
| 168 | .ok() |
| 169 | .flatten() |
| 170 | .unwrap_or_default(); |
| 171 | crate::stored::can_edit(record, &my_user_id) |
| 172 | } |
| 173 | |
| 174 | pub async fn post_to_channel( |
| 175 | &self, |
| 176 | channel_id: &str, |
| 177 | markdown: &str, |
| 178 | subject: Option<&str>, |
| 179 | ) -> Result<MessageRecord> { |
| 180 | self.post_to_channel_with_mentions(channel_id, markdown, subject, &[]) |
| 181 | .await |
| 182 | } |
| 183 | |
| 184 | pub async fn post_to_channel_with_mentions( |
| 185 | &self, |
| 186 | channel_id: &str, |
| 187 | markdown: &str, |
| 188 | subject: Option<&str>, |
| 189 | mentions: &[MentionInput], |
| 190 | ) -> Result<MessageRecord> { |
| 191 | self.post_to_channel_with_extras( |
| 192 | channel_id, |
| 193 | &markdown_to_html(markdown), |
| 194 | subject, |
| 195 | mentions, |
| 196 | &MessageExtras::default(), |
| 197 | ) |
| 198 | .await |
| 199 | } |
| 200 | |
| 201 | pub async fn post_to_channel_with_extras( |
| 202 | &self, |
| 203 | channel_id: &str, |
| 204 | html: &str, |
| 205 | subject: Option<&str>, |
| 206 | mentions: &[MentionInput], |
| 207 | extras: &MessageExtras, |
| 208 | ) -> Result<MessageRecord> { |
| 209 | let conversation = self.resolve(channel_id).await?; |
| 210 | let Conversation::Channel { team_id } = &conversation else { |
| 211 | return Err(Error::Unsupported("posting a root message to a chat")); |
| 212 | }; |
| 213 | let (html, mentions) = outgoing(&conversation, html, mentions)?; |
| 214 | let sent = self |
| 215 | .remote |
| 216 | .send_channel_message(team_id, channel_id, &html, subject, &mentions, extras) |
| 217 | .await?; |
| 218 | self.cache_sent(channel_id, &sent, &html, extras) |
| 219 | } |
| 220 | |
| 221 | /// Upload then share; the app uses the two steps apart so a failed share does not upload again. |
| 222 | pub async fn upload_attachment( |
| 223 | &self, |
| 224 | conversation_id: &str, |
| 225 | file_name: &str, |
| 226 | bytes: &[u8], |
| 227 | progress: impl Fn(u8) + Send + Sync, |
| 228 | ) -> Result<FileReference> { |
| 229 | let uploaded = self |
| 230 | .upload_attachment_file(conversation_id, file_name, bytes, progress) |
| 231 | .await?; |
| 232 | self.share_attachment(conversation_id, &uploaded).await?; |
| 233 | uploaded.reference().ok_or_else(missing_attachment_id) |
| 234 | } |
| 235 | |
| 236 | /// Chat files go to the sender's "Microsoft Teams Chat Files", channel files to the channel's folder. |
| 237 | pub async fn upload_attachment_file( |
| 238 | &self, |
| 239 | conversation_id: &str, |
| 240 | file_name: &str, |
| 241 | bytes: &[u8], |
| 242 | progress: impl Fn(u8) + Send + Sync, |
| 243 | ) -> Result<UploadedFile> { |
| 244 | let destination = match self.resolve(conversation_id).await? { |
| 245 | Conversation::Chat => UploadDestination::ChatFiles, |
| 246 | Conversation::Channel { team_id } => { |
| 247 | UploadDestination::Folder(self.channel_folder(&team_id, conversation_id).await?) |
| 248 | } |
| 249 | }; |
| 250 | let uploaded = self |
| 251 | .remote |
| 252 | .upload_file(&destination, file_name, bytes, &progress) |
| 253 | .await?; |
| 254 | if uploaded.reference().is_none() { |
| 255 | let _ = self.remote.delete_file(&uploaded).await; |
| 256 | return Err(missing_attachment_id()); |
| 257 | } |
| 258 | Ok(uploaded) |
| 259 | } |
| 260 | |
| 261 | /// Read-only for the other chat members; channels need nothing. |
| 262 | pub async fn share_attachment( |
| 263 | &self, |
| 264 | conversation_id: &str, |
| 265 | uploaded: &UploadedFile, |
| 266 | ) -> Result<()> { |
| 267 | if !matches!(self.resolve(conversation_id).await?, Conversation::Chat) { |
| 268 | return Ok(()); |
| 269 | } |
| 270 | let other_members = self.other_member_ids(conversation_id).await?; |
| 271 | self.remote.share_file(uploaded, &other_members).await |
| 272 | } |
| 273 | |
| 274 | pub async fn discard_attachment(&self, uploaded: &UploadedFile) -> Result<()> { |
| 275 | self.remote.delete_file(uploaded).await |
| 276 | } |
| 277 | |
| 278 | async fn channel_folder(&self, team_id: &str, channel_id: &str) -> Result<graph::DriveFolder> { |
| 279 | let cached = self |
| 280 | .channel_folders |
| 281 | .lock() |
| 282 | .expect("channel folder cache poisoned") |
| 283 | .get(channel_id) |
| 284 | .cloned(); |
| 285 | if let Some(folder) = cached { |
| 286 | return Ok(folder); |
| 287 | } |
| 288 | let folder = self |
| 289 | .remote |
| 290 | .channel_files_folder(team_id, channel_id) |
| 291 | .await?; |
| 292 | self.channel_folders |
| 293 | .lock() |
| 294 | .expect("channel folder cache poisoned") |
| 295 | .insert(channel_id.to_owned(), folder.clone()); |
| 296 | Ok(folder) |
| 297 | } |
| 298 | |
| 299 | async fn other_member_ids(&self, chat_id: &str) -> Result<Vec<String>> { |
| 300 | if chat_id == NOTES_CHAT_ID { |
| 301 | return Ok(Vec::new()); |
| 302 | } |
| 303 | let my_user_id = self.my_user_id().await?; |
| 304 | let members = self |
| 305 | .store |
| 306 | .chat(chat_id)? |
| 307 | .map(|chat| chat.members) |
| 308 | .filter(|members| !members.is_empty()) |
| 309 | .ok_or_else(|| graph::Error::Upload("Chat members not loaded yet.".into()))?; |
| 310 | Ok(members |
| 311 | .into_iter() |
| 312 | .filter_map(|member| member.user_id) |
| 313 | .filter(|user_id| *user_id != my_user_id) |
| 314 | .collect()) |
| 315 | } |
| 316 | |
| 317 | pub async fn refresh_message( |
| 318 | &self, |
| 319 | conversation_id: &str, |
| 320 | message_id: &str, |
| 321 | ) -> Result<Option<MessageRecord>> { |
| 322 | let fetched = match self.resolve(conversation_id).await? { |
| 323 | Conversation::Chat => self.remote.chat_message(conversation_id, message_id).await, |
| 324 | Conversation::Channel { team_id } => { |
| 325 | self.fetch_channel_message(&team_id, conversation_id, message_id) |
| 326 | .await |
| 327 | } |
| 328 | }; |
| 329 | let message = match fetched { |
| 330 | Ok(message) => message, |
| 331 | Err(error) => { |
| 332 | self.report(format!("cannot refresh a message: {error}")); |
| 333 | return Err(error); |
| 334 | } |
| 335 | }; |
| 336 | let Some(record) = message_record(conversation_id, &message) else { |
| 337 | return Ok(None); |
| 338 | }; |
| 339 | let mut record = record; |
| 340 | self.apply_chat_emotions(conversation_id, std::slice::from_mut(&mut record)) |
| 341 | .await; |
| 342 | let delta = self.ingest(conversation_id, vec![record.clone()])?; |
| 343 | self.announce_stored_late(conversation_id, &delta); |
| 344 | let links_changed = self.attach_links(conversation_id, &mut [&mut record]).await; |
| 345 | self.announce(conversation_id, !delta.is_empty() || links_changed); |
| 346 | Ok(Some(record)) |
| 347 | } |
| 348 | |
| 349 | pub async fn attach_link_preview( |
| 350 | &self, |
| 351 | conversation_id: &str, |
| 352 | message_id: &str, |
| 353 | preview: &LinkPreview, |
| 354 | ) -> Result<()> { |
| 355 | let target = self.message_target(conversation_id, message_id)?; |
| 356 | let links_json = preview.links_json(); |
| 357 | self.remote.set_message_links(&target, &links_json).await?; |
| 358 | if self |
| 359 | .store |
| 360 | .set_message_links(conversation_id, message_id, &links_json)? |
| 361 | { |
| 362 | self.announce(conversation_id, true); |
| 363 | } |
| 364 | Ok(()) |
| 365 | } |
| 366 | |
| 367 | pub async fn mark_read(&self, conversation_id: &str) -> Result<()> { |
| 368 | if let Conversation::Channel { .. } = self.resolve(conversation_id).await? { |
| 369 | return self.mark_channel_read(conversation_id).await; |
| 370 | } |
| 371 | let user_id = self.my_user_id().await?; |
| 372 | let tenant_id = self.my_tenant_id(conversation_id).await?; |
| 373 | self.remote |
| 374 | .mark_chat_read(conversation_id, &user_id, &tenant_id) |
| 375 | .await?; |
| 376 | self.store.mark_chat_read(conversation_id, Utc::now())?; |
| 377 | let _ = self.events.send(crate::events::CoreEvent::SidebarChanged); |
| 378 | Ok(()) |
| 379 | } |
| 380 | |
| 381 | async fn mark_channel_read(&self, channel_id: &str) -> Result<()> { |
| 382 | let now = Utc::now(); |
| 383 | let last_message_at = self |
| 384 | .store |
| 385 | .channel(channel_id)? |
| 386 | .and_then(|channel| channel.last_message_at) |
| 387 | .unwrap_or(now); |
| 388 | self.remote |
| 389 | .set_read_horizon( |
| 390 | channel_id, |
| 391 | &chatsvc::read_horizon::read_up_to(last_message_at, now), |
| 392 | ) |
| 393 | .await?; |
| 394 | self.store.mark_channel_read(channel_id)?; |
| 395 | self.store.clear_channel_mentions(channel_id)?; |
| 396 | let _ = self.events.send(crate::events::CoreEvent::SidebarChanged); |
| 397 | Ok(()) |
| 398 | } |
| 399 | |
| 400 | pub async fn mark_all_channels_read(&self) -> Result<()> { |
| 401 | let horizon = self |
| 402 | .remote |
| 403 | .mark_all_channels_read(Utc::now().timestamp_millis()) |
| 404 | .await?; |
| 405 | *self.app_channel_horizon_ms.lock().unwrap() = horizon.channel_ms; |
| 406 | self.store.mark_all_channels_read()?; |
| 407 | let _ = self.events.send(crate::events::CoreEvent::SidebarChanged); |
| 408 | Ok(()) |
| 409 | } |
| 410 | |
| 411 | pub async fn mark_unread(&self, chat_id: &str) -> Result<()> { |
| 412 | let last_message_at = self |
| 413 | .store |
| 414 | .chat(chat_id)? |
| 415 | .and_then(|chat| chat.last_message_at) |
| 416 | .ok_or(Error::Unsupported( |
| 417 | "marking a chat without messages as unread", |
| 418 | ))?; |
| 419 | self.mark_unread_from(chat_id, last_message_at).await |
| 420 | } |
| 421 | |
| 422 | pub async fn mark_unread_from( |
| 423 | &self, |
| 424 | chat_id: &str, |
| 425 | message_created_at: DateTime<Utc>, |
| 426 | ) -> Result<()> { |
| 427 | let last_read_at = message_created_at - Duration::milliseconds(1); |
| 428 | let user_id = self.my_user_id().await?; |
| 429 | let tenant_id = self.my_tenant_id(chat_id).await?; |
| 430 | self.remote |
| 431 | .mark_chat_unread(chat_id, &user_id, &tenant_id, last_read_at) |
| 432 | .await?; |
| 433 | self.store.mark_chat_unread(chat_id, last_read_at)?; |
| 434 | let _ = self.events.send(crate::events::CoreEvent::SidebarChanged); |
| 435 | Ok(()) |
| 436 | } |
| 437 | |
| 438 | pub async fn set_chat_muted(&self, chat_id: &str, muted: bool) -> Result<()> { |
| 439 | self.remote.set_chat_muted(chat_id, muted).await?; |
| 440 | self.store.set_chat_muted(chat_id, muted)?; |
| 441 | let _ = self.events.send(crate::events::CoreEvent::SidebarChanged); |
| 442 | Ok(()) |
| 443 | } |
| 444 | |
| 445 | pub async fn set_channel_notifications( |
| 446 | &self, |
| 447 | channel_id: &str, |
| 448 | notifications: store::ChannelNotifications, |
| 449 | ) -> Result<()> { |
| 450 | self.remote |
| 451 | .set_channel_notifications(channel_id, notifications) |
| 452 | .await?; |
| 453 | self.store |
| 454 | .set_channel_notifications(channel_id, notifications)?; |
| 455 | let _ = self.events.send(crate::events::CoreEvent::SidebarChanged); |
| 456 | Ok(()) |
| 457 | } |
| 458 | |
| 459 | pub async fn hide_chat(&self, chat_id: &str) -> Result<()> { |
| 460 | let user_id = self.my_user_id().await?; |
| 461 | let tenant_id = self.my_tenant_id(chat_id).await?; |
| 462 | self.remote.hide_chat(chat_id, &user_id, &tenant_id).await?; |
| 463 | let hidden_after = self |
| 464 | .store |
| 465 | .chat(chat_id)? |
| 466 | .and_then(|chat| chat.last_message_at) |
| 467 | .unwrap_or_else(Utc::now); |
| 468 | self.with_locally_hidden(|locally| locally.hide(chat_id, hidden_after))?; |
| 469 | self.store.remove_chats(&[chat_id.to_owned()])?; |
| 470 | let _ = self.events.send(crate::events::CoreEvent::SidebarChanged); |
| 471 | Ok(()) |
| 472 | } |
| 473 | |
| 474 | pub async fn unhide_chat(&self, chat_id: &str) -> Result<()> { |
| 475 | let user_id = self.my_user_id().await?; |
| 476 | let tenant_id = self.my_tenant_id(chat_id).await?; |
| 477 | self.remote |
| 478 | .unhide_chat(chat_id, &user_id, &tenant_id) |
| 479 | .await?; |
| 480 | let now = Utc::now(); |
| 481 | self.with_locally_hidden(|locally| locally.unhide(chat_id, now))?; |
| 482 | self.refresh_sidebar_full().await?; |
| 483 | Ok(()) |
| 484 | } |
| 485 | |
| 486 | /// Teams' "Remove chat history": the chat stays hidden until a message newer than the last one. |
| 487 | pub async fn remove_chat_history(&self, chat_id: &str) -> Result<()> { |
| 488 | let last_message_at = self |
| 489 | .store |
| 490 | .chat(chat_id)? |
| 491 | .and_then(|chat| chat.last_message_at); |
| 492 | let cleared_until_ms = cleared_until_ms(last_message_at, Utc::now()); |
| 493 | self.remote |
| 494 | .clear_chat_history(chat_id, cleared_until_ms) |
| 495 | .await?; |
| 496 | self.with_locally_hidden(|locally| { |
| 497 | locally.hide(chat_id, last_message_at.unwrap_or_else(Utc::now)) |
| 498 | })?; |
| 499 | self.store.remove_chats(&[chat_id.to_owned()])?; |
| 500 | let _ = self.events.send(crate::events::CoreEvent::SidebarChanged); |
| 501 | Ok(()) |
| 502 | } |
| 503 | |
| 504 | pub async fn rename_chat(&self, chat_id: &str, topic: &str) -> Result<()> { |
| 505 | self.remote.set_chat_topic(chat_id, topic).await?; |
| 506 | if let Some(mut chat) = self.store.chat(chat_id)? { |
| 507 | chat.title = topic.to_owned(); |
| 508 | self.store.upsert_chats(std::slice::from_ref(&chat))?; |
| 509 | } |
| 510 | let _ = self.events.send(crate::events::CoreEvent::SidebarChanged); |
| 511 | Ok(()) |
| 512 | } |
| 513 | |
| 514 | pub async fn leave_chat(&self, chat_id: &str) -> Result<()> { |
| 515 | let user_id = self.my_user_id().await?; |
| 516 | self.remote.remove_chat_member(chat_id, &user_id).await?; |
| 517 | self.store.remove_chats(&[chat_id.to_owned()])?; |
| 518 | let _ = self.events.send(crate::events::CoreEvent::SidebarChanged); |
| 519 | Ok(()) |
| 520 | } |
| 521 | |
| 522 | pub async fn remove_chat_member(&self, chat_id: &str, user_id: &str) -> Result<()> { |
| 523 | self.remote.remove_chat_member(chat_id, user_id).await?; |
| 524 | if let Some(mut chat) = self.store.chat(chat_id)? { |
| 525 | chat.members |
| 526 | .retain(|member| member.user_id.as_deref() != Some(user_id)); |
| 527 | self.store.upsert_chats(std::slice::from_ref(&chat))?; |
| 528 | } |
| 529 | let _ = self.events.send(crate::events::CoreEvent::SidebarChanged); |
| 530 | Ok(()) |
| 531 | } |
| 532 | |
| 533 | pub async fn add_chat_members( |
| 534 | &self, |
| 535 | chat_id: &str, |
| 536 | people: &[store::MemberRecord], |
| 537 | share: chatsvc::HistoryShare, |
| 538 | ) -> Result<()> { |
| 539 | let user_ids: Vec<String> = people |
| 540 | .iter() |
| 541 | .filter_map(|person| person.user_id.clone()) |
| 542 | .collect(); |
| 543 | self.remote |
| 544 | .add_chat_members(chat_id, &user_ids, share) |
| 545 | .await?; |
| 546 | if let Some(mut chat) = self.store.chat(chat_id)? { |
| 547 | crate::roster_event::merge_members(&mut chat.members, people); |
| 548 | self.store.upsert_chats(std::slice::from_ref(&chat))?; |
| 549 | } |
| 550 | let _ = self.events.send(crate::events::CoreEvent::SidebarChanged); |
| 551 | Ok(()) |
| 552 | } |
| 553 | |
| 554 | pub async fn send_typing( |
| 555 | &self, |
| 556 | conversation_id: &str, |
| 557 | thread_root_id: Option<&str>, |
| 558 | active: bool, |
| 559 | ) -> Result<()> { |
| 560 | let conversation = match (self.resolve(conversation_id).await?, thread_root_id) { |
| 561 | (Conversation::Chat, _) => ConversationRef::chat(conversation_id), |
| 562 | (Conversation::Channel { .. }, None) => ConversationRef::channel_root(conversation_id), |
| 563 | (Conversation::Channel { .. }, Some(root_id)) => { |
| 564 | ConversationRef::channel_reply(conversation_id, root_id) |
| 565 | } |
| 566 | }; |
| 567 | self.remote.send_typing(&conversation, active).await |
| 568 | } |
| 569 | |
| 570 | pub async fn set_reaction( |
| 571 | &self, |
| 572 | conversation_id: &str, |
| 573 | message_id: &str, |
| 574 | reaction_type: &str, |
| 575 | ) -> Result<()> { |
| 576 | let target = self.message_target(conversation_id, message_id)?; |
| 577 | self.remote.set_reaction(&target, reaction_type).await?; |
| 578 | self.refresh_message(conversation_id, message_id).await?; |
| 579 | Ok(()) |
| 580 | } |
| 581 | |
| 582 | pub async fn unset_reaction( |
| 583 | &self, |
| 584 | conversation_id: &str, |
| 585 | message_id: &str, |
| 586 | reaction_type: &str, |
| 587 | ) -> Result<()> { |
| 588 | let target = self.message_target(conversation_id, message_id)?; |
| 589 | self.remote.unset_reaction(&target, reaction_type).await?; |
| 590 | self.refresh_message(conversation_id, message_id).await?; |
| 591 | Ok(()) |
| 592 | } |
| 593 | |
| 594 | pub async fn edit_message( |
| 595 | &self, |
| 596 | conversation_id: &str, |
| 597 | message_id: &str, |
| 598 | markdown: &str, |
| 599 | ) -> Result<()> { |
| 600 | self.edit_message_with_mentions(conversation_id, message_id, markdown, &[]) |
| 601 | .await |
| 602 | } |
| 603 | |
| 604 | pub async fn edit_message_with_mentions( |
| 605 | &self, |
| 606 | conversation_id: &str, |
| 607 | message_id: &str, |
| 608 | markdown: &str, |
| 609 | mentions: &[MentionInput], |
| 610 | ) -> Result<()> { |
| 611 | self.edit_message_html( |
| 612 | conversation_id, |
| 613 | message_id, |
| 614 | &markdown_to_html(markdown), |
| 615 | mentions, |
| 616 | ) |
| 617 | .await |
| 618 | } |
| 619 | |
| 620 | pub async fn edit_message_html( |
| 621 | &self, |
| 622 | conversation_id: &str, |
| 623 | message_id: &str, |
| 624 | html: &str, |
| 625 | mentions: &[MentionInput], |
| 626 | ) -> Result<()> { |
| 627 | let conversation = self.resolve(conversation_id).await?; |
| 628 | let (mut html, mentions) = outgoing(&conversation, html, mentions)?; |
| 629 | let target = self.message_target(conversation_id, message_id)?; |
| 630 | let mut extras = MessageExtras::default(); |
| 631 | if let Some(record) = self |
| 632 | .store |
| 633 | .messages_by_id(conversation_id, &[message_id.to_owned()])? |
| 634 | .remove(message_id) |
| 635 | { |
| 636 | let preserved = crate::stored::edit_preserved(&record); |
| 637 | html.push_str(&preserved.images_html); |
| 638 | extras.files = preserved.files; |
| 639 | extras.kept = preserved.kept; |
| 640 | } |
| 641 | self.remote |
| 642 | .edit_message(&target, &html, &mentions, &extras) |
| 643 | .await?; |
| 644 | self.refresh_message(conversation_id, message_id).await?; |
| 645 | Ok(()) |
| 646 | } |
| 647 | |
| 648 | pub async fn soft_delete_message(&self, conversation_id: &str, message_id: &str) -> Result<()> { |
| 649 | let target = self.message_target(conversation_id, message_id)?; |
| 650 | let user_id = self.my_user_id().await?; |
| 651 | self.remote.soft_delete_message(&user_id, &target).await?; |
| 652 | self.refresh_message(conversation_id, message_id).await?; |
| 653 | Ok(()) |
| 654 | } |
| 655 | |
| 656 | pub async fn undo_soft_delete_message( |
| 657 | &self, |
| 658 | conversation_id: &str, |
| 659 | message_id: &str, |
| 660 | ) -> Result<()> { |
| 661 | let target = self.message_target(conversation_id, message_id)?; |
| 662 | let user_id = self.my_user_id().await?; |
| 663 | self.remote |
| 664 | .undo_soft_delete_message(&user_id, &target) |
| 665 | .await?; |
| 666 | self.refresh_message(conversation_id, message_id).await?; |
| 667 | Ok(()) |
| 668 | } |
| 669 | |
| 670 | fn message_target(&self, conversation_id: &str, message_id: &str) -> Result<MessageTarget> { |
| 671 | Ok(match self.resolve_stored(conversation_id)? { |
| 672 | Conversation::Chat => MessageTarget::chat(conversation_id, message_id), |
| 673 | Conversation::Channel { team_id } => { |
| 674 | let root_id = self |
| 675 | .store |
| 676 | .messages_by_id(conversation_id, &[message_id.to_owned()])? |
| 677 | .remove(message_id) |
| 678 | .and_then(|record| record.reply_to_id); |
| 679 | MessageTarget::channel(&team_id, conversation_id, message_id, root_id.as_deref()) |
| 680 | } |
| 681 | }) |
| 682 | } |
| 683 | |
| 684 | pub async fn create_one_on_one(&self, user_id: &str) -> Result<String> { |
| 685 | let my_user_id = self.my_user_id().await?; |
| 686 | let chat = self.remote.create_one_on_one(&my_user_id, user_id).await?; |
| 687 | self.store_new_chat(&chat, &my_user_id) |
| 688 | } |
| 689 | |
| 690 | pub async fn create_group(&self, user_ids: &[String], topic: Option<&str>) -> Result<String> { |
| 691 | let my_user_id = self.my_user_id().await?; |
| 692 | let chat = self |
| 693 | .remote |
| 694 | .create_group(&my_user_id, user_ids, topic) |
| 695 | .await?; |
| 696 | self.store_new_chat(&chat, &my_user_id) |
| 697 | } |
| 698 | |
| 699 | fn store_new_chat(&self, chat: &graph::Chat, my_user_id: &str) -> Result<String> { |
| 700 | self.store |
| 701 | .upsert_chats(&[self.partner_chat_record(chat, my_user_id)])?; |
| 702 | let _ = self.events.send(crate::events::CoreEvent::SidebarChanged); |
| 703 | Ok(chat.id.clone()) |
| 704 | } |
| 705 | |
| 706 | fn seed_sent_images( |
| 707 | &self, |
| 708 | record: &MessageRecord, |
| 709 | html: &str, |
| 710 | extras: &MessageExtras, |
| 711 | ) -> Result<()> { |
| 712 | let uploaded: Vec<_> = crate::stored::images(record) |
| 713 | .into_iter() |
| 714 | .filter(|image| external_image_url(&image.url).is_none()) |
| 715 | .collect(); |
| 716 | let order = hosted_image_order(html, extras); |
| 717 | if uploaded.len() != order.len() { |
| 718 | return Ok(()); |
| 719 | } |
| 720 | let seeds: Vec<_> = uploaded |
| 721 | .iter() |
| 722 | .zip(order) |
| 723 | .map(|(image, index)| (image.key(), &extras.images[index])) |
| 724 | .collect(); |
| 725 | self.seed_images(&seeds) |
| 726 | } |
| 727 | |
| 728 | fn cache_sent( |
| 729 | &self, |
| 730 | conversation_id: &str, |
| 731 | sent: &graph::Message, |
| 732 | html: &str, |
| 733 | extras: &MessageExtras, |
| 734 | ) -> Result<MessageRecord> { |
| 735 | let record = message_record(conversation_id, sent) |
| 736 | .ok_or(Error::Unsupported("a send answer without a creation time"))?; |
| 737 | let _ = self.seed_sent_images(&record, html, extras); |
| 738 | self.store.upsert_messages(std::slice::from_ref(&record))?; |
| 739 | self.update_chat_preview(std::slice::from_ref(&record))?; |
| 740 | let mut state = self.store.sync_state(conversation_id)?.unwrap_or_default(); |
| 741 | if state |
| 742 | .newest_seen |
| 743 | .is_some_and(|newest| newest < record.created_at) |
| 744 | { |
| 745 | state.newest_seen = Some(record.created_at); |
| 746 | self.store.set_sync_state(conversation_id, &state)?; |
| 747 | } |
| 748 | self.announce(conversation_id, true); |
| 749 | Ok(record) |
| 750 | } |
| 751 | |
| 752 | async fn fetch_channel_message( |
| 753 | &self, |
| 754 | team_id: &str, |
| 755 | channel_id: &str, |
| 756 | message_id: &str, |
| 757 | ) -> Result<graph::Message> { |
| 758 | let known = self |
| 759 | .store |
| 760 | .messages_by_id(channel_id, &[message_id.to_owned()])?; |
| 761 | match known |
| 762 | .get(message_id) |
| 763 | .and_then(|record| record.reply_to_id.clone()) |
| 764 | { |
| 765 | Some(root_id) => { |
| 766 | self.remote |
| 767 | .channel_reply(team_id, channel_id, &root_id, message_id) |
| 768 | .await |
| 769 | } |
| 770 | None => { |
| 771 | self.remote |
| 772 | .channel_message(team_id, channel_id, message_id) |
| 773 | .await |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | fn outgoing( |
| 780 | conversation: &Conversation, |
| 781 | html: &str, |
| 782 | mentions: &[MentionInput], |
| 783 | ) -> Result<(String, Vec<OutgoingMention>)> { |
| 784 | if matches!(conversation, Conversation::Chat) { |
| 785 | ensure_allowed_in_chat(mentions)?; |
| 786 | } |
| 787 | Ok(apply_mentions(html, mentions)) |
| 788 | } |
| 789 | |
| 790 | fn missing_attachment_id() -> Error { |
| 791 | graph::Error::Upload("the uploaded file has no attachment id".into()).into() |
| 792 | } |