zaun_community_protocol/command/
lockdown.rs1use serde::{Deserialize, Serialize};
9
10use super::object::{LockdownView, Restriction};
11use super::{BodyError, Validate, check};
12use crate::limits::{DAY_MS, LOCKDOWN_MS, REASON_MAX, SLOWMODE_MAX_S};
13
14pub const NEWCOMER_WINDOW_MAX_S: u64 = 30 * DAY_MS / 1000;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
19pub struct Get {}
20
21#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
24pub struct GetResponse {
25 #[serde(default)]
27 pub lockdown: Option<LockdownView>,
28}
29
30#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
33pub struct Enable {
34 pub restrictions: Vec<Restriction>,
36 pub expires_in_s: u64,
38 #[serde(default, skip_serializing_if = "Option::is_none")]
40 pub forced_slowmode_s: Option<u32>,
41 #[serde(default, skip_serializing_if = "Option::is_none")]
43 pub newcomer_window_s: Option<u64>,
44 #[serde(default, skip_serializing_if = "Option::is_none")]
46 pub reason: Option<String>,
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
51pub struct Disable {}
52
53impl Validate for Get {
54 fn validate(&self) -> Result<(), BodyError> {
55 Ok(())
56 }
57}
58
59impl Validate for Disable {
60 fn validate(&self) -> Result<(), BodyError> {
61 Ok(())
62 }
63}
64
65impl Validate for Enable {
66 fn validate(&self) -> Result<(), BodyError> {
71 if self.restrictions.is_empty() {
72 return Err(BodyError::bad("restrictions", "at least one is required"));
73 }
74 check::unique("restrictions", &self.restrictions)?;
75 let (lo, hi) = LOCKDOWN_MS;
76 check::range("expires_in_s", self.expires_in_s, lo / 1000, hi / 1000)?;
77 if let Some(s) = self.forced_slowmode_s {
78 check::range("forced_slowmode_s", s, 0, SLOWMODE_MAX_S)?;
79 }
80 if let Some(window) = self.newcomer_window_s {
81 check::range("newcomer_window_s", window, 1, NEWCOMER_WINDOW_MAX_S)?;
82 if !self.restrictions.contains(&Restriction::RestrictNewcomers) {
83 return Err(BodyError::bad(
84 "newcomer_window_s",
85 "needs restrict_newcomers",
86 ));
87 }
88 }
89 check::opt_text("reason", self.reason.as_deref(), (0, REASON_MAX))
90 }
91}