From 5ed2f0f2c315c2ff378dfc29376cb0dd67dfd870 Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Thu, 24 Aug 2023 21:05:39 +0200 Subject: [PATCH] add get guild preview route --- src/api/guilds/guilds.rs | 29 +++++++++++++++- src/lib.rs | 9 +++++ src/types/entities/sticker.rs | 64 ++++++++++++++++++++++++++++++++++- src/types/schema/guild.rs | 20 +++++++++-- 4 files changed, 118 insertions(+), 4 deletions(-) diff --git a/src/api/guilds/guilds.rs b/src/api/guilds/guilds.rs index 85a18b7..389c08d 100644 --- a/src/api/guilds/guilds.rs +++ b/src/api/guilds/guilds.rs @@ -8,7 +8,8 @@ use crate::errors::ChorusResult; use crate::instance::UserMeta; use crate::ratelimiter::ChorusRequest; use crate::types::{ - Channel, ChannelCreateSchema, Guild, GuildBanCreateSchema, GuildCreateSchema, GuildModifySchema, + Channel, ChannelCreateSchema, Guild, GuildBanCreateSchema, GuildCreateSchema, + GuildModifySchema, GuildPreview, MFALevel, }; use crate::types::{GuildBan, Snowflake}; @@ -179,12 +180,38 @@ impl Guild { guild_id, )) .header("Authorization", user.token()) + .header("Content-Type", "application/json") .body(to_string(&schema).unwrap()), limit_type: LimitType::Guild(guild_id), }; let response = chorus_request.deserialize_response::(user).await?; Ok(response) } + + /// Returns a guild preview object for the given guild ID. If the user is not in the guild, the guild must be discoverable. + /// # Reference: + /// + /// See + pub async fn get_preview( + guild_id: Snowflake, + user: &mut UserMeta, + ) -> ChorusResult { + let chorus_request = ChorusRequest { + request: Client::new() + .patch(format!( + "{}/guilds/{}/preview", + user.belongs_to.borrow().urls.api, + guild_id, + )) + .header("Authorization", user.token()) + .header("Content-Type", "application/json"), + limit_type: LimitType::Guild(guild_id), + }; + let response = chorus_request + .deserialize_response::(user) + .await?; + Ok(response) + } } impl Channel { diff --git a/src/lib.rs b/src/lib.rs index 6563bed..7a957dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,6 +74,15 @@ impl UrlBundle { } } +/// Unwraps an `Option`. Returns an empty string if the String is `None`, or the String contents +/// if it is `Some`. +pub(crate) fn unwrap_empty_if_none(string: Option) -> String { + match string { + Some(str) => str, + None => "".to_string(), + } +} + #[cfg(test)] mod lib { use super::*; diff --git a/src/types/entities/sticker.rs b/src/types/entities/sticker.rs index 5413112..c2cdb46 100644 --- a/src/types/entities/sticker.rs +++ b/src/types/entities/sticker.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use crate::types::{entities::User, utils::Snowflake}; -#[derive(Debug, Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))] /// Represents a sticker that can be sent in messages. /// @@ -28,6 +28,68 @@ pub struct Sticker { pub sort_value: Option, } +impl PartialEq for Sticker { + fn eq(&self, other: &Self) -> bool { + self.id == other.id + && self.pack_id == other.pack_id + && self.name == other.name + && self.description == other.description + && self.tags == other.tags + && self.asset == other.asset + && self.sticker_type == other.sticker_type + && self.format_type == other.format_type + && self.available == other.available + && self.guild_id == other.guild_id + && self.sort_value == other.sort_value + } +} + +impl PartialOrd for Sticker { + fn partial_cmp(&self, other: &Self) -> Option { + match self.id.partial_cmp(&other.id) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + match self.pack_id.partial_cmp(&other.pack_id) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + match self.name.partial_cmp(&other.name) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + match self.description.partial_cmp(&other.description) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + match self.tags.partial_cmp(&other.tags) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + match self.asset.partial_cmp(&other.asset) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + match self.sticker_type.partial_cmp(&other.sticker_type) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + match self.format_type.partial_cmp(&other.format_type) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + match self.available.partial_cmp(&other.available) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + match self.guild_id.partial_cmp(&other.guild_id) { + Some(core::cmp::Ordering::Equal) => {} + ord => return ord, + } + self.sort_value.partial_cmp(&other.sort_value) + } +} + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] /// A partial sticker object. /// diff --git a/src/types/schema/guild.rs b/src/types/schema/guild.rs index 75452df..2622796 100644 --- a/src/types/schema/guild.rs +++ b/src/types/schema/guild.rs @@ -5,8 +5,8 @@ use serde::{Deserialize, Serialize}; use crate::types::entities::Channel; use crate::types::types::guild_configuration::GuildFeatures; use crate::types::{ - ExplicitContentFilterLevel, MessageNotificationLevel, Snowflake, SystemChannelFlags, - VerificationLevel, + Emoji, ExplicitContentFilterLevel, MessageNotificationLevel, Snowflake, Sticker, + SystemChannelFlags, VerificationLevel, }; #[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] @@ -77,3 +77,19 @@ impl std::default::Default for GetUserGuildSchema { } } } + +#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq, PartialOrd)] +pub struct GuildPreview { + pub id: Snowflake, + pub name: String, + pub icon: Option, + pub description: Option, + pub splash: Option, + pub discovery_splash: Option, + pub home_header: Option, + pub features: Vec, + pub emojis: Vec, + pub stickers: Vec, + pub approximate_member_count: u32, + pub approximate_presence_count: u32, +}