| 1 | use chatsvc::{CallLogEntry, EventKind, ForwardResult, MessageEvent, PinnedMessage, SavedMessage}; |
| 2 | |
| 3 | use crate::engine::{Conversation, SyncEngine}; |
| 4 | use crate::error::{Error, Result}; |
| 5 | use crate::events::CoreEvent; |
| 6 | use crate::markdown::markdown_to_html; |
| 7 | use crate::message_link::{ChannelLinkInput, channel_message_link, chat_message_link}; |
| 8 | use crate::remote::Remote; |
| 9 | use crate::scheduled::NOTES_CHAT_ID; |
| 10 | |
| 11 | impl<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, chat_id: &str) -> Result<Vec<PinnedMessage>> { |
| 72 | self.ensure_chat(chat_id)?; |
| 73 | self.remote.chat_pins(chat_id).await |
| 74 | } |
| 75 | |
| 76 | pub async fn pin_message(&self, chat_id: &str, message_id: &str) -> Result<()> { |
| 77 | self.ensure_chat(chat_id)?; |
| 78 | self.remote.pin_message(chat_id, message_id).await?; |
| 79 | self.announce_pins(chat_id); |
| 80 | Ok(()) |
| 81 | } |
| 82 | |
| 83 | pub async fn unpin_message( |
| 84 | &self, |
| 85 | chat_id: &str, |
| 86 | message_id: &str, |
| 87 | parent_id: Option<&str>, |
| 88 | ) -> Result<()> { |
| 89 | self.ensure_chat(chat_id)?; |
| 90 | self.remote |
| 91 | .unpin_message(chat_id, message_id, parent_id) |
| 92 | .await?; |
| 93 | self.announce_pins(chat_id); |
| 94 | Ok(()) |
| 95 | } |
| 96 | |
| 97 | pub async fn message_link(&self, conversation_id: &str, message_id: &str) -> Result<String> { |
| 98 | let Conversation::Channel { team_id } = self.resolve(conversation_id).await? else { |
| 99 | let notes_oid = if conversation_id == NOTES_CHAT_ID { |
| 100 | Some(self.my_user_id().await?) |
| 101 | } else { |
| 102 | None |
| 103 | }; |
| 104 | return Ok(chat_message_link( |
| 105 | conversation_id, |
| 106 | message_id, |
| 107 | notes_oid.as_deref(), |
| 108 | )); |
| 109 | }; |
| 110 | let channel = self |
| 111 | .store |
| 112 | .channel(conversation_id)? |
| 113 | .ok_or_else(|| Error::UnknownConversation(conversation_id.to_owned()))?; |
| 114 | let team = self |
| 115 | .store |
| 116 | .team(&team_id)? |
| 117 | .ok_or_else(|| Error::UnknownConversation(team_id.clone()))?; |
| 118 | let record = self |
| 119 | .store |
| 120 | .messages_by_id(conversation_id, &[message_id.to_owned()])? |
| 121 | .remove(message_id) |
| 122 | .ok_or(Error::Unsupported("a link to an uncached message"))?; |
| 123 | let any_chat_id = self |
| 124 | .store |
| 125 | .sidebar()? |
| 126 | .chats |
| 127 | .into_iter() |
| 128 | .next() |
| 129 | .map(|chat| chat.id) |
| 130 | .ok_or(Error::Unsupported("a channel link without a known chat"))?; |
| 131 | let tenant_id = self.my_tenant_id(&any_chat_id).await?; |
| 132 | Ok(channel_message_link(&ChannelLinkInput { |
| 133 | tenant_id, |
| 134 | group_id: team_id, |
| 135 | channel_id: conversation_id.to_owned(), |
| 136 | team_name: team.name, |
| 137 | channel_name: channel.name, |
| 138 | root_id: record |
| 139 | .reply_to_id |
| 140 | .clone() |
| 141 | .unwrap_or_else(|| record.message_id.clone()), |
| 142 | message_id: record.message_id, |
| 143 | created_ms: record.created_at.timestamp_millis(), |
| 144 | })) |
| 145 | } |
| 146 | |
| 147 | pub fn handle_pin_event(&self, event: &MessageEvent) { |
| 148 | if event.kind != EventKind::PinsChanged { |
| 149 | return; |
| 150 | } |
| 151 | if let Some(conversation_id) = event.conversation_id.as_deref() { |
| 152 | self.announce_pins(conversation_id); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | fn ensure_chat(&self, conversation_id: &str) -> Result<()> { |
| 157 | match self.resolve_stored(conversation_id)? { |
| 158 | Conversation::Chat => Ok(()), |
| 159 | Conversation::Channel { .. } => Err(Error::Unsupported("pinned messages in a channel")), |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | fn announce_pins(&self, conversation_id: &str) { |
| 164 | let _ = self.events.send(CoreEvent::PinsChanged { |
| 165 | conversation_id: conversation_id.to_owned(), |
| 166 | }); |
| 167 | } |
| 168 | } |