feat: add affinities

This commit is contained in:
kozabrada123 2024-08-13 09:21:59 +02:00
parent f966a787d9
commit e06bc147b4
3 changed files with 136 additions and 49 deletions

View File

@ -17,10 +17,11 @@ use crate::{
types::{
AuthorizeConnectionSchema, ConnectionType, CreateUserHarvestSchema,
DeleteDisableUserSchema, GetPomeloEligibilityReturn, GetPomeloSuggestionsReturn,
GetRecentMentionsSchema, GetUserProfileSchema, Harvest, HarvestBackendType, LimitType,
ModifyUserNoteSchema, PublicUser, Snowflake, User, UserModifyProfileSchema,
UserModifySchema, UserNote, UserProfile, UserProfileMetadata, UserSettings,
VerifyUserEmailChangeResponse, VerifyUserEmailChangeSchema,
GetRecentMentionsSchema, GetUserProfileSchema, GuildAffinities, Harvest,
HarvestBackendType, LimitType, ModifyUserNoteSchema, PublicUser, Snowflake, User,
UserAffinities, UserModifyProfileSchema, UserModifySchema, UserNote, UserProfile,
UserProfileMetadata, UserSettings, VerifyUserEmailChangeResponse,
VerifyUserEmailChangeSchema,
},
};
@ -571,6 +572,48 @@ impl ChorusUser {
) -> ChorusResult<()> {
User::set_note(self, target_user_id, note).await
}
/// Fetches the current user's affinity scores for other users.
///
/// (Affinity scores are a measure of how likely a user is to be friends with another user.)
///
/// # Reference
/// See <https://docs.discord.sex/resources/user#get-user-affinities>
pub async fn get_user_affinities(&mut self) -> ChorusResult<UserAffinities> {
let request = Client::new()
.get(format!(
"{}/users/@me/affinities/users",
self.belongs_to.read().unwrap().urls.api,
))
.header("Authorization", self.token());
let chorus_request = ChorusRequest {
request,
limit_type: LimitType::default(),
};
chorus_request.deserialize_response(self).await
}
/// Fetches the current user's affinity scores for their joined guilds.
///
/// # Reference
/// See <https://docs.discord.sex/resources/user#get-guild-affinities>
pub async fn get_guild_affinities(&mut self) -> ChorusResult<GuildAffinities> {
let request = Client::new()
.get(format!(
"{}/users/@me/affinities/guilds",
self.belongs_to.read().unwrap().urls.api,
))
.header("Authorization", self.token());
let chorus_request = ChorusRequest {
request,
limit_type: LimitType::default(),
};
chorus_request.deserialize_response(self).await
}
}
impl User {

View File

@ -304,10 +304,9 @@ pub struct UserProfile {
pub mutual_friends_count: Option<u32>,
pub connected_accounts: Vec<PublicConnection>,
pub connected_accounts: Vec<PublicConnection>,
// TODO: Add application role connections!
/// The type of premium (Nitro) a user has
pub premium_type: Option<PremiumType>,
/// The date the user's premium (Nitro) subscribtion started
@ -786,3 +785,27 @@ pub struct UserNote {
/// The ID of the user who created the note (always the current user)
pub user_id: Snowflake,
}
/// Structure which defines an affinity the local user has with another user.
///
/// # Reference
/// See <https://docs.discord.sex/resources/user#user-affinity-structure>
#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, PartialOrd)]
pub struct UserAffinity {
/// The other user's id
pub user_id: Snowflake,
/// The affinity score
pub affinity: f32,
}
/// Structure which defines an affinity the local user has with a guild.
///
/// # Reference
/// See <https://docs.discord.sex/resources/user#guild-affinity-structure>
#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, PartialOrd)]
pub struct GuildAffinity {
/// The guild's id
pub guild_id: Snowflake,
/// The affinity score
pub affinity: f32,
}

View File

@ -7,7 +7,9 @@ use std::collections::HashMap;
use chrono::NaiveDate;
use serde::{Deserialize, Serialize};
use crate::types::{HarvestBackendType, Snowflake, ThemeColors, TwoWayLinkType};
use crate::types::{
GuildAffinity, HarvestBackendType, Snowflake, ThemeColors, TwoWayLinkType, UserAffinity,
};
#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
@ -303,18 +305,18 @@ pub(crate) struct AuthorizeConnectionReturn {
///
/// See <https://docs.discord.sex/resources/user#create-user-connection-callback>
pub struct CreateConnectionCallbackSchema {
/// The authorization code for the connection
pub code: String,
/// The "state" used to authorize a connection
// TODO: what is this?
pub state: String,
pub two_way_link_code: Option<String>,
pub insecure: Option<bool>,
pub friend_sync: Option<bool>,
/// Additional parameters used for OpenID Connect
// FIXME: Is this correct? in other connections additional info
// is provided like this, only being string - string
pub openid_params: Option<HashMap<String, String>>
/// The authorization code for the connection
pub code: String,
/// The "state" used to authorize a connection
// TODO: what is this?
pub state: String,
pub two_way_link_code: Option<String>,
pub insecure: Option<bool>,
pub friend_sync: Option<bool>,
/// Additional parameters used for OpenID Connect
// FIXME: Is this correct? in other connections additional info
// is provided like this, only being string - string
pub openid_params: Option<HashMap<String, String>>,
}
#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq, Eq)]
@ -322,10 +324,10 @@ pub struct CreateConnectionCallbackSchema {
///
/// See <https://docs.discord.sex/resources/user#create-contact-sync-connection>
pub struct CreateContactSyncConnectionSchema {
/// The username of the connection account
pub name: String,
/// Whether to sync friends over the connection
pub friend_sync: Option<bool>,
/// The username of the connection account
pub name: String,
/// Whether to sync friends over the connection
pub friend_sync: Option<bool>,
}
#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq, Eq)]
@ -335,36 +337,36 @@ pub struct CreateContactSyncConnectionSchema {
///
/// See <https://docs.discord.sex/resources/user#modify-user-connection>
pub struct ModifyConnectionSchema {
/// The connection account's username
///
/// Note: We have not found which connection this could apply to
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
/// The connection account's username
///
/// Note: We have not found which connection this could apply to
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
/// Whether activities related to this connection will be shown in presence
///
/// e.g. on a Spotify connection, "Display Spotify as your status"
#[serde(skip_serializing_if = "Option::is_none")]
pub show_activity: Option<bool>,
/// Whether activities related to this connection will be shown in presence
///
/// e.g. on a Spotify connection, "Display Spotify as your status"
#[serde(skip_serializing_if = "Option::is_none")]
pub show_activity: Option<bool>,
/// Whether or not to sync friends from this connection
///
/// Note: we have not found which connections this can apply to
#[serde(skip_serializing_if = "Option::is_none")]
pub friend_sync: Option<bool>,
/// Whether or not to sync friends from this connection
///
/// Note: we have not found which connections this can apply to
#[serde(skip_serializing_if = "Option::is_none")]
pub friend_sync: Option<bool>,
/// Whether to show additional metadata on the user's profile
///
/// e.g. on a Steam connection, "Display details on profile"
/// (number of games, items, member since)
///
/// on a Twitter connection, number of posts / followers, member since
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata_visibility: Option<bool>,
/// Whether to show additional metadata on the user's profile
///
/// e.g. on a Steam connection, "Display details on profile"
/// (number of games, items, member since)
///
/// on a Twitter connection, number of posts / followers, member since
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata_visibility: Option<bool>,
/// Whether to show the connection on the user's profile
#[serde(skip_serializing_if = "Option::is_none")]
pub visibility: Option<bool>,
/// Whether to show the connection on the user's profile
#[serde(skip_serializing_if = "Option::is_none")]
pub visibility: Option<bool>,
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
@ -374,3 +376,22 @@ pub struct ModifyConnectionSchema {
pub(crate) struct GetConnectionAccessTokenReturn {
pub access_token: String,
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
/// Return type for the [crate::instance::ChorusUser::get_user_affinities] endpoint.
///
/// See <https://docs.discord.sex/resources/user#get-user-affinities>
pub struct UserAffinities {
pub user_affinities: Vec<UserAffinity>,
// FIXME: Is this also a UserAffinity vec?
// Also, no idea what this means
pub inverse_user_affinities: Vec<UserAffinity>,
}
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
/// Return type for the [crate::instance::ChorusUser::get_guild_affinities] endpoint.
///
/// See <https://docs.discord.sex/resources/user#get-guild-affinities>
pub struct GuildAffinities {
pub guild_affinities: Vec<GuildAffinity>,
}