| 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( |
| 122 | &mut self, |
| 123 | user_id: &str, |
| 124 | previous: Presence, |
| 125 | next: PresenceKind, |
| 126 | ) { |
| 127 | if !self.is_presence_followed(user_id) { |
| 128 | return; |
| 129 | } |
| 130 | if let Some(text) = status_change_text(previous, next) { |
| 131 | self.followed_changes.push((user_id.to_owned(), text)); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | pub(crate) fn announce_followed_changes(&mut self, cx: &mut Context<Self>) { |
| 136 | for (user_id, text) in std::mem::take(&mut self.followed_changes) { |
| 137 | cx.emit(AppEvent::FollowedStatus { user_id, text }); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | #[cfg(test)] |
| 143 | mod tests { |
| 144 | use super::{NOW_AVAILABLE, NOW_OFFLINE, follow_notice, status_change_text}; |
| 145 | use crate::data::{Presence, PresenceKind}; |
| 146 | |
| 147 | #[test] |
| 148 | fn only_a_change_to_available_or_offline_is_announced() { |
| 149 | let busy = Presence::Live(PresenceKind::Busy); |
| 150 | assert_eq!( |
| 151 | status_change_text(busy, PresenceKind::Available), |
| 152 | Some(NOW_AVAILABLE) |
| 153 | ); |
| 154 | assert_eq!( |
| 155 | status_change_text(busy, PresenceKind::Offline), |
| 156 | Some(NOW_OFFLINE) |
| 157 | ); |
| 158 | assert_eq!(status_change_text(busy, PresenceKind::Away), None); |
| 159 | assert_eq!(status_change_text(busy, PresenceKind::OutOfOffice), None); |
| 160 | let available = Presence::Live(PresenceKind::Available); |
| 161 | assert_eq!(status_change_text(available, PresenceKind::Available), None); |
| 162 | } |
| 163 | |
| 164 | #[test] |
| 165 | fn the_first_known_status_is_not_announced() { |
| 166 | assert_eq!( |
| 167 | status_change_text(Presence::Loading, PresenceKind::Available), |
| 168 | None |
| 169 | ); |
| 170 | assert_eq!( |
| 171 | status_change_text( |
| 172 | Presence::Cached(PresenceKind::Busy), |
| 173 | PresenceKind::Available |
| 174 | ), |
| 175 | None |
| 176 | ); |
| 177 | assert_eq!( |
| 178 | status_change_text( |
| 179 | Presence::Live(PresenceKind::Unknown), |
| 180 | PresenceKind::Available |
| 181 | ), |
| 182 | None |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | #[test] |
| 187 | fn the_notice_uses_the_teams_text() { |
| 188 | assert_eq!( |
| 189 | follow_notice("Anna", true), |
| 190 | "You are now following the presence of Anna" |
| 191 | ); |
| 192 | assert_eq!( |
| 193 | follow_notice("Anna", false), |
| 194 | "You are no longer following the presence of Anna" |
| 195 | ); |
| 196 | } |
| 197 | } |