| 1 | use chatsvc::{PresenceUpdate, TrouterEndpoint}; |
| 2 | use graph::MAX_PRESENCE_IDS; |
| 3 | |
| 4 | use crate::engine::SyncEngine; |
| 5 | use crate::error::Result; |
| 6 | use crate::events::CoreEvent; |
| 7 | use crate::receipts::locked; |
| 8 | use crate::remote::Remote; |
| 9 | |
| 10 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 11 | pub enum Availability { |
| 12 | Available, |
| 13 | Busy, |
| 14 | DoNotDisturb, |
| 15 | Away, |
| 16 | Offline, |
| 17 | Unknown, |
| 18 | } |
| 19 | |
| 20 | impl Availability { |
| 21 | pub fn from_service(name: &str) -> Self { |
| 22 | match name { |
| 23 | "Available" | "AvailableIdle" => Availability::Available, |
| 24 | "Busy" | "BusyIdle" => Availability::Busy, |
| 25 | "DoNotDisturb" => Availability::DoNotDisturb, |
| 26 | "Away" | "BeRightBack" => Availability::Away, |
| 27 | "Offline" => Availability::Offline, |
| 28 | _ => Availability::Unknown, |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 34 | pub struct Presence { |
| 35 | pub availability: Availability, |
| 36 | pub in_call: bool, |
| 37 | } |
| 38 | |
| 39 | fn is_call_activity(activity: Option<&str>) -> bool { |
| 40 | matches!(activity, Some("InACall" | "InAConferenceCall")) |
| 41 | } |
| 42 | |
| 43 | impl<R: Remote> SyncEngine<R> { |
| 44 | pub fn presence(&self, user_id: &str) -> Option<Presence> { |
| 45 | self.presences |
| 46 | .lock() |
| 47 | .unwrap_or_else(|poisoned| poisoned.into_inner()) |
| 48 | .get(user_id) |
| 49 | .copied() |
| 50 | } |
| 51 | |
| 52 | pub async fn refresh_presence(&self, user_ids: &[String]) -> Result<()> { |
| 53 | let mut unique = user_ids.to_vec(); |
| 54 | unique.sort(); |
| 55 | unique.dedup(); |
| 56 | let mut changed = false; |
| 57 | for chunk in unique.chunks(MAX_PRESENCE_IDS) { |
| 58 | let found = self.remote.presences(chunk).await?; |
| 59 | changed |= self.record_presences(found.iter().map(|entry| { |
| 60 | ( |
| 61 | &entry.user_id, |
| 62 | entry.availability.as_str(), |
| 63 | entry.activity.as_deref(), |
| 64 | ) |
| 65 | })); |
| 66 | } |
| 67 | if changed { |
| 68 | let _ = self.events.send(CoreEvent::PresenceChanged); |
| 69 | } |
| 70 | Ok(()) |
| 71 | } |
| 72 | |
| 73 | pub fn apply_presence(&self, updates: &[PresenceUpdate]) { |
| 74 | let changed = self.record_presences(updates.iter().map(|update| { |
| 75 | ( |
| 76 | &update.user_id, |
| 77 | update.availability.as_str(), |
| 78 | update.activity.as_deref(), |
| 79 | ) |
| 80 | })); |
| 81 | if changed { |
| 82 | let _ = self.events.send(CoreEvent::PresenceChanged); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | pub async fn watch_presence(&self, user_ids: &[String]) -> Result<()> { |
| 87 | let _subscribing = self.presence_subscribing.lock().await; |
| 88 | let added = { |
| 89 | let mut watched = locked(&self.watched_presence); |
| 90 | let mut added: Vec<String> = user_ids |
| 91 | .iter() |
| 92 | .filter(|user_id| watched.insert((*user_id).clone())) |
| 93 | .cloned() |
| 94 | .collect(); |
| 95 | added.sort(); |
| 96 | added |
| 97 | }; |
| 98 | let endpoint = locked(&self.presence_endpoint).clone(); |
| 99 | let Some(endpoint) = endpoint.filter(|_| !added.is_empty()) else { |
| 100 | return Ok(()); |
| 101 | }; |
| 102 | let outcome = self |
| 103 | .remote |
| 104 | .subscribe_presence(&endpoint.endpoint_id, &endpoint.trouter_uri, &added, false) |
| 105 | .await; |
| 106 | if outcome.is_err() { |
| 107 | let mut watched = locked(&self.watched_presence); |
| 108 | for user_id in &added { |
| 109 | watched.remove(user_id); |
| 110 | } |
| 111 | } |
| 112 | outcome |
| 113 | } |
| 114 | |
| 115 | /// No-op for the endpoint already subscribed; a failed subscribe forgets it so the next announcement retries. |
| 116 | pub async fn presence_endpoint(&self, endpoint: TrouterEndpoint) -> Result<()> { |
| 117 | let _subscribing = self.presence_subscribing.lock().await; |
| 118 | if locked(&self.presence_endpoint).as_ref() == Some(&endpoint) { |
| 119 | return Ok(()); |
| 120 | } |
| 121 | let outcome = self.subscribe_watched(&endpoint).await; |
| 122 | *locked(&self.presence_endpoint) = outcome.is_ok().then_some(endpoint); |
| 123 | outcome |
| 124 | } |
| 125 | |
| 126 | pub async fn resubscribe_presence(&self) -> Result<()> { |
| 127 | let _subscribing = self.presence_subscribing.lock().await; |
| 128 | let endpoint = locked(&self.presence_endpoint).clone(); |
| 129 | match endpoint { |
| 130 | Some(endpoint) => self.subscribe_watched(&endpoint).await, |
| 131 | None => Ok(()), |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | async fn subscribe_watched(&self, endpoint: &TrouterEndpoint) -> Result<()> { |
| 136 | let mut watched: Vec<String> = locked(&self.watched_presence).iter().cloned().collect(); |
| 137 | watched.sort(); |
| 138 | if watched.is_empty() { |
| 139 | return Ok(()); |
| 140 | } |
| 141 | self.remote |
| 142 | .subscribe_presence(&endpoint.endpoint_id, &endpoint.trouter_uri, &watched, true) |
| 143 | .await |
| 144 | } |
| 145 | |
| 146 | fn record_presences<'a>( |
| 147 | &self, |
| 148 | found: impl Iterator<Item = (&'a String, &'a str, Option<&'a str>)>, |
| 149 | ) -> bool { |
| 150 | let mut known = locked(&self.presences); |
| 151 | let mut changed = false; |
| 152 | for (user_id, availability, activity) in found { |
| 153 | let presence = Presence { |
| 154 | availability: Availability::from_service(availability), |
| 155 | in_call: is_call_activity(activity), |
| 156 | }; |
| 157 | changed |= known.insert(user_id.clone(), presence) != Some(presence); |
| 158 | } |
| 159 | changed |
| 160 | } |
| 161 | } |