djs_cache/permissions.rs
1//! Members' permissions computed from the cache (D-292, D-294, D-295).
2
3use djs_model::{
4 channel::Channel,
5 id::{GuildId, UserId},
6 permissions::Permissions,
7 util::Timestamp,
8};
9
10use crate::{Backend, Cache, CachedMember};
11
12/// Permissions computed from the cached guild, roles, member and channel (D-292).
13impl<B: Backend> Cache<B> {
14 /// The permissions of the member `user_id` in the guild `guild_id`, as Discord applies them
15 /// now: [`Permissions::guild_level`] from the cached guild owner, `@everyone` role and the
16 /// member's roles, then [`Permissions::during_timeout`] if the member is timed out (D-294).
17 ///
18 /// `None` when the cache lacks something the computation needs: the guild, the member, or
19 /// one of the member's roles (so enable [`Resource::Member`](crate::Resource::Member),
20 /// which is off by default, and keep [`Resource::Role`](crate::Resource::Role)). A
21 /// missing piece never gives a wrong answer.
22 ///
23 /// discord.js counterpart: `GuildMember#permissions`.
24 ///
25 /// # Examples
26 ///
27 /// ```
28 /// use djs_cache::Cache;
29 /// use djs_model::{id::{GuildId, UserId}, permissions::Permissions};
30 ///
31 /// fn can_ban(cache: &Cache, guild: GuildId, user: UserId) -> bool {
32 /// cache
33 /// .member_permissions(guild, user)
34 /// .is_some_and(|permissions| permissions.contains(Permissions::BAN_MEMBERS))
35 /// }
36 /// # assert!(!can_ban(&Cache::new(), GuildId::new(1), UserId::new(2)));
37 /// ```
38 #[must_use]
39 pub fn member_permissions(&self, guild_id: GuildId, user_id: UserId) -> Option<Permissions> {
40 let member = self.members(guild_id).get(user_id)?;
41 let base = self.guild_level(guild_id, &member)?;
42 Some(apply_timeout(base, &member))
43 }
44
45 /// The permissions of the member `user_id` in `channel` (a guild channel or a thread,
46 /// cached or not), as Discord applies them now: [`member_permissions`](Self::member_permissions)
47 /// before the timeout, then [`Permissions::in_channel`] with the channel's overwrites and
48 /// the implicit denials (D-295), then the timeout (D-294). A thread takes the overwrites of
49 /// its parent channel, which must be cached.
50 ///
51 /// `None` for a channel outside a guild, or when the cache lacks something the computation
52 /// needs (see [`member_permissions`](Self::member_permissions); for a thread, its parent).
53 ///
54 /// discord.js counterpart: `GuildMember#permissionsIn(channel)` /
55 /// `GuildChannel#permissionsFor(member)`.
56 #[must_use]
57 pub fn member_permissions_in(&self, channel: &Channel, user_id: UserId) -> Option<Permissions> {
58 let guild_id = channel.guild_id()?;
59 let member = self.members(guild_id).get(user_id)?;
60 let base = self.guild_level(guild_id, &member)?;
61 let in_channel = if channel.kind().is_thread() {
62 let parent = self.channels().get(channel.parent_id()?)?;
63 base.in_channel(
64 guild_id,
65 user_id,
66 &member.roles,
67 parent.permission_overwrites(),
68 )
69 } else {
70 base.in_channel(
71 guild_id,
72 user_id,
73 &member.roles,
74 channel.permission_overwrites(),
75 )
76 };
77 Some(apply_timeout(in_channel, &member))
78 }
79
80 /// [`Permissions::guild_level`] of `member` from the cached guild and roles.
81 fn guild_level(&self, guild_id: GuildId, member: &CachedMember) -> Option<Permissions> {
82 let guild = self.guilds().get(guild_id)?;
83 if guild.owner_id == member.user.id {
84 return Some(Permissions::all());
85 }
86 let roles = self.roles(guild_id);
87 // `@everyone` has the guild's ID.
88 let everyone = roles.get(guild_id.cast())?.permissions;
89 let mut granted = Permissions::empty();
90 for &role_id in &*member.roles {
91 granted |= roles.get(role_id)?.permissions;
92 }
93 Some(Permissions::guild_level(false, everyone, [granted]))
94 }
95}
96
97/// Applies [`Permissions::during_timeout`] if `member`'s timeout has not ended yet.
98fn apply_timeout(permissions: Permissions, member: &CachedMember) -> Permissions {
99 match member.communication_disabled_until() {
100 Some(until) if until > Timestamp::now() => permissions.during_timeout(),
101 _ => permissions,
102 }
103}