Skip to main content

zaun_community_protocol/command/object/
community.rs

1//! Community-level shapes: the host-managed profile, profiles of new members, bindings and
2//! signed root rotations (specification §4.6, §6.2, §8.1).
3
4use std::fmt;
5
6use serde::{Deserialize, Serialize};
7
8use crate::command::{BodyError, check};
9use crate::ids::{ChannelId, CommunityId, MemberId};
10use crate::limits::DISPLAY_NAME;
11
12/// An empty body or response, `{}`. Unknown fields are ignored.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
14pub struct Empty {}
15
16/// The host-managed community profile (§8.1 `community.update`; `community.get`;
17/// `community.updated`).
18///
19/// SPEC: "profile" is never defined. §8.1 names only `system_channel_id` and
20/// `default_slowmode_s` as host-managed profile fields, so only these are modelled; other
21/// fields a host sends are ignored. Distinct from a member's `profile: {display_name}`.
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
23pub struct CommunityProfile {
24    /// Where the host posts system notices, if anywhere.
25    pub system_channel_id: Option<ChannelId>,
26    /// Slowmode applied to new channels, 0–21 600 s.
27    #[serde(default)]
28    pub default_slowmode_s: u32,
29}
30
31/// `profile: {display_name}` of a joining or creating member (§8.1, §8.6, §8.9).
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
33pub struct NewProfile {
34    /// 1–32 scalars.
35    pub display_name: String,
36}
37
38impl NewProfile {
39    pub(crate) fn check(&self) -> Result<(), BodyError> {
40        check::text("profile.display_name", &self.display_name, DISPLAY_NAME)
41    }
42}
43
44/// `session.welcome.binding` (§6.2).
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
46pub struct Binding {
47    /// The bound community.
48    pub community_id: CommunityId,
49    /// The bound member.
50    pub member_id: MemberId,
51}
52
53/// A `SignedRootRotation` in JSON (§4.6): `{bytes, sig_old, sig_new}`, all base64url.
54/// Kept undecoded, like [`crate::json::SignedJson`]; the `signed` module verifies it.
55#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
56pub struct SignedRotation {
57    /// Canonical bytes of the `RootRotation`.
58    #[serde(with = "crate::b64::bytes")]
59    pub bytes: Vec<u8>,
60    /// Signature by the retiring root key.
61    #[serde(with = "crate::b64::bytes")]
62    pub sig_old: Vec<u8>,
63    /// Signature by the new root key.
64    #[serde(with = "crate::b64::bytes")]
65    pub sig_new: Vec<u8>,
66}
67
68impl fmt::Debug for SignedRotation {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        f.debug_struct("SignedRotation")
71            .field("bytes_len", &self.bytes.len())
72            .field("sig_old_len", &self.sig_old.len())
73            .field("sig_new_len", &self.sig_new.len())
74            .finish()
75    }
76}