zaun_community_protocol/command/channel/
validate.rs1use super::{
4 Delete, PermissionsUpdate, Reorder, Update, check_history, check_overrides, check_user_limit,
5};
6use crate::command::{BodyError, Validate, check};
7use crate::limits::{
8 CHANNEL_NAME, CHANNEL_TOPIC_MAX, CHANNELS_PER_COMMUNITY, OVERRIDES_PER_CHANNEL, SLOWMODE_MAX_S,
9};
10
11impl Validate for Update {
12 fn validate(&self) -> Result<(), BodyError> {
15 let untouched = self.name.is_keep()
16 && self.topic.is_keep()
17 && self.parent_id.is_keep()
18 && self.slowmode_s.is_keep()
19 && self.locked.is_keep()
20 && self.typing.is_keep()
21 && self.retention.is_keep()
22 && self.sync_with_parent.is_keep()
23 && self.history.is_keep()
24 && self.public_distribution.is_keep()
25 && self.user_limit.is_keep();
26 if untouched {
27 return Err(BodyError::bad("body", "no field to update"));
28 }
29 check::not_null("name", &self.name)?;
30 check::not_null("slowmode_s", &self.slowmode_s)?;
31 check::not_null("locked", &self.locked)?;
32 check::not_null("typing", &self.typing)?;
33 check::not_null("retention", &self.retention)?;
34 check::not_null("sync_with_parent", &self.sync_with_parent)?;
35 check::not_null("history", &self.history)?;
36 check::not_null("public_distribution", &self.public_distribution)?;
37 if let Some(name) = check::set(&self.name) {
38 check::text("name", name, CHANNEL_NAME)?;
39 }
40 if let Some(topic) = check::set(&self.topic) {
41 check::text("topic", topic, (0, CHANNEL_TOPIC_MAX))?;
42 }
43 if let Some(s) = check::set(&self.slowmode_s) {
44 check::range("slowmode_s", *s, 0, SLOWMODE_MAX_S)?;
45 }
46 if let Some(r) = check::set(&self.retention) {
47 r.check("retention")?;
48 }
49 if let Some(h) = check::set(&self.history) {
50 check_history(h)?;
51 }
52 if let Some(n) = check::set(&self.user_limit) {
53 check_user_limit(*n)?;
54 }
55 if check::set(&self.public_distribution) == Some(&true) {
56 return Err(BodyError::unsupported("public_distribution"));
57 }
58 Ok(())
59 }
60}
61
62impl Validate for Delete {
63 fn validate(&self) -> Result<(), BodyError> {
64 Ok(())
65 }
66}
67
68impl Validate for Reorder {
69 fn validate(&self) -> Result<(), BodyError> {
70 if self.positions.is_empty() {
71 return Err(BodyError::bad("positions", "is empty"));
72 }
73 check::count("positions", self.positions.len(), CHANNELS_PER_COMMUNITY)?;
74 check::unique("positions", self.positions.iter().map(|p| &p.channel_id))?;
75 if self
76 .positions
77 .iter()
78 .any(|p| p.parent_id == Some(p.channel_id))
79 {
80 return Err(BodyError::bad(
81 "positions",
82 "a channel cannot be its own parent",
83 ));
84 }
85 Ok(())
86 }
87}
88
89impl Validate for PermissionsUpdate {
90 fn validate(&self) -> Result<(), BodyError> {
92 if self.set.is_empty() && self.remove.is_empty() {
93 return Err(BodyError::bad("body", "no override to change"));
94 }
95 check_overrides("set", &self.set)?;
96 check::count("remove", self.remove.len(), OVERRIDES_PER_CHANNEL)?;
97 let targets = self.set.iter().map(|o| &o.target).chain(&self.remove);
98 check::unique("remove", targets)
99 }
100}