BlueGit

ict rusty-teams

Files

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

pins.rs

pins.rs 8.0 KiB Download Raw
1use std::collections::HashMap;
2use std::rc::Rc;
3
4use gpui_kit::*;
5use store::MessageRecord;
6
7use super::ConversationView;
8use crate::app_state::Selection;
9use crate::rows::reply_excerpt;
10use crate::runtime;
11use crate::views::message_actions::Action;
12use crate::views::pin_banner::{PinBannerActions, PinBannerData, cycle_pin, render_pin_banner};
13
14pub(super) struct PinPreview {
15 author: Option<String>,
16 text: String,
17}
18
19impl PinPreview {
20 fn of(record: &MessageRecord) -> Self {
21 PinPreview {
22 author: record.sender_name.clone(),
23 text: reply_excerpt(record),
24 }
25 }
26}
27
28impl ConversationView {
29 pub(super) fn chat_id(&self) -> Option<String> {
30 match self.current.as_ref()?.selection {
31 Selection::Chat(ref chat_id) => Some(chat_id.clone()),
32 Selection::Channel(_) => None,
33 }
34 }
35
36 pub(super) fn pin_conversation_id(&self) -> Option<String> {
37 self.current
38 .as_ref()
39 .map(|current| current.selection.conversation_id().to_owned())
40 }
41
42 pub(super) fn refresh_pin_previews(&mut self, cx: &mut Context<Self>) {
43 self.pin_previews.clear();
44 self.pin_fetches.clear();
45 self.pin_index = 0;
46 let Some(conversation_id) = self.pin_conversation_id() else {
47 return;
48 };
49 self.update_pin_previews(&conversation_id, cx);
50 cx.notify();
51 }
52
53 pub(super) fn on_pins_changed(&mut self, conversation_id: &str, cx: &mut Context<Self>) {
54 if self.pin_conversation_id().as_deref() != Some(conversation_id) {
55 return;
56 }
57 self.pin_index = 0;
58 self.update_pin_previews(conversation_id, cx);
59 cx.notify();
60 }
61
62 fn update_pin_previews(&mut self, conversation_id: &str, cx: &mut Context<Self>) {
63 let state = self.app.read(cx);
64 let ids: Vec<String> = state
65 .pinned_messages(conversation_id)
66 .iter()
67 .map(|pin| pin.message_id.clone())
68 .collect();
69 let records = state
70 .store
71 .messages_by_id(conversation_id, &ids)
72 .unwrap_or_default();
73 let mut previews: HashMap<String, PinPreview> = records
74 .into_iter()
75 .map(|(message_id, record)| (message_id, PinPreview::of(&record)))
76 .collect();
77 for id in &ids {
78 if !previews.contains_key(id)
79 && let Some(preview) = self.pin_previews.remove(id)
80 {
81 previews.insert(id.clone(), preview);
82 }
83 }
84 self.pin_previews = previews;
85 let count = ids.len();
86 if self.pin_index >= count {
87 self.pin_index = 0;
88 }
89 for id in ids {
90 if !self.pin_previews.contains_key(&id) {
91 self.fetch_pin_preview(conversation_id, id, cx);
92 }
93 }
94 }
95
96 fn fetch_pin_preview(
97 &mut self,
98 conversation_id: &str,
99 message_id: String,
100 cx: &mut Context<Self>,
101 ) {
102 let Some(engine) = self.app.read(cx).engine.clone() else {
103 return;
104 };
105 if self.chat_id().is_none() || !self.pin_fetches.insert(message_id.clone()) {
106 return;
107 }
108 let conversation_id = conversation_id.to_owned();
109 let task_conversation_id = conversation_id.clone();
110 let task_message_id = message_id.clone();
111 let receiver = runtime::spawn(async move {
112 engine
113 .fetch_chat_message(&task_conversation_id, &task_message_id)
114 .await
115 });
116 cx.spawn(async move |this, cx| {
117 let Ok(Ok(Some(record))) = receiver.await else {
118 return;
119 };
120 this.update(cx, |this, cx| {
121 if this.conversation_id().as_deref() == Some(conversation_id.as_str()) {
122 this.pin_previews
123 .insert(message_id, PinPreview::of(&record));
124 cx.notify();
125 }
126 })
127 .ok();
128 })
129 .detach();
130 }
131
132 fn pin_action(
133 &self,
134 view: WeakEntity<Self>,
135 run: fn(&mut Self, &mut Window, &mut Context<Self>),
136 ) -> Action {
137 Rc::new(move |window, cx| {
138 view.update(cx, |this, cx| run(this, window, cx)).ok();
139 })
140 }
141
142 pub(super) fn render_pin_banner(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
143 let conversation_id = self.chat_id()?;
144 let pins = self.app.read(cx).pinned_messages(&conversation_id);
145 let pin = pins.get(self.pin_index)?;
146 let preview = self.pin_previews.get(&pin.message_id);
147 let data = PinBannerData {
148 author: preview.and_then(|preview| preview.author.clone()),
149 preview: preview.map(|preview| preview.text.clone()),
150 position: self.pin_index,
151 total: pins.len(),
152 };
153 let view = cx.weak_entity();
154 let actions = PinBannerActions {
155 open: self.pin_action(view.clone(), |this, _, cx| this.open_shown_pin(cx)),
156 previous: self.pin_action(view.clone(), |this, _, cx| this.step_pin(-1, cx)),
157 next: self.pin_action(view.clone(), |this, _, cx| this.step_pin(1, cx)),
158 unpin: self.pin_action(view, |this, _, cx| this.unpin_shown_pin(cx)),
159 };
160 Some(render_pin_banner(data, actions))
161 }
162
163 fn shown_pin_id(&self, cx: &App) -> Option<String> {
164 let conversation_id = self.pin_conversation_id()?;
165 self.app
166 .read(cx)
167 .pinned_messages(&conversation_id)
168 .get(self.pin_index)
169 .map(|pin| pin.message_id.clone())
170 }
171
172 fn open_shown_pin(&mut self, cx: &mut Context<Self>) {
173 let (Some(conversation_id), Some(message_id)) =
174 (self.pin_conversation_id(), self.shown_pin_id(cx))
175 else {
176 return;
177 };
178 self.jump_to_message(&conversation_id, &message_id, cx);
179 }
180
181 fn step_pin(&mut self, delta: isize, cx: &mut Context<Self>) {
182 let Some(conversation_id) = self.pin_conversation_id() else {
183 return;
184 };
185 let total = self.app.read(cx).pinned_messages(&conversation_id).len();
186 self.pin_index = cycle_pin(self.pin_index, total, delta);
187 cx.notify();
188 }
189
190 fn unpin_shown_pin(&mut self, cx: &mut Context<Self>) {
191 let (Some(conversation_id), Some(message_id)) =
192 (self.pin_conversation_id(), self.shown_pin_id(cx))
193 else {
194 return;
195 };
196 self.app.update(cx, |state, cx| {
197 state.set_pinned(&conversation_id, &message_id, false, cx)
198 });
199 }
200
201 pub(super) fn forward_message(&mut self, message_id: &str, cx: &mut Context<Self>) {
202 if let Some(conversation_id) = self.conversation_id() {
203 self.app.update(cx, |state, cx| {
204 state.request_forward(&conversation_id, message_id, cx)
205 });
206 }
207 }
208
209 pub(super) fn copy_link(&mut self, message_id: &str, cx: &mut Context<Self>) {
210 if let Some(conversation_id) = self.conversation_id() {
211 self.app.update(cx, |state, cx| {
212 state.copy_message_link(&conversation_id, message_id, cx)
213 });
214 }
215 }
216
217 pub(super) fn toggle_saved(&mut self, message_id: &str, cx: &mut Context<Self>) {
218 if let Some(conversation_id) = self.conversation_id() {
219 self.app.update(cx, |state, cx| {
220 state.toggle_saved(&conversation_id, message_id, cx)
221 });
222 }
223 }
224
225 pub(super) fn toggle_pinned(&mut self, message_id: &str, cx: &mut Context<Self>) {
226 if let Some(conversation_id) = self.pin_conversation_id() {
227 self.app.update(cx, |state, cx| {
228 state.toggle_pinned(&conversation_id, message_id, cx)
229 });
230 }
231 }
232
233 pub(super) fn mark_unread_from(&mut self, message_id: &str, cx: &mut Context<Self>) {
234 if let Some(chat_id) = self.chat_id() {
235 self.app.update(cx, |state, cx| {
236 state.mark_unread_from_message(&chat_id, message_id, cx)
237 });
238 }
239 }
240}