Skip to main content

zaun_community_protocol/permissions/
members.rs

1//! Member and membership checks (permissions §4.3, §4.5, §4.6; specification §8.3, §8.9).
2//! The required bit of each command is checked by the table first.
3
4use crate::bits::Permissions;
5use crate::limits::TIMEOUT_MAX_MS;
6use crate::model::MembershipState;
7
8use super::denial::{ConflictReason, Denial, Invalid, Refusal};
9use super::hierarchy::{Party, act_on, h1};
10use super::input::Role;
11use super::roles::can_grant;
12
13/// `CONFLICT{already_member}` for an admitted target, else a reason-less `CONFLICT`.
14const fn wrong_state(state: MembershipState) -> Refusal {
15    Refusal::Conflict(if state.is_admitted() {
16        ConflictReason::AlreadyMember
17    } else {
18        ConflictReason::WrongState
19    })
20}
21
22const fn not_self() -> Denial {
23    Denial::missing(Permissions::empty())
24}
25
26/// `member.kick` and `member.ban`: not self (use `community.leave`); H1; H2.
27pub fn kick_or_ban(actor: &Party, target: &Party) -> Result<(), Refusal> {
28    if actor.id == target.id {
29        return Err(not_self().into());
30    }
31    Ok(act_on(actor, target, false)?)
32}
33
34/// `member.timeout` (set or clear): H1; H2; never the owner; never a target holding
35/// `ADMINISTRATOR`; `until ≤ now + 28 days`.
36pub fn timeout(
37    actor: &Party,
38    target: &Party,
39    target_is_admin: bool,
40    until_ms: Option<u64>,
41    now_ms: u64,
42) -> Result<(), Refusal> {
43    if target.is_owner {
44        return Err(Denial::hierarchy().into());
45    }
46    act_on(actor, target, false)?;
47    if target_is_admin {
48        return Err(Denial::missing(Permissions::empty()).into());
49    }
50    match until_ms {
51        Some(until) if until > now_ms.saturating_add(TIMEOUT_MAX_MS) => {
52            Err(Invalid::Duration.into())
53        }
54        _ => Ok(()),
55    }
56}
57
58/// `member.update` of a nickname: one's own needs only `CHANGE_NICKNAME` (table); another's
59/// needs H1 and H2.
60pub fn nickname(actor: &Party, target: &Party) -> Result<(), Refusal> {
61    Ok(act_on(actor, target, true)?)
62}
63
64/// Deleting another member's message or removing their reaction: H1 only, no H2 (§4.5).
65pub fn moderate_content(actor: &Party, author: &Party) -> Result<(), Refusal> {
66    if actor.id == author.id {
67        return Ok(());
68    }
69    Ok(h1(actor, author)?)
70}
71
72/// `membership.suspend`: not self; H1; H2; the target is `Probation` or `Active`.
73pub fn suspend(actor: &Party, target: &Party, state: MembershipState) -> Result<(), Refusal> {
74    kick_or_ban(actor, target)?;
75    match state {
76        MembershipState::Probation | MembershipState::Active => Ok(()),
77        other => Err(wrong_state(other)),
78    }
79}
80
81/// `membership.restore`: not self; H1; H2; the target is `Suspended`.
82pub fn restore(actor: &Party, target: &Party, state: MembershipState) -> Result<(), Refusal> {
83    kick_or_ban(actor, target)?;
84    if state == MembershipState::Suspended {
85        Ok(())
86    } else {
87        Err(wrong_state(state))
88    }
89}
90
91/// `membership.approve`. A `Pending` target has no roles, so no H2; but the approver must be
92/// able to assign every default role applied at approval (specification §8.9). Ending a
93/// `Probation` early needs H1 and H2 (permissions §4.6).
94// Specification §8.9, permissions §4.6: H4 and H3 for every default role; `MANAGE_ROLES`
95// itself is not required.
96pub fn approve(
97    actor: &Party,
98    cp: Permissions,
99    target: &Party,
100    state: MembershipState,
101    default_roles: &[Role],
102) -> Result<(), Refusal> {
103    match state {
104        MembershipState::Pending => {
105            for role in default_roles {
106                can_grant(actor, cp, role)?;
107            }
108            Ok(())
109        }
110        MembershipState::Probation => Ok(act_on(actor, target, false)?),
111        other => Err(wrong_state(other)),
112    }
113}
114
115/// `membership.reject`: the target is `Pending`.
116pub fn reject(state: MembershipState) -> Result<(), Refusal> {
117    if state == MembershipState::Pending {
118        Ok(())
119    } else {
120        Err(wrong_state(state))
121    }
122}