Skip to main content

zaun_client/compose/
edit_mentions.rs

1//! Who an edit notifies: the same builders as a post's, sent with `message.edit`. Naming any
2//! replaces the message's mentions; naming none leaves them as they are.
3
4use zaun_community_protocol::command::object::Mentions;
5use zaun_community_protocol::ids::{MemberId, RoleId};
6
7use super::mentions::{add_member, add_role};
8use super::options::EditMessage;
9use crate::mention;
10
11impl EditMessage {
12    /// Notifies `member`, as [`MessageOptions::mention_member`](crate::MessageOptions::mention_member).
13    /// Any mention on an edit replaces all of the message's mentions, and needs
14    /// `SEND_MESSAGES`.
15    #[must_use]
16    pub fn mention_member(mut self, member: MemberId) -> Self {
17        add_member(&mut self.mentions, member);
18        self
19    }
20
21    /// Notifies the members with `role`, as
22    /// [`MessageOptions::mention_role`](crate::MessageOptions::mention_role).
23    #[must_use]
24    pub fn mention_role(mut self, role: RoleId) -> Self {
25        add_role(&mut self.mentions, role);
26        self
27    }
28
29    /// Notifies everyone who can see the channel (needs `MENTION_EVERYONE`; dropped without).
30    #[must_use]
31    pub fn mention_everyone(mut self) -> Self {
32        self.mentions.get_or_insert_with(Mentions::default).everyone = true;
33        self
34    }
35
36    /// Replaces the message's mentions with exactly `mentions`; [`Mentions::default`] clears
37    /// them, `@everyone` too.
38    #[must_use]
39    pub fn mentions(mut self, mentions: Mentions) -> Self {
40        self.mentions = Some(mentions);
41        self
42    }
43
44    /// Also notifies whoever the text shows (see [`mention::parse`]): the new text, or the
45    /// message's own when the edit keeps it. The edit then always sends mentions, so text
46    /// that shows none clears them. Only for text this member wrote.
47    #[must_use]
48    pub const fn mentions_from_content(mut self) -> Self {
49        self.mentions_from_content = true;
50        self
51    }
52
53    /// The mentions to send with `content` (the text the edit leaves the message with):
54    /// `None` when the edit names none, so the message keeps its own.
55    pub(crate) fn mentions_for(&self, content: &str) -> Option<Mentions> {
56        if !self.mentions_from_content {
57            return self.mentions.clone();
58        }
59        let mut out = self.mentions.clone().unwrap_or_default();
60        mention::merge(&mut out, mention::parse(content));
61        Some(out)
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    const A: MemberId = MemberId::from_bytes([1; 16]);
70    const B: MemberId = MemberId::from_bytes([3; 16]);
71    const R: RoleId = RoleId::from_bytes([2; 16]);
72
73    #[test]
74    fn an_edit_naming_nobody_leaves_the_mentions_and_the_text_alone_never_names() {
75        let text = format!("hi {}", mention::member(A));
76        assert_eq!(EditMessage::from(text.as_str()).mentions_for(&text), None);
77        let cleared = EditMessage::new().mentions(Mentions::default());
78        assert_eq!(cleared.mentions_for(&text), Some(Mentions::default()));
79    }
80
81    #[test]
82    fn an_edits_mentions_are_named_once_and_read_from_the_text_it_leaves() {
83        let o = EditMessage::new()
84            .mention_member(B)
85            .mention_member(B)
86            .mention_role(R)
87            .mentions_from_content();
88        // The message keeps its text: the mentions it shows are read from that.
89        let kept = format!(
90            "{} and {} @everyone",
91            mention::member(A),
92            mention::member(B)
93        );
94        assert_eq!(
95            o.mentions_for(&kept),
96            Some(Mentions {
97                everyone: true,
98                members: vec![B, A],
99                roles: vec![R],
100            })
101        );
102        let everyone = EditMessage::new().mention_everyone().mentions_for("x");
103        assert_eq!(everyone.map(|m| m.everyone), Some(true));
104        // From the text alone, with nothing named: an empty set, which clears them.
105        let none = EditMessage::new()
106            .mentions_from_content()
107            .mentions_for("plain");
108        assert_eq!(none, Some(Mentions::default()));
109    }
110}