Skip to main content

zaun_community_protocol/command/object/
invite.rs

1//! Invitation shapes (specification §8.6, governance §6, permissions §4.7).
2
3use std::fmt;
4
5use serde::{Deserialize, Serialize};
6
7use crate::bits::Permissions;
8use crate::command::{BodyError, check};
9use crate::ids::{ChannelId, InviteId, MemberId};
10use crate::model::MembershipState;
11
12/// A `zaun://join/…` link. It embeds the [`crate::ids::InviteSecret`], so `Debug` is
13/// redacted; the text is reachable only through [`InviteLink::expose`].
14#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
15#[serde(transparent)]
16pub struct InviteLink(String);
17
18impl InviteLink {
19    /// Wraps a link built by the host.
20    #[must_use]
21    pub const fn new(link: String) -> Self {
22        Self(link)
23    }
24
25    /// The link text, for the one place it is shown.
26    #[must_use]
27    pub fn expose(&self) -> &str {
28        &self.0
29    }
30}
31
32impl fmt::Debug for InviteLink {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        f.write_str("InviteLink(<redacted>)")
35    }
36}
37
38/// One entry of `invite.list` (§8.6). Never carries the secret.
39#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
40pub struct InviteSummary {
41    /// Public handle.
42    pub invite_id: InviteId,
43    /// The issuing member.
44    pub creator_id: MemberId,
45    /// Redemptions so far (pending requests count).
46    pub uses: u32,
47    /// 0 = unlimited.
48    pub max_uses: u32,
49    /// Expiry, ms; none = never.
50    pub expires_at: Option<u64>,
51    /// The channel the invitation opens on, if any.
52    pub channel_id: Option<ChannelId>,
53    /// Joins through it start `pending`.
54    pub requires_approval: bool,
55    /// The creator sponsors members admitted through it.
56    pub sponsored: bool,
57}
58
59/// One entry of `invite.uses` (§8.6).
60#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
61pub struct InviteUse {
62    /// The admitted member.
63    pub member_id: MemberId,
64    /// Admission time, ms.
65    pub joined_at: u64,
66    /// Current state.
67    pub state: MembershipState,
68}
69
70/// `invite.create.admission_overrides`: stricter-only admission settings (§8.6).
71///
72/// SPEC: the shape is never defined. permissions §4.7 lists "approval required where it was
73/// not, longer probation, more newcomer restrictions"; this models those plus the waiting
74/// period and activity minimum. Whether each value is stricter needs the community policy,
75/// so that check is the engine's; other policy fields sent here are ignored.
76#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
77pub struct AdmissionOverrides {
78    /// Require approval.
79    #[serde(default, skip_serializing_if = "Option::is_none")]
80    pub requires_approval: Option<bool>,
81    /// Longer waiting period, s.
82    #[serde(default, skip_serializing_if = "Option::is_none")]
83    pub waiting_period_s: Option<u64>,
84    /// Longer probation, s.
85    #[serde(default, skip_serializing_if = "Option::is_none")]
86    pub probation_period_s: Option<u64>,
87    /// Higher activity requirement.
88    #[serde(default, skip_serializing_if = "Option::is_none")]
89    pub probation_activity_min: Option<u32>,
90    /// More newcomer restrictions.
91    #[serde(default, skip_serializing_if = "Option::is_none")]
92    pub newcomer_restrictions: Option<Permissions>,
93}
94
95impl AdmissionOverrides {
96    pub(crate) fn check(&self) -> Result<(), BodyError> {
97        for value in [self.waiting_period_s, self.probation_period_s]
98            .into_iter()
99            .flatten()
100        {
101            check::json_safe("admission_overrides", value)?;
102        }
103        match self.newcomer_restrictions {
104            Some(mask) => super::admission::check_newcomer_mask("admission_overrides", mask),
105            None => Ok(()),
106        }
107    }
108}