BlueGit

ict rusty-teams

Files

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

pins.rs

pins.rs 7.4 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 refresh_pin_previews(&mut self, cx: &mut Context<Self>) {
37 self.pin_previews.clear();
38 self.pin_fetches.clear();
39 self.pin_index = 0;
40 let Some(chat_id) = self.chat_id() else {
41 return;
42 };
43 self.update_pin_previews(&chat_id, cx);
44 cx.notify();
45 }
46
47 pub(super) fn on_pins_changed(&mut self, chat_id: &str, cx: &mut Context<Self>) {
48 if self.chat_id().as_deref() != Some(chat_id) {
49 return;
50 }
51 self.pin_index = 0;
52 self.update_pin_previews(chat_id, cx);
53 cx.notify();
54 }
55
56 fn update_pin_previews(&mut self, chat_id: &str, cx: &mut Context<Self>) {
57 let state = self.app.read(cx);
58 let ids: Vec<String> = state
59 .pinned_messages(chat_id)
60 .iter()
61 .map(|pin| pin.message_id.clone())
62 .collect();
63 let records = state
64 .store
65 .messages_by_id(chat_id, &ids)
66 .unwrap_or_default();
67 let mut previews: HashMap<String, PinPreview> = records
68 .into_iter()
69 .map(|(message_id, record)| (message_id, PinPreview::of(&record)))
70 .collect();
71 for id in &ids {
72 if !previews.contains_key(id)
73 && let Some(preview) = self.pin_previews.remove(id)
74 {
75 previews.insert(id.clone(), preview);
76 }
77 }
78 self.pin_previews = previews;
79 let count = ids.len();
80 if self.pin_index >= count {
81 self.pin_index = 0;
82 }
83 for id in ids {
84 if !self.pin_previews.contains_key(&id) {
85 self.fetch_pin_preview(chat_id, id, cx);
86 }
87 }
88 }
89
90 fn fetch_pin_preview(&mut self, chat_id: &str, message_id: String, cx: &mut Context<Self>) {
91 let Some(engine) = self.app.read(cx).engine.clone() else {
92 return;
93 };
94 if !self.pin_fetches.insert(message_id.clone()) {
95 return;
96 }
97 let chat_id = chat_id.to_owned();
98 let task_chat_id = chat_id.clone();
99 let task_message_id = message_id.clone();
100 let receiver = runtime::spawn(async move {
101 engine
102 .fetch_chat_message(&task_chat_id, &task_message_id)
103 .await
104 });
105 cx.spawn(async move |this, cx| {
106 let Ok(Ok(Some(record))) = receiver.await else {
107 return;
108 };
109 this.update(cx, |this, cx| {
110 if this.chat_id().as_deref() == Some(chat_id.as_str()) {
111 this.pin_previews.insert(message_id, PinPreview::of(&record));
112 cx.notify();
113 }
114 })
115 .ok();
116 })
117 .detach();
118 }
119
120 fn pin_action(
121 &self,
122 view: WeakEntity<Self>,
123 run: fn(&mut Self, &mut Window, &mut Context<Self>),
124 ) -> Action {
125 Rc::new(move |window, cx| {
126 view.update(cx, |this, cx| run(this, window, cx)).ok();
127 })
128 }
129
130 pub(super) fn render_pin_banner(&self, cx: &mut Context<Self>) -> Option<AnyElement> {
131 let chat_id = self.chat_id()?;
132 let pins = self.app.read(cx).pinned_messages(&chat_id);
133 let pin = pins.get(self.pin_index)?;
134 let preview = self.pin_previews.get(&pin.message_id);
135 let data = PinBannerData {
136 author: preview.and_then(|preview| preview.author.clone()),
137 preview: preview.map(|preview| preview.text.clone()),
138 position: self.pin_index,
139 total: pins.len(),
140 };
141 let view = cx.weak_entity();
142 let actions = PinBannerActions {
143 open: self.pin_action(view.clone(), |this, _, cx| this.open_shown_pin(cx)),
144 previous: self.pin_action(view.clone(), |this, _, cx| this.step_pin(-1, cx)),
145 next: self.pin_action(view.clone(), |this, _, cx| this.step_pin(1, cx)),
146 unpin: self.pin_action(view, |this, _, cx| this.unpin_shown_pin(cx)),
147 };
148 Some(render_pin_banner(data, actions))
149 }
150
151 fn shown_pin_id(&self, cx: &App) -> Option<String> {
152 let chat_id = self.chat_id()?;
153 self.app
154 .read(cx)
155 .pinned_messages(&chat_id)
156 .get(self.pin_index)
157 .map(|pin| pin.message_id.clone())
158 }
159
160 fn open_shown_pin(&mut self, cx: &mut Context<Self>) {
161 let (Some(chat_id), Some(message_id)) = (self.chat_id(), self.shown_pin_id(cx)) else {
162 return;
163 };
164 self.jump_to_message(&chat_id, &message_id, cx);
165 }
166
167 fn step_pin(&mut self, delta: isize, cx: &mut Context<Self>) {
168 let Some(chat_id) = self.chat_id() else {
169 return;
170 };
171 let total = self.app.read(cx).pinned_messages(&chat_id).len();
172 self.pin_index = cycle_pin(self.pin_index, total, delta);
173 cx.notify();
174 }
175
176 fn unpin_shown_pin(&mut self, cx: &mut Context<Self>) {
177 let (Some(chat_id), Some(message_id)) = (self.chat_id(), self.shown_pin_id(cx)) else {
178 return;
179 };
180 self.app.update(cx, |state, cx| {
181 state.set_pinned(&chat_id, &message_id, false, cx)
182 });
183 }
184
185 pub(super) fn forward_message(&mut self, message_id: &str, cx: &mut Context<Self>) {
186 if let Some(conversation_id) = self.conversation_id() {
187 self.app.update(cx, |state, cx| {
188 state.request_forward(&conversation_id, message_id, cx)
189 });
190 }
191 }
192
193 pub(super) fn copy_link(&mut self, message_id: &str, cx: &mut Context<Self>) {
194 if let Some(conversation_id) = self.conversation_id() {
195 self.app.update(cx, |state, cx| {
196 state.copy_message_link(&conversation_id, message_id, cx)
197 });
198 }
199 }
200
201 pub(super) fn toggle_saved(&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.toggle_saved(&conversation_id, message_id, cx)
205 });
206 }
207 }
208
209 pub(super) fn toggle_pinned(&mut self, message_id: &str, cx: &mut Context<Self>) {
210 if let Some(chat_id) = self.chat_id() {
211 self.app.update(cx, |state, cx| {
212 state.toggle_pinned(&chat_id, message_id, cx)
213 });
214 }
215 }
216
217 pub(super) fn mark_unread_from(&mut self, message_id: &str, cx: &mut Context<Self>) {
218 if let Some(chat_id) = self.chat_id() {
219 self.app.update(cx, |state, cx| {
220 state.mark_unread_from_message(&chat_id, message_id, cx)
221 });
222 }
223 }
224}