Skip to main content

zaun_community_protocol/command/message/
validate.rs

1//! State-free checks of the message and reaction commands.
2
3use super::{Delete, Edit, History, ReactionAdd, ReactionRemove, Send};
4use crate::command::object::{
5    check_embeds, check_message_components, component_attachments, flags,
6};
7use crate::command::{BodyError, Validate, check};
8use crate::limits::{ATTACHMENTS_PER_MESSAGE, MESSAGE_CONTENT, PAGE_SMALL, REASON_MAX};
9
10/// Content may be empty only beside an attachment or an embed (ยง8.5).
11fn content(value: &str, has_more: bool) -> Result<(), BodyError> {
12    let (min, max) = MESSAGE_CONTENT;
13    check::text("content", value, (if has_more { 0 } else { min }, max))
14}
15
16impl Validate for Send {
17    fn validate(&self) -> Result<(), BodyError> {
18        let bits = self.flags.unwrap_or(0);
19        if bits & !(flags::SETTABLE & !flags::EPHEMERAL) != 0 {
20            return Err(BodyError::bad("flags", "sets a flag a message cannot have"));
21        }
22        let v2 = bits & flags::IS_COMPONENTS_V2 != 0;
23        if v2 && (!self.content.is_empty() || !self.embeds.is_empty()) {
24            return Err(BodyError::bad(
25                "content",
26                "a components V2 message has no content or embeds",
27            ));
28        }
29        check_message_components(&self.components, v2)?;
30        for (field, id) in component_attachments(&self.components) {
31            if !self.attachments.contains(&id) {
32                return Err(BodyError::bad(
33                    field,
34                    "is not one of the message's attachments",
35                ));
36            }
37        }
38        content(
39            &self.content,
40            !self.attachments.is_empty() || !self.embeds.is_empty() || !self.components.is_empty(),
41        )?;
42        if let Some(mentions) = &self.mentions {
43            mentions.check()?;
44        }
45        check::count(
46            "attachments",
47            self.attachments.len(),
48            ATTACHMENTS_PER_MESSAGE,
49        )?;
50        check::unique("attachments", &self.attachments)?;
51        check_embeds(&self.embeds, Some(&self.attachments))
52    }
53}
54
55impl Validate for Edit {
56    /// Whether an empty `content` leaves the message with an attachment or an embed depends
57    /// on the stored message; the host checks that.
58    fn validate(&self) -> Result<(), BodyError> {
59        content(&self.content, true)?;
60        if let Some(mentions) = &self.mentions {
61            mentions.check()?;
62        }
63        if let Some(components) = &self.components {
64            // V1 or V2 depends on the stored flags; the host re-checks with them.
65            check_message_components(components, true)?;
66        }
67        match &self.embeds {
68            Some(embeds) => check_embeds(embeds, None),
69            None => Ok(()),
70        }
71    }
72}
73
74impl Validate for Delete {
75    fn validate(&self) -> Result<(), BodyError> {
76        check::opt_text("reason", self.reason.as_deref(), (0, REASON_MAX))
77    }
78}
79
80impl Validate for History {
81    fn validate(&self) -> Result<(), BodyError> {
82        check::page(self.limit, PAGE_SMALL)?;
83        if let Some(before) = self.before {
84            check::json_safe("before", before)?;
85        }
86        if let Some(after) = self.after {
87            check::json_safe("after", after)?;
88        }
89        match (self.after, self.before) {
90            (Some(after), Some(before)) if after >= before => {
91                Err(BodyError::bad("after", "must be below before"))
92            }
93            _ => Ok(()),
94        }
95    }
96}
97
98impl Validate for ReactionAdd {
99    fn validate(&self) -> Result<(), BodyError> {
100        check::emoji("emoji", &self.emoji)
101    }
102}
103
104impl Validate for ReactionRemove {
105    fn validate(&self) -> Result<(), BodyError> {
106        check::emoji("emoji", &self.emoji)
107    }
108}