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