zaun_community_protocol/command/object/
channel.rs1use 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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13pub struct Channel {
14 pub channel_id: ChannelId,
16 pub kind: ChannelKind,
18 pub name: String,
20 pub topic: Option<String>,
22 pub parent_id: Option<ChannelId>,
24 pub position: u32,
26 pub slowmode_s: u32,
28 pub locked: bool,
30 #[serde(default, skip_serializing_if = "TypingMode::is_named")]
33 pub typing: TypingMode,
34 pub retention: Retention,
36 pub sync_with_parent: bool,
38 pub history: HistoryAccess,
40 pub history_rev: u64,
42 pub distribution: DistributionMode,
44 pub overrides: Vec<Override>,
46 #[serde(default)]
48 pub config: Map<String, Value>,
49 #[serde(default, skip_serializing_if = "Option::is_none")]
51 pub head_seq: Option<u64>,
52 #[serde(default, skip_serializing_if = "Option::is_none")]
54 pub user_limit: Option<u32>,
55 #[serde(default, skip_serializing_if = "Option::is_none")]
57 pub voice_participants: Option<Vec<super::VoiceParticipant>>,
58 #[serde(default, skip_serializing_if = "Vec::is_empty")]
60 pub participants: Vec<MemberId>,
61}
62
63#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
68pub struct Retention {
69 pub max_age_s: Option<u64>,
71 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#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
86pub struct Override {
87 pub target: OverrideTarget,
89 pub allow: Permissions,
91 pub deny: Permissions,
93}
94
95impl Override {
96 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#[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 Role(RoleId),
121 Member(MemberId),
123}
124
125#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
127pub struct ChannelPosition {
128 pub channel_id: ChannelId,
130 pub position: u32,
132 #[serde(deserialize_with = "check::nullable::deserialize")]
134 pub parent_id: Option<ChannelId>,
135}