Skip to main content

zaun_community_protocol/command/
signal.rs

1//! Ephemeral signals (specification §10.5): commands without responses (`request_id`
2//! null), never journaled.
3//!
4//! SPEC: signals are commands (§7.3, §10.5) but appear in no §8 table.
5
6use serde::{Deserialize, Serialize};
7
8use super::object::Activity;
9use super::{BodyError, Validate};
10use crate::ids::ChannelId;
11use crate::json::Patch;
12
13pub use crate::limits::ACTIVITY_MAX;
14
15/// A signal has no response. Uninhabited.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
17pub enum NoResponse {}
18
19/// `signal.typing { channel_id }` (§10.5).
20///
21/// SPEC: the body names `channel_id`, which a channel-scoped envelope also carries; it is
22/// optional here as for the channel commands.
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
24pub struct Typing {
25    /// The channel typed in.
26    #[serde(default, skip_serializing_if = "Option::is_none")]
27    pub channel_id: Option<ChannelId>,
28}
29
30/// `signal.presence` (§10.5): `{ status?, activity?, viewing? }`, at least one of `status`
31/// and `viewing`.
32///
33/// SPEC: §10.5 writes the body as `{ status, activity? }` and later as
34/// `signal.presence { viewing }` (the client says which channel it views). Both forms are
35/// accepted, so `status` is optional here; a body with neither `status` nor `viewing` is a
36/// bad request. `viewing: null` means "no channel"; `viewing` is never forwarded to other
37/// members.
38#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
39pub struct Presence {
40    /// The member's presence.
41    #[serde(default, skip_serializing_if = "Option::is_none")]
42    pub status: Option<PresenceStatus>,
43    /// What the member is doing (absent: nothing). Dropped when `status` is `invisible`.
44    #[serde(default, skip_serializing_if = "Option::is_none")]
45    pub activity: Option<Activity>,
46    /// The channel the client is viewing (typing signals go only to viewers); `null` = none.
47    #[serde(default, skip_serializing_if = "Patch::is_keep")]
48    pub viewing: Patch<ChannelId>,
49}
50
51/// Presence status (§10.5), in the §7 tagged form. A client sends `online`, `idle`, `dnd` or
52/// `invisible` (`offline` from a client means `invisible`); others only ever see `online`,
53/// `idle`, `dnd` and `offline` (invisible members and members without a connection).
54#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
55#[serde(tag = "t", content = "c", rename_all = "snake_case")]
56pub enum PresenceStatus {
57    /// Active.
58    Online,
59    /// Away.
60    Idle,
61    /// Do not disturb.
62    Dnd,
63    /// Shown as offline to everyone else, while connected.
64    Invisible,
65    /// Not connected (or invisible, as others see it).
66    Offline,
67}
68
69impl PresenceStatus {
70    /// How others see it: `invisible` (and a client's `offline`) is `offline`.
71    #[must_use]
72    pub const fn public(self) -> Self {
73        match self {
74            Self::Invisible | Self::Offline => Self::Offline,
75            other => other,
76        }
77    }
78}
79
80impl Validate for Typing {
81    fn validate(&self) -> Result<(), BodyError> {
82        Ok(())
83    }
84}
85
86impl Validate for Presence {
87    fn validate(&self) -> Result<(), BodyError> {
88        if self.status.is_none() && self.viewing.is_keep() {
89            return Err(BodyError::bad("body", "needs status or viewing"));
90        }
91        self.activity.as_ref().map_or(Ok(()), Activity::check)
92    }
93}