BlueGit

ict rusty-teams

Files

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

presence.rs

Initial commit
metis Daniel Däschle · pushed by metis · 7 weeks ago
06e0ae51 History
presence.rs 1.9 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}
35
36impl<R: Remote> SyncEngine<R> {
37 pub fn presence(&self, user_id: &str) -> Option<Presence> {
38 self.presences
39 .lock()
40 .unwrap_or_else(|poisoned| poisoned.into_inner())
41 .get(user_id)
42 .copied()
43 }
44
45 pub async fn refresh_presence(&self, user_ids: &[String]) -> Result<()> {
46 let mut unique = user_ids.to_vec();
47 unique.sort();
48 unique.dedup();
49 let mut changed = false;
50 for chunk in unique.chunks(MAX_PRESENCE_IDS) {
51 let found = self.remote.presences(chunk).await?;
52 let mut known = self
53 .presences
54 .lock()
55 .unwrap_or_else(|poisoned| poisoned.into_inner());
56 for entry in found {
57 let presence = Presence {
58 availability: Availability::from_service(&entry.availability),
59 };
60 changed |= known.insert(entry.user_id, presence) != Some(presence);
61 }
62 }
63 if changed {
64 let _ = self.events.send(CoreEvent::PresenceChanged);
65 }
66 Ok(())
67 }
68}