| 1 | use std::collections::HashMap; |
| 2 | |
| 3 | use chrono::{DateTime, Duration, Utc}; |
| 4 | use gpui_kit::*; |
| 5 | |
| 6 | use super::feed::{Actor, Entry, Feed, RETENTION_DAYS, ReactedMessage, start_time}; |
| 7 | use super::preview_label; |
| 8 | use super::reactions::reacted_messages; |
| 9 | use crate::agent_api; |
| 10 | use crate::app_state::{AppEvent, AppState}; |
| 11 | use crate::call::MissedCall; |
| 12 | use crate::demo; |
| 13 | use crate::notify::{IncomingTracker, alerts}; |
| 14 | use crate::people::resolve_names; |
| 15 | |
| 16 | const WATERMARK_KEY: &str = "activity_watermark"; |
| 17 | const RECENT_WINDOW: usize = 20; |
| 18 | |
| 19 | pub struct ActivityCenter { |
| 20 | app: Entity<AppState>, |
| 21 | main_window: AnyWindowHandle, |
| 22 | feed: Feed, |
| 23 | tracker: IncomingTracker, |
| 24 | cutoff: DateTime<Utc>, |
| 25 | _subscription: Subscription, |
| 26 | _proposal_observation: Option<Subscription>, |
| 27 | } |
| 28 | |
| 29 | impl ActivityCenter { |
| 30 | pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self { |
| 31 | let state = app.read(cx); |
| 32 | let store = state.store.clone(); |
| 33 | let now = Utc::now(); |
| 34 | let _ = store.prune_activity(now - Duration::days(RETENTION_DAYS)); |
| 35 | let cutoff = start_time(store.meta_time(WATERMARK_KEY).ok().flatten(), now); |
| 36 | let mut feed = Feed::load(store.activity().unwrap_or_default()); |
| 37 | if state.mode.demo { |
| 38 | for entry in demo::activity_entries() { |
| 39 | feed.add(entry); |
| 40 | } |
| 41 | } |
| 42 | let subscription = cx.subscribe(&app, |this, _, event: &AppEvent, cx| { |
| 43 | this.on_app_event(event, cx); |
| 44 | }); |
| 45 | let proposal_observation = |
| 46 | agent_api::handle(cx).map(|api| cx.observe(&api, |this, _, cx| this.on_proposals(cx))); |
| 47 | let mut center = ActivityCenter { |
| 48 | app, |
| 49 | main_window: window.window_handle(), |
| 50 | feed, |
| 51 | tracker: IncomingTracker::new(cutoff), |
| 52 | cutoff, |
| 53 | _subscription: subscription, |
| 54 | _proposal_observation: proposal_observation, |
| 55 | }; |
| 56 | center.flush(cx); |
| 57 | center |
| 58 | } |
| 59 | |
| 60 | pub fn feed(&self) -> &Feed { |
| 61 | &self.feed |
| 62 | } |
| 63 | |
| 64 | pub fn mark_read(&mut self, id: i64, cx: &mut Context<Self>) { |
| 65 | if self.feed.mark_read(id) { |
| 66 | self.flush(cx); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | pub fn mark_all_read(&mut self, cx: &mut Context<Self>) { |
| 71 | agent_api::mark_all_proposals_seen(cx); |
| 72 | if self.feed.mark_all_read() { |
| 73 | self.flush(cx); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | fn on_app_event(&mut self, event: &AppEvent, cx: &mut Context<Self>) { |
| 78 | match event { |
| 79 | AppEvent::Messages(conversation_id) => self.on_messages(conversation_id, cx), |
| 80 | AppEvent::Selection => self.on_selection(cx), |
| 81 | AppEvent::Sidebar => self.on_sidebar(cx), |
| 82 | AppEvent::MissedCall(missed) => self.on_missed_call(missed, cx), |
| 83 | _ => {} |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | fn on_missed_call(&mut self, missed: &MissedCall, cx: &mut Context<Self>) { |
| 88 | let conversation_id = self |
| 89 | .app |
| 90 | .read(cx) |
| 91 | .conversation_of_missed_call(missed) |
| 92 | .unwrap_or_default(); |
| 93 | let actor = Actor { |
| 94 | user_id: missed |
| 95 | .caller_mri |
| 96 | .strip_prefix("8:orgid:") |
| 97 | .map(str::to_owned), |
| 98 | name: missed.caller_name.clone(), |
| 99 | }; |
| 100 | self.feed |
| 101 | .record_missed_call(&conversation_id, actor, missed.at); |
| 102 | self.flush(cx); |
| 103 | } |
| 104 | |
| 105 | fn on_selection(&mut self, cx: &mut Context<Self>) { |
| 106 | let selected = self |
| 107 | .app |
| 108 | .read(cx) |
| 109 | .selection |
| 110 | .as_ref() |
| 111 | .map(|selection| selection.conversation_id().to_owned()); |
| 112 | let Some(conversation_id) = selected else { |
| 113 | return; |
| 114 | }; |
| 115 | agent_api::mark_conversation_proposals_seen(&conversation_id, cx); |
| 116 | if self.feed.mark_conversation_read(&conversation_id) { |
| 117 | self.flush(cx); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | fn on_proposals(&mut self, cx: &mut Context<Self>) { |
| 122 | let selected = self |
| 123 | .app |
| 124 | .read(cx) |
| 125 | .selection |
| 126 | .as_ref() |
| 127 | .map(|selection| selection.conversation_id().to_owned()); |
| 128 | if let Some(conversation_id) = selected |
| 129 | && self.in_foreground(&conversation_id, cx) |
| 130 | { |
| 131 | agent_api::mark_conversation_proposals_seen(&conversation_id, cx); |
| 132 | } |
| 133 | cx.notify(); |
| 134 | } |
| 135 | |
| 136 | fn on_sidebar(&mut self, cx: &mut Context<Self>) { |
| 137 | self.record_offline_chats(cx); |
| 138 | let sidebar = &self.app.read(cx).sidebar; |
| 139 | let read_at: HashMap<&str, DateTime<Utc>> = sidebar |
| 140 | .chats |
| 141 | .iter() |
| 142 | .filter(|chat| !chat.unread) |
| 143 | .filter_map(|chat| Some((chat.id.as_str(), chat.last_read_at?))) |
| 144 | .collect(); |
| 145 | if self |
| 146 | .feed |
| 147 | .sync_read(|conversation_id| read_at.get(conversation_id).copied()) |
| 148 | { |
| 149 | self.flush(cx); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | fn record_offline_chats(&mut self, cx: &mut Context<Self>) { |
| 154 | let state = self.app.read(cx); |
| 155 | let my_user_id = state.directory.me.as_ref().map(|me| me.user_id.as_str()); |
| 156 | let selected = state |
| 157 | .selection |
| 158 | .as_ref() |
| 159 | .map(|selection| selection.conversation_id()); |
| 160 | for chat in state.sidebar.chats.iter().filter(|chat| chat.unread) { |
| 161 | let (Some(at), Some(preview)) = (chat.last_message_at, &chat.last_message_preview) |
| 162 | else { |
| 163 | continue; |
| 164 | }; |
| 165 | let from_me = |
| 166 | my_user_id.is_some() && chat.last_message_sender_id.as_deref() == my_user_id; |
| 167 | if at <= self.cutoff |
| 168 | || chat.muted |
| 169 | || from_me |
| 170 | || chat.last_message_deleted |
| 171 | || selected == Some(chat.id.as_str()) |
| 172 | { |
| 173 | continue; |
| 174 | } |
| 175 | let actor = Actor { |
| 176 | user_id: chat.last_message_sender_id.clone(), |
| 177 | name: chat.last_message_sender_name.clone().unwrap_or_default(), |
| 178 | }; |
| 179 | self.feed |
| 180 | .record_chat_preview(&chat.id, actor, preview.clone(), at); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | fn on_messages(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 185 | let foreground = self.in_foreground(conversation_id, cx); |
| 186 | let state = self.app.read(cx); |
| 187 | let my_user_id = state.directory.me.as_ref().map(|me| me.user_id.clone()); |
| 188 | let found = self.tracker.collect( |
| 189 | &state.store, |
| 190 | &state.sidebar, |
| 191 | my_user_id.as_deref(), |
| 192 | &state.late_arrivals, |
| 193 | conversation_id, |
| 194 | ); |
| 195 | let reacted = my_user_id |
| 196 | .as_deref() |
| 197 | .map(|me| reacted_in(state, conversation_id, me)) |
| 198 | .unwrap_or_default(); |
| 199 | let _ = state.store.set_meta_time(WATERMARK_KEY, Utc::now()); |
| 200 | if !foreground { |
| 201 | for incoming in found.iter().filter(|incoming| alerts(incoming)) { |
| 202 | self.feed |
| 203 | .record_message(incoming, preview_label(&incoming.preview)); |
| 204 | } |
| 205 | } |
| 206 | if let Some(me) = my_user_id { |
| 207 | for message in &reacted { |
| 208 | self.feed |
| 209 | .record_reactions(conversation_id, message, &me, self.cutoff, Utc::now()); |
| 210 | } |
| 211 | } |
| 212 | if foreground { |
| 213 | self.feed.mark_conversation_read(conversation_id); |
| 214 | } |
| 215 | self.flush(cx); |
| 216 | } |
| 217 | |
| 218 | fn in_foreground(&self, conversation_id: &str, cx: &mut Context<Self>) -> bool { |
| 219 | let selected = self |
| 220 | .app |
| 221 | .read(cx) |
| 222 | .selection |
| 223 | .as_ref() |
| 224 | .is_some_and(|selection| selection.conversation_id() == conversation_id); |
| 225 | selected |
| 226 | && self |
| 227 | .main_window |
| 228 | .update(cx, |_, window, _| window.is_window_active()) |
| 229 | .unwrap_or(false) |
| 230 | } |
| 231 | |
| 232 | fn flush(&mut self, cx: &mut Context<Self>) { |
| 233 | let dirty = self.feed.take_dirty(); |
| 234 | if !dirty.is_empty() { |
| 235 | let records: Vec<_> = dirty.iter().map(Entry::to_record).collect(); |
| 236 | let _ = self.app.read(cx).store.upsert_activity(&records); |
| 237 | let user_ids: Vec<String> = dirty |
| 238 | .iter() |
| 239 | .filter_map(|entry| entry.latest_actor()?.user_id.clone()) |
| 240 | .collect(); |
| 241 | self.app |
| 242 | .update(cx, |state, cx| state.request_avatars(user_ids, cx)); |
| 243 | } |
| 244 | cx.notify(); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | fn reacted_in(state: &AppState, conversation_id: &str, my_user_id: &str) -> Vec<ReactedMessage> { |
| 249 | let Ok(records) = state.store.messages(conversation_id, None, RECENT_WINDOW) else { |
| 250 | return Vec::new(); |
| 251 | }; |
| 252 | let reactor_ids: Vec<String> = records |
| 253 | .iter() |
| 254 | .flat_map(teams_core::reactions) |
| 255 | .filter(|reaction| reaction.user_name.is_none()) |
| 256 | .filter_map(|reaction| reaction.user_id) |
| 257 | .collect(); |
| 258 | let names = resolve_names(state, &reactor_ids); |
| 259 | reacted_messages(&records, my_user_id, |user_id| names.get(user_id).cloned()) |
| 260 | } |