Skip to main content

zaun_community_protocol/command/sync/
response.rs

1//! Responses of the synchronization commands (§10.1–§10.3). Events inside them are full event
2//! envelopes, kept raw for the `envelope` module.
3
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6use serde_json::value::RawValue;
7
8use super::PageCursor;
9use crate::command::object::{
10    Channel, ChannelFloor, ChannelHead, CommunityProfile, Member, Resync, Role,
11};
12use crate::ids::ChannelId;
13use crate::json::SignedJson;
14
15/// `sync.snapshot` response (§10.1).
16#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
17pub struct SnapshotPage {
18    /// The member stream position the page was read at.
19    pub as_of_member_seq: u64,
20    /// The items of the requested part.
21    pub items: Vec<SnapshotItem>,
22    /// The `after` of the next page.
23    #[serde(default, skip_serializing_if = "Option::is_none")]
24    pub next: Option<PageCursor>,
25}
26
27/// One snapshot item; the variant follows the requested part.
28#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
29#[serde(untagged)]
30pub enum SnapshotItem {
31    /// `part: "channels"`.
32    Channel(Box<Channel>),
33    /// `part: "roles"`.
34    Role(Role),
35    /// `part: "community"`.
36    Community(Box<CommunitySnapshot>),
37}
38
39/// The single item of `part: "community"` (§10.1).
40///
41/// SPEC: the item's field names are not given ("profile, descriptor, delegation, the
42/// member's own member object"); `profile`, `descriptor`, `delegation`, `member` are used.
43#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
44pub struct CommunitySnapshot {
45    /// Host-managed profile.
46    pub profile: CommunityProfile,
47    /// Signed descriptor; absent when it is the one the session's welcome gave or confirmed
48    /// (§6.2).
49    #[serde(default, skip_serializing_if = "Option::is_none")]
50    pub descriptor: Option<SignedJson>,
51    /// Signed delegation; absent with the descriptor.
52    #[serde(default, skip_serializing_if = "Option::is_none")]
53    pub delegation: Option<SignedJson>,
54    /// The member's own member object.
55    pub member: Member,
56}
57
58/// `sync.events` response (§10.2), also the `member` part of `sync.resume`.
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct EventsPage {
61    /// Event envelopes in sequence order.
62    pub events: Vec<Box<RawValue>>,
63    /// More events follow.
64    pub has_more: bool,
65    /// Channel stream only: the member's floor (§10.6).
66    #[serde(default, skip_serializing_if = "Option::is_none")]
67    pub floor: Option<u64>,
68}
69
70/// One channel of `sync.resume.channels`.
71#[derive(Debug, Clone, Serialize, Deserialize)]
72pub struct ChannelEvents {
73    /// The channel.
74    pub channel_id: ChannelId,
75    /// Event envelopes in sequence order (≤ 50).
76    pub events: Vec<Box<RawValue>>,
77    /// Continue with `sync.events`.
78    pub has_more: bool,
79}
80
81/// `sync.resume` response (§10.3).
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct Resumed {
84    /// Member-stream events; absent or `null` when replaced by a `resync` entry.
85    #[serde(default, skip_serializing_if = "Option::is_none")]
86    pub member: Option<EventsPage>,
87    /// Per listed channel.
88    #[serde(default)]
89    pub channels: Vec<ChannelEvents>,
90    /// Streams whose cursor is older than retention.
91    #[serde(default)]
92    pub resync: Vec<Resync>,
93    /// Listed channels the member can no longer view.
94    #[serde(default)]
95    pub hidden: Vec<ChannelId>,
96    /// Viewable channels the client did not list.
97    #[serde(default)]
98    pub new: Vec<ChannelHead>,
99    /// Floors of listed channels.
100    #[serde(default)]
101    pub floors: Vec<ChannelFloor>,
102    /// Appendix A only (experimental); empty otherwise. Kept raw.
103    #[serde(default)]
104    pub chain_heads: Vec<Value>,
105}