Skip to main content

zaun_client/
perms.rs

1//! Permissions as the cache sees them, through the protocol's own resolver (permissions.md §3).
2//!
3//! The view leaves out what the cache does not hold: an active lockdown, the admission policy's
4//! newcomer restrictions and waiting period, and whether a member other than this client is
5//! the owner. It can therefore show more than the host allows, never less for the owner. Use
6//! it to hide controls, and let the host decide.
7
8use zaun_community_protocol::bits::Permissions;
9use zaun_community_protocol::ids::{ChannelId, MemberId, RoleId};
10use zaun_community_protocol::model::MembershipState;
11use zaun_community_protocol::permissions::{
12    Channel, Context, Override, OverrideTarget, Resolved, Resolver, Role, Subject,
13};
14
15use crate::cache::CommunityData;
16
17fn to_input(o: &zaun_community_protocol::command::object::Override) -> Override {
18    use zaun_community_protocol::command::object::OverrideTarget as Wire;
19    Override {
20        target: match o.target {
21            Wire::Role(id) => OverrideTarget::Role(id),
22            Wire::Member(id) => OverrideTarget::Member(id),
23        },
24        allow: o.allow,
25        deny: o.deny,
26    }
27}
28
29impl CommunityData {
30    /// What `member` may do in `channel` (or community-wide with `None`) at `now_ms`, or `None`
31    /// when the member or the channel is not in the cache.
32    #[must_use]
33    pub fn permissions(
34        &self,
35        member: &MemberId,
36        channel: Option<&ChannelId>,
37        now_ms: u64,
38    ) -> Option<Resolved> {
39        let m = self.members.get(member)?;
40        let roles: Vec<Role> = m
41            .role_ids
42            .iter()
43            .filter_map(|id| self.roles.get(id))
44            .map(|r| Role {
45                id: r.role_id,
46                permissions: r.permissions,
47                position: r.position,
48            })
49            .collect();
50        let everyone = self
51            .roles
52            .get(&RoleId::EVERYONE)
53            .map_or(Permissions::empty(), |r| r.permissions);
54        let subject = Subject {
55            id: *member,
56            is_owner: self.is_owner && *member == self.me,
57            state: m.state.unwrap_or(MembershipState::Active),
58            roles: &roles,
59            timeout_until_ms: m.timeout_until,
60            admitted_at_ms: m.joined_at,
61        };
62        let ctx = Context {
63            now_ms,
64            everyone,
65            newcomer_restrictions: Permissions::empty(),
66            waiting_period_ms: 0,
67            lockdown: None,
68        };
69        let resolver = Resolver::new(subject, &ctx);
70        let Some(id) = channel else {
71            return Some(resolver.community());
72        };
73        let ch = self.channels.get(id)?;
74        let own: Vec<Override> = ch.overrides.iter().map(to_input).collect();
75        let parent: Option<Vec<Override>> = ch
76            .parent_id
77            .and_then(|p| self.channels.get(&p))
78            .map(|p| p.overrides.iter().map(to_input).collect());
79        Some(resolver.channel(&Channel {
80            kind: ch.kind,
81            overrides: &own,
82            parent_overrides: parent.as_deref(),
83            sync_with_parent: ch.sync_with_parent,
84            locked: ch.locked,
85            // DMs live in `dms`, never in `channels`, so a cached channel has no participants.
86            participants: None,
87        }))
88    }
89
90    /// Whether `member` has every bit of `bits` in `channel` at `now_ms` (see
91    /// [`permissions`](Self::permissions)); `false` when the cache does not know.
92    #[must_use]
93    pub fn can(
94        &self,
95        member: &MemberId,
96        channel: Option<&ChannelId>,
97        bits: Permissions,
98        now_ms: u64,
99    ) -> bool {
100        self.permissions(member, channel, now_ms)
101            .is_some_and(|r| r.has(bits))
102    }
103}
104
105impl CommunityData {
106    /// Whether `member` may use bot command `command` in `channel` (§8.14): `VIEW_CHANNEL` and
107    /// `USE_APPLICATION_COMMANDS` there, plus the command's `default_member_permissions`
108    /// (`"0"`: the owner only). As the rest of this view, it can show more than the host allows.
109    #[must_use]
110    pub fn can_use(
111        &self,
112        member: &MemberId,
113        channel: &ChannelId,
114        command: &zaun_community_protocol::command::object::ApplicationCommand,
115        now_ms: u64,
116    ) -> bool {
117        let Some(r) = self.permissions(member, Some(channel), now_ms) else {
118            return false;
119        };
120        let base = Permissions::VIEW_CHANNEL | Permissions::USE_APPLICATION_COMMANDS;
121        let owner = self.is_owner && *member == self.me;
122        match command.default_member_permissions {
123            _ if !r.has(base) => false,
124            None => true,
125            Some(bits) if bits.is_empty() => owner,
126            Some(bits) => owner || r.has(bits),
127        }
128    }
129}