Skip to main content

zaun_community_protocol/command/object/
channel.rs

1//! The channel object, overrides and retention (specification §8.2, permissions §2.2).
2
3use serde::{Deserialize, Serialize};
4use serde_json::{Map, Value};
5
6use crate::bits::Permissions;
7use crate::command::{BodyError, check};
8use crate::ids::{ChannelId, MemberId, RoleId};
9use crate::model::{ChannelKind, DistributionMode, HistoryAccess, TypingMode};
10
11/// A channel or category as members see it (§8.2).
12#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13pub struct Channel {
14    /// Host-assigned id.
15    pub channel_id: ChannelId,
16    /// The kind.
17    pub kind: ChannelKind,
18    /// 1–100 scalars.
19    pub name: String,
20    /// ≤ 1 024 scalars.
21    pub topic: Option<String>,
22    /// The parent category; categories have none.
23    pub parent_id: Option<ChannelId>,
24    /// Position among siblings.
25    pub position: u32,
26    /// 0–21 600 s (§8.7).
27    pub slowmode_s: u32,
28    /// The `locked` flag (permissions §3.3, step 7d).
29    pub locked: bool,
30    /// Who sees typing in the channel (§10.5); absent means `named`, and `named` is written
31    /// absent.
32    #[serde(default, skip_serializing_if = "TypingMode::is_named")]
33    pub typing: TypingMode,
34    /// Retention settings.
35    pub retention: Retention,
36    /// Whether the effective overrides are the parent's (permissions §2.4).
37    pub sync_with_parent: bool,
38    /// History access policy (ADR-0014).
39    pub history: HistoryAccess,
40    /// Incremented on each `history` change.
41    pub history_rev: u64,
42    /// Host-chosen distribution mode; clients never send it.
43    pub distribution: DistributionMode,
44    /// The channel's own overrides.
45    pub overrides: Vec<Override>,
46    /// SPEC: shown only as `{}` in §8.2 and never defined; kept as an opaque object.
47    #[serde(default)]
48    pub config: Map<String, Value>,
49    /// `sync.snapshot` only: the latest `channel_seq` (§10.1).
50    #[serde(default, skip_serializing_if = "Option::is_none")]
51    pub head_seq: Option<u64>,
52    /// Voice channels: the most participants, when limited (§8.12).
53    #[serde(default, skip_serializing_if = "Option::is_none")]
54    pub user_limit: Option<u32>,
55    /// `sync.snapshot` of a voice channel: who is in it now, without key packages (§8.12).
56    #[serde(default, skip_serializing_if = "Option::is_none")]
57    pub voice_participants: Option<Vec<super::VoiceParticipant>>,
58    /// A `dm` channel's two members, sorted by id bytes (§8.15); absent otherwise.
59    #[serde(default, skip_serializing_if = "Vec::is_empty")]
60    pub participants: Vec<MemberId>,
61}
62
63/// Message retention of a channel (§8.2).
64///
65/// SPEC: the spec gives no bounds or semantics; this crate requires each set value ≥ 1 and
66/// below 2^53, and lets `null` mean "no limit".
67#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
68pub struct Retention {
69    /// Maximum message age in seconds.
70    pub max_age_s: Option<u64>,
71    /// Maximum number of messages kept.
72    pub max_count: Option<u64>,
73}
74
75impl Retention {
76    pub(crate) fn check(&self, field: &str) -> Result<(), BodyError> {
77        for value in [self.max_age_s, self.max_count].into_iter().flatten() {
78            check::range(field, value, 1, check::JSON_SAFE_MAX)?;
79        }
80        Ok(())
81    }
82}
83
84/// A permission override on a channel or category (permissions §2.2).
85#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
86pub struct Override {
87    /// Whom it applies to.
88    pub target: OverrideTarget,
89    /// Bits allowed.
90    pub allow: Permissions,
91    /// Bits denied.
92    pub deny: Permissions,
93}
94
95impl Override {
96    /// `allow & deny == 0` and only channel-scoped bits (permissions §2.2).
97    ///
98    /// SPEC: permissions §1.2 says unknown bits in overrides are preserved, but §2.2 says
99    /// bits outside `CHANNEL_SCOPED` MUST be 0; for a command that sets an override the
100    /// stricter §2.2 rule applies, so unknown bits are rejected here.
101    pub(crate) fn check(&self, field: &str) -> Result<(), BodyError> {
102        if self.allow.intersects(self.deny) {
103            return Err(BodyError::bad(field, "allow and deny overlap"));
104        }
105        if !Permissions::CHANNEL_SCOPED.contains(self.allow.union(self.deny)) {
106            return Err(BodyError::bad(
107                field,
108                "only channel-scoped bits may be overridden",
109            ));
110        }
111        Ok(())
112    }
113}
114
115/// The subject of an override: `{"t":"role","c":RoleId}` or `{"t":"member","c":MemberId}`.
116#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
117#[serde(tag = "t", content = "c", rename_all = "snake_case")]
118pub enum OverrideTarget {
119    /// A role (including `@everyone`).
120    Role(RoleId),
121    /// A single member.
122    Member(MemberId),
123}
124
125/// One entry of `channel.reorder` and `channel.reordered` (§8.2, §9.2).
126#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
127pub struct ChannelPosition {
128    /// The channel moved.
129    pub channel_id: ChannelId,
130    /// Its new position.
131    pub position: u32,
132    /// Its new parent. Required, `null` = top level.
133    #[serde(deserialize_with = "check::nullable::deserialize")]
134    pub parent_id: Option<ChannelId>,
135}