Skip to main content

zaun_community_protocol/command/object/
admission.rs

1//! The admission policy and its public summary (specification §8.9, governance §4).
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6use crate::bits::Permissions;
7use crate::command::{BodyError, check};
8
9/// The full admission policy (§8.9). Visible to `MANAGE_SAFETY` holders.
10///
11/// SPEC: governance §4 lists different fields (`admission: open|invite_only`, `max_pending`)
12/// and different `invite_creation` values; §8.9 is followed. §8.9 says membership requests
13/// need "open admission" but the full policy has no field saying so.
14#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
15pub struct AdmissionPolicy {
16    /// The preset, or `custom` once a field is edited.
17    pub level: PolicyLevel,
18    /// Joins start `pending`.
19    pub requires_approval: bool,
20    /// Delay before the first message, reaction or invitation, s.
21    pub waiting_period_s: u64,
22    /// Time in `probation` before automatic promotion, s (0 = none).
23    pub probation_period_s: u64,
24    /// Messages or reactions required during probation.
25    pub probation_activity_min: u32,
26    /// Bits removed during probation; never `VIEW_CHANNEL`/`READ_MESSAGE_HISTORY`.
27    pub newcomer_restrictions: Permissions,
28    /// Aggregate admission rate.
29    pub join_rate: JoinRate,
30    /// Pending requests expire after this, s (≤ 30 days).
31    pub pending_expiry_s: u64,
32    /// Who may create invitations.
33    pub invite_creation: InviteCreation,
34    /// Reserved (§8.11); MUST be empty in v1.
35    pub verification: Vec<Value>,
36}
37
38/// The public summary (§8.9): `{level, requires_approval, invite_creation, open}`. Hosts
39/// MUST send `open`.
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
41pub struct PolicySummary {
42    /// The preset.
43    pub level: PolicyLevel,
44    /// Joins start `pending`.
45    pub requires_approval: bool,
46    /// Who may create invitations.
47    pub invite_creation: InviteCreation,
48    /// Whether the community accepts `membership.request` (open admission, §8.9).
49    pub open: bool,
50}
51
52/// The full policy or the public summary, by the viewer's permissions
53/// (`admission.policy.updated` body, §9.2). The full form is tried first.
54#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
55#[serde(untagged)]
56pub enum PolicyView {
57    /// For `MANAGE_SAFETY` holders.
58    Full(AdmissionPolicy),
59    /// For everyone else.
60    Summary(PolicySummary),
61}
62
63/// `level` (§8.9).
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
65#[serde(tag = "t", content = "c", rename_all = "snake_case")]
66pub enum PolicyLevel {
67    /// No friction beyond infrastructure limits.
68    None,
69    /// Low.
70    Low,
71    /// Medium (public default).
72    Medium,
73    /// High.
74    High,
75    /// Highest.
76    Highest,
77    /// Edited fields.
78    Custom,
79}
80
81/// `invite_creation` (§8.9).
82#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
83#[serde(tag = "t", content = "c", rename_all = "snake_case")]
84pub enum InviteCreation {
85    /// Anyone with `CREATE_INVITE`.
86    Permission,
87    /// Active members with `CREATE_INVITE`.
88    Active,
89    /// Only members with `MANAGE_INVITES`.
90    Moderators,
91}
92
93/// `join_rate: {per_hour}` (§8.9).
94#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
95pub struct JoinRate {
96    /// Admissions per hour for the community.
97    pub per_hour: u32,
98}
99
100/// Known bits only, and never `VIEW_CHANNEL`/`READ_MESSAGE_HISTORY` (permissions §3.3).
101pub(crate) fn check_newcomer_mask(field: &str, mask: Permissions) -> Result<(), BodyError> {
102    check::known_bits(field, mask)?;
103    if mask.intersects(Permissions::VIEW_CHANNEL.union(Permissions::READ_MESSAGE_HISTORY)) {
104        return Err(BodyError::bad(
105            field,
106            "newcomer restrictions may not hide channels or history",
107        ));
108    }
109    Ok(())
110}