Skip to main content

zaun_client/
message_mentions.rs

1//! Mentions on the objects: who a message names, whether it names a member, and the text that
2//! shows a member or a role.
3
4use zaun_community_protocol::command::object::{Mentions, flags};
5
6use crate::mention;
7use crate::objects::{Member, Message, Role};
8
9impl Message {
10    /// Who it mentions, as the host honoured them: `everyone`, `members`, `roles`. For a
11    /// message this client just posted, the ones it asked for.
12    #[must_use]
13    pub const fn mentions(&self) -> &Mentions {
14        &self.mentions
15    }
16
17    /// Whether it mentions `member`: by id, by one of the member's roles (as the cache has
18    /// them), or by `@everyone`.
19    #[must_use]
20    pub fn mentions_member(&self, member: &Member) -> bool {
21        let m = &self.mentions;
22        m.everyone
23            || m.members.contains(&member.id())
24            || (!m.roles.is_empty() && member.roles().iter().any(|r| m.roles.contains(&r.id())))
25    }
26
27    /// Whether it mentions this client's member (a client notifies its member then, unless
28    /// the message is [`silent`](Self::silent)).
29    #[must_use]
30    pub fn mentions_me(&self) -> bool {
31        self.channel
32            .community
33            .me()
34            .is_some_and(|me| self.mentions_member(&me))
35    }
36
37    /// Sent with `SUPPRESS_NOTIFICATIONS`: its mentions notify nobody.
38    #[must_use]
39    pub const fn silent(&self) -> bool {
40        self.flags & flags::SUPPRESS_NOTIFICATIONS != 0
41    }
42}
43
44impl Member {
45    /// The text that shows a mention of this member, `<@id>`.
46    #[must_use]
47    pub fn mention(&self) -> String {
48        mention::member(self.id())
49    }
50}
51
52impl Role {
53    /// The text that shows a mention of this role, `<@&id>`.
54    #[must_use]
55    pub fn mention(&self) -> String {
56        mention::role(self.id())
57    }
58}