BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / conversation / pins.rs

pins.rs

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