From 92e768d5a6be161022f66055166338cac7f738a7 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 4 Jun 2023 14:20:06 +0200 Subject: [PATCH 01/13] Change file structure --- src/api/channels/reactions.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/api/channels/reactions.rs b/src/api/channels/reactions.rs index 0fc265b..0dd2a57 100644 --- a/src/api/channels/reactions.rs +++ b/src/api/channels/reactions.rs @@ -7,12 +7,11 @@ use crate::{ }; /** -Extends the [`types::Reaction`] struct with useful metadata. +Useful metadata for working with [`types::Reaction`], bundled together nicely. */ pub struct ReactionMeta { pub message_id: types::Snowflake, pub channel_id: types::Snowflake, - pub reaction: types::Reaction, } impl ReactionMeta { @@ -131,9 +130,7 @@ impl ReactionMeta { ) .await } -} -impl types::Reaction { /** Create a reaction for the message. @@ -156,8 +153,7 @@ impl types::Reaction { See [https://discord.com/developers/docs/resources/channel#create-reaction](https://discord.com/developers/docs/resources/channel#create-reaction) */ pub async fn create( - channel_id: &Snowflake, - message_id: &Snowflake, + &self, emoji: &str, user: &mut UserMeta, ) -> Result { @@ -165,8 +161,8 @@ impl types::Reaction { let url = format!( "{}/channels/{}/messages/{}/reactions/{}/@me/", belongs_to.urls.get_api(), - channel_id, - message_id, + self.channel_id, + self.message_id, emoji ); let request = Client::new().put(url).bearer_auth(user.token()); From 359bfc7b8b538e4928ce6ac1f3568026287017b4 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 4 Jun 2023 14:29:43 +0200 Subject: [PATCH 02/13] Add delete_user method --- src/api/channels/reactions.rs | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/api/channels/reactions.rs b/src/api/channels/reactions.rs index 0dd2a57..c043f22 100644 --- a/src/api/channels/reactions.rs +++ b/src/api/channels/reactions.rs @@ -176,4 +176,51 @@ impl ReactionMeta { ) .await } + + /** + Delete a user's reaction to a message. + + This endpoint requires the MANAGE_MESSAGES permission to be present on the current user. + + # Arguments + * `user_id` - A string slice containing the ID of the user whose reaction is to be deleted. + * `emoji` - A string slice containing the emoji to delete. The `emoji` must be URL Encoded or + the request will fail with 10014: Unknown Emoji. To use custom emoji, you must encode it in the + format name:id with the emoji name and emoji id. + * `user` - A mutable reference to a [`UserMeta`] instance. + + # Returns + A `Result` containing a [`reqwest::Response`] or a [`crate::errors::InstanceServerError`]. + Returns a 204 empty response on success. + Fires a Message Reaction Remove Gateway event. + + # Reference + See [https://discord.com/developers/docs/resources/channel#delete-own-reaction](https://discord.com/developers/docs/resources/channel#delete-own-reaction) + */ + pub async fn delete_user( + &self, + user_id: &str, + emoji: &str, + user: &mut UserMeta, + ) -> Result { + let mut belongs_to = user.belongs_to.borrow_mut(); + let url = format!( + "{}/channels/{}/messages/{}/reactions/{}/{}", + belongs_to.urls.get_api(), + self.channel_id, + self.message_id, + emoji, + user_id + ); + let request = Client::new().delete(url).bearer_auth(user.token()); + LimitedRequester::new() + .await + .send_request( + request, + crate::api::limits::LimitType::Channel, + &mut belongs_to.limits, + &mut user.limits, + ) + .await + } } From 7a811e17921ef31970c4d02d79fccad93ffafc36 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 4 Jun 2023 14:32:57 +0200 Subject: [PATCH 03/13] improve existing documentation --- src/api/channels/reactions.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/api/channels/reactions.rs b/src/api/channels/reactions.rs index c043f22..52a0134 100644 --- a/src/api/channels/reactions.rs +++ b/src/api/channels/reactions.rs @@ -16,16 +16,19 @@ pub struct ReactionMeta { impl ReactionMeta { /** - Deletes all reactions for a message. - # Arguments - * `user` - A mutable reference to a [`UserMeta`] instance. + Deletes all reactions for a message. + This endpoint requires the `MANAGE_MESSAGES` permission to be present on the current user. - # Returns - A `Result` containing a [`reqwest::Response`] or a [`crate::errors::InstanceServerError`]. + # Arguments + * `user` - A mutable reference to a [`UserMeta`] instance. + + # Returns + A `Result` containing a [`reqwest::Response`] or a [`crate::errors::InstanceServerError`]. + Fires a `Message Reaction Remove All` Gateway event. # Reference See [https://discord.com/developers/docs/resources/channel#delete-all-reactions](https://discord.com/developers/docs/resources/channel#delete-all-reactions) - */ + */ pub async fn delete_all( &self, user: &mut UserMeta, @@ -91,8 +94,7 @@ impl ReactionMeta { /** Deletes all the reactions for a given `emoji` on a message. This endpoint requires the - MANAGE_MESSAGES permission to be present on the current user. Fires a `Message Reaction - Remove Emoji` Gateway event. + MANAGE_MESSAGES permission to be present on the current user. # Arguments * `emoji` - A string slice containing the emoji to delete. The `emoji` must be URL Encoded or @@ -102,6 +104,7 @@ impl ReactionMeta { # Returns A `Result` containing a [`reqwest::Response`] or a [`crate::errors::InstanceServerError`]. + Fires a `Message Reaction Remove Emoji` Gateway event. # Reference See [https://discord.com/developers/docs/resources/channel#delete-all-reactions-for-emoji](https://discord.com/developers/docs/resources/channel#delete-all-reactions-for-emoji) @@ -137,8 +140,7 @@ impl ReactionMeta { This endpoint requires the READ_MESSAGE_HISTORY permission to be present on the current user. Additionally, if nobody else has reacted to the message using this emoji, this endpoint requires the ADD_REACTIONS permission to be present on the current - user. Fires a Message Reaction Add Gateway event. - + user. # Arguments * `emoji` - A string slice containing the emoji to delete. The `emoji` must be URL Encoded or the request will fail with 10014: Unknown Emoji. To use custom emoji, you must encode it in the @@ -148,6 +150,7 @@ impl ReactionMeta { # Returns A `Result` containing a [`reqwest::Response`] or a [`crate::errors::InstanceServerError`]. Returns a 204 empty response on success. + Fires a Message Reaction Add Gateway event. # Reference See [https://discord.com/developers/docs/resources/channel#create-reaction](https://discord.com/developers/docs/resources/channel#create-reaction) From 0a106d2e1d453190878b095d5a9386cc5342a13c Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 4 Jun 2023 14:39:22 +0200 Subject: [PATCH 04/13] Add method to remove own reaction --- src/api/channels/reactions.rs | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/api/channels/reactions.rs b/src/api/channels/reactions.rs index 52a0134..f95db3a 100644 --- a/src/api/channels/reactions.rs +++ b/src/api/channels/reactions.rs @@ -180,6 +180,45 @@ impl ReactionMeta { .await } + /** + Delete a reaction the current user has made for the message. + + # Arguments + * `emoji` - A string slice containing the emoji to delete. The `emoji` must be URL Encoded or + the request will fail with 10014: Unknown Emoji. To use custom emoji, you must encode it in the + format name:id with the emoji name and emoji id. + * `user` - A mutable reference to a [`UserMeta`] instance. + + # Returns + A `Result` containing a [`reqwest::Response`] or a [`crate::errors::InstanceServerError`]. + Returns a 204 empty response on success. + Fires a `Message Reaction Remove` Gateway event. + */ + pub async fn remove( + &self, + emoji: &str, + user: &mut UserMeta, + ) -> Result { + let mut belongs_to = user.belongs_to.borrow_mut(); + let url = format!( + "{}/channels/{}/messages/{}/reactions/{}/@me/", + belongs_to.urls.get_api(), + self.channel_id, + self.message_id, + emoji + ); + let request = Client::new().delete(url).bearer_auth(user.token()); + LimitedRequester::new() + .await + .send_request( + request, + crate::api::limits::LimitType::Channel, + &mut belongs_to.limits, + &mut user.limits, + ) + .await + } + /** Delete a user's reaction to a message. From 3692f15f0ffa3b519ac7423d2e2b09c14428a739 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 4 Jun 2023 14:41:24 +0200 Subject: [PATCH 05/13] Add reference to docs --- src/api/channels/reactions.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/api/channels/reactions.rs b/src/api/channels/reactions.rs index f95db3a..0ca8e26 100644 --- a/src/api/channels/reactions.rs +++ b/src/api/channels/reactions.rs @@ -193,6 +193,9 @@ impl ReactionMeta { A `Result` containing a [`reqwest::Response`] or a [`crate::errors::InstanceServerError`]. Returns a 204 empty response on success. Fires a `Message Reaction Remove` Gateway event. + + # Reference + See [https://discord.com/developers/docs/resources/channel#delete-own-reaction](https://discord.com/developers/docs/resources/channel#delete-own-reaction) */ pub async fn remove( &self, From e6ed4257dad85b2a9469322e8106296934ecce5f Mon Sep 17 00:00:00 2001 From: Flori <39242991+bitfl0wer@users.noreply.github.com> Date: Sun, 4 Jun 2023 14:48:39 +0200 Subject: [PATCH 06/13] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 83cebca..3ceb6ae 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ - [x] [Channel management (name, description, icon, etc.)](https://github.com/polyphony-chat/chorus/issues/48) - [ ] Deleting messages - [ ] Message threads -- [ ] [Reactions](https://github.com/polyphony-chat/chorus/issues/85) +- [x] [Reactions](https://github.com/polyphony-chat/chorus/issues/85) - [ ] Message Search - [ ] Message history From dcc83d7622c2e30359b62ece671f8165ae351aa7 Mon Sep 17 00:00:00 2001 From: Flori <39242991+bitfl0wer@users.noreply.github.com> Date: Sun, 4 Jun 2023 19:31:39 +0200 Subject: [PATCH 07/13] Update README.md --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3ceb6ae..471ed1c 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ - [x] Channel creation - [x] Channel deletion - [x] [Channel management (name, description, icon, etc.)](https://github.com/polyphony-chat/chorus/issues/48) +- [ ] [Join and Leave Guilds](https://github.com/polyphony-chat/chorus/issues/45) +- [ ] [Start DMs](https://github.com/polyphony-chat/chorus/issues/45) - [ ] Deleting messages - [ ] Message threads - [x] [Reactions](https://github.com/polyphony-chat/chorus/issues/85) @@ -70,8 +72,8 @@ - [ ] Video chat support ### Permissions and Roles -- [ ] Role management (creation, deletion, modification) -- [ ] Permission management (assigning and revoking permissions) +- [ ] [Role management](https://github.com/polyphony-chat/chorus/issues/46) (creation, deletion, modification) +- [ ] [Permission management](https://github.com/polyphony-chat/chorus/issues/46) (assigning and revoking permissions) - [ ] Channel-specific permissions - [ ] Role-based access control From 9282c89096e12aba94d95b871125a2bcc8684643 Mon Sep 17 00:00:00 2001 From: Flori <39242991+bitfl0wer@users.noreply.github.com> Date: Sun, 4 Jun 2023 19:33:46 +0200 Subject: [PATCH 08/13] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 471ed1c..6fa0256 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ ### Permissions and Roles - [ ] [Role management](https://github.com/polyphony-chat/chorus/issues/46) (creation, deletion, modification) - [ ] [Permission management](https://github.com/polyphony-chat/chorus/issues/46) (assigning and revoking permissions) -- [ ] Channel-specific permissions +- [ ] [Channel-specific permissions](https://github.com/polyphony-chat/chorus/issues/88) - [ ] Role-based access control ### Server Management From 495ea30a657469aa0cf9a9f6c74afc9dd32d1e96 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Sun, 4 Jun 2023 19:49:04 +0200 Subject: [PATCH 09/13] Re-order README, link issues --- README.md | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 6fa0256..d0ab1f4 100644 --- a/README.md +++ b/README.md @@ -45,31 +45,32 @@ - [x] [Channel management (name, description, icon, etc.)](https://github.com/polyphony-chat/chorus/issues/48) - [ ] [Join and Leave Guilds](https://github.com/polyphony-chat/chorus/issues/45) - [ ] [Start DMs](https://github.com/polyphony-chat/chorus/issues/45) -- [ ] Deleting messages -- [ ] Message threads +- [ ] [Group DM creation, deletion and member management](https://github.com/polyphony-chat/chorus/issues/89) +- [ ] [Deleting messages](https://github.com/polyphony-chat/chorus/issues/91) +- [ ] [Message threads](https://github.com/polyphony-chat/chorus/issues/90) - [x] [Reactions](https://github.com/polyphony-chat/chorus/issues/85) - [ ] Message Search - [ ] Message history +- [ ] Emoji +- [ ] Stickers +- [ ] [Forum channels](https://github.com/polyphony-chat/chorus/issues/90) ### User Management -- [ ] User profile customization +- [ ] [User profile customization](https://github.com/polyphony-chat/chorus/issues/41) - [x] Gettings users and user profiles -- [ ] Friend requests -- [ ] Blocking users +- [ ] [Friend requests](https://github.com/polyphony-chat/chorus/issues/92) +- [ ] [Blocking users](https://github.com/polyphony-chat/chorus/issues/92) - [ ] User presence (online, offline, idle, etc.) - [ ] User status (custom status, etc.) - [x] Account deletion ### Additional Features -- [ ] Emoji -- [ ] Stickers -- [ ] Forum channels - [ ] Server discovery - [ ] Server templates ### Voice and Video -- [ ] Voice chat support -- [ ] Video chat support +- [ ] [Voice chat support](https://github.com/polyphony-chat/chorus/issues/49) +- [ ] [Video chat support](https://github.com/polyphony-chat/chorus/issues/49) ### Permissions and Roles - [ ] [Role management](https://github.com/polyphony-chat/chorus/issues/46) (creation, deletion, modification) @@ -77,12 +78,11 @@ - [ ] [Channel-specific permissions](https://github.com/polyphony-chat/chorus/issues/88) - [ ] Role-based access control -### Server Management -- [x] Server creation -- [x] Server deletion -- [ ] Server settings management (name, description, icon, etc.) -- [ ] Member management (adding, removing, banning, unbanning) -- [ ] Server invites +### Guild Management +- [x] Guild creation +- [x] Guild deletion +- [ ] [Guild settings (name, description, icon, etc.)](https://github.com/polyphony-chat/chorus/issues/43) +- [ ] Guild invites ### Moderation - [ ] Channel moderation (slow mode, etc.) From f74dfbe1acf106d56916c918d7ea3350f7256c9e Mon Sep 17 00:00:00 2001 From: Flori <39242991+bitfl0wer@users.noreply.github.com> Date: Sun, 4 Jun 2023 22:17:56 +0200 Subject: [PATCH 10/13] Create SECURITY.md --- SECURITY.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 SECURITY.md diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..e2bf737 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,8 @@ +# Security Policy + +## Reporting a Vulnerability + +If you find a security vulnerability, please report it in one of the following ways: +- contact a code owner directly on our Discord server +- open an Issue on GitHub +- Reach out to us via E-Mail: lignite-due-00@icloud.com From e07387fc77216af1bbfaeea69a48c389db1d53d1 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 6 Jun 2023 20:05:34 +0200 Subject: [PATCH 11/13] Add PermissionFlags --- src/types/entities/role.rs | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/types/entities/role.rs b/src/types/entities/role.rs index f7e0164..587ee1f 100644 --- a/src/types/entities/role.rs +++ b/src/types/entities/role.rs @@ -50,3 +50,52 @@ pub struct RoleTags { // available_for_purchase: bool, // guild_connections: bool, } + +#[derive(Debug)] +enum PermissionFlags { + CreateInstantInvite = 0x0000000000000001, + KickMembers = 0x0000000000000002, + BanMembers = 0x0000000000000004, + Administrator = 0x0000000000000008, + ManageChannels = 0x0000000000000010, + ManageGuild = 0x0000000000000020, + AddReactions = 0x0000000000000040, + ViewAuditLog = 0x0000000000000080, + PrioritySpeaker = 0x0000000000000100, + Stream = 0x0000000000000200, + ViewChannel = 0x0000000000000400, + SendMessages = 0x0000000000000800, + SendTtsMessages = 0x0000000000001000, + ManageMessages = 0x0000000000002000, + EmbedLinks = 0x0000000000004000, + AttachFiles = 0x0000000000008000, + ReadMessageHistory = 0x0000000000010000, + MentionEveryone = 0x0000000000020000, + UseExternalEmojis = 0x0000000000040000, + ViewGuildInsights = 0x0000000000080000, + Connect = 0x0000000000100000, + Speak = 0x0000000000200000, + MuteMembers = 0x0000000000400000, + DeafenMembers = 0x0000000000800000, + MoveMembers = 0x0000000001000000, + UseVad = 0x0000000002000000, + ChangeNickname = 0x0000000004000000, + ManageNicknames = 0x0000000008000000, + ManageRoles = 0x0000000010000000, + ManageWebhooks = 0x0000000020000000, + ManageGuildExpressions = 0x0000000040000000, + UseApplicationCommands = 0x0000000080000000, + RequestToSpeak = 0x0000000100000000, + ManageEvents = 0x0000000200000000, + ManageThreads = 0x0000000400000000, + CreatePublicThreads = 0x0000000800000000, + CreatePrivateThreads = 0x0000001000000000, + UseExternalStickers = 0x0000002000000000, + SendMessagesInThreads = 0x0000004000000000, + UseEmbeddedActivities = 0x0000008000000000, + ModerateMembers = 0x0000010000000000, + ViewCreatorMonetizationAnalytics = 0x0000020000000000, + UseSoundboard = 0x0000040000000000, + UseExternalSounds = 0x0000200000000000, + SendVoiceMessages = 0x0000400000000000, +} From 3cd633f7adb0d3448c0ed5db80927b9638d5cbfa Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 6 Jun 2023 20:10:50 +0200 Subject: [PATCH 12/13] Make enum pub --- src/types/entities/role.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/entities/role.rs b/src/types/entities/role.rs index 587ee1f..22e9910 100644 --- a/src/types/entities/role.rs +++ b/src/types/entities/role.rs @@ -52,7 +52,7 @@ pub struct RoleTags { } #[derive(Debug)] -enum PermissionFlags { +pub enum PermissionFlags { CreateInstantInvite = 0x0000000000000001, KickMembers = 0x0000000000000002, BanMembers = 0x0000000000000004, From e65aa964f0560895980f856ef9c3a09768788e0a Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Tue, 6 Jun 2023 21:38:28 +0200 Subject: [PATCH 13/13] Fix enum, add has_permission() for RoleObject --- src/types/entities/role.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/types/entities/role.rs b/src/types/entities/role.rs index 22e9910..6293698 100644 --- a/src/types/entities/role.rs +++ b/src/types/entities/role.rs @@ -52,6 +52,7 @@ pub struct RoleTags { } #[derive(Debug)] +#[repr(u64)] pub enum PermissionFlags { CreateInstantInvite = 0x0000000000000001, KickMembers = 0x0000000000000002, @@ -99,3 +100,28 @@ pub enum PermissionFlags { UseExternalSounds = 0x0000200000000000, SendVoiceMessages = 0x0000400000000000, } + +impl RoleObject { + /// Checks if the role has a specific permission. + /// + /// # Arguments + /// + /// * `permission` - The permission to check for. + /// + /// # Example + /// + /// ``` + /// use chorus::types; + /// let mut role = types::RoleObject::default(); + /// let permission = types::PermissionFlags::ModerateMembers as u64 | types::PermissionFlags::UseSoundboard as u64; + /// role.permissions = permission.to_string(); + /// assert_eq!(true, role.has_permission(types::PermissionFlags::ModerateMembers)); + /// assert_eq!(true, role.has_permission(types::PermissionFlags::UseSoundboard)); + /// ``` + pub fn has_permission(&self, permission: PermissionFlags) -> bool { + if self.permissions.parse::().unwrap() & permission as u64 != 0 { + return true; + } + false + } +}