Skip to main content

zaun_community_protocol/command/
lockdown.rs

1//! Emergency lockdown commands (specification §8.10, governance §9).
2//!
3//! There is no `moderation.lockdown.update` command: renewal is a new `enable`, which
4//! replaces the active lockdown (§8.10, permissions §4.6); the `lockdown.update` audit action
5//! (§8.8) records such a replacement. An unknown `moderation.lockdown.*` type gets
6//! `UNSUPPORTED_COMMAND`.
7
8use 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
14/// Largest `newcomer_window_s`: 30 days (§8.10).
15pub const NEWCOMER_WINDOW_MAX_S: u64 = 30 * DAY_MS / 1000;
16
17/// `moderation.lockdown.get` (`{}`).
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
19pub struct Get {}
20
21/// `moderation.lockdown.get` response (§8.10): `{ "lockdown": object | null }`, the full form
22/// for `MANAGE_SAFETY` holders, otherwise the public form; `null` when none is active.
23#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
24pub struct GetResponse {
25    /// The lockdown as the caller may see it; `null` when none is active.
26    #[serde(default)]
27    pub lockdown: Option<LockdownView>,
28}
29
30/// `moderation.lockdown.enable` (§8.10); responds with the full
31/// [`Lockdown`](super::object::Lockdown).
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
33pub struct Enable {
34    /// ≥ 1, each once.
35    pub restrictions: Vec<Restriction>,
36    /// 60 – 604 800 s. Required.
37    pub expires_in_s: u64,
38    /// ≤ 21 600 s.
39    #[serde(default, skip_serializing_if = "Option::is_none")]
40    pub forced_slowmode_s: Option<u32>,
41    /// 1 s – 30 days; only with `restrict_newcomers`.
42    #[serde(default, skip_serializing_if = "Option::is_none")]
43    pub newcomer_window_s: Option<u64>,
44    /// ≤ 512 scalars.
45    #[serde(default, skip_serializing_if = "Option::is_none")]
46    pub reason: Option<String>,
47}
48
49/// `moderation.lockdown.disable` (`{}`).
50#[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    /// `newcomer_window_s` without `restrict_newcomers` is `BAD_REQUEST` (§8.10).
67    ///
68    /// SPEC: §8.10 gives no lower bound for `newcomer_window_s`; a window of 0 would restrict
69    /// nobody, so it is refused as well (the range is 1 s – 30 days).
70    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}