BlueGit

ict rusty-teams

Files

rusty-teams / crates / core / src / presence.rs

presence.rs

6c4ed4f5 History
presence.rs 2.1 KiB Download Raw
1use graph::MAX_PRESENCE_IDS;
2
3use crate::engine::SyncEngine;
4use crate::error::Result;
5use crate::events::CoreEvent;
6use crate::remote::Remote;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum Availability {
10 Available,
11 Busy,
12 DoNotDisturb,
13 Away,
14 Offline,
15 Unknown,
16}
17
18impl Availability {
19 pub fn from_service(name: &str) -> Self {
20 match name {
21 "Available" | "AvailableIdle" => Availability::Available,
22 "Busy" | "BusyIdle" => Availability::Busy,
23 "DoNotDisturb" => Availability::DoNotDisturb,
24 "Away" | "BeRightBack" => Availability::Away,
25 "Offline" => Availability::Offline,
26 _ => Availability::Unknown,
27 }
28 }
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub struct Presence {
33 pub availability: Availability,
34 pub in_call: bool,
35}
36
37fn is_call_activity(activity: Option<&str>) -> bool {
38 matches!(activity, Some("InACall" | "InAConferenceCall"))
39}
40
41impl<R: Remote> SyncEngine<R> {
42 pub fn presence(&self, user_id: &str) -> Option<Presence> {
43 self.presences
44 .lock()
45 .unwrap_or_else(|poisoned| poisoned.into_inner())
46 .get(user_id)
47 .copied()
48 }
49
50 pub async fn refresh_presence(&self, user_ids: &[String]) -> Result<()> {
51 let mut unique = user_ids.to_vec();
52 unique.sort();
53 unique.dedup();
54 let mut changed = false;
55 for chunk in unique.chunks(MAX_PRESENCE_IDS) {
56 let found = self.remote.presences(chunk).await?;
57 let mut known = self
58 .presences
59 .lock()
60 .unwrap_or_else(|poisoned| poisoned.into_inner());
61 for entry in found {
62 let presence = Presence {
63 availability: Availability::from_service(&entry.availability),
64 in_call: is_call_activity(entry.activity.as_deref()),
65 };
66 changed |= known.insert(entry.user_id, presence) != Some(presence);
67 }
68 }
69 if changed {
70 let _ = self.events.send(CoreEvent::PresenceChanged);
71 }
72 Ok(())
73 }
74}