Add modify_user_relationship

This commit is contained in:
Flori Weber 2023-06-18 23:02:49 +02:00
parent fa524b1791
commit 8c68e8bd77
1 changed files with 59 additions and 2 deletions

View File

@ -1,11 +1,11 @@
use reqwest::Client; use reqwest::{Client, RequestBuilder};
use serde_json::to_string; use serde_json::to_string;
use crate::{ use crate::{
api::{deserialize_response, handle_request_as_option}, api::{deserialize_response, handle_request_as_option},
errors::ChorusLibError, errors::ChorusLibError,
instance::UserMeta, instance::UserMeta,
types, types::{self, CreateUserRelationshipSchema, RelationshipType},
}; };
impl UserMeta { impl UserMeta {
@ -73,4 +73,61 @@ impl UserMeta {
let request = Client::new().post(url).bearer_auth(self.token()).body(body); let request = Client::new().post(url).bearer_auth(self.token()).body(body);
handle_request_as_option(request, self, crate::api::limits::LimitType::Global).await 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<ChorusLibError> {
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,
}
}
} }