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