Skip to main content

zaun_community_protocol/command/object/
presence.rs

1//! Activities shown with a member's presence (specification §10.5), in Discord's model:
2//! "Playing X", "Listening to X", … Plain data: no pictures, and a link only for streaming.
3
4use serde::{Deserialize, Serialize};
5
6use super::embed::link;
7use crate::command::{BodyError, check};
8use crate::limits::ACTIVITY_MAX;
9
10/// What the member is doing.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12#[serde(rename_all = "snake_case")]
13pub enum ActivityType {
14    /// "Playing {name}".
15    Playing,
16    /// "Streaming {name}", with a link.
17    Streaming,
18    /// "Listening to {name}".
19    Listening,
20    /// "Watching {name}".
21    Watching,
22    /// "Competing in {name}".
23    Competing,
24    /// `{emoji} {name}`: free text.
25    Custom,
26}
27
28/// An activity (§10.5).
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
30pub struct Activity {
31    /// The kind.
32    #[serde(rename = "type")]
33    pub kind: ActivityType,
34    /// 1–128 scalars: the game, song, show, …
35    pub name: String,
36    /// ≤ 128 scalars: e.g. the track or the level.
37    #[serde(default, skip_serializing_if = "Option::is_none")]
38    pub details: Option<String>,
39    /// ≤ 128 scalars: e.g. the artist or the party size.
40    #[serde(default, skip_serializing_if = "Option::is_none")]
41    pub state: Option<String>,
42    /// `streaming` only: where to watch (`https://`, the §8.5 link rules; opened only
43    /// after the reader confirms).
44    #[serde(default, skip_serializing_if = "Option::is_none")]
45    pub url: Option<String>,
46    /// When it started, ms since the epoch.
47    #[serde(default, skip_serializing_if = "Option::is_none")]
48    pub started_at: Option<u64>,
49    /// `custom` only: a Unicode emoji.
50    #[serde(default, skip_serializing_if = "Option::is_none")]
51    pub emoji: Option<String>,
52}
53
54impl Activity {
55    /// The state-free rules.
56    ///
57    /// # Errors
58    /// The first violated rule, with the field named `activity.<field>`.
59    pub fn check(&self) -> Result<(), BodyError> {
60        check::text("activity.name", &self.name, (1, ACTIVITY_MAX))?;
61        check::opt_text(
62            "activity.details",
63            self.details.as_deref(),
64            (0, ACTIVITY_MAX),
65        )?;
66        check::opt_text("activity.state", self.state.as_deref(), (0, ACTIVITY_MAX))?;
67        if let Some(at) = self.started_at {
68            check::json_safe("activity.started_at", at)?;
69        }
70        match (&self.url, self.kind) {
71            (Some(url), ActivityType::Streaming) => link("activity.url", url)?,
72            (Some(_), _) => return Err(BodyError::bad("activity.url", "only for streaming")),
73            (None, _) => {}
74        }
75        match (&self.emoji, self.kind) {
76            (Some(e), ActivityType::Custom) => check::emoji("activity.emoji", e),
77            (Some(_), _) => Err(BodyError::bad("activity.emoji", "only for custom")),
78            (None, _) => Ok(()),
79        }
80    }
81}