Skip to main content

zaun_community_protocol/permissions/
input.rs

1//! Plain-data inputs of the resolution algorithm (permissions.md §2–3).
2//!
3//! Nothing here touches storage: the engine fills these from its snapshot and passes borrowed
4//! slices, so resolving one member over many channels allocates nothing.
5
6use crate::bits::Permissions;
7use crate::ids::{MemberId, RoleId};
8use crate::model::{ChannelKind, MembershipState};
9
10use super::lockdown::Lockdown;
11
12/// A role as the resolver sees it (permissions §2.1).
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14pub struct Role {
15    /// The role id; [`RoleId::EVERYONE`] for `@everyone`.
16    pub id: RoleId,
17    /// Granted bits, unknown bits retained.
18    pub permissions: Permissions,
19    /// Position: 0 is `@everyone`, higher means more authority. Used only by §4.
20    pub position: u32,
21}
22
23/// Whom an override applies to (permissions §2.2).
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
25pub enum OverrideTarget {
26    /// Every holder of the role (`@everyone` included).
27    Role(RoleId),
28    /// One member.
29    Member(MemberId),
30}
31
32/// A permission override on one channel or category (permissions §2.2).
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
34pub struct Override {
35    /// The role or member it applies to.
36    pub target: OverrideTarget,
37    /// Bits explicitly allowed.
38    pub allow: Permissions,
39    /// Bits explicitly denied.
40    pub deny: Permissions,
41}
42
43/// The channel context of a channel-level resolution.
44#[derive(Debug, Clone, Copy)]
45pub struct Channel<'a> {
46    /// The channel kind; it decides which bits apply (§1.3).
47    pub kind: ChannelKind,
48    /// The channel's own stored overrides.
49    pub overrides: &'a [Override],
50    /// The parent category's overrides, when the channel has a parent.
51    pub parent_overrides: Option<&'a [Override]>,
52    /// Whether the channel follows its parent's overrides (§2.4).
53    pub sync_with_parent: bool,
54    /// The channel `locked` flag (§3.3, 7d).
55    pub locked: bool,
56    /// A `dm` channel's two members (§8.15): only they get anything in it, owner and
57    /// administrators included.
58    pub participants: Option<[MemberId; 2]>,
59}
60
61impl<'a> Channel<'a> {
62    /// The overrides that apply (§2.4): the parent's while synced, else the channel's own.
63    /// A category never syncs.
64    #[must_use]
65    pub fn effective_overrides(&self) -> &'a [Override] {
66        match self.parent_overrides {
67            Some(parent) if self.sync_with_parent && self.kind != ChannelKind::Category => parent,
68            _ => self.overrides,
69        }
70    }
71}
72
73/// The member whose permissions are resolved.
74#[derive(Debug, Clone, Copy)]
75pub struct Subject<'a> {
76    /// The member id (matches member overrides).
77    pub id: MemberId,
78    /// Whether this member is the owner bound in the current descriptor.
79    pub is_owner: bool,
80    /// The lifecycle state (§3.0).
81    pub state: MembershipState,
82    /// The roles the member holds, joined from the role table. `@everyone` need not be
83    /// listed (its permissions come from [`Context::everyone`]); listing it is harmless.
84    pub roles: &'a [Role],
85    /// `members.timeout_until` in ms since the epoch.
86    pub timeout_until_ms: Option<u64>,
87    /// When the member was admitted (left `Pending`), in ms since the epoch. Drives the
88    /// waiting period (specification §6.4) and a lockdown's `restrict_newcomers` window.
89    pub admitted_at_ms: u64,
90}
91
92/// Community-wide inputs shared by every resolution in one request.
93#[derive(Debug, Clone, Copy)]
94pub struct Context<'a> {
95    /// The current time in ms since the epoch.
96    pub now_ms: u64,
97    /// The `@everyone` role's permissions.
98    pub everyone: Permissions,
99    /// The admission policy's `newcomer_restrictions` (step 7b).
100    pub newcomer_restrictions: Permissions,
101    /// The admission policy's `waiting_period_s`, in ms (specification §6.4).
102    pub waiting_period_ms: u64,
103    /// The community's lockdown row, if any; ignored once expired.
104    pub lockdown: Option<&'a Lockdown>,
105}