| 1 | use std::collections::HashMap; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use gpui_kit::*; |
| 5 | use store::MessageRecord; |
| 6 | |
| 7 | use super::ConversationView; |
| 8 | use gpui_kit::component::WindowExt as _; |
| 9 | use gpui_kit::component::button::{Button, ButtonVariants as _}; |
| 10 | use gpui_kit::component::dialog::DialogFooter; |
| 11 | |
| 12 | use crate::app_state::{AppState, Selection, selection_title}; |
| 13 | use crate::channel_menu::post_pin_dialog; |
| 14 | use crate::post_follow::post_followed; |
| 15 | use crate::rows::{Row, reply_excerpt}; |
| 16 | use crate::runtime; |
| 17 | use crate::views::message_actions::Action; |
| 18 | use crate::views::pin_banner::{PinBannerActions, PinBannerData, cycle_pin, render_pin_banner}; |
| 19 | |
| 20 | pub(super) struct PinPreview { |
| 21 | author: Option<String>, |
| 22 | text: String, |
| 23 | } |
| 24 | |
| 25 | impl PinPreview { |
| 26 | fn of(record: &MessageRecord) -> Self { |
| 27 | PinPreview { |
| 28 | author: record.sender_name.clone(), |
| 29 | text: reply_excerpt(record), |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | fn confirm_post_pin( |
| 35 | state: &Entity<AppState>, |
| 36 | channel_id: &str, |
| 37 | message_id: &str, |
| 38 | pin: bool, |
| 39 | window: &mut Window, |
| 40 | cx: &mut App, |
| 41 | ) { |
| 42 | let dialog = post_pin_dialog(pin); |
| 43 | let (state, channel_id, message_id) = |
| 44 | (state.clone(), channel_id.to_owned(), message_id.to_owned()); |
| 45 | window.open_alert_dialog(cx, move |alert, _, _| { |
| 46 | let (state, channel_id, message_id) = |
| 47 | (state.clone(), channel_id.clone(), message_id.clone()); |
| 48 | let cancel = Button::new("post-pin-cancel") |
| 49 | .label("Cancel") |
| 50 | .on_click(|_, window, cx| window.close_dialog(cx)); |
| 51 | let confirm = Button::new("post-pin-confirm") |
| 52 | .label(dialog.confirm) |
| 53 | .primary() |
| 54 | .on_click(move |_, window, cx| { |
| 55 | state.update(cx, |state, cx| { |
| 56 | state.set_pinned(&channel_id, &message_id, pin, cx) |
| 57 | }); |
| 58 | window.close_dialog(cx); |
| 59 | }); |
| 60 | alert |
| 61 | .title(dialog.title) |
| 62 | .description(dialog.body) |
| 63 | .footer(DialogFooter::new().child(cancel).child(confirm)) |
| 64 | .on_ok(|_, _, _| false) |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | impl ConversationView { |
| 69 | pub(super) fn chat_id(&self) -> Option<String> { |
| 70 | match self.current.as_ref()?.selection { |
| 71 | Selection::Chat(ref chat_id) => Some(chat_id.clone()), |
| 72 | Selection::Channel(_) => None, |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | pub(super) fn pin_conversation_id(&self) -> Option<String> { |
| 77 | self.current |
| 78 | .as_ref() |
| 79 | .map(|current| current.selection.conversation_id().to_owned()) |
| 80 | } |
| 81 | |
| 82 | pub(super) fn refresh_pin_previews(&mut self, cx: &mut Context<Self>) { |
| 83 | self.pin_previews.clear(); |
| 84 | self.pin_fetches.clear(); |
| 85 | self.pin_index = 0; |
| 86 | let Some(conversation_id) = self.pin_conversation_id() else { |
| 87 | return; |
| 88 | }; |
| 89 | self.update_pin_previews(&conversation_id, cx); |
| 90 | cx.notify(); |
| 91 | } |
| 92 | |
| 93 | pub(super) fn on_pins_changed(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 94 | if self.pin_conversation_id().as_deref() != Some(conversation_id) { |
| 95 | return; |
| 96 | } |
| 97 | self.pin_index = 0; |
| 98 | self.update_pin_previews(conversation_id, cx); |
| 99 | cx.notify(); |
| 100 | } |
| 101 | |
| 102 | fn update_pin_previews(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 103 | let state = self.app.read(cx); |
| 104 | let ids: Vec<String> = state |
| 105 | .pinned_messages(conversation_id) |
| 106 | .iter() |
| 107 | .map(|pin| pin.message_id.clone()) |
| 108 | .collect(); |
| 109 | let records = state |
| 110 | .store |
| 111 | .messages_by_id(conversation_id, &ids) |
| 112 | .unwrap_or_default(); |
| 113 | let mut previews: HashMap<String, PinPreview> = records |
| 114 | .into_iter() |
| 115 | .map(|(message_id, record)| (message_id, PinPreview::of(&record))) |
| 116 | .collect(); |
| 117 | for id in &ids { |
| 118 | if !previews.contains_key(id) |
| 119 | && let Some(preview) = self.pin_previews.remove(id) |
| 120 | { |
| 121 | previews.insert(id.clone(), preview); |
| 122 | } |
| 123 | } |
| 124 | self.pin_previews = previews; |
| 125 | let count = ids.len(); |
| 126 | if self.pin_index >= count { |
| 127 | self.pin_index = 0; |
| 128 | } |
| 129 | for id in ids { |
| 130 | if !self.pin_previews.contains_key(&id) { |
| 131 | self.fetch_pin_preview(conversation_id, id, cx); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | fn fetch_pin_preview( |
| 137 | &mut self, |
| 138 | conversation_id: &str, |
| 139 | message_id: String, |
| 140 | cx: &mut Context<Self>, |
| 141 | ) { |
| 142 | let Some(engine) = self.app.read(cx).engine.clone() else { |
| 143 | return; |
| 144 | }; |
| 145 | if self.chat_id().is_none() || !self.pin_fetches.insert(message_id.clone()) { |
| 146 | return; |
| 147 | } |
| 148 | let conversation_id = conversation_id.to_owned(); |
| 149 | let task_conversation_id = conversation_id.clone(); |
| 150 | let task_message_id = message_id.clone(); |
| 151 | let receiver = runtime::spawn(async move { |
| 152 | engine |
| 153 | .fetch_chat_message(&task_conversation_id, &task_message_id) |
| 154 | .await |
| 155 | }); |
| 156 | cx.spawn(async move |this, cx| { |
| 157 | let Ok(Ok(Some(record))) = receiver.await else { |
| 158 | return; |
| 159 | }; |
| 160 | this.update(cx, |this, cx| { |
| 161 | if this.conversation_id().as_deref() == Some(conversation_id.as_str()) { |
| 162 | this.pin_previews |
| 163 | .insert(message_id, PinPreview::of(&record)); |
| 164 | cx.notify(); |
| 165 | } |
| 166 | }) |
| 167 | .ok(); |
| 168 | }) |
| 169 | .detach(); |
| 170 | } |
| 171 | |
| 172 | fn pin_action( |
| 173 | &self, |
| 174 | view: WeakEntity<Self>, |
| 175 | run: fn(&mut Self, &mut Window, &mut Context<Self>), |
| 176 | ) -> Action { |
| 177 | Rc::new(move |window, cx| { |
| 178 | view.update(cx, |this, cx| run(this, window, cx)).ok(); |
| 179 | }) |
| 180 | } |
| 181 | |
| 182 | pub(super) fn render_pin_banner(&self, cx: &mut Context<Self>) -> Option<AnyElement> { |
| 183 | let conversation_id = self.chat_id()?; |
| 184 | let pins = self.app.read(cx).pinned_messages(&conversation_id); |
| 185 | let pin = pins.get(self.pin_index)?; |
| 186 | let preview = self.pin_previews.get(&pin.message_id); |
| 187 | let data = PinBannerData { |
| 188 | author: preview.and_then(|preview| preview.author.clone()), |
| 189 | preview: preview.map(|preview| preview.text.clone()), |
| 190 | position: self.pin_index, |
| 191 | total: pins.len(), |
| 192 | }; |
| 193 | let view = cx.weak_entity(); |
| 194 | let actions = PinBannerActions { |
| 195 | open: self.pin_action(view.clone(), |this, _, cx| this.open_shown_pin(cx)), |
| 196 | previous: self.pin_action(view.clone(), |this, _, cx| this.step_pin(-1, cx)), |
| 197 | next: self.pin_action(view.clone(), |this, _, cx| this.step_pin(1, cx)), |
| 198 | unpin: self.pin_action(view, |this, _, cx| this.unpin_shown_pin(cx)), |
| 199 | }; |
| 200 | Some(render_pin_banner(data, actions)) |
| 201 | } |
| 202 | |
| 203 | fn shown_pin_id(&self, cx: &App) -> Option<String> { |
| 204 | let conversation_id = self.pin_conversation_id()?; |
| 205 | self.app |
| 206 | .read(cx) |
| 207 | .pinned_messages(&conversation_id) |
| 208 | .get(self.pin_index) |
| 209 | .map(|pin| pin.message_id.clone()) |
| 210 | } |
| 211 | |
| 212 | fn open_shown_pin(&mut self, cx: &mut Context<Self>) { |
| 213 | let (Some(conversation_id), Some(message_id)) = |
| 214 | (self.pin_conversation_id(), self.shown_pin_id(cx)) |
| 215 | else { |
| 216 | return; |
| 217 | }; |
| 218 | self.jump_to_message(&conversation_id, &message_id, cx); |
| 219 | } |
| 220 | |
| 221 | fn step_pin(&mut self, delta: isize, cx: &mut Context<Self>) { |
| 222 | let Some(conversation_id) = self.pin_conversation_id() else { |
| 223 | return; |
| 224 | }; |
| 225 | let total = self.app.read(cx).pinned_messages(&conversation_id).len(); |
| 226 | self.pin_index = cycle_pin(self.pin_index, total, delta); |
| 227 | cx.notify(); |
| 228 | } |
| 229 | |
| 230 | fn unpin_shown_pin(&mut self, cx: &mut Context<Self>) { |
| 231 | let (Some(conversation_id), Some(message_id)) = |
| 232 | (self.pin_conversation_id(), self.shown_pin_id(cx)) |
| 233 | else { |
| 234 | return; |
| 235 | }; |
| 236 | self.app.update(cx, |state, cx| { |
| 237 | state.set_pinned(&conversation_id, &message_id, false, cx) |
| 238 | }); |
| 239 | } |
| 240 | |
| 241 | pub(super) fn forward_message(&mut self, message_id: &str, cx: &mut Context<Self>) { |
| 242 | if let Some(conversation_id) = self.conversation_id() { |
| 243 | self.app.update(cx, |state, cx| { |
| 244 | state.request_forward(&conversation_id, message_id, cx) |
| 245 | }); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | pub(super) fn copy_link(&mut self, message_id: &str, cx: &mut Context<Self>) { |
| 250 | if let Some(conversation_id) = self.conversation_id() { |
| 251 | self.app.update(cx, |state, cx| { |
| 252 | state.copy_message_link(&conversation_id, message_id, cx) |
| 253 | }); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | pub(super) fn share_to_outlook(&mut self, message_id: &str, cx: &mut Context<Self>) { |
| 258 | let Some(current) = self.current.as_ref() else { |
| 259 | return; |
| 260 | }; |
| 261 | let conversation_id = current.selection.conversation_id().to_owned(); |
| 262 | let title = selection_title(&self.app.read(cx).sidebar, ¤t.selection); |
| 263 | self.app.update(cx, |state, cx| { |
| 264 | state.share_to_outlook(&conversation_id, message_id, &title, cx) |
| 265 | }); |
| 266 | } |
| 267 | |
| 268 | pub(super) fn toggle_saved(&mut self, message_id: &str, cx: &mut Context<Self>) { |
| 269 | if let Some(conversation_id) = self.conversation_id() { |
| 270 | self.app.update(cx, |state, cx| { |
| 271 | state.toggle_saved(&conversation_id, message_id, cx) |
| 272 | }); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | pub(super) fn toggle_pinned( |
| 277 | &mut self, |
| 278 | message_id: &str, |
| 279 | window: &mut Window, |
| 280 | cx: &mut Context<Self>, |
| 281 | ) { |
| 282 | let Some(conversation_id) = self.pin_conversation_id() else { |
| 283 | return; |
| 284 | }; |
| 285 | if self.chat_id().is_some() { |
| 286 | self.app.update(cx, |state, cx| { |
| 287 | state.toggle_pinned(&conversation_id, message_id, cx) |
| 288 | }); |
| 289 | return; |
| 290 | } |
| 291 | let pin = !self.app.read(cx).is_pinned(&conversation_id, message_id); |
| 292 | confirm_post_pin(&self.app, &conversation_id, message_id, pin, window, cx); |
| 293 | } |
| 294 | |
| 295 | /// `Some(followed)` for a channel root post, `None` for everything else. |
| 296 | pub(super) fn post_follow(&self, message_id: &str, cx: &App) -> Option<bool> { |
| 297 | let Selection::Channel(ref channel_id) = self.current.as_ref()?.selection else { |
| 298 | return None; |
| 299 | }; |
| 300 | let participated = self.rows.borrow().iter().find_map(|row| match row { |
| 301 | Row::Post(post) if post.root.key == message_id => { |
| 302 | Some(post.messages().any(|message| message.own)) |
| 303 | } |
| 304 | _ => None, |
| 305 | })?; |
| 306 | let state = self.app.read(cx).post_follow_state(channel_id, message_id); |
| 307 | Some(post_followed(state, participated)) |
| 308 | } |
| 309 | |
| 310 | pub(super) fn request_post_follow(&mut self, message_id: &str, cx: &mut Context<Self>) { |
| 311 | if let Some(Selection::Channel(channel_id)) = |
| 312 | self.current.as_ref().map(|current| current.selection.clone()) |
| 313 | && self.post_follow(message_id, cx).is_some() |
| 314 | { |
| 315 | self.app.update(cx, |state, cx| { |
| 316 | state.request_post_follow_state(&channel_id, message_id, cx) |
| 317 | }); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | pub(super) fn toggle_post_follow(&mut self, message_id: &str, cx: &mut Context<Self>) { |
| 322 | let Some(followed) = self.post_follow(message_id, cx) else { |
| 323 | return; |
| 324 | }; |
| 325 | let Some(channel_id) = self.pin_conversation_id() else { |
| 326 | return; |
| 327 | }; |
| 328 | self.app.update(cx, |state, cx| { |
| 329 | state.set_post_followed(&channel_id, message_id, !followed, cx) |
| 330 | }); |
| 331 | } |
| 332 | |
| 333 | pub(super) fn mark_unread_from(&mut self, message_id: &str, cx: &mut Context<Self>) { |
| 334 | if let Some(chat_id) = self.chat_id() { |
| 335 | self.app.update(cx, |state, cx| { |
| 336 | state.mark_unread_from_message(&chat_id, message_id, cx) |
| 337 | }); |
| 338 | } |
| 339 | } |
| 340 | } |