Skip to main content

zaun_community_protocol/permissions/
safety.rs

1//! Admission policy and lockdown checks (permissions §3.3, §4.6; specification §8.9–8.10).
2
3use crate::bits::Permissions;
4use crate::limits::LOCKDOWN_MS;
5
6use super::denial::{Denial, Invalid, Refusal};
7use super::hierarchy::{Party, Top, h3_owner_only, no_new_unknown};
8use super::lockdown::{Lockdown, LockdownRestrictions};
9
10/// Bits `newcomer_restrictions` must never contain (§3.3): newcomers always see and read.
11pub const NEWCOMER_FORBIDDEN: Permissions =
12    Permissions::VIEW_CHANNEL.union(Permissions::READ_MESSAGE_HISTORY);
13
14/// Validates a new `newcomer_restrictions` mask: no `VIEW_CHANNEL` or
15/// `READ_MESSAGE_HISTORY`, no newly set unknown bit.
16pub fn validate_newcomer_restrictions(old: Permissions, new: Permissions) -> Result<(), Invalid> {
17    if new.intersects(NEWCOMER_FORBIDDEN) {
18        return Err(Invalid::NewcomerMaskHidesChannels);
19    }
20    no_new_unknown(old, new)
21}
22
23/// `admission.policy.update` of `newcomer_restrictions`: valid, and H3 on the bits added or
24/// removed (`old XOR new ⊆ cp`; only the owner exempt, see [`h3_owner_only`]).
25pub fn admission_policy_update(
26    actor: &Party,
27    cp: Permissions,
28    old: Permissions,
29    new: Permissions,
30) -> Result<(), Refusal> {
31    validate_newcomer_restrictions(old, new)?;
32    Ok(h3_owner_only(actor, old ^ new, cp)?)
33}
34
35/// The lockdown currently in force, as lockdown management sees it.
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
37pub struct ActiveLockdown {
38    /// The enabling actor's top position, recorded at enable time.
39    pub enabled_by: Top,
40    /// Its whole permission mask, [`Lockdown::max_mask`].
41    pub mask: Permissions,
42}
43
44/// `moderation.lockdown.enable` (also renewal and replacement): duration within 60 s – 7 days
45/// and H3 on the new lockdown's mask (only the owner exempt). Enabling while one is active
46/// replaces it, so the actor must also pass [`lockdown_disable`] for the active one: a lower
47/// moderator cannot swap a higher one's lockdown.
48///
49/// `held` is the actor's community permissions **before the lockdown step**
50/// ([`Resolved::before_lockdown`](super::Resolved::before_lockdown) of `cp`): timeout and
51/// probation still apply, but a lockdown never prevents its own management.
52pub fn lockdown_enable(
53    actor: &Party,
54    held: Permissions,
55    restrictions: LockdownRestrictions,
56    newcomer_restrictions: Permissions,
57    expires_in_ms: u64,
58    active: Option<ActiveLockdown>,
59) -> Result<(), Refusal> {
60    if let Some(active) = active {
61        lockdown_disable(actor, held, active)?;
62    }
63    if !(LOCKDOWN_MS.0..=LOCKDOWN_MS.1).contains(&expires_in_ms) {
64        return Err(Invalid::Duration.into());
65    }
66    let mask = Lockdown::max_mask(restrictions, newcomer_restrictions);
67    Ok(h3_owner_only(actor, mask, held)?)
68}
69
70/// `moderation.lockdown.disable`: `top(A) ≥` the enabling actor's recorded top (owner
71/// exempt), so a lower moderator cannot lift a higher one's lockdown, and H3 on the bits the
72/// lockdown restricts, against `held` as in [`lockdown_enable`].
73pub fn lockdown_disable(
74    actor: &Party,
75    held: Permissions,
76    active: ActiveLockdown,
77) -> Result<(), Refusal> {
78    if !actor.is_owner && actor.top < active.enabled_by {
79        return Err(Denial::hierarchy().into());
80    }
81    Ok(h3_owner_only(actor, active.mask, held)?)
82}