revolt_permissions/models/
channel.rs

1use once_cell::sync::Lazy;
2use std::{fmt, ops::Add};
3
4/// Abstract channel type
5pub enum ChannelType {
6    SavedMessages,
7    DirectMessage,
8    Group,
9    ServerChannel,
10    Unknown,
11}
12
13/// Permission value on Revolt
14///
15/// This should be restricted to the lower 52 bits to prevent any
16/// potential issues with Javascript. Also leave empty spaces for
17/// future permission flags to be added.
18#[derive(Debug, PartialEq, Eq, Copy, Clone)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
20#[cfg_attr(feature = "try-from-primitive", derive(num_enum::TryFromPrimitive))]
21#[repr(u64)]
22pub enum ChannelPermission {
23    // * Generic permissions
24    /// Manage the channel or channels on the server
25    ManageChannel = 1 << 0,
26    /// Manage the server
27    ManageServer = 1 << 1,
28    /// Manage permissions on servers or channels
29    ManagePermissions = 1 << 2,
30    /// Manage roles on server
31    ManageRole = 1 << 3,
32    /// Manage server customisation (includes emoji)
33    ManageCustomisation = 1 << 4,
34
35    // % 1 bit reserved
36
37    // * Member permissions
38    /// Kick other members below their ranking
39    KickMembers = 1 << 6,
40    /// Ban other members below their ranking
41    BanMembers = 1 << 7,
42    /// Timeout other members below their ranking
43    TimeoutMembers = 1 << 8,
44    /// Assign roles to members below their ranking
45    AssignRoles = 1 << 9,
46    /// Change own nickname
47    ChangeNickname = 1 << 10,
48    /// Change or remove other's nicknames below their ranking
49    ManageNicknames = 1 << 11,
50    /// Change own avatar
51    ChangeAvatar = 1 << 12,
52    /// Remove other's avatars below their ranking
53    RemoveAvatars = 1 << 13,
54
55    // % 7 bits reserved
56
57    // * Channel permissions
58    /// View a channel
59    ViewChannel = 1 << 20,
60    /// Read a channel's past message history
61    ReadMessageHistory = 1 << 21,
62    /// Send a message in a channel
63    SendMessage = 1 << 22,
64    /// Delete messages in a channel
65    ManageMessages = 1 << 23,
66    /// Manage webhook entries on a channel
67    ManageWebhooks = 1 << 24,
68    /// Create invites to this channel
69    InviteOthers = 1 << 25,
70    /// Send embedded content in this channel
71    SendEmbeds = 1 << 26,
72    /// Send attachments and media in this channel
73    UploadFiles = 1 << 27,
74    /// Masquerade messages using custom nickname and avatar
75    Masquerade = 1 << 28,
76    /// React to messages with emojis
77    React = 1 << 29,
78
79    // * Voice permissions
80    /// Connect to a voice channel
81    Connect = 1 << 30,
82    /// Speak in a voice call
83    Speak = 1 << 31,
84    /// Share video in a voice call
85    Video = 1 << 32,
86    /// Mute other members with lower ranking in a voice call
87    MuteMembers = 1 << 33,
88    /// Deafen other members with lower ranking in a voice call
89    DeafenMembers = 1 << 34,
90    /// Move members between voice channels
91    MoveMembers = 1 << 35,
92    /// Listen to other users
93    Listen = 1 << 36,
94
95    // * Channel permissions two electric boogaloo
96    /// Mention everyone and online members
97    MentionEveryone = 1 << 37,
98    /// Mention roles
99    MentionRoles = 1 << 38,
100
101    // * Misc. permissions
102    // % Bits 38 to 52: free area
103    // % Bits 53 to 64: do not use
104
105    // * Grant all permissions
106    /// Safely grant all permissions
107    GrantAllSafe = 0x000F_FFFF_FFFF_FFFF,
108
109    /// Grant all permissions
110    GrantAll = u64::MAX,
111}
112
113impl fmt::Display for ChannelPermission {
114    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
115        fmt::Debug::fmt(self, f)
116    }
117}
118
119impl_op_ex!(+ |a: &ChannelPermission, b: &ChannelPermission| -> u64 { *a as u64 | *b as u64 });
120impl_op_ex_commutative!(+ |a: &u64, b: &ChannelPermission| -> u64 { *a | *b as u64 });
121
122pub static ALLOW_IN_TIMEOUT: Lazy<u64> =
123    Lazy::new(|| ChannelPermission::ViewChannel + ChannelPermission::ReadMessageHistory);
124
125pub static DEFAULT_PERMISSION_VIEW_ONLY: Lazy<u64> =
126    Lazy::new(|| ChannelPermission::ViewChannel + ChannelPermission::ReadMessageHistory);
127
128pub static DEFAULT_PERMISSION: Lazy<u64> = Lazy::new(|| {
129    DEFAULT_PERMISSION_VIEW_ONLY.add(
130        ChannelPermission::SendMessage
131            + ChannelPermission::InviteOthers
132            + ChannelPermission::SendEmbeds
133            + ChannelPermission::UploadFiles
134            + ChannelPermission::Connect
135            + ChannelPermission::Speak
136            + ChannelPermission::Listen
137            + ChannelPermission::Video
138    )
139});
140
141pub static DEFAULT_PERMISSION_SAVED_MESSAGES: u64 = ChannelPermission::GrantAllSafe as u64;
142
143pub static DEFAULT_PERMISSION_DIRECT_MESSAGE: Lazy<u64> = Lazy::new(|| {
144    DEFAULT_PERMISSION.add(ChannelPermission::ManageChannel + ChannelPermission::React)
145});
146
147pub static DEFAULT_PERMISSION_SERVER: Lazy<u64> = Lazy::new(|| {
148    DEFAULT_PERMISSION.add(
149        ChannelPermission::React
150            + ChannelPermission::ChangeNickname
151            + ChannelPermission::ChangeAvatar,
152    )
153});
154
155pub static DEFAULT_WEBHOOK_PERMISSIONS: Lazy<u64> = Lazy::new(|| {
156    ChannelPermission::SendMessage
157        + ChannelPermission::SendEmbeds
158        + ChannelPermission::Masquerade
159        + ChannelPermission::React
160});