Skip to main content

zaun_client/
presence.rs

1//! Presence (specification §10.5): who is online, idle or do-not-disturb, and doing what
2//! ("Playing chess", "Listening to lofi radio"). The cache follows `presence.list` and every
3//! `signal.presence`; a member missing from it is offline. Custom status text is part of the
4//! member object (`member.status.set`, §8.3).
5
6pub use zaun_community_protocol::command::object::{Activity, ActivityType};
7pub use zaun_community_protocol::command::signal::PresenceStatus as Status;
8use zaun_community_protocol::event::PresenceSignal;
9use zaun_community_protocol::ids::MemberId;
10
11use crate::cache::CommunityData;
12
13/// A member's presence as everyone else sees it (`invisible` members look `Offline`).
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct Presence {
16    /// The member.
17    pub member: MemberId,
18    /// `Online`, `Idle`, `Dnd` or `Offline`.
19    pub status: Status,
20    /// What they are doing, if they said.
21    pub activity: Option<Activity>,
22}
23
24impl From<&PresenceSignal> for Presence {
25    fn from(p: &PresenceSignal) -> Self {
26        Self {
27            member: p.member_id,
28            status: p.status,
29            activity: p.activity.clone(),
30        }
31    }
32}
33
34impl Presence {
35    /// How clients show the activity: "Playing chess", "Listening to lofi radio", or the
36    /// custom text with its emoji.
37    #[must_use]
38    pub fn activity_line(&self) -> Option<String> {
39        self.activity.as_ref().map(line)
40    }
41}
42
43/// "Playing {name}", "Streaming {name}", "Listening to {name}", "Watching {name}",
44/// "Competing in {name}", or `{emoji} {name}` for a custom one.
45#[must_use]
46pub fn line(a: &Activity) -> String {
47    match a.kind {
48        ActivityType::Playing => format!("Playing {}", a.name),
49        ActivityType::Streaming => format!("Streaming {}", a.name),
50        ActivityType::Listening => format!("Listening to {}", a.name),
51        ActivityType::Watching => format!("Watching {}", a.name),
52        ActivityType::Competing => format!("Competing in {}", a.name),
53        ActivityType::Custom => match &a.emoji {
54            Some(e) => format!("{e} {}", a.name),
55            None => a.name.clone(),
56        },
57    }
58}
59
60/// An activity of `kind` named `name`, for [`Client::set_presence`](crate::Client::set_presence).
61/// Set `details`, `state`, `started_at`, `url` (streaming) or `emoji` (custom) on it as needed.
62#[must_use]
63pub fn activity(kind: ActivityType, name: impl Into<String>) -> Activity {
64    Activity {
65        kind,
66        name: name.into(),
67        details: None,
68        state: None,
69        url: None,
70        started_at: None,
71        emoji: None,
72    }
73}
74
75impl CommunityData {
76    /// Applies one `signal.presence`: an offline member leaves the presence list.
77    pub(crate) fn saw_presence(&mut self, p: &PresenceSignal) {
78        if p.status.public() == Status::Offline {
79            self.presences.remove(&p.member_id);
80        } else {
81            self.presences.insert(p.member_id, p.clone());
82        }
83    }
84
85    /// Replaces the presences with a `presence.list` answer.
86    pub(crate) fn set_presences(&mut self, list: Vec<PresenceSignal>) {
87        self.presences = list.into_iter().map(|p| (p.member_id, p)).collect();
88    }
89
90    /// `member`'s presence (offline when the cache has none).
91    #[must_use]
92    pub fn presence_of(&self, member: MemberId) -> Presence {
93        self.presences.get(&member).map_or(
94            Presence {
95                member,
96                status: Status::Offline,
97                activity: None,
98            },
99            Presence::from,
100        )
101    }
102}