Skip to main content

zaun_community_protocol/permissions/
restrict.rs

1//! The two-phase resolver and step 7 restrictions (permissions §3, §3.3).
2
3mod accessors;
4
5use crate::bits::Permissions;
6use crate::ids::MemberId;
7use crate::model::MembershipState;
8
9use super::input::{Channel, Context, Subject};
10use super::resolve::{
11    ALL, LOCK_MASK, TIMEOUT_KEEP, all_in, base, channel_grants, community_dependencies,
12    dependencies,
13};
14use super::resolved::{Resolved, Steps};
15
16/// Bits the admission policy's waiting period withholds: posting, reacting and creating
17/// invitations (specification §6.4).
18// permissions §3.3. SPEC: specification §6.4 names "messages, reactions or invitations"
19// without bits.
20pub const WAITING_MASK: Permissions = Permissions::SEND_MESSAGES
21    .union(Permissions::SEND_MESSAGES_IN_THREADS)
22    .union(Permissions::CREATE_THREADS)
23    .union(Permissions::ADD_REACTIONS)
24    .union(Permissions::CREATE_INVITE);
25
26/// What both participants of a `dm` channel may always do (specification §8.15).
27pub const DM_GRANTS: Permissions = Permissions::VIEW_CHANNEL
28    .union(Permissions::SEND_MESSAGES)
29    .union(Permissions::READ_MESSAGE_HISTORY)
30    .union(Permissions::ADD_REACTIONS)
31    .union(Permissions::USE_APPLICATION_COMMANDS);
32
33/// What a participant may do in a `dm` channel only when its roles grant it in the community.
34pub const DM_IF_GRANTED: Permissions = Permissions::EMBED_LINKS.union(Permissions::ATTACH_FILES);
35
36/// Per-member state computed once; then [`Resolver::community`] and [`Resolver::channel`]
37/// are cheap enough to call for every channel of a snapshot.
38#[derive(Debug, Clone, Copy)]
39pub struct Resolver<'a> {
40    subject: Subject<'a>,
41    owner: bool,
42    admitted: bool,
43    base: Permissions,
44    admin: bool,
45    timed_out: bool,
46    waiting: bool,
47    probation: Permissions,
48    lockdown: Permissions,
49}
50
51impl<'a> Resolver<'a> {
52    /// Phase one: the membership gate, `base(m)` and the member's restriction masks.
53    #[must_use]
54    pub fn new(subject: Subject<'a>, ctx: &Context<'_>) -> Self {
55        let owner = subject.is_owner;
56        // §3.0: roles of a non-admitted member grant nothing. The owner is always Active.
57        let admitted = owner || subject.state.is_admitted();
58        let base = match (owner, admitted) {
59            (true, _) => ALL,
60            (false, true) => base(ctx.everyone, subject.roles),
61            (false, false) => Permissions::empty(),
62        };
63        let admin = admitted && base == ALL;
64        let now = ctx.now_ms;
65        let exempt = owner || !admitted;
66        let timed_out = !exempt && !admin && subject.timeout_until_ms.is_some_and(|t| t > now);
67        // SPEC: specification §6.4 does not say whether the waiting period binds
68        // administrators; as a step 7b restriction it does (never the owner).
69        let waiting = !exempt && now < subject.admitted_at_ms.saturating_add(ctx.waiting_period_ms);
70        let mut probation = Permissions::empty();
71        if !exempt && subject.state == MembershipState::Probation {
72            probation |= ctx.newcomer_restrictions;
73        }
74        if waiting {
75            probation |= WAITING_MASK;
76        }
77        let lockdown = match ctx.lockdown {
78            Some(l) if !exempt => {
79                l.mask_for(subject.admitted_at_ms, ctx.newcomer_restrictions, now)
80            }
81            _ => Permissions::empty(),
82        };
83        Self {
84            subject,
85            owner,
86            admitted,
87            base,
88            admin,
89            timed_out,
90            waiting,
91            probation,
92            lockdown,
93        }
94    }
95
96    /// `community_permissions(m, ctx)`: `cp(A)` of §4.
97    #[must_use]
98    pub fn community(&self) -> Resolved {
99        if self.owner {
100            return Resolved::unrestricted(ALL);
101        }
102        if !self.admitted {
103            return Resolved::unrestricted(Permissions::empty());
104        }
105        self.restrict(community_dependencies(self.base), None)
106    }
107
108    /// `channel_permissions(m, ch, ctx)`: `chp(A, ch)` of §4.
109    #[must_use]
110    pub fn channel(&self, ch: &Channel<'_>) -> Resolved {
111        if let Some(pair) = ch.participants {
112            return self.dm(pair);
113        }
114        if self.owner {
115            return Resolved::unrestricted(all_in(ch.kind));
116        }
117        if !self.admitted {
118            return Resolved::unrestricted(Permissions::empty());
119        }
120        let grants = if self.admin {
121            all_in(ch.kind)
122        } else {
123            channel_grants(self.base, self.subject.id, self.subject.roles, ch)
124        };
125        self.restrict(grants, Some(ch.locked))
126    }
127
128    /// A `dm` channel (specification §8.15): nothing for anyone but its two participants,
129    /// whatever their roles (the owner and administrators included); for them, a fixed set
130    /// plus links and files when their roles allow those, then the usual restrictions
131    /// (timeout, newcomer restrictions, lockdown). The owner's own grants are never
132    /// restricted.
133    fn dm(&self, pair: [MemberId; 2]) -> Resolved {
134        if !self.admitted || !pair.contains(&self.subject.id) {
135            return Resolved::unrestricted(Permissions::empty());
136        }
137        let grants = DM_GRANTS | (self.base & DM_IF_GRANTED);
138        if self.owner {
139            return Resolved::unrestricted(dependencies(grants));
140        }
141        self.restrict(grants, Some(false))
142    }
143
144    /// Step 7 on a non-owner, admitted member's grants; `locked` is `None` at community level,
145    /// where the `VIEW_CHANNEL` gate does not apply (permissions §3.1).
146    fn restrict(&self, grants: Permissions, locked: Option<bool>) -> Resolved {
147        let close = |p| {
148            if locked.is_some() {
149                dependencies(p)
150            } else {
151                community_dependencies(p)
152            }
153        };
154        let timeout = if self.timed_out {
155            ALL - TIMEOUT_KEEP
156        } else {
157            Permissions::empty()
158        };
159        let before_lockdown = close(grants - timeout - self.probation);
160        let mut p = grants - timeout - self.probation - self.lockdown;
161        let lock = locked == Some(true) && !self.admin && !p.contains(Permissions::MANAGE_CHANNELS);
162        let lock = if lock {
163            LOCK_MASK
164        } else {
165            Permissions::empty()
166        };
167        p -= lock;
168        let steps = Steps {
169            timeout,
170            probation: self.probation,
171            lockdown: self.lockdown,
172            lock,
173        };
174        Resolved {
175            effective: close(p),
176            grants,
177            before_lockdown,
178            steps,
179            timed_out: self.timed_out,
180            waiting: self.waiting,
181        }
182    }
183}