Skip to main content

zaun_community_protocol/permissions/
resolve.rs

1//! Steps 1–6 of the resolution algorithm: grants only, no restriction (permissions §3).
2
3use crate::bits::Permissions;
4use crate::ids::{MemberId, RoleId};
5use crate::model::ChannelKind;
6
7use super::input::{Channel, Override, OverrideTarget, Role};
8
9/// `ALL` means every known bit, never `u64::MAX` (permissions §1.1).
10pub const ALL: Permissions = Permissions::KNOWN;
11
12/// Bits a timed-out member keeps (step 7a).
13pub const TIMEOUT_KEEP: Permissions =
14    Permissions::VIEW_CHANNEL.union(Permissions::READ_MESSAGE_HISTORY);
15
16/// Bits the channel `locked` flag removes (step 7d).
17pub const LOCK_MASK: Permissions = Permissions::SEND_MESSAGES
18    .union(Permissions::SEND_MESSAGES_IN_THREADS)
19    .union(Permissions::CREATE_THREADS)
20    .union(Permissions::ADD_REACTIONS)
21    .union(Permissions::ATTACH_FILES)
22    .union(Permissions::EMBED_LINKS)
23    .union(Permissions::MENTION_EVERYONE);
24
25const SEND_DEPENDANTS: Permissions = Permissions::ATTACH_FILES
26    .union(Permissions::EMBED_LINKS)
27    .union(Permissions::MENTION_EVERYONE);
28
29const CONNECT_DEPENDANTS: Permissions = Permissions::VOICE.difference(Permissions::CONNECT);
30
31/// The channel bits that apply to a channel kind (§1.3).
32#[must_use]
33pub const fn applicable(kind: ChannelKind) -> Permissions {
34    match kind {
35        ChannelKind::Category => Permissions::CATEGORY_APPLICABLE,
36        ChannelKind::Text | ChannelKind::Announcement | ChannelKind::Forum | ChannelKind::Dm => {
37            Permissions::CHANNEL_SCOPED.difference(Permissions::VOICE)
38        }
39        ChannelKind::Voice | ChannelKind::Stage => Permissions::CHANNEL_SCOPED,
40    }
41}
42
43/// Implicit dependencies (step 6) at channel level: no channel bit without `VIEW_CHANNEL`,
44/// then [`community_dependencies`].
45#[must_use]
46pub const fn dependencies(mut p: Permissions) -> Permissions {
47    if !p.contains(Permissions::VIEW_CHANNEL) {
48        p = p.intersection(Permissions::COMMUNITY_SCOPED);
49    }
50    community_dependencies(p)
51}
52
53/// Implicit dependencies at community level: no attachment, embed or mass mention without
54/// `SEND_MESSAGES`, no voice bit without `CONNECT`. There is no `VIEW_CHANNEL` gate here:
55/// whether a member sees a channel is decided per channel (permissions §3.1).
56#[must_use]
57pub const fn community_dependencies(mut p: Permissions) -> Permissions {
58    if !p.contains(Permissions::SEND_MESSAGES) {
59        p = p.difference(SEND_DEPENDANTS);
60    }
61    if !p.contains(Permissions::CONNECT) {
62        p = p.difference(CONNECT_DEPENDANTS);
63    }
64    p
65}
66
67/// Every bit that [`dependencies`] would also drop once `mask` is removed: used to attribute
68/// a lost dependant to the restriction that removed its prerequisite.
69#[must_use]
70pub const fn with_dependants(mask: Permissions) -> Permissions {
71    let mut out = mask;
72    if mask.contains(Permissions::VIEW_CHANNEL) {
73        out = out.union(Permissions::CHANNEL_SCOPED);
74    }
75    if out.contains(Permissions::SEND_MESSAGES) {
76        out = out.union(SEND_DEPENDANTS);
77    }
78    if out.contains(Permissions::CONNECT) {
79        out = out.union(CONNECT_DEPENDANTS);
80    }
81    out
82}
83
84/// `base(m)` for a non-owner (steps 1–2): `@everyone` OR the member's roles; `ALL` with
85/// `ADMINISTRATOR`; otherwise masked by `KNOWN`.
86#[must_use]
87pub fn base(everyone: Permissions, roles: &[Role]) -> Permissions {
88    let p = roles.iter().fold(everyone, |p, r| p | r.permissions);
89    if p.contains(Permissions::ADMINISTRATOR) {
90        ALL
91    } else {
92        p & Permissions::KNOWN
93    }
94}
95
96/// The grants of an owner or administrator in a channel.
97#[must_use]
98pub const fn all_in(kind: ChannelKind) -> Permissions {
99    ALL.intersection(applicable(kind))
100        .union(Permissions::COMMUNITY_SCOPED)
101}
102
103/// `grants(m, ch)` (steps 3–6) for a non-administrator whose base is `base`.
104///
105/// Overrides are aggregated per layer in one pass, so duplicate or reordered entries give
106/// the same result (property 1).
107#[must_use]
108pub fn channel_grants(
109    base: Permissions,
110    member: MemberId,
111    roles: &[Role],
112    ch: &Channel<'_>,
113) -> Permissions {
114    let (mut every, mut role, mut own) = (Layer::default(), Layer::default(), Layer::default());
115    for o in ch.effective_overrides() {
116        match o.target {
117            OverrideTarget::Role(id) if id == RoleId::EVERYONE => every.add(o),
118            OverrideTarget::Role(id) if roles.iter().any(|r| r.id == id) => role.add(o),
119            OverrideTarget::Member(id) if id == member => own.add(o),
120            _ => {}
121        }
122    }
123    let p = own.apply(role.apply(every.apply(base))) & Permissions::KNOWN;
124    if !p.contains(Permissions::VIEW_CHANNEL) {
125        return p & Permissions::COMMUNITY_SCOPED;
126    }
127    dependencies(p) & (applicable(ch.kind) | Permissions::COMMUNITY_SCOPED)
128}
129
130/// One override layer: its denies apply first, then its allows (§3.2 rule 4).
131#[derive(Default)]
132struct Layer {
133    allow: Permissions,
134    deny: Permissions,
135}
136
137impl Layer {
138    fn add(&mut self, o: &Override) {
139        // SPEC: overrides may hold only channel-scoped bits (§2.2). If a malformed one holds
140        // other known bits, least privilege honours its deny but never its allow.
141        self.allow |= o.allow & Permissions::CHANNEL_SCOPED;
142        self.deny |= o.deny;
143    }
144
145    fn apply(&self, p: Permissions) -> Permissions {
146        (p - self.deny) | self.allow
147    }
148}