Skip to main content

zaun_community_protocol/command/
presence.rs

1//! A member's custom status text (`member.status.set`, kept by the host) and the current
2//! presences (`presence.list`, live only) (specification §8.3, §10.5).
3
4use serde::{Deserialize, Serialize};
5
6use super::{BodyError, Validate, check};
7use crate::event::PresenceSignal;
8use crate::limits::ACTIVITY_MAX;
9
10/// `member.status.set`: replaces the actor's custom status. A body with neither `text` nor
11/// `emoji` clears it.
12#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
13pub struct StatusSet {
14    /// 1–128 scalars.
15    #[serde(default, skip_serializing_if = "Option::is_none")]
16    pub text: Option<String>,
17    /// A Unicode emoji.
18    #[serde(default, skip_serializing_if = "Option::is_none")]
19    pub emoji: Option<String>,
20    /// When the host clears it, ms since the epoch; absent: never.
21    #[serde(default, skip_serializing_if = "Option::is_none")]
22    pub expires_at: Option<u64>,
23}
24
25impl StatusSet {
26    /// Whether this clears the status.
27    #[must_use]
28    pub const fn clears(&self) -> bool {
29        self.text.is_none() && self.emoji.is_none()
30    }
31}
32
33impl Validate for StatusSet {
34    fn validate(&self) -> Result<(), BodyError> {
35        check::opt_text("text", self.text.as_deref(), (1, ACTIVITY_MAX))?;
36        if let Some(e) = &self.emoji {
37            check::emoji("emoji", e)?;
38        }
39        match self.expires_at {
40            Some(_) if self.clears() => Err(BodyError::bad("expires_at", "nothing to expire")),
41            Some(at) => check::json_safe("expires_at", at),
42            None => Ok(()),
43        }
44    }
45}
46
47/// `presence.list`: the presences of the community's connected, visible members.
48#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
49pub struct List {}
50
51impl Validate for List {
52    fn validate(&self) -> Result<(), BodyError> {
53        Ok(())
54    }
55}
56
57/// `presence.list` response: one entry per member not `offline` (members missing from the
58/// list are offline).
59#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
60pub struct Presences {
61    /// The presences, as `signal.presence` events carry them.
62    pub presences: Vec<PresenceSignal>,
63}