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
93    // * Channel permissions two electric boogaloo
94    /// Mention everyone and online members
95    MentionEveryone = 1 << 37,
96    /// Mention roles
97    MentionRoles = 1 << 38,
98
99    // * Misc. permissions
100    // % Bits 38 to 52: free area
101    // % Bits 53 to 64: do not use
102
103    // * Grant all permissions
104    /// Safely grant all permissions
105    GrantAllSafe = 0x000F_FFFF_FFFF_FFFF,
106
107    /// Grant all permissions
108    GrantAll = u64::MAX,
109}
110
111impl fmt::Display for ChannelPermission {
112    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
113        fmt::Debug::fmt(self, f)
114    }
115}
116
117impl_op_ex!(+ |a: &ChannelPermission, b: &ChannelPermission| -> u64 { *a as u64 | *b as u64 });
118impl_op_ex_commutative!(+ |a: &u64, b: &ChannelPermission| -> u64 { *a | *b as u64 });
119
120pub static ALLOW_IN_TIMEOUT: Lazy<u64> =
121    Lazy::new(|| ChannelPermission::ViewChannel + ChannelPermission::ReadMessageHistory);
122
123pub static DEFAULT_PERMISSION_VIEW_ONLY: Lazy<u64> =
124    Lazy::new(|| ChannelPermission::ViewChannel + ChannelPermission::ReadMessageHistory);
125
126pub static DEFAULT_PERMISSION: Lazy<u64> = Lazy::new(|| {
127    DEFAULT_PERMISSION_VIEW_ONLY.add(
128        ChannelPermission::SendMessage
129            + ChannelPermission::InviteOthers
130            + ChannelPermission::SendEmbeds
131            + ChannelPermission::UploadFiles
132            + ChannelPermission::Connect
133            + ChannelPermission::Speak,
134    )
135});
136
137pub static DEFAULT_PERMISSION_SAVED_MESSAGES: u64 = ChannelPermission::GrantAllSafe as u64;
138
139pub static DEFAULT_PERMISSION_DIRECT_MESSAGE: Lazy<u64> = Lazy::new(|| {
140    DEFAULT_PERMISSION.add(ChannelPermission::ManageChannel + ChannelPermission::React)
141});
142
143pub static DEFAULT_PERMISSION_SERVER: Lazy<u64> = Lazy::new(|| {
144    DEFAULT_PERMISSION.add(
145        ChannelPermission::React
146            + ChannelPermission::ChangeNickname
147            + ChannelPermission::ChangeAvatar,
148    )
149});
150
151pub static DEFAULT_WEBHOOK_PERMISSIONS: Lazy<u64> = Lazy::new(|| {
152    ChannelPermission::SendMessage
153        + ChannelPermission::SendEmbeds
154        + ChannelPermission::Masquerade
155        + ChannelPermission::React
156});