BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / activity / service.rs

service.rs

09e74698 History
service.rs 6.0 KiB Download Raw
1use chrono::Utc;
2use gpui_kit::*;
3use teams_core::{ActivitySnapshot, stored_activity_feed};
4
5use super::feed::{Entry, Feed};
6use super::server::feed_of;
7use crate::agent_api;
8use crate::app_state::{AppEvent, AppState};
9use crate::demo;
10
11const SEEN_KEY: &str = "activity_seen_ms";
12
13pub struct ActivityCenter {
14 app: Entity<AppState>,
15 main_window: AnyWindowHandle,
16 snapshot: ActivitySnapshot,
17 seen_locally_ms: i64,
18 feed: Feed,
19 _subscription: Subscription,
20 _proposal_observation: Option<Subscription>,
21}
22
23impl ActivityCenter {
24 pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self {
25 let state = app.read(cx);
26 let snapshot = if state.mode.demo {
27 demo::activity_snapshot()
28 } else {
29 stored_activity_feed(&state.store).unwrap_or_default()
30 };
31 let seen_locally_ms = state
32 .store
33 .meta(SEEN_KEY)
34 .ok()
35 .flatten()
36 .and_then(|text| text.parse().ok())
37 .unwrap_or(0);
38 let subscription = cx.subscribe(&app, |this, _, event: &AppEvent, cx| {
39 this.on_app_event(event, cx);
40 });
41 let proposal_observation =
42 agent_api::handle(cx).map(|api| cx.observe(&api, |this, _, cx| this.on_proposals(cx)));
43 let feed = feed_of(&snapshot, seen_locally_ms);
44 let center = ActivityCenter {
45 app,
46 main_window: window.window_handle(),
47 snapshot,
48 seen_locally_ms,
49 feed,
50 _subscription: subscription,
51 _proposal_observation: proposal_observation,
52 };
53 center.request_avatars(cx);
54 center
55 }
56
57 pub fn feed(&self) -> &Feed {
58 &self.feed
59 }
60
61 pub fn mark_read(&mut self, id: &str, cx: &mut Context<Self>) {
62 self.mark_read_where(|entry| entry.id == id, cx);
63 }
64
65 pub fn mark_all_read(&mut self, cx: &mut Context<Self>) {
66 agent_api::mark_all_proposals_seen(cx);
67 if self.feed.unread_count() == 0 {
68 return;
69 }
70 self.snapshot.mark_all_read(Utc::now().timestamp_millis());
71 self.rebuild(cx);
72 self.app
73 .update(cx, |state, cx| state.mark_all_activity_read(cx));
74 }
75
76 /// Teams resets the bell when the feed is opened.
77 pub fn mark_seen(&mut self, cx: &mut Context<Self>) {
78 if self.feed.bell_count() == 0 {
79 return;
80 }
81 self.seen_locally_ms = Utc::now().timestamp_millis();
82 let _ = self
83 .app
84 .read(cx)
85 .store
86 .set_meta(SEEN_KEY, &self.seen_locally_ms.to_string());
87 self.rebuild(cx);
88 }
89
90 fn mark_read_where(&mut self, matches: impl Fn(&Entry) -> bool, cx: &mut Context<Self>) {
91 let message_ids: Vec<String> = self
92 .feed
93 .unread_entries()
94 .filter(|entry| matches(entry))
95 .map(|entry| entry.id.clone())
96 .collect();
97 if message_ids.is_empty() {
98 return;
99 }
100 for message_id in &message_ids {
101 self.snapshot.mark_read(message_id);
102 }
103 self.rebuild(cx);
104 self.app
105 .update(cx, |state, cx| state.mark_activity_read(message_ids, cx));
106 }
107
108 fn on_app_event(&mut self, event: &AppEvent, cx: &mut Context<Self>) {
109 match event {
110 AppEvent::Activity => self.reload(cx),
111 AppEvent::Selection => self.on_selection(cx),
112 AppEvent::Messages(conversation_id) if self.in_foreground(conversation_id, cx) => {
113 let conversation_id = conversation_id.clone();
114 self.mark_read_where(|entry| entry.conversation_id == conversation_id, cx);
115 }
116 _ => {}
117 }
118 }
119
120 fn reload(&mut self, cx: &mut Context<Self>) {
121 let state = self.app.read(cx);
122 if state.mode.demo {
123 return;
124 }
125 let Some(snapshot) = stored_activity_feed(&state.store) else {
126 return;
127 };
128 self.snapshot = snapshot;
129 self.rebuild(cx);
130 self.request_avatars(cx);
131 }
132
133 fn on_selection(&mut self, cx: &mut Context<Self>) {
134 let Some(conversation_id) = self.selected_conversation(cx) else {
135 return;
136 };
137 agent_api::mark_conversation_proposals_seen(&conversation_id, cx);
138 self.mark_read_where(|entry| entry.conversation_id == conversation_id, cx);
139 }
140
141 fn on_proposals(&mut self, cx: &mut Context<Self>) {
142 if let Some(conversation_id) = self.selected_conversation(cx)
143 && self.in_foreground(&conversation_id, cx)
144 {
145 agent_api::mark_conversation_proposals_seen(&conversation_id, cx);
146 }
147 cx.notify();
148 }
149
150 fn selected_conversation(&self, cx: &App) -> Option<String> {
151 self.app
152 .read(cx)
153 .selection
154 .as_ref()
155 .map(|selection| selection.conversation_id().to_owned())
156 }
157
158 /// Points at a main window reopened from the tray.
159 pub fn attach_main_window(&mut self, window: &Window) {
160 self.main_window = window.window_handle();
161 }
162
163 fn in_foreground(&self, conversation_id: &str, cx: &mut Context<Self>) -> bool {
164 self.selected_conversation(cx).as_deref() == Some(conversation_id)
165 && self
166 .main_window
167 .update(cx, |_, window, _| window.is_window_active())
168 .unwrap_or(false)
169 }
170
171 fn rebuild(&mut self, cx: &mut Context<Self>) {
172 self.feed = feed_of(&self.snapshot, self.seen_locally_ms);
173 cx.notify();
174 }
175
176 fn request_avatars(&self, cx: &mut Context<Self>) {
177 let mut user_ids: Vec<String> = self
178 .feed
179 .entries()
180 .iter()
181 .filter_map(|entry| entry.latest_actor()?.user_id.clone())
182 .collect();
183 user_ids.sort();
184 user_ids.dedup();
185 if user_ids.is_empty() {
186 return;
187 }
188 self.app
189 .update(cx, |state, cx| state.request_avatars(user_ids, cx));
190 }
191}