chorus/types/utils/
rights.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use std::num::ParseIntError;
6use std::str::FromStr;
7use bitflags::bitflags;
8use serde::{Deserialize, Deserializer, Serialize, Serializer};
9use crate::types::UserFlags;
10
11bitflags! {
12    /// Rights are instance-wide, per-user permissions for everything you may perform on the instance,
13    /// such as sending messages, editing messages, or shutting down the server.
14    /// They are separate from guild member permissions, which only apply to a given guild.
15    ///
16    /// # Notes
17    /// The default rights on Discord.com are 648540060672 ([source](https://github.com/spacebarchat/server/issues/878#issuecomment-1234669715))
18    ///
19    /// # Reference
20    /// See <https://docs.spacebar.chat/setup/server/security/rights/>
21    #[derive(Debug, Clone, Copy, Eq, PartialEq, chorus_macros::SerdeBitFlags)]
22    #[cfg_attr(feature = "sqlx", derive(chorus_macros::SqlxBitFlags))]
23    pub struct Rights: u64 {
24        /// All rights
25        const OPERATOR = 1 << 0;
26        /// Ability to alter or remove others' applications
27        const MANAGE_APPLICATIONS = 1 << 1;
28        /// Same as the per-guild [MANAGE_GUILD] permission, but applies to all guilds and DM channels, can join any guild without invite
29        const MANAGE_GUILDS = 1 << 2;
30        /// Can delete or edit any message they can read
31        const MANAGE_MESSAGES = 1 << 3;
32        /// Can add, change, define rate limits of other users,
33        /// can also grant others [BYPASS_RATE_LIMITS] when combined
34        /// with [BYPASS_RATE_LIMITS] and [MANAGE_USERS].
35        const MANAGE_RATE_LIMITS = 1 << 4;
36        /// Can create, alter, enable and disable custom message routing rules in any channel/guild
37        const MANAGE_ROUTING = 1 << 5;
38        /// Respond to or resolve other users' support tickets
39        const MANAGE_TICKETS = 1 << 6;
40        /// Can create, alter, remove and ban users; can also create, modify and remove user groups
41        const MANAGE_USERS = 1 << 7;
42        /// Can manually add members into their guilds and group DMs
43        const ADD_MEMBERS = 1 << 8;
44        /// Makes the user exempt from all rate limits
45        const BYPASS_RATE_LIMITS = 1 << 9;
46        /// Can create, edit and remove own applications
47        const CREATE_APPLICATIONS = 1 << 10;
48        /// Can create guild channels and custom channels
49        const CREATE_CHANNELS = 1 << 11;
50        /// Can create 1:1 DMs
51        ///
52        /// # Notes
53        /// A user without [SEND_MESSAGES] cannot be added to a DM
54        const CREATE_DMS = 1 << 12;
55        /// Can create group DMs
56        ///
57        /// # Notes
58        /// A user without [SEND_MESSAGES] cannot be added to a DM
59        const CREATE_DM_GROUPS = 1 << 13;
60        /// Can create guilds
61        const CREATE_GUILDS = 1 << 14;
62        /// Can create mass invites in guilds where they have [CREATE_INSTANT_INVITE]
63        const CREATE_INVITES = 1 << 15;
64        /// Can create roles and per-guild or per-channel permission
65        /// overrides in the guilds that they have permissions
66        const CREATE_ROLES = 1 << 16;
67        /// Can create templates for guilds, custom channels and channels with custom routing
68        const CREATE_TEMPLATES = 1 << 17;
69        /// Can create webhooks in the guilds that they have permissions
70        const CREATE_WEBHOOKS = 1 << 18;
71        /// Can join guilds by using invites or vanity names
72        const JOIN_GUILDS = 1 << 19;
73        /// Can modify the pinned messages in the guilds that they have permission
74        const PIN_MESSAGES = 1 << 20;
75        /// Can react to messages, subject to permissions
76        const SELF_ADD_REACTIONS = 1 << 21;
77        /// Can delete own messages
78        const SELF_DELETE_MESSAGES = 1 << 22;
79        /// Can edit own messages
80        const SELF_EDIT_MESSAGES = 1 << 23;
81        /// Can edit own username, nickname and avatar
82        const SELF_EDIT_NAME = 1 << 24;
83        /// Can send messages in the channels that they have permissions
84        const SEND_MESSAGES = 1 << 25;
85        /// Can use voice activities, such as watch together or whiteboard
86        const USE_ACTIVITIES = 1 << 26;
87        /// Can use video and screenshare in guilds/channels that they have permissions
88        const USE_VIDEO = 1 << 27;
89        /// Can use voice in guilds/channels that they have permissions
90        const USE_VOICE = 1 << 28;
91        /// Can create user-specific invites in guilds that they have the [`INVITE_USERS`] right in.
92        const INVITE_USERS = 1 << 29;
93        /// Can delete/disable own account
94        const SELF_DELETE_DISABLE = 1 << 30;
95        /// Can use pay-to-use features once paid
96        const DEBTABLE = 1 << 31;
97        /// Can earn money using monetization features in guilds that have [`MonetizationEnabled`](crate::types::types::guild_configuration::GuildFeatures::MonetizationEnabled)
98        const CREDITABLE = 1 << 32;
99        /// Can kick or ban guild or group DM members in the guilds/groups where they have [`KICK_MEMBERS`](crate::types::PermissionFlags::KICK_MEMBERS) or [`BAN_MEMBERS`](crate::types::PermissionFlags::BAN_MEMBERS)
100        const KICK_BAN_MEMBERS = 1 << 33;
101        /// Can leave the guilds or group DMs that they joined on their own (one can always leave a guild or group DMs where they have been force-added)
102        const SELF_LEAVE_GROUPS = 1 << 34;
103        /// Inverts the presence confidentiality default ([`OPERATOR`]'s presence is not routed by default, others' are) for a given user
104        const PRESENCE = 1 << 35;
105        /// Can mark discoverable guilds where they have permissions to mark as discoverable
106        const SELF_ADD_DISCOVERABLE = 1 << 36;
107        /// Can change anything in the primary guild directory
108        const MANAGE_GUILD_DIRECTORY = 1 << 37;
109        /// Can send confetti, screenshake and use the random user mention (@someone)
110        const POGGERS = 1 << 38;
111        /// Can use achievements and cheers
112        const USE_ACHIEVEMENTS = 1 << 39;
113        /// Can initiate interactions
114        const INITIATE_INTERACTIONS = 1 << 40;
115        /// Can respond to interactions
116        const RESPOND_TO_INTERACTIONS = 1 << 41;
117        /// Can send backdated events
118        const SEND_BACKDATED_EVENTS = 1 << 42;
119        /// Can accept mass (guild) invites
120        const USE_MASS_INVITES = 1 << 43;
121        /// Can accept user-specific invites and DM requests
122        const ACCEPT_INVITES = 1 << 44;
123        /// Can modify own flags
124        const SELF_EDIT_FLAGS = 1 << 45;
125        /// Can modify other's flags
126        const EDIT_FLAGS = 1 << 46;
127        /// Can manage other's groups
128        const MANAGE_GROUPS = 1 << 47;
129        /// Can view server stats at /api/policies/stats
130        const VIEW_SERVER_STATS = 1 << 48;
131        /// Can resend verification emails using /auth/verify/resend
132        const RESEND_VERIFICATION_EMAIL = 1 << 49;
133    }
134}
135
136impl Rights {
137    pub fn any(&self, permission: Rights, check_operator: bool) -> bool {
138        (check_operator && self.contains(Rights::OPERATOR)) || self.contains(permission)
139    }
140
141    /// Returns whether or not the Rights object has specific rights
142    pub fn has(&self, permission: Rights, check_operator: bool) -> bool {
143        (check_operator && self.contains(Rights::OPERATOR)) || self.contains(permission)
144    }
145
146    /// Returns whether or not the Rights object has specific rights.
147    ///
148    /// # Notes
149    /// Unlike has, this returns an Error if we are missing rights
150    /// and Ok(true) otherwise
151    pub fn has_throw(&self, permission: Rights) -> Result<bool, &'static str> {
152        if self.has(permission, true) {
153            Ok(true)
154        } else {
155            Err("You are missing the following rights")
156        }
157    }
158}
159
160impl Default for Rights {
161    fn default() -> Self {
162        Self::empty()
163    }
164}
165
166#[allow(dead_code)] // FIXME: Remove this when we  use this
167fn all_rights() -> Rights {
168    Rights::OPERATOR
169        | Rights::MANAGE_APPLICATIONS
170        | Rights::MANAGE_GUILDS
171        | Rights::MANAGE_MESSAGES
172        | Rights::MANAGE_RATE_LIMITS
173        | Rights::MANAGE_ROUTING
174        | Rights::MANAGE_TICKETS
175        | Rights::MANAGE_USERS
176        | Rights::ADD_MEMBERS
177        | Rights::BYPASS_RATE_LIMITS
178        | Rights::CREATE_APPLICATIONS
179        | Rights::CREATE_CHANNELS
180        | Rights::CREATE_DMS
181        | Rights::CREATE_DM_GROUPS
182        | Rights::CREATE_GUILDS
183        | Rights::CREATE_INVITES
184        | Rights::CREATE_ROLES
185        | Rights::CREATE_TEMPLATES
186        | Rights::CREATE_WEBHOOKS
187        | Rights::JOIN_GUILDS
188        | Rights::PIN_MESSAGES
189        | Rights::SELF_ADD_REACTIONS
190        | Rights::SELF_DELETE_MESSAGES
191        | Rights::SELF_EDIT_MESSAGES
192        | Rights::SELF_EDIT_NAME
193        | Rights::SEND_MESSAGES
194        | Rights::USE_ACTIVITIES
195        | Rights::USE_VIDEO
196        | Rights::USE_VOICE
197        | Rights::INVITE_USERS
198        | Rights::SELF_DELETE_DISABLE
199        | Rights::DEBTABLE
200        | Rights::CREDITABLE
201        | Rights::KICK_BAN_MEMBERS
202        | Rights::SELF_LEAVE_GROUPS
203        | Rights::PRESENCE
204        | Rights::SELF_ADD_DISCOVERABLE
205        | Rights::MANAGE_GUILD_DIRECTORY
206        | Rights::POGGERS
207        | Rights::USE_ACHIEVEMENTS
208        | Rights::INITIATE_INTERACTIONS
209        | Rights::RESPOND_TO_INTERACTIONS
210        | Rights::SEND_BACKDATED_EVENTS
211        | Rights::USE_MASS_INVITES
212        | Rights::ACCEPT_INVITES
213        | Rights::SELF_EDIT_FLAGS
214        | Rights::EDIT_FLAGS
215        | Rights::MANAGE_GROUPS
216        | Rights::VIEW_SERVER_STATS
217        | Rights::RESEND_VERIFICATION_EMAIL
218}