feat(presence): notify when available
- "Notify when available" / "Turn off notifications" in the 1:1 chat menu and on the profile card - Toast when a followed person becomes Available or Offline (not when out of office, not in DND) - Settings > Notifications: "Manage status notifications" with a Turn off per person - Deviation: followed list stored locally, not as the Tagged system tag on the CAPI v3 contact (write not verifiable read-only), so it does not sync with Teams Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -76,6 +76,10 @@ pub enum AppEvent {
76
76
CallTone(chatsvc::sounds::CallTone),
77
77
SoundSettings,
78
78
ChatSettings,
79
FollowedStatus {
80
user_id: String,
81
text: &'static str,
82
},
79
83
}
80
84
81
85
#[derive(Debug, Clone, Copy, Default)]
@@ -112,6 +116,8 @@ pub struct AppState {
112
116
pub connection_banner: crate::connection_banner::BannerTiming,
113
117
pub live_retry: Option<Arc<tokio::sync::Notify>>,
114
118
pub notice_count: u64,
119
pub followed_presence: Vec<crate::presence_follow::FollowedPerson>,
120
pub followed_changes: Vec<(String, &'static str)>,
115
121
pub keep_unread: Option<String>,
116
122
pub restorable_deletes: crate::restorable_deletes::RestorableDeletes,
117
123
pub typing: TypingState,
@@ -273,6 +279,8 @@ impl AppState {
273
279
connection_banner: Default::default(),
274
280
live_retry: None,
275
281
notice_count: 0,
282
followed_presence: Vec::new(),
283
followed_changes: Vec::new(),
276
284
keep_unread: None,
277
285
restorable_deletes: Default::default(),
278
286
typing: TypingState::default(),
@@ -343,6 +351,7 @@ impl AppState {
343
351
section_changes: crate::section_actions::SectionChanges::default(),
344
352
};
345
353
state.local_previews = load_local_previews(&state.store);
354
state.followed_presence = crate::presence_follow::load_followed(&state.store);
346
355
state.collapsed = state.load_collapsed();
347
356
state.call_share_sound = crate::call::load_share_sound(&state.store);
348
357
state.call_share_motion = crate::call::load_flag(&state.store, crate::call::SHARE_MOTION_META_KEY);
@@ -676,7 +685,12 @@ impl AppState {
676
685
let activity_changed = self
677
686
.directory
678
687
.set_activity(user_id, data::presence_activity(&engine, user_id));
679
self.directory.set_presence(user_id, kind) || activity_changed
688
let previous = self.directory.presence_of(user_id);
689
let changed = self.directory.set_presence(user_id, kind);
690
if changed {
691
self.note_followed_change(user_id, previous, kind);
692
}
693
changed || activity_changed
680
694
})
681
695
.collect()
682
696
}
@@ -695,6 +709,7 @@ impl AppState {
695
709
let done = data::refresh_presence(&engine, wanted.clone());
696
710
self.on_done(done, cx, move |state, cx| {
697
711
state.sync_presence();
712
state.announce_followed_changes(cx);
698
713
state.directory.save_presence(&state.store, &wanted);
699
714
state.directory.settle_presence(&wanted);
700
715
cx.emit(AppEvent::Directory);
@@ -1020,6 +1035,7 @@ impl AppState {
1020
1035
self.refresh_chat_settings(cx);
1021
1036
let waiting = self.directory.waiting_presence_ids();
1022
1037
self.request_presence(waiting, cx);
1038
self.watch_followed_presence(cx);
1023
1039
self.refresh_own_status(cx);
1024
1040
cx.emit(AppEvent::Status);
1025
1041
}
@@ -1058,6 +1074,7 @@ impl AppState {
1058
1074
}
1059
1075
BackendEvent::Core(CoreEvent::PresenceChanged) => {
1060
1076
let changed = self.sync_presence();
1077
self.announce_followed_changes(cx);
1061
1078
self.directory.save_presence(&self.store, &changed);
1062
1079
cx.emit(AppEvent::Directory);
1063
1080
cx.notify();
@@ -76,6 +76,10 @@ pub enum AppEvent {76
CallTone(chatsvc::sounds::CallTone),76
CallTone(chatsvc::sounds::CallTone),77
SoundSettings,77
SoundSettings,78
ChatSettings,78
ChatSettings,79
FollowedStatus {80
user_id: String,81
text: &'static str,82
},79
}83
}80
84
81
#[derive(Debug, Clone, Copy, Default)]85
#[derive(Debug, Clone, Copy, Default)]@@ -112,6 +116,8 @@ pub struct AppState {112
pub connection_banner: crate::connection_banner::BannerTiming,116
pub connection_banner: crate::connection_banner::BannerTiming,113
pub live_retry: Option<Arc<tokio::sync::Notify>>,117
pub live_retry: Option<Arc<tokio::sync::Notify>>,114
pub notice_count: u64,118
pub notice_count: u64,119
pub followed_presence: Vec<crate::presence_follow::FollowedPerson>,120
pub followed_changes: Vec<(String, &'static str)>,115
pub keep_unread: Option<String>,121
pub keep_unread: Option<String>,116
pub restorable_deletes: crate::restorable_deletes::RestorableDeletes,122
pub restorable_deletes: crate::restorable_deletes::RestorableDeletes,117
pub typing: TypingState,123
pub typing: TypingState,@@ -273,6 +279,8 @@ impl AppState {273
connection_banner: Default::default(),279
connection_banner: Default::default(),274
live_retry: None,280
live_retry: None,275
notice_count: 0,281
notice_count: 0,282
followed_presence: Vec::new(),283
followed_changes: Vec::new(),276
keep_unread: None,284
keep_unread: None,277
restorable_deletes: Default::default(),285
restorable_deletes: Default::default(),278
typing: TypingState::default(),286
typing: TypingState::default(),@@ -343,6 +351,7 @@ impl AppState {343
section_changes: crate::section_actions::SectionChanges::default(),351
section_changes: crate::section_actions::SectionChanges::default(),344
};352
};345
state.local_previews = load_local_previews(&state.store);353
state.local_previews = load_local_previews(&state.store);354
state.followed_presence = crate::presence_follow::load_followed(&state.store);346
state.collapsed = state.load_collapsed();355
state.collapsed = state.load_collapsed();347
state.call_share_sound = crate::call::load_share_sound(&state.store);356
state.call_share_sound = crate::call::load_share_sound(&state.store);348
state.call_share_motion = crate::call::load_flag(&state.store, crate::call::SHARE_MOTION_META_KEY);357
state.call_share_motion = crate::call::load_flag(&state.store, crate::call::SHARE_MOTION_META_KEY);@@ -676,7 +685,12 @@ impl AppState {676
let activity_changed = self685
let activity_changed = self677
.directory686
.directory678
.set_activity(user_id, data::presence_activity(&engine, user_id));687
.set_activity(user_id, data::presence_activity(&engine, user_id));679
self.directory.set_presence(user_id, kind) || activity_changed688
let previous = self.directory.presence_of(user_id);689
let changed = self.directory.set_presence(user_id, kind);690
if changed {691
self.note_followed_change(user_id, previous, kind);692
}693
changed || activity_changed680
})694
})681
.collect()695
.collect()682
}696
}@@ -695,6 +709,7 @@ impl AppState {695
let done = data::refresh_presence(&engine, wanted.clone());709
let done = data::refresh_presence(&engine, wanted.clone());696
self.on_done(done, cx, move |state, cx| {710
self.on_done(done, cx, move |state, cx| {697
state.sync_presence();711
state.sync_presence();712
state.announce_followed_changes(cx);698
state.directory.save_presence(&state.store, &wanted);713
state.directory.save_presence(&state.store, &wanted);699
state.directory.settle_presence(&wanted);714
state.directory.settle_presence(&wanted);700
cx.emit(AppEvent::Directory);715
cx.emit(AppEvent::Directory);@@ -1020,6 +1035,7 @@ impl AppState {1020
self.refresh_chat_settings(cx);1035
self.refresh_chat_settings(cx);1021
let waiting = self.directory.waiting_presence_ids();1036
let waiting = self.directory.waiting_presence_ids();1022
self.request_presence(waiting, cx);1037
self.request_presence(waiting, cx);1038
self.watch_followed_presence(cx);1023
self.refresh_own_status(cx);1039
self.refresh_own_status(cx);1024
cx.emit(AppEvent::Status);1040
cx.emit(AppEvent::Status);1025
}1041
}@@ -1058,6 +1074,7 @@ impl AppState {1058
}1074
}1059
BackendEvent::Core(CoreEvent::PresenceChanged) => {1075
BackendEvent::Core(CoreEvent::PresenceChanged) => {1060
let changed = self.sync_presence();1076
let changed = self.sync_presence();1077
self.announce_followed_changes(cx);1061
self.directory.save_presence(&self.store, &changed);1078
self.directory.save_presence(&self.store, &changed);1062
cx.emit(AppEvent::Directory);1079
cx.emit(AppEvent::Directory);1063
cx.notify();1080
cx.notify();@@ -257,7 +257,7 @@ impl AppState {
257
257
.detach();
258
258
}
259
259
260
fn one_on_one_chat_with(&self, user_id: &str) -> Option<String> {
260
pub fn one_on_one_chat_with(&self, user_id: &str) -> Option<String> {
261
261
self.sidebar
262
262
.chats
263
263
.iter()
@@ -257,7 +257,7 @@ impl AppState {257
.detach();257
.detach();258
}258
}259
259
260
fn one_on_one_chat_with(&self, user_id: &str) -> Option<String> {260
pub fn one_on_one_chat_with(&self, user_id: &str) -> Option<String> {261
self.sidebar261
self.sidebar262
.chats262
.chats263
.iter()263
.iter()@@ -34,6 +34,7 @@ pub mod notify;
34
34
pub mod outbox;
35
35
pub mod outgoing;
36
36
pub mod own_status;
37
pub mod presence_follow;
37
38
pub mod pending_rows;
38
39
pub mod people;
39
40
pub mod profile_model;
@@ -34,6 +34,7 @@ pub mod notify;34
pub mod outbox;34
pub mod outbox;35
pub mod outgoing;35
pub mod outgoing;36
pub mod own_status;36
pub mod own_status;37
pub mod presence_follow;37
pub mod pending_rows;38
pub mod pending_rows;38
pub mod people;39
pub mod people;39
pub mod profile_model;40
pub mod profile_model;@@ -200,6 +200,7 @@ mod tests {
200
200
dismissed: false,
201
201
fade: super::super::stack::Fade::shown(std::time::Instant::now()),
202
202
agent: None,
203
status_change: false,
203
204
urgent: false,
204
205
};
205
206
assert_eq!(toast_height(&model("Hi"), 1), 112.);
@@ -200,6 +200,7 @@ mod tests {200
dismissed: false,200
dismissed: false,201
fade: super::super::stack::Fade::shown(std::time::Instant::now()),201
fade: super::super::stack::Fade::shown(std::time::Instant::now()),202
agent: None,202
agent: None,203
status_change: false,203
urgent: false,204
urgent: false,204
};205
};205
assert_eq!(toast_height(&model("Hi"), 1), 112.);206
assert_eq!(toast_height(&model("Hi"), 1), 112.);@@ -194,6 +194,7 @@ pub struct ToastModel {
194
194
pub dismissed: bool,
195
195
pub fade: Fade,
196
196
pub agent: Option<AgentToast>,
197
pub status_change: bool,
197
198
pub urgent: bool,
198
199
}
199
200
@@ -233,7 +234,9 @@ pub struct ToastStack {
233
234
impl ToastStack {
234
235
pub fn push(&mut self, incoming: &Incoming, now: Instant) -> PushOutcome {
235
236
if let Some(existing) = self.toasts.iter_mut().find(|toast| {
236
toast.agent.is_none() && toast.conversation_id == incoming.conversation_id
237
toast.agent.is_none()
238
&& !toast.status_change
239
&& toast.conversation_id == incoming.conversation_id
237
240
}) {
238
241
let became_mention = incoming.mentions_me && !existing.mentions_me;
239
242
existing.count += 1;
@@ -273,6 +276,7 @@ impl ToastStack {
273
276
dismissed: false,
274
277
fade: Fade::new(0., 1., now, FADE_IN, Curve::Linear),
275
278
agent: None,
279
status_change: false,
276
280
urgent: incoming.is_urgent(),
277
281
});
278
282
PushOutcome {
@@ -311,6 +315,43 @@ impl ToastStack {
311
315
dismissed: false,
312
316
fade: Fade::new(0., 1., now, FADE_IN, Curve::Linear),
313
317
agent: Some(agent),
318
status_change: false,
319
urgent: false,
320
});
321
self.next_id
322
}
323
324
pub fn push_status_change(
325
&mut self,
326
user_id: &str,
327
name: &str,
328
text: &str,
329
conversation_id: &str,
330
now: Instant,
331
) -> u64 {
332
self.next_id += 1;
333
let queued = self.toasts.len() >= MAX_VISIBLE;
334
self.toasts.push(ToastModel {
335
id: self.next_id,
336
conversation_id: conversation_id.to_owned(),
337
message_id: String::new(),
338
kind: ChatKind::Direct,
339
chat_title: name.to_owned(),
340
sender_id: Some(user_id.to_owned()),
341
sender_name: name.to_owned(),
342
preview: Preview::Text(text.to_owned()),
343
mentions_me: false,
344
count: 1,
345
timer: ToastTimer::new(now, false),
346
reply: ReplyState::Closed,
347
reply_text: String::new(),
348
hovered: false,
349
time: String::new(),
350
queued,
351
dismissed: false,
352
fade: Fade::new(0., 1., now, FADE_IN, Curve::Linear),
353
agent: None,
354
status_change: true,
314
355
urgent: false,
315
356
});
316
357
self.next_id
@@ -194,6 +194,7 @@ pub struct ToastModel {194
pub dismissed: bool,194
pub dismissed: bool,195
pub fade: Fade,195
pub fade: Fade,196
pub agent: Option<AgentToast>,196
pub agent: Option<AgentToast>,197
pub status_change: bool,197
pub urgent: bool,198
pub urgent: bool,198
}199
}199
200
@@ -233,7 +234,9 @@ pub struct ToastStack {233
impl ToastStack {234
impl ToastStack {234
pub fn push(&mut self, incoming: &Incoming, now: Instant) -> PushOutcome {235
pub fn push(&mut self, incoming: &Incoming, now: Instant) -> PushOutcome {235
if let Some(existing) = self.toasts.iter_mut().find(|toast| {236
if let Some(existing) = self.toasts.iter_mut().find(|toast| {236
toast.agent.is_none() && toast.conversation_id == incoming.conversation_id237
toast.agent.is_none()238
&& !toast.status_change239
&& toast.conversation_id == incoming.conversation_id237
}) {240
}) {238
let became_mention = incoming.mentions_me && !existing.mentions_me;241
let became_mention = incoming.mentions_me && !existing.mentions_me;239
existing.count += 1;242
existing.count += 1;@@ -273,6 +276,7 @@ impl ToastStack {273
dismissed: false,276
dismissed: false,274
fade: Fade::new(0., 1., now, FADE_IN, Curve::Linear),277
fade: Fade::new(0., 1., now, FADE_IN, Curve::Linear),275
agent: None,278
agent: None,279
status_change: false,276
urgent: incoming.is_urgent(),280
urgent: incoming.is_urgent(),277
});281
});278
PushOutcome {282
PushOutcome {@@ -311,6 +315,43 @@ impl ToastStack {311
dismissed: false,315
dismissed: false,312
fade: Fade::new(0., 1., now, FADE_IN, Curve::Linear),316
fade: Fade::new(0., 1., now, FADE_IN, Curve::Linear),313
agent: Some(agent),317
agent: Some(agent),318
status_change: false,319
urgent: false,320
});321
self.next_id322
}323
324
pub fn push_status_change(325
&mut self,326
user_id: &str,327
name: &str,328
text: &str,329
conversation_id: &str,330
now: Instant,331
) -> u64 {332
self.next_id += 1;333
let queued = self.toasts.len() >= MAX_VISIBLE;334
self.toasts.push(ToastModel {335
id: self.next_id,336
conversation_id: conversation_id.to_owned(),337
message_id: String::new(),338
kind: ChatKind::Direct,339
chat_title: name.to_owned(),340
sender_id: Some(user_id.to_owned()),341
sender_name: name.to_owned(),342
preview: Preview::Text(text.to_owned()),343
mentions_me: false,344
count: 1,345
timer: ToastTimer::new(now, false),346
reply: ReplyState::Closed,347
reply_text: String::new(),348
hovered: false,349
time: String::new(),350
queued,351
dismissed: false,352
fade: Fade::new(0., 1., now, FADE_IN, Curve::Linear),353
agent: None,354
status_change: true,314
urgent: false,355
urgent: false,315
});356
});316
self.next_id357
self.next_id@@ -34,6 +34,7 @@ pub fn describe(model: &ToastModel, preview_on: bool) -> ToastText {
34
34
};
35
35
let subtitle = subtitle_of(&model.kind);
36
36
let body = match (&model.preview, preview_on) {
37
(Preview::Text(text), false) if model.status_change => text.clone(),
37
38
(_, false) => PREVIEW_OFF.to_owned(),
38
39
(Preview::Text(text), true) => text.clone(),
39
40
(Preview::Image, true) => IMAGE_LABEL.to_owned(),
@@ -106,6 +107,7 @@ mod tests {
106
107
dismissed: false,
107
108
fade: Fade::shown(Instant::now()),
108
109
agent: None,
110
status_change: false,
109
111
urgent: false,
110
112
}
111
113
}
@@ -34,6 +34,7 @@ pub fn describe(model: &ToastModel, preview_on: bool) -> ToastText {34
};34
};35
let subtitle = subtitle_of(&model.kind);35
let subtitle = subtitle_of(&model.kind);36
let body = match (&model.preview, preview_on) {36
let body = match (&model.preview, preview_on) {37
(Preview::Text(text), false) if model.status_change => text.clone(),37
(_, false) => PREVIEW_OFF.to_owned(),38
(_, false) => PREVIEW_OFF.to_owned(),38
(Preview::Text(text), true) => text.clone(),39
(Preview::Text(text), true) => text.clone(),39
(Preview::Image, true) => IMAGE_LABEL.to_owned(),40
(Preview::Image, true) => IMAGE_LABEL.to_owned(),@@ -106,6 +107,7 @@ mod tests {106
dismissed: false,107
dismissed: false,107
fade: Fade::shown(Instant::now()),108
fade: Fade::shown(Instant::now()),108
agent: None,109
agent: None,110
status_change: false,109
urgent: false,111
urgent: false,110
}112
}111
}113
}@@ -0,0 +1,177 @@
1
use gpui_kit::*;
2
use serde::{Deserialize, Serialize};
3
use store::Store;
4
5
use crate::app_state::{AppEvent, AppState};
6
use crate::data::{Presence, PresenceKind};
7
8
const FOLLOWED_META_KEY: &str = "presence.followed";
9
pub const NOTIFY_WHEN_AVAILABLE: &str = "Notify when available";
10
pub const TURN_OFF_NOTIFICATIONS: &str = "Turn off notifications";
11
pub const MANAGE_STATUS_NOTIFICATIONS: &str = "Manage status notifications";
12
pub const STATUS_NOTIFICATIONS_DESCRIPTION: &str =
13
"Keep track of someone's status and get notified when they appear available or offline.";
14
pub const TURN_OFF: &str = "Turn off";
15
pub const NOW_AVAILABLE: &str = "Now available";
16
pub const NOW_OFFLINE: &str = "Now offline";
17
18
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
19
pub struct FollowedPerson {
20
pub user_id: String,
21
pub name: String,
22
}
23
24
pub fn load_followed(store: &Store) -> Vec<FollowedPerson> {
25
store
26
.meta(FOLLOWED_META_KEY)
27
.ok()
28
.flatten()
29
.and_then(|json| serde_json::from_str(&json).ok())
30
.unwrap_or_default()
31
}
32
33
fn save_followed(store: &Store, followed: &[FollowedPerson]) {
34
if let Ok(json) = serde_json::to_string(followed) {
35
let _ = store.set_meta(FOLLOWED_META_KEY, &json);
36
}
37
}
38
39
/// Teams only tells about a change to available or offline from another known status.
40
pub fn status_change_text(previous: Presence, next: PresenceKind) -> Option<&'static str> {
41
let Presence::Live(previous) = previous else {
42
return None;
43
};
44
if previous == next || previous == PresenceKind::Unknown {
45
return None;
46
}
47
match next {
48
PresenceKind::Available => Some(NOW_AVAILABLE),
49
PresenceKind::Offline => Some(NOW_OFFLINE),
50
_ => None,
51
}
52
}
53
54
pub fn follow_notice(name: &str, follow: bool) -> String {
55
if follow {
56
format!("You are now following the presence of {name}")
57
} else {
58
format!("You are no longer following the presence of {name}")
59
}
60
}
61
62
impl AppState {
63
pub fn one_on_one_partner(&self, chat_id: &str) -> Option<FollowedPerson> {
64
if teams_core::is_notes_chat(chat_id) {
65
return None;
66
}
67
let chat = self.sidebar.chats.iter().find(|chat| chat.id == chat_id)?;
68
if !crate::data::is_one_on_one(chat) {
69
return None;
70
}
71
crate::data::others(chat, self.directory.me.as_ref())
72
.into_iter()
73
.find_map(|(user_id, name)| {
74
let user_id = user_id.filter(|user_id| !store::is_bot_id(user_id))?;
75
Some(FollowedPerson { user_id, name })
76
})
77
}
78
79
pub fn is_presence_followed(&self, user_id: &str) -> bool {
80
self.followed_presence
81
.iter()
82
.any(|person| person.user_id == user_id)
83
}
84
85
pub fn set_presence_followed(
86
&mut self,
87
user_id: &str,
88
name: &str,
89
follow: bool,
90
cx: &mut Context<Self>,
91
) {
92
if follow == self.is_presence_followed(user_id) {
93
return;
94
}
95
if follow {
96
self.followed_presence.push(FollowedPerson {
97
user_id: user_id.to_owned(),
98
name: name.to_owned(),
99
});
100
self.request_presence(vec![user_id.to_owned()], cx);
101
} else {
102
self.followed_presence
103
.retain(|person| person.user_id != user_id);
104
}
105
save_followed(&self.store, &self.followed_presence);
106
self.raise_notice(follow_notice(name, follow), None, cx);
107
cx.notify();
108
}
109
110
pub fn watch_followed_presence(&mut self, cx: &mut Context<Self>) {
111
let user_ids: Vec<String> = self
112
.followed_presence
113
.iter()
114
.map(|person| person.user_id.clone())
115
.collect();
116
if !user_ids.is_empty() {
117
self.request_presence(user_ids, cx);
118
}
119
}
120
121
pub(crate) fn note_followed_change(&mut self, user_id: &str, previous: Presence, next: PresenceKind) {
122
if !self.is_presence_followed(user_id) {
123
return;
124
}
125
if let Some(text) = status_change_text(previous, next) {
126
self.followed_changes.push((user_id.to_owned(), text));
127
}
128
}
129
130
pub(crate) fn announce_followed_changes(&mut self, cx: &mut Context<Self>) {
131
for (user_id, text) in std::mem::take(&mut self.followed_changes) {
132
cx.emit(AppEvent::FollowedStatus { user_id, text });
133
}
134
}
135
}
136
137
#[cfg(test)]
138
mod tests {
139
use super::{NOW_AVAILABLE, NOW_OFFLINE, follow_notice, status_change_text};
140
use crate::data::{Presence, PresenceKind};
141
142
#[test]
143
fn only_a_change_to_available_or_offline_is_announced() {
144
let busy = Presence::Live(PresenceKind::Busy);
145
assert_eq!(status_change_text(busy, PresenceKind::Available), Some(NOW_AVAILABLE));
146
assert_eq!(status_change_text(busy, PresenceKind::Offline), Some(NOW_OFFLINE));
147
assert_eq!(status_change_text(busy, PresenceKind::Away), None);
148
assert_eq!(status_change_text(busy, PresenceKind::OutOfOffice), None);
149
let available = Presence::Live(PresenceKind::Available);
150
assert_eq!(status_change_text(available, PresenceKind::Available), None);
151
}
152
153
#[test]
154
fn the_first_known_status_is_not_announced() {
155
assert_eq!(status_change_text(Presence::Loading, PresenceKind::Available), None);
156
assert_eq!(
157
status_change_text(Presence::Cached(PresenceKind::Busy), PresenceKind::Available),
158
None
159
);
160
assert_eq!(
161
status_change_text(Presence::Live(PresenceKind::Unknown), PresenceKind::Available),
162
None
163
);
164
}
165
166
#[test]
167
fn the_notice_uses_the_teams_text() {
168
assert_eq!(
169
follow_notice("Anna", true),
170
"You are now following the presence of Anna"
171
);
172
assert_eq!(
173
follow_notice("Anna", false),
174
"You are no longer following the presence of Anna"
175
);
176
}
177
}
@@ -0,0 +1,177 @@1
use gpui_kit::*;2
use serde::{Deserialize, Serialize};3
use store::Store;4
5
use crate::app_state::{AppEvent, AppState};6
use crate::data::{Presence, PresenceKind};7
8
const FOLLOWED_META_KEY: &str = "presence.followed";9
pub const NOTIFY_WHEN_AVAILABLE: &str = "Notify when available";10
pub const TURN_OFF_NOTIFICATIONS: &str = "Turn off notifications";11
pub const MANAGE_STATUS_NOTIFICATIONS: &str = "Manage status notifications";12
pub const STATUS_NOTIFICATIONS_DESCRIPTION: &str =13
"Keep track of someone's status and get notified when they appear available or offline.";14
pub const TURN_OFF: &str = "Turn off";15
pub const NOW_AVAILABLE: &str = "Now available";16
pub const NOW_OFFLINE: &str = "Now offline";17
18
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]19
pub struct FollowedPerson {20
pub user_id: String,21
pub name: String,22
}23
24
pub fn load_followed(store: &Store) -> Vec<FollowedPerson> {25
store26
.meta(FOLLOWED_META_KEY)27
.ok()28
.flatten()29
.and_then(|json| serde_json::from_str(&json).ok())30
.unwrap_or_default()31
}32
33
fn save_followed(store: &Store, followed: &[FollowedPerson]) {34
if let Ok(json) = serde_json::to_string(followed) {35
let _ = store.set_meta(FOLLOWED_META_KEY, &json);36
}37
}38
39
/// Teams only tells about a change to available or offline from another known status.40
pub fn status_change_text(previous: Presence, next: PresenceKind) -> Option<&'static str> {41
let Presence::Live(previous) = previous else {42
return None;43
};44
if previous == next || previous == PresenceKind::Unknown {45
return None;46
}47
match next {48
PresenceKind::Available => Some(NOW_AVAILABLE),49
PresenceKind::Offline => Some(NOW_OFFLINE),50
_ => None,51
}52
}53
54
pub fn follow_notice(name: &str, follow: bool) -> String {55
if follow {56
format!("You are now following the presence of {name}")57
} else {58
format!("You are no longer following the presence of {name}")59
}60
}61
62
impl AppState {63
pub fn one_on_one_partner(&self, chat_id: &str) -> Option<FollowedPerson> {64
if teams_core::is_notes_chat(chat_id) {65
return None;66
}67
let chat = self.sidebar.chats.iter().find(|chat| chat.id == chat_id)?;68
if !crate::data::is_one_on_one(chat) {69
return None;70
}71
crate::data::others(chat, self.directory.me.as_ref())72
.into_iter()73
.find_map(|(user_id, name)| {74
let user_id = user_id.filter(|user_id| !store::is_bot_id(user_id))?;75
Some(FollowedPerson { user_id, name })76
})77
}78
79
pub fn is_presence_followed(&self, user_id: &str) -> bool {80
self.followed_presence81
.iter()82
.any(|person| person.user_id == user_id)83
}84
85
pub fn set_presence_followed(86
&mut self,87
user_id: &str,88
name: &str,89
follow: bool,90
cx: &mut Context<Self>,91
) {92
if follow == self.is_presence_followed(user_id) {93
return;94
}95
if follow {96
self.followed_presence.push(FollowedPerson {97
user_id: user_id.to_owned(),98
name: name.to_owned(),99
});100
self.request_presence(vec![user_id.to_owned()], cx);101
} else {102
self.followed_presence103
.retain(|person| person.user_id != user_id);104
}105
save_followed(&self.store, &self.followed_presence);106
self.raise_notice(follow_notice(name, follow), None, cx);107
cx.notify();108
}109
110
pub fn watch_followed_presence(&mut self, cx: &mut Context<Self>) {111
let user_ids: Vec<String> = self112
.followed_presence113
.iter()114
.map(|person| person.user_id.clone())115
.collect();116
if !user_ids.is_empty() {117
self.request_presence(user_ids, cx);118
}119
}120
121
pub(crate) fn note_followed_change(&mut self, user_id: &str, previous: Presence, next: PresenceKind) {122
if !self.is_presence_followed(user_id) {123
return;124
}125
if let Some(text) = status_change_text(previous, next) {126
self.followed_changes.push((user_id.to_owned(), text));127
}128
}129
130
pub(crate) fn announce_followed_changes(&mut self, cx: &mut Context<Self>) {131
for (user_id, text) in std::mem::take(&mut self.followed_changes) {132
cx.emit(AppEvent::FollowedStatus { user_id, text });133
}134
}135
}136
137
#[cfg(test)]138
mod tests {139
use super::{NOW_AVAILABLE, NOW_OFFLINE, follow_notice, status_change_text};140
use crate::data::{Presence, PresenceKind};141
142
#[test]143
fn only_a_change_to_available_or_offline_is_announced() {144
let busy = Presence::Live(PresenceKind::Busy);145
assert_eq!(status_change_text(busy, PresenceKind::Available), Some(NOW_AVAILABLE));146
assert_eq!(status_change_text(busy, PresenceKind::Offline), Some(NOW_OFFLINE));147
assert_eq!(status_change_text(busy, PresenceKind::Away), None);148
assert_eq!(status_change_text(busy, PresenceKind::OutOfOffice), None);149
let available = Presence::Live(PresenceKind::Available);150
assert_eq!(status_change_text(available, PresenceKind::Available), None);151
}152
153
#[test]154
fn the_first_known_status_is_not_announced() {155
assert_eq!(status_change_text(Presence::Loading, PresenceKind::Available), None);156
assert_eq!(157
status_change_text(Presence::Cached(PresenceKind::Busy), PresenceKind::Available),158
None159
);160
assert_eq!(161
status_change_text(Presence::Live(PresenceKind::Unknown), PresenceKind::Available),162
None163
);164
}165
166
#[test]167
fn the_notice_uses_the_teams_text() {168
assert_eq!(169
follow_notice("Anna", true),170
"You are now following the presence of Anna"171
);172
assert_eq!(173
follow_notice("Anna", false),174
"You are no longer following the presence of Anna"175
);176
}177
}@@ -388,6 +388,9 @@ impl NotificationCenter {
388
388
AppEvent::Call => self.sync_call_sounds(cx),
389
389
AppEvent::SoundSettings => self.on_account_sounds(cx),
390
390
AppEvent::ChatSettings => self.on_account_notifications(cx),
391
AppEvent::FollowedStatus { user_id, text } => {
392
self.present_status_change(user_id, text, cx)
393
}
391
394
_ => {}
392
395
}
393
396
}
@@ -513,6 +516,32 @@ impl NotificationCenter {
513
516
self.sync_windows(cx);
514
517
}
515
518
519
fn present_status_change(&mut self, user_id: &str, text: &str, cx: &mut Context<Self>) {
520
if self.settings.do_not_disturb {
521
return;
522
}
523
let (name, conversation_id) = {
524
let state = self.app.read(cx);
525
let name = state
526
.followed_presence
527
.iter()
528
.find(|person| person.user_id == user_id)
529
.map(|person| person.name.clone())
530
.unwrap_or_default();
531
(name, state.one_on_one_chat_with(user_id).unwrap_or_default())
532
};
533
let id = self
534
.stack
535
.push_status_change(user_id, &name, text, &conversation_id, Instant::now());
536
if let Some(model) = self.stack.get_mut(id) {
537
model.time = chrono::Local::now().format("%H:%M").to_string();
538
}
539
let user_id = user_id.to_owned();
540
self.app
541
.update(cx, |state, cx| state.request_avatars(vec![user_id], cx));
542
self.sync_windows(cx);
543
}
544
516
545
pub fn present_agent_run(&mut self, run: &AgentRun, cx: &mut Context<Self>) {
517
546
let failed = matches!(run.state, RunState::Failed { .. });
518
547
let verb = if failed { "failed" } else { "finished" };
@@ -388,6 +388,9 @@ impl NotificationCenter {388
AppEvent::Call => self.sync_call_sounds(cx),388
AppEvent::Call => self.sync_call_sounds(cx),389
AppEvent::SoundSettings => self.on_account_sounds(cx),389
AppEvent::SoundSettings => self.on_account_sounds(cx),390
AppEvent::ChatSettings => self.on_account_notifications(cx),390
AppEvent::ChatSettings => self.on_account_notifications(cx),391
AppEvent::FollowedStatus { user_id, text } => {392
self.present_status_change(user_id, text, cx)393
}391
_ => {}394
_ => {}392
}395
}393
}396
}@@ -513,6 +516,32 @@ impl NotificationCenter {513
self.sync_windows(cx);516
self.sync_windows(cx);514
}517
}515
518
519
fn present_status_change(&mut self, user_id: &str, text: &str, cx: &mut Context<Self>) {520
if self.settings.do_not_disturb {521
return;522
}523
let (name, conversation_id) = {524
let state = self.app.read(cx);525
let name = state526
.followed_presence527
.iter()528
.find(|person| person.user_id == user_id)529
.map(|person| person.name.clone())530
.unwrap_or_default();531
(name, state.one_on_one_chat_with(user_id).unwrap_or_default())532
};533
let id = self534
.stack535
.push_status_change(user_id, &name, text, &conversation_id, Instant::now());536
if let Some(model) = self.stack.get_mut(id) {537
model.time = chrono::Local::now().format("%H:%M").to_string();538
}539
let user_id = user_id.to_owned();540
self.app541
.update(cx, |state, cx| state.request_avatars(vec![user_id], cx));542
self.sync_windows(cx);543
}544
516
pub fn present_agent_run(&mut self, run: &AgentRun, cx: &mut Context<Self>) {545
pub fn present_agent_run(&mut self, run: &AgentRun, cx: &mut Context<Self>) {517
let failed = matches!(run.state, RunState::Failed { .. });546
let failed = matches!(run.state, RunState::Failed { .. });518
let verb = if failed { "failed" } else { "finished" };547
let verb = if failed { "failed" } else { "finished" };@@ -533,7 +533,9 @@ impl Render for ToastView {
533
533
.child(self.avatar(&model, fill, cx))
534
534
.child(self.body(&model, &text, hovered)),
535
535
)
536
.child(self.action_row(mention, cx)),
536
.when(!model.status_change, |content| {
537
content.child(self.action_row(mention, cx))
538
}),
537
539
};
538
540
let center = self.center.clone();
539
541
let hover_center = self.center.clone();
@@ -533,7 +533,9 @@ impl Render for ToastView {533
.child(self.avatar(&model, fill, cx))533
.child(self.avatar(&model, fill, cx))534
.child(self.body(&model, &text, hovered)),534
.child(self.body(&model, &text, hovered)),535
)535
)536
.child(self.action_row(mention, cx)),536
.when(!model.status_change, |content| {537
content.child(self.action_row(mention, cx))538
}),537
};539
};538
let center = self.center.clone();540
let center = self.center.clone();539
let hover_center = self.center.clone();541
let hover_center = self.center.clone();@@ -9,6 +9,7 @@ use super::account::{
9
9
};
10
10
use super::rows::{Choice, caption, control_row, select, text_button, toggle_row};
11
11
use crate::app_state::AppState;
12
use crate::presence_follow::{MANAGE_STATUS_NOTIFICATIONS, STATUS_NOTIFICATIONS_DESCRIPTION, TURN_OFF};
12
13
use crate::notify::NotificationCenter;
13
14
use crate::notify::rules::{Corner, Settings};
14
15
use crate::notify::sounds::AlertSound;
@@ -192,6 +193,35 @@ pub fn render(center: &Entity<NotificationCenter>, app: &Entity<AppState>, cx: &
192
193
ChatSettingChange::MeetingChats,
193
194
))
194
195
.child(hide_in_calls)
196
.child(caption("PEOPLE"))
197
.child(control_row(
198
MANAGE_STATUS_NOTIFICATIONS,
199
STATUS_NOTIFICATIONS_DESCRIPTION,
200
div(),
201
))
202
.children(followed_rows(app, cx))
203
}
204
205
fn followed_rows(app: &Entity<AppState>, cx: &App) -> Vec<Div> {
206
app.read(cx)
207
.followed_presence
208
.iter()
209
.enumerate()
210
.map(|(index, person)| {
211
let (app, user_id, name) = (app.clone(), person.user_id.clone(), person.name.clone());
212
control_row(
213
person.name.clone(),
214
"",
215
text_button(("settings-unfollow-presence", index), TURN_OFF).on_click(
216
move |_, _, cx| {
217
app.update(cx, |state, cx| {
218
state.set_presence_followed(&user_id, &name, false, cx)
219
})
220
},
221
),
222
)
223
})
224
.collect()
195
225
}
196
226
197
227
fn preview_toggle(app: &Entity<AppState>, on: bool) -> Stateful<Div> {
@@ -9,6 +9,7 @@ use super::account::{9
};9
};10
use super::rows::{Choice, caption, control_row, select, text_button, toggle_row};10
use super::rows::{Choice, caption, control_row, select, text_button, toggle_row};11
use crate::app_state::AppState;11
use crate::app_state::AppState;12
use crate::presence_follow::{MANAGE_STATUS_NOTIFICATIONS, STATUS_NOTIFICATIONS_DESCRIPTION, TURN_OFF};12
use crate::notify::NotificationCenter;13
use crate::notify::NotificationCenter;13
use crate::notify::rules::{Corner, Settings};14
use crate::notify::rules::{Corner, Settings};14
use crate::notify::sounds::AlertSound;15
use crate::notify::sounds::AlertSound;@@ -192,6 +193,35 @@ pub fn render(center: &Entity<NotificationCenter>, app: &Entity<AppState>, cx: &192
ChatSettingChange::MeetingChats,193
ChatSettingChange::MeetingChats,193
))194
))194
.child(hide_in_calls)195
.child(hide_in_calls)196
.child(caption("PEOPLE"))197
.child(control_row(198
MANAGE_STATUS_NOTIFICATIONS,199
STATUS_NOTIFICATIONS_DESCRIPTION,200
div(),201
))202
.children(followed_rows(app, cx))203
}204
205
fn followed_rows(app: &Entity<AppState>, cx: &App) -> Vec<Div> {206
app.read(cx)207
.followed_presence208
.iter()209
.enumerate()210
.map(|(index, person)| {211
let (app, user_id, name) = (app.clone(), person.user_id.clone(), person.name.clone());212
control_row(213
person.name.clone(),214
"",215
text_button(("settings-unfollow-presence", index), TURN_OFF).on_click(216
move |_, _, cx| {217
app.update(cx, |state, cx| {218
state.set_presence_followed(&user_id, &name, false, cx)219
})220
},221
),222
)223
})224
.collect()195
}225
}196
226
197
fn preview_toggle(app: &Entity<AppState>, on: bool) -> Stateful<Div> {227
fn preview_toggle(app: &Entity<AppState>, on: bool) -> Stateful<Div> {@@ -713,6 +713,7 @@ impl ConversationView {
713
713
| AppEvent::MeetingChime
714
714
| AppEvent::CallTone(_)
715
715
| AppEvent::SoundSettings
716
| AppEvent::FollowedStatus { .. }
716
717
| AppEvent::Call => {}
717
718
AppEvent::LiveMeeting => cx.notify(),
718
719
AppEvent::Saved => cx.notify(),
@@ -713,6 +713,7 @@ impl ConversationView {713
| AppEvent::MeetingChime713
| AppEvent::MeetingChime714
| AppEvent::CallTone(_)714
| AppEvent::CallTone(_)715
| AppEvent::SoundSettings715
| AppEvent::SoundSettings716
| AppEvent::FollowedStatus { .. }716
| AppEvent::Call => {}717
| AppEvent::Call => {}717
AppEvent::LiveMeeting => cx.notify(),718
AppEvent::LiveMeeting => cx.notify(),718
AppEvent::Saved => cx.notify(),719
AppEvent::Saved => cx.notify(),@@ -20,6 +20,7 @@ use crate::profile_model::{
20
20
mailto_url, status_box, status_line, work_location_label,
21
21
};
22
22
use crate::theme::{self, ElevatedSurface as _};
23
use crate::presence_follow::{NOTIFY_WHEN_AVAILABLE, TURN_OFF_NOTIFICATIONS};
23
24
24
25
const CARD_WIDTH: f32 = 340.;
25
26
const CARD_RADIUS: f32 = 10.;
@@ -276,6 +277,8 @@ impl ProfileCard {
276
277
};
277
278
let chat_id = user_id.clone();
278
279
let (video_id, audio_id) = (user_id.clone(), user_id.clone());
280
let followed = self.app.read(cx).is_presence_followed(&user_id);
281
let (follow_id, follow_name) = (user_id.clone(), name.clone());
279
282
h_flex()
280
283
.gap(px(8.))
281
284
.when(!is_me, |row| {
@@ -326,6 +329,28 @@ impl ProfileCard {
326
329
cx.emit(ProfileCardEvent::Close);
327
330
})),
328
331
)
332
.child(
333
icon_action(
334
"profile-follow-presence",
335
if followed {
336
IconName::BellOff
337
} else {
338
IconName::BellRing
339
},
340
if followed {
341
TURN_OFF_NOTIFICATIONS
342
} else {
343
NOTIFY_WHEN_AVAILABLE
344
}
345
.to_owned(),
346
)
347
.on_click(cx.listener(move |this, _, _, cx| {
348
let (user_id, name) = (follow_id.clone(), follow_name.clone());
349
this.app.update(cx, |state, cx| {
350
state.set_presence_followed(&user_id, &name, !followed, cx)
351
});
352
})),
353
)
329
354
})
330
355
.when_some(mail_email, |row, address| {
331
356
row.child(
@@ -20,6 +20,7 @@ use crate::profile_model::{20
mailto_url, status_box, status_line, work_location_label,20
mailto_url, status_box, status_line, work_location_label,21
};21
};22
use crate::theme::{self, ElevatedSurface as _};22
use crate::theme::{self, ElevatedSurface as _};23
use crate::presence_follow::{NOTIFY_WHEN_AVAILABLE, TURN_OFF_NOTIFICATIONS};23
24
24
const CARD_WIDTH: f32 = 340.;25
const CARD_WIDTH: f32 = 340.;25
const CARD_RADIUS: f32 = 10.;26
const CARD_RADIUS: f32 = 10.;@@ -276,6 +277,8 @@ impl ProfileCard {276
};277
};277
let chat_id = user_id.clone();278
let chat_id = user_id.clone();278
let (video_id, audio_id) = (user_id.clone(), user_id.clone());279
let (video_id, audio_id) = (user_id.clone(), user_id.clone());280
let followed = self.app.read(cx).is_presence_followed(&user_id);281
let (follow_id, follow_name) = (user_id.clone(), name.clone());279
h_flex()282
h_flex()280
.gap(px(8.))283
.gap(px(8.))281
.when(!is_me, |row| {284
.when(!is_me, |row| {@@ -326,6 +329,28 @@ impl ProfileCard {326
cx.emit(ProfileCardEvent::Close);329
cx.emit(ProfileCardEvent::Close);327
})),330
})),328
)331
)332
.child(333
icon_action(334
"profile-follow-presence",335
if followed {336
IconName::BellOff337
} else {338
IconName::BellRing339
},340
if followed {341
TURN_OFF_NOTIFICATIONS342
} else {343
NOTIFY_WHEN_AVAILABLE344
}345
.to_owned(),346
)347
.on_click(cx.listener(move |this, _, _, cx| {348
let (user_id, name) = (follow_id.clone(), follow_name.clone());349
this.app.update(cx, |state, cx| {350
state.set_presence_followed(&user_id, &name, !followed, cx)351
});352
})),353
)329
})354
})330
.when_some(mail_email, |row, address| {355
.when_some(mail_email, |row, address| {331
row.child(356
row.child(@@ -26,6 +26,7 @@ use super::section_dialog::{SectionNameTarget, open_section_name_dialog};
26
26
use super::widgets::{count_badge, icon, menu_icon, page_header, symbol, unread_marker};
27
27
use crate::agents::AgentsPanel;
28
28
use crate::app_state::{AppEvent, AppState, Selection};
29
use crate::presence_follow::{NOTIFY_WHEN_AVAILABLE, TURN_OFF_NOTIFICATIONS};
29
30
use crate::channel_menu::{
30
31
ChannelMenuItem, MenuEntry, TeamMenuItem, channel_menu_entries, team_menu_entries,
31
32
};
@@ -232,7 +233,32 @@ fn chat_menu(
232
233
mute_state.update(cx, |state, cx| state.set_chat_muted(&mute_id, !muted, cx));
233
234
}),
234
235
);
235
popup = chat_notifications_submenu(popup, window, cx, context, target)
236
popup = chat_notifications_submenu(popup, window, cx, context, target);
237
let partner = (!target.is_group)
238
.then(|| context.state.read(cx).one_on_one_partner(&target.id))
239
.flatten();
240
if let Some(partner) = partner {
241
let follow_state = context.state.clone();
242
let follow = !context.state.read(cx).is_presence_followed(&partner.user_id);
243
popup = popup.item(
244
PopupMenuItem::new(if follow {
245
NOTIFY_WHEN_AVAILABLE
246
} else {
247
TURN_OFF_NOTIFICATIONS
248
})
249
.icon(menu_icon(if follow {
250
IconName::BellRing
251
} else {
252
IconName::BellOff
253
}))
254
.on_click(move |_, _, cx| {
255
follow_state.update(cx, |state, cx| {
256
state.set_presence_followed(&partner.user_id, &partner.name, follow, cx)
257
});
258
}),
259
);
260
}
261
popup = popup
236
262
.item(pop_out_item(context, target))
237
263
.separator()
238
264
.item(
@@ -26,6 +26,7 @@ use super::section_dialog::{SectionNameTarget, open_section_name_dialog};26
use super::widgets::{count_badge, icon, menu_icon, page_header, symbol, unread_marker};26
use super::widgets::{count_badge, icon, menu_icon, page_header, symbol, unread_marker};27
use crate::agents::AgentsPanel;27
use crate::agents::AgentsPanel;28
use crate::app_state::{AppEvent, AppState, Selection};28
use crate::app_state::{AppEvent, AppState, Selection};29
use crate::presence_follow::{NOTIFY_WHEN_AVAILABLE, TURN_OFF_NOTIFICATIONS};29
use crate::channel_menu::{30
use crate::channel_menu::{30
ChannelMenuItem, MenuEntry, TeamMenuItem, channel_menu_entries, team_menu_entries,31
ChannelMenuItem, MenuEntry, TeamMenuItem, channel_menu_entries, team_menu_entries,31
};32
};@@ -232,7 +233,32 @@ fn chat_menu(232
mute_state.update(cx, |state, cx| state.set_chat_muted(&mute_id, !muted, cx));233
mute_state.update(cx, |state, cx| state.set_chat_muted(&mute_id, !muted, cx));233
}),234
}),234
);235
);235
popup = chat_notifications_submenu(popup, window, cx, context, target)236
popup = chat_notifications_submenu(popup, window, cx, context, target);237
let partner = (!target.is_group)238
.then(|| context.state.read(cx).one_on_one_partner(&target.id))239
.flatten();240
if let Some(partner) = partner {241
let follow_state = context.state.clone();242
let follow = !context.state.read(cx).is_presence_followed(&partner.user_id);243
popup = popup.item(244
PopupMenuItem::new(if follow {245
NOTIFY_WHEN_AVAILABLE246
} else {247
TURN_OFF_NOTIFICATIONS248
})249
.icon(menu_icon(if follow {250
IconName::BellRing251
} else {252
IconName::BellOff253
}))254
.on_click(move |_, _, cx| {255
follow_state.update(cx, |state, cx| {256
state.set_presence_followed(&partner.user_id, &partner.name, follow, cx)257
});258
}),259
);260
}261
popup = popup236
.item(pop_out_item(context, target))262
.item(pop_out_item(context, target))237
.separator()263
.separator()238
.item(264
.item(