Skip to main content

zaun_community_protocol/command/object/
message.rs

1//! Messages, mentions, attachments and reaction counts (specification §8.5).
2
3use serde::{Deserialize, Serialize};
4
5use super::{Component, Embed, InteractionMetadata};
6use crate::command::{BodyError, check};
7use crate::ids::{AttachmentId, ChannelId, MemberId, MessageId, RoleId};
8use crate::limits::{MENTION_MEMBERS, MENTION_ROLES};
9
10/// A stored message as returned by `message.history` (§8.5).
11#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
12pub struct Message {
13    /// Host-assigned id.
14    pub message_id: MessageId,
15    /// The channel.
16    pub channel_id: ChannelId,
17    /// Its `channel_seq`.
18    pub sequence: u64,
19    /// The author, from the connection binding.
20    pub author_id: MemberId,
21    /// Acceptance time, ms.
22    pub created_at: u64,
23    /// Last edit time, ms.
24    #[serde(default, skip_serializing_if = "Option::is_none")]
25    pub edited_at: Option<u64>,
26    /// ≤ 4 000 scalars; empty only when the message has an attachment or an embed.
27    pub content: String,
28    /// Rich embeds, in the author's order; absent when none.
29    #[serde(default, skip_serializing_if = "Vec::is_empty")]
30    pub embeds: Vec<Embed>,
31    /// Components (bots only, §8.14); absent when none.
32    #[serde(default, skip_serializing_if = "Vec::is_empty")]
33    pub components: Vec<Component>,
34    /// Message flags (§8.14); absent when 0.
35    #[serde(default, skip_serializing_if = "crate::json::is_zero_u32")]
36    pub flags: u32,
37    /// The interaction this message answers.
38    #[serde(default, skip_serializing_if = "Option::is_none")]
39    pub interaction_metadata: Option<Box<InteractionMetadata>>,
40    /// The message replied to.
41    #[serde(default, skip_serializing_if = "Option::is_none")]
42    pub reply_to: Option<MessageId>,
43    /// Who is mentioned.
44    pub mentions: Mentions,
45    /// Attachments, in the order the author listed them.
46    pub attachments: Vec<Attachment>,
47    /// Reaction counts.
48    pub reactions: Vec<ReactionCount>,
49}
50
51/// `mentions: {everyone, members, roles}` (§8.5).
52#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
53pub struct Mentions {
54    /// `@everyone` was mentioned.
55    pub everyone: bool,
56    /// Mentioned members, ≤ 50.
57    pub members: Vec<MemberId>,
58    /// Mentioned roles, ≤ 20.
59    pub roles: Vec<RoleId>,
60}
61
62impl Mentions {
63    /// ≤ 50 members, ≤ 20 roles (§12), no repeats.
64    ///
65    /// SPEC: the spec does not say whether repeats are allowed; they are rejected so the
66    /// counts mean distinct mentions.
67    pub(crate) fn check(&self) -> Result<(), BodyError> {
68        check::count("mentions.members", self.members.len(), MENTION_MEMBERS)?;
69        check::count("mentions.roles", self.roles.len(), MENTION_ROLES)?;
70        check::unique("mentions.members", &self.members)?;
71        check::unique("mentions.roles", &self.roles)
72    }
73}
74
75/// An attachment record (§8.5): what a message carries, and what `attachment.download`
76/// and `attachment.updated` answer.
77#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
78pub struct Attachment {
79    /// Host-assigned id.
80    pub attachment_id: AttachmentId,
81    /// File name.
82    pub name: String,
83    /// Size in bytes.
84    pub size: u64,
85    /// MIME type.
86    pub mime: String,
87    /// `SHA3-256` of the bytes, base64url.
88    #[serde(with = "crate::b64::bytes")]
89    pub sha3_256: Vec<u8>,
90    /// Whether the host still holds the bytes.
91    pub availability: Availability,
92    /// Why an upload never became `stored`; only with `availability: failed`.
93    #[serde(default, skip_serializing_if = "Option::is_none")]
94    pub failure: Option<UploadFailure>,
95}
96
97/// `availability` (§8.5). SPEC: a plain string, as written in the spec.
98#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
99#[serde(rename_all = "snake_case")]
100pub enum Availability {
101    /// The host holds the bytes.
102    Stored,
103    /// Retention expired.
104    Expired,
105    /// Removed by moderation or the author.
106    Removed,
107    /// The upload never reached the host, or arrived different from its declaration; the
108    /// attachment is gone and `failure` says why (only in `attachment.updated`).
109    Failed,
110}
111
112/// `failure` of an upload (§8.5): why the host does not hold it.
113#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
114#[serde(rename_all = "snake_case")]
115pub enum UploadFailure {
116    /// The file went to an XFTP server the host does not download from. A member uploads
117    /// only to the servers `session.welcome.attachments.file_servers` lists.
118    FileServer,
119    /// The host's download failed or was cancelled.
120    Transfer,
121    /// The bytes that arrived differ in size or SHA3-256 from the declaration.
122    Mismatch,
123    /// No file arrived before `expires_at`.
124    NotReceived,
125}
126
127/// `reactions: [{emoji, count, me}]` (§8.5).
128#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
129pub struct ReactionCount {
130    /// The emoji.
131    pub emoji: String,
132    /// How many members reacted with it.
133    pub count: u32,
134    /// Whether the viewer is one of them.
135    pub me: bool,
136}