From e06bc147b44e8aaa6b53c37729fbbe3aa547fc9b Mon Sep 17 00:00:00 2001 From: kozabrada123 Date: Tue, 13 Aug 2024 09:21:59 +0200 Subject: [PATCH] feat: add affinities --- src/api/users/users.rs | 51 ++++++++++++++++-- src/types/entities/user.rs | 27 +++++++++- src/types/schema/user.rs | 107 ++++++++++++++++++++++--------------- 3 files changed, 136 insertions(+), 49 deletions(-) diff --git a/src/api/users/users.rs b/src/api/users/users.rs index 9064efd..759b2d4 100644 --- a/src/api/users/users.rs +++ b/src/api/users/users.rs @@ -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 + pub async fn get_user_affinities(&mut self) -> ChorusResult { + 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 + pub async fn get_guild_affinities(&mut self) -> ChorusResult { + 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 { diff --git a/src/types/entities/user.rs b/src/types/entities/user.rs index 1ba1efe..2c8cd5b 100644 --- a/src/types/entities/user.rs +++ b/src/types/entities/user.rs @@ -304,10 +304,9 @@ pub struct UserProfile { pub mutual_friends_count: Option, - pub connected_accounts: Vec, + pub connected_accounts: Vec, // TODO: Add application role connections! - /// The type of premium (Nitro) a user has pub premium_type: Option, /// 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 +#[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 +#[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, +} diff --git a/src/types/schema/user.rs b/src/types/schema/user.rs index 808b96c..b2ea6fc 100644 --- a/src/types/schema/user.rs +++ b/src/types/schema/user.rs @@ -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 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, - pub insecure: Option, - pub friend_sync: Option, - /// 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> + /// 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, + pub insecure: Option, + pub friend_sync: Option, + /// 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>, } #[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq, Eq)] @@ -322,10 +324,10 @@ pub struct CreateConnectionCallbackSchema { /// /// See pub struct CreateContactSyncConnectionSchema { - /// The username of the connection account - pub name: String, - /// Whether to sync friends over the connection - pub friend_sync: Option, + /// The username of the connection account + pub name: String, + /// Whether to sync friends over the connection + pub friend_sync: Option, } #[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq, Eq)] @@ -335,36 +337,36 @@ pub struct CreateContactSyncConnectionSchema { /// /// See 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, + /// 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, - /// 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, + /// 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, - /// 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, + /// 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, - /// 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, + /// 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, - /// Whether to show the connection on the user's profile - #[serde(skip_serializing_if = "Option::is_none")] - pub visibility: Option, + /// Whether to show the connection on the user's profile + #[serde(skip_serializing_if = "Option::is_none")] + pub visibility: Option, } #[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 +pub struct UserAffinities { + pub user_affinities: Vec, + // FIXME: Is this also a UserAffinity vec? + // Also, no idea what this means + pub inverse_user_affinities: Vec, +} + +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +/// Return type for the [crate::instance::ChorusUser::get_guild_affinities] endpoint. +/// +/// See +pub struct GuildAffinities { + pub guild_affinities: Vec, +}