Skip to main content

zaun_community_protocol/permissions/
lockdown.rs

1//! Emergency lockdown restrictions (specification §8.10, permissions §3.3 step 7c).
2
3mod restrictions;
4
5pub use restrictions::LockdownRestrictions;
6
7use crate::bits::Permissions;
8
9/// `restrict_expensive`: a restricted member reads only the newest this-many sequences of a
10/// channel, `sequence > head − EXPENSIVE_HISTORY_DEPTH` (specification §8.10, §12). One full
11/// `message.history` page.
12pub const EXPENSIVE_HISTORY_DEPTH: u64 = 100;
13
14/// `restrict_expensive`: the most members one `member.list` page returns to a restricted
15/// member, who gets only the first page (specification §8.10, §12).
16pub const EXPENSIVE_MEMBER_PAGE: u32 = 50;
17
18/// The community's lockdown row (specification §8.10).
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct Lockdown {
21    /// The named restrictions.
22    pub restrictions: LockdownRestrictions,
23    /// Expiry in ms since the epoch; the lockdown is active while `now < expires_at_ms`.
24    pub expires_at_ms: u64,
25    /// `newcomer_window_s` in ms: `restrict_newcomers` covers members admitted this recently.
26    pub newcomer_window_ms: u64,
27}
28
29impl Lockdown {
30    /// Whether the lockdown is in force at `now_ms`.
31    #[must_use]
32    pub const fn is_active(&self, now_ms: u64) -> bool {
33        now_ms < self.expires_at_ms
34    }
35
36    /// Whether `restrict_expensive` binds a member at `now_ms`: the lockdown is in force,
37    /// names it, and the member is not the owner (the only exemption, ADR-0016).
38    #[must_use]
39    pub const fn limits_expensive(&self, is_owner: bool, now_ms: u64) -> bool {
40        !is_owner
41            && self.is_active(now_ms)
42            && self
43                .restrictions
44                .contains(LockdownRestrictions::RESTRICT_EXPENSIVE)
45    }
46
47    /// The whole permission mask the lockdown can remove from anyone, given the policy's
48    /// newcomer mask. H3 checks a lockdown enable against this (permissions §4.6).
49    #[must_use]
50    pub fn max_mask(restrictions: LockdownRestrictions, newcomers: Permissions) -> Permissions {
51        let mut mask = restrictions.permission_mask();
52        if restrictions.contains(LockdownRestrictions::RESTRICT_NEWCOMERS) {
53            mask |= newcomer_mask(newcomers);
54        }
55        mask
56    }
57
58    /// The mask for one member admitted at `admitted_at_ms`; empty once expired.
59    #[must_use]
60    pub fn mask_for(
61        &self,
62        admitted_at_ms: u64,
63        newcomers: Permissions,
64        now_ms: u64,
65    ) -> Permissions {
66        if !self.is_active(now_ms) {
67            return Permissions::empty();
68        }
69        let mut mask = self.restrictions.permission_mask();
70        let recent = now_ms.saturating_sub(admitted_at_ms) < self.newcomer_window_ms;
71        if recent
72            && self
73                .restrictions
74                .contains(LockdownRestrictions::RESTRICT_NEWCOMERS)
75        {
76            mask |= newcomer_mask(newcomers);
77        }
78        mask
79    }
80}
81
82/// The mask `restrict_newcomers` applies: the policy's newcomer restrictions.
83// permissions §3.3, specification §8.10, governance.md §9.
84const fn newcomer_mask(policy: Permissions) -> Permissions {
85    policy
86}