From 636cb4c7518d7d3ac89f37e587327121f74e1e89 Mon Sep 17 00:00:00 2001 From: Flori Weber Date: Sun, 18 Jun 2023 23:02:49 +0200 Subject: [PATCH] Add modify_user_relationship --- src/api/users/relationships.rs | 61 ++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/src/api/users/relationships.rs b/src/api/users/relationships.rs index a678da9..bab9a58 100644 --- a/src/api/users/relationships.rs +++ b/src/api/users/relationships.rs @@ -1,11 +1,11 @@ -use reqwest::Client; +use reqwest::{Client, RequestBuilder}; use serde_json::to_string; use crate::{ api::{deserialize_response, handle_request_as_option}, errors::ChorusLibError, instance::UserMeta, - types, + types::{self, CreateUserRelationshipSchema, RelationshipType}, }; impl UserMeta { @@ -73,4 +73,61 @@ impl UserMeta { let request = Client::new().post(url).bearer_auth(self.token()).body(body); handle_request_as_option(request, self, crate::api::limits::LimitType::Global).await } + + /// Modifies the relationship between the authenticated user and the specified user. + /// + /// # Arguments + /// + /// * `user_id` - A string slice that holds the ID of the user to modify the relationship with. + /// * `relationship_type` - A [`RelationshipType`] enum that specifies the type of relationship to modify. + /// * [`RelationshipType::None`]: Removes the relationship between the two users. + /// * [`RelationshipType::Friends`] | [`RelationshipType::Incoming`] | [`RelationshipType::Outgoing`]: + /// Either accepts an incoming friend request, or sends a new friend request, if there is no + /// incoming friend request from the specified `user_id`. + /// * [`RelationshipType::Blocked`]: Blocks the specified user_id. + /// + /// # Returns + /// This function returns an [`Option`] that holds a [`ChorusLibError`] if the request fails. + pub async fn modify_user_relationship( + &mut self, + user_id: &str, + relationship_type: RelationshipType, + ) -> Option { + let belongs_to = self.belongs_to.borrow(); + let api_url = belongs_to.urls.api.clone(); + drop(belongs_to); + match relationship_type { + RelationshipType::None => { + let request = Client::new() + .delete(format!("{}/users/@me/relationships/{}/", api_url, user_id)) + .bearer_auth(self.token()); + handle_request_as_option(request, self, crate::api::limits::LimitType::Global).await + } + RelationshipType::Friends | RelationshipType::Incoming | RelationshipType::Outgoing => { + let body = CreateUserRelationshipSchema { + relationship_type: None, // Selecting 'None' here will accept an incoming FR or send a new FR. + from_friend_suggestion: None, + friend_token: None, + }; + let request = Client::new() + .put(format!("{}/users/@me/relationships/{}/", api_url, user_id)) + .bearer_auth(self.token()) + .body(to_string(&body).unwrap()); + handle_request_as_option(request, self, crate::api::limits::LimitType::Global).await + } + RelationshipType::Blocked => { + let body = CreateUserRelationshipSchema { + relationship_type: Some(RelationshipType::Blocked), + from_friend_suggestion: None, + friend_token: None, + }; + let request = Client::new() + .put(format!("{}/users/@me/relationships/{}/", api_url, user_id)) + .bearer_auth(self.token()) + .body(to_string(&body).unwrap()); + handle_request_as_option(request, self, crate::api::limits::LimitType::Global).await + } + RelationshipType::Suggestion | RelationshipType::Implicit => None, + } + } }