zaun_community_protocol/command/
channel.rs1use serde::{Deserialize, Serialize};
10
11mod validate;
12
13use super::object::{ChannelPosition, Override, OverrideTarget, Retention};
14use super::{BodyError, Validate, check};
15use crate::ids::ChannelId;
16use crate::json::Patch;
17use crate::limits::{
18 CHANNEL_NAME, CHANNEL_TOPIC_MAX, OVERRIDES_PER_CHANNEL, SLOWMODE_MAX_S, VOICE_USER_LIMIT,
19};
20use crate::model::{ChannelKind, HistoryAccess, TypingMode};
21
22#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
27pub struct Create {
28 pub kind: ChannelKind,
30 pub name: String,
32 #[serde(default, skip_serializing_if = "Option::is_none")]
34 pub topic: Option<String>,
35 #[serde(default, skip_serializing_if = "Option::is_none")]
37 pub parent_id: Option<ChannelId>,
38 #[serde(default, skip_serializing_if = "Option::is_none")]
40 pub position: Option<u32>,
41 #[serde(default, skip_serializing_if = "Option::is_none")]
43 pub slowmode_s: Option<u32>,
44 #[serde(default, skip_serializing_if = "Option::is_none")]
46 pub overrides: Option<Vec<Override>>,
47 #[serde(default, skip_serializing_if = "Option::is_none")]
49 pub retention: Option<Retention>,
50 #[serde(default, skip_serializing_if = "Option::is_none")]
52 pub typing: Option<TypingMode>,
53 #[serde(default, skip_serializing_if = "Option::is_none")]
56 pub user_limit: Option<u32>,
57}
58
59#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
61pub struct Update {
62 #[serde(default, skip_serializing_if = "Option::is_none")]
64 pub channel_id: Option<ChannelId>,
65 #[serde(default, skip_serializing_if = "Patch::is_keep")]
67 pub name: Patch<String>,
68 #[serde(default, skip_serializing_if = "Patch::is_keep")]
70 pub topic: Patch<String>,
71 #[serde(default, skip_serializing_if = "Patch::is_keep")]
73 pub parent_id: Patch<ChannelId>,
74 #[serde(default, skip_serializing_if = "Patch::is_keep")]
76 pub slowmode_s: Patch<u32>,
77 #[serde(default, skip_serializing_if = "Patch::is_keep")]
79 pub locked: Patch<bool>,
80 #[serde(default, skip_serializing_if = "Patch::is_keep")]
82 pub typing: Patch<TypingMode>,
83 #[serde(default, skip_serializing_if = "Patch::is_keep")]
85 pub retention: Patch<Retention>,
86 #[serde(default, skip_serializing_if = "Patch::is_keep")]
88 pub sync_with_parent: Patch<bool>,
89 #[serde(default, skip_serializing_if = "Patch::is_keep")]
91 pub history: Patch<HistoryAccess>,
92 #[serde(default, skip_serializing_if = "Patch::is_keep")]
94 pub public_distribution: Patch<bool>,
95 #[serde(default, skip_serializing_if = "Patch::is_keep")]
97 pub user_limit: Patch<u32>,
98}
99
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
102pub struct Delete {
103 #[serde(default, skip_serializing_if = "Option::is_none")]
105 pub channel_id: Option<ChannelId>,
106}
107
108#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
110pub struct Reorder {
111 pub positions: Vec<ChannelPosition>,
113}
114
115#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
117pub struct PermissionsUpdate {
118 #[serde(default, skip_serializing_if = "Option::is_none")]
120 pub channel_id: Option<ChannelId>,
121 pub set: Vec<Override>,
123 pub remove: Vec<OverrideTarget>,
125}
126
127pub(crate) fn check_kind(kind: ChannelKind) -> Result<(), BodyError> {
128 match kind {
129 ChannelKind::Category
130 | ChannelKind::Text
131 | ChannelKind::Announcement
132 | ChannelKind::Voice => Ok(()),
133 ChannelKind::Forum => Err(BodyError::unsupported("kind.forum")),
134 ChannelKind::Stage => Err(BodyError::unsupported("kind.stage")),
135 ChannelKind::Dm => Err(BodyError::bad("kind", "a dm is opened with dm.open")),
137 }
138}
139
140pub(crate) fn check_user_limit(n: u32) -> Result<(), BodyError> {
141 check::range("user_limit", n, VOICE_USER_LIMIT.0, VOICE_USER_LIMIT.1)
142}
143
144fn check_overrides(field: &str, overrides: &[Override]) -> Result<(), BodyError> {
145 check::count(field, overrides.len(), OVERRIDES_PER_CHANNEL)?;
146 check::unique(field, overrides.iter().map(|o| &o.target))?;
147 overrides.iter().try_for_each(|o| o.check(field))
148}
149
150fn check_history(history: &HistoryAccess) -> Result<(), BodyError> {
151 let (lo, hi) = crate::limits::LAST_N;
152 match *history {
153 HistoryAccess::LastN(n) => check::range("history", n, lo, hi),
154 HistoryAccess::None | HistoryAccess::SinceJoined | HistoryAccess::Full => Ok(()),
155 }
156}
157
158impl Validate for Create {
159 fn validate(&self) -> Result<(), BodyError> {
160 check_kind(self.kind)?;
161 check::text("name", &self.name, CHANNEL_NAME)?;
162 check::opt_text("topic", self.topic.as_deref(), (0, CHANNEL_TOPIC_MAX))?;
163 if self.kind == ChannelKind::Category && self.parent_id.is_some() {
164 return Err(BodyError::bad("parent_id", "a category has no parent"));
165 }
166 if let Some(s) = self.slowmode_s {
167 check::range("slowmode_s", s, 0, SLOWMODE_MAX_S)?;
168 }
169 if let Some(n) = self.user_limit {
170 if self.kind != ChannelKind::Voice {
171 return Err(BodyError::bad("user_limit", "voice channels only"));
172 }
173 check_user_limit(n)?;
174 }
175 check_overrides("overrides", self.overrides.as_deref().unwrap_or_default())?;
176 self.retention.map_or(Ok(()), |r| r.check("retention"))
177 }
178}