From b4a8082f29c3b054bd8f4302a336db59fc1c810a Mon Sep 17 00:00:00 2001 From: Quat3rnion <81202811+Quat3rnion@users.noreply.github.com> Date: Wed, 19 Jun 2024 13:59:39 -0400 Subject: [PATCH] Update and add some types in support of the backend (#507) --- src/types/entities/channel.rs | 29 ++++++++++++++++++++++------- src/types/entities/role.rs | 2 +- src/types/entities/webhook.rs | 16 +++++++++++++--- src/types/schema/channel.rs | 12 ++++++++++++ tests/channels.rs | 14 ++++---------- 5 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/types/entities/channel.rs b/src/types/entities/channel.rs index 782b6a6..c1219ad 100644 --- a/src/types/entities/channel.rs +++ b/src/types/entities/channel.rs @@ -8,8 +8,8 @@ use serde_aux::prelude::deserialize_string_from_number; use serde_repr::{Deserialize_repr, Serialize_repr}; use std::fmt::Debug; -use crate::types::Shared; use crate::types::{ + PermissionFlags, Shared, entities::{GuildMember, User}, utils::Snowflake, }; @@ -148,14 +148,20 @@ pub struct Tag { pub struct PermissionOverwrite { pub id: Snowflake, #[serde(rename = "type")] - #[serde(deserialize_with = "deserialize_string_from_number")] - pub overwrite_type: String, + pub overwrite_type: PermissionOverwriteType, #[serde(default)] - #[serde(deserialize_with = "deserialize_string_from_number")] - pub allow: String, + pub allow: PermissionFlags, #[serde(default)] - #[serde(deserialize_with = "deserialize_string_from_number")] - pub deny: String, + pub deny: PermissionFlags, +} + + +#[derive(Debug, Serialize_repr, Deserialize_repr, Clone, PartialEq, Eq, PartialOrd)] +#[repr(u8)] +/// # Reference +pub enum PermissionOverwriteType { + Role = 0, + Member = 1, } #[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)] @@ -260,3 +266,12 @@ pub enum ChannelType { // TODO: Couldn't find reference Unhandled = 255, } + + +/// # Reference +/// See +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)] +pub struct FollowedChannel { + pub channel_id: Snowflake, + pub webhook_id: Snowflake +} \ No newline at end of file diff --git a/src/types/entities/role.rs b/src/types/entities/role.rs index 8a2c513..7e804f5 100644 --- a/src/types/entities/role.rs +++ b/src/types/entities/role.rs @@ -71,7 +71,7 @@ pub struct RoleTags { } bitflags! { - #[derive(Debug, Default, Clone, Hash, PartialEq, Eq, chorus_macros::SerdeBitFlags)] + #[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, chorus_macros::SerdeBitFlags)] #[cfg_attr(feature = "sqlx", derive(chorus_macros::SqlxBitFlags))] /// Permissions limit what users of certain roles can do on a Guild to Guild basis. /// diff --git a/src/types/entities/webhook.rs b/src/types/entities/webhook.rs index 3fcc43b..aea9f41 100644 --- a/src/types/entities/webhook.rs +++ b/src/types/entities/webhook.rs @@ -32,13 +32,13 @@ use crate::types::{ pub struct Webhook { pub id: Snowflake, #[serde(rename = "type")] - pub webhook_type: i32, + pub webhook_type: WebhookType, pub name: String, pub avatar: String, pub token: String, - pub guild_id: Snowflake, + pub guild_id: Snowflake, pub channel_id: Snowflake, - pub application_id: Snowflake, + pub application_id: Option, #[serde(skip_serializing_if = "Option::is_none")] #[cfg_attr(feature = "sqlx", sqlx(skip))] pub user: Option>, @@ -48,3 +48,13 @@ pub struct Webhook { #[serde(skip_serializing_if = "Option::is_none")] pub url: Option, } + +#[derive(Serialize, Deserialize, Debug, Default, Clone, Copy)] +#[repr(u8)] +#[cfg_attr(feature = "sqlx", derive(sqlx::Type))] +pub enum WebhookType { + #[default] + Incoming = 1, + ChannelFollower = 2, + Application = 3, +} \ No newline at end of file diff --git a/src/types/schema/channel.rs b/src/types/schema/channel.rs index 9ae64c6..33df3ff 100644 --- a/src/types/schema/channel.rs +++ b/src/types/schema/channel.rs @@ -177,3 +177,15 @@ pub struct ModifyChannelPositionsSchema { pub lock_permissions: Option, pub parent_id: Option, } + +/// See +#[derive(Debug, Deserialize, Serialize, Clone, Default, PartialOrd, Ord, PartialEq, Eq)] +pub struct AddFollowingChannelSchema { + pub webhook_channel_id: Snowflake, +} + +#[derive(Debug, Deserialize, Serialize, Clone, Default, PartialOrd, Ord, PartialEq, Eq)] +pub struct CreateWebhookSchema { + pub name: String, + pub avatar: Option, +} \ No newline at end of file diff --git a/tests/channels.rs b/tests/channels.rs index 14359d2..e00744a 100644 --- a/tests/channels.rs +++ b/tests/channels.rs @@ -2,10 +2,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. -use chorus::types::{ - self, Channel, GetChannelMessagesSchema, MessageSendSchema, PermissionFlags, - PermissionOverwrite, PrivateChannelCreateSchema, RelationshipType, Snowflake, -}; +use chorus::types::{self, Channel, GetChannelMessagesSchema, MessageSendSchema, PermissionFlags, PermissionOverwrite, PermissionOverwriteType, PrivateChannelCreateSchema, RelationshipType, Snowflake}; mod common; @@ -69,16 +66,13 @@ async fn modify_channel() { .unwrap(); assert_eq!(modified_channel.name, Some(CHANNEL_NAME.to_string())); - let permission_override = PermissionFlags::from_vec(Vec::from([ - PermissionFlags::MANAGE_CHANNELS, - PermissionFlags::MANAGE_MESSAGES, - ])); + let permission_override = PermissionFlags::MANAGE_CHANNELS | PermissionFlags::MANAGE_MESSAGES; let user_id: types::Snowflake = bundle.user.object.read().unwrap().id; let permission_override = PermissionOverwrite { id: user_id, - overwrite_type: "1".to_string(), + overwrite_type: PermissionOverwriteType::Member, allow: permission_override, - deny: "0".to_string(), + deny: PermissionFlags::empty(), }; let channel_id: Snowflake = bundle.channel.read().unwrap().id; Channel::modify_permissions(