Skip to main content

zaun_community_protocol/command/
message.rs

1//! Message and reaction commands (specification §8.5). All are channel-scoped: the
2//! channel is the envelope's `channel_id`.
3
4mod validate;
5
6use serde::{Deserialize, Serialize};
7
8use super::object::{Component, Embed, Mentions, Message};
9use crate::ids::{AttachmentId, MemberId, MessageId};
10
11/// `message.send` (§8.5).
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
13pub struct Send {
14    /// ≤ 4 000 scalars; at least 1 unless the message has an attachment or an embed.
15    pub content: String,
16    /// Up to 10 rich embeds (`EMBED_LINKS`). Every picture an embed shows is one of
17    /// `attachments`.
18    #[serde(default, skip_serializing_if = "Vec::is_empty")]
19    pub embeds: Vec<Embed>,
20    /// A message in the same channel.
21    #[serde(default, skip_serializing_if = "Option::is_none")]
22    pub reply_to: Option<MessageId>,
23    /// Who to notify.
24    #[serde(default, skip_serializing_if = "Option::is_none")]
25    pub mentions: Option<Mentions>,
26    /// Up to 10 distinct attachments this member uploaded to this channel and the host
27    /// stored (`attachment.upload`), not yet on a message.
28    ///
29    /// SPEC: written without `?` in §8.5 but absent from the §7.1 example body; it defaults
30    /// to empty.
31    #[serde(default, skip_serializing_if = "Vec::is_empty")]
32    pub attachments: Vec<AttachmentId>,
33    /// Components; bot members only (§8.14).
34    #[serde(default, skip_serializing_if = "Vec::is_empty")]
35    pub components: Vec<Component>,
36    /// Message flags (§8.14); `EPHEMERAL` only in interaction replies.
37    #[serde(default, skip_serializing_if = "Option::is_none")]
38    pub flags: Option<u32>,
39}
40
41/// `message.send` response (§7.2).
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
43pub struct Sent {
44    /// Host-assigned id.
45    pub message_id: MessageId,
46    /// Its `channel_seq`.
47    pub sequence: u64,
48    /// Acceptance time, ms.
49    pub timestamp: u64,
50}
51
52/// `message.edit` (§8.5). Author only.
53#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
54pub struct Edit {
55    /// The message.
56    pub message_id: MessageId,
57    /// ≤ 4 000 scalars; the edited message must still have content, an attachment or an
58    /// embed.
59    pub content: String,
60    /// The message's new embeds (`EMBED_LINKS` unless empty): absent leaves them unchanged,
61    /// `[]` removes them all. Their pictures must be attachments already on the message.
62    #[serde(default, skip_serializing_if = "Option::is_none")]
63    pub embeds: Option<Vec<Embed>>,
64    /// New components (bots only): absent leaves them unchanged, `[]` removes them.
65    #[serde(default, skip_serializing_if = "Option::is_none")]
66    pub components: Option<Vec<Component>>,
67    /// Who the edited message mentions, by `message.send`'s rules: absent leaves the
68    /// mentions unchanged, otherwise they replace them all.
69    #[serde(default, skip_serializing_if = "Option::is_none")]
70    pub mentions: Option<Mentions>,
71}
72
73/// `message.edit` and `message.delete` response: the event's `channel_seq`.
74#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
75pub struct Sequenced {
76    /// The `channel_seq` of the resulting event.
77    pub sequence: u64,
78}
79
80/// `message.delete` (§8.5).
81#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
82pub struct Delete {
83    /// The message.
84    pub message_id: MessageId,
85    /// ≤ 512 scalars (audited when a moderator deletes).
86    #[serde(default, skip_serializing_if = "Option::is_none")]
87    pub reason: Option<String>,
88}
89
90/// `message.history` (§8.5).
91///
92/// SPEC: the spec does not say how `before` and `after` combine; when both are given this
93/// crate requires `after < before`.
94#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
95pub struct History {
96    /// Only messages with a lower `channel_seq`.
97    #[serde(default, skip_serializing_if = "Option::is_none")]
98    pub before: Option<u64>,
99    /// Only messages with a higher `channel_seq`.
100    #[serde(default, skip_serializing_if = "Option::is_none")]
101    pub after: Option<u64>,
102    /// 1–100.
103    pub limit: u32,
104}
105
106/// `message.history` response.
107#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
108pub struct HistoryPage {
109    /// Visible, undeleted messages.
110    pub messages: Vec<Message>,
111    /// More exist in the requested direction.
112    pub has_more: bool,
113    /// The member's floor in this channel (§10.6).
114    pub floor: u64,
115}
116
117/// `reaction.add` (§8.5).
118#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
119pub struct ReactionAdd {
120    /// The message.
121    pub message_id: MessageId,
122    /// ≤ 64 bytes, a Unicode emoji sequence.
123    pub emoji: String,
124}
125
126/// `reaction.remove` (§8.5).
127#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
128pub struct ReactionRemove {
129    /// The message.
130    pub message_id: MessageId,
131    /// The emoji.
132    pub emoji: String,
133    /// Someone else's reaction (needs `MANAGE_MESSAGES`); absent = own.
134    #[serde(default, skip_serializing_if = "Option::is_none")]
135    pub member_id: Option<MemberId>,
136}