add get guild preview route

This commit is contained in:
bitfl0wer 2023-08-24 21:05:39 +02:00
parent 895dea3780
commit 5324116b0f
No known key found for this signature in database
GPG Key ID: 0ACD574FCF5226CF
4 changed files with 118 additions and 4 deletions

View File

@ -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::<Guild>(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 <https://discord-userdoccers.vercel.app/resources/guild#get-guild-preview>
pub async fn get_preview(
guild_id: Snowflake,
user: &mut UserMeta,
) -> ChorusResult<GuildPreview> {
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::<GuildPreview>(user)
.await?;
Ok(response)
}
}
impl Channel {

View File

@ -74,6 +74,15 @@ impl UrlBundle {
}
}
/// Unwraps an `Option<String>`. 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>) -> String {
match string {
Some(str) => str,
None => "".to_string(),
}
}
#[cfg(test)]
mod lib {
use super::*;

View File

@ -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<u8>,
}
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<std::cmp::Ordering> {
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.
///

View File

@ -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<String>,
pub description: Option<String>,
pub splash: Option<String>,
pub discovery_splash: Option<String>,
pub home_header: Option<String>,
pub features: Vec<String>,
pub emojis: Vec<Emoji>,
pub stickers: Vec<Sticker>,
pub approximate_member_count: u32,
pub approximate_presence_count: u32,
}