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