From 6dfb0610ec423cfbbb80363b9abd538a7b262c89 Mon Sep 17 00:00:00 2001 From: Flori Weber Date: Thu, 15 Jun 2023 18:48:01 +0200 Subject: [PATCH 1/3] Implement test_get_mutual_relationships --- tests/relationships.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/relationships.rs diff --git a/tests/relationships.rs b/tests/relationships.rs new file mode 100644 index 0000000..08f7efb --- /dev/null +++ b/tests/relationships.rs @@ -0,0 +1,30 @@ +use chorus::types; + +mod common; + +#[tokio::test] +async fn test_get_mutual_relationships() { + let register_schema = types::RegisterSchema::new( + "integrationtestuser2".to_string(), + None, + true, + None, + None, + None, + Some("2000-01-01".to_string()), + None, + None, + None, + ) + .unwrap(); + + let bundle = common::setup().await; + let mut belongs_to = bundle.instance; + let mut user = bundle.user; + let other_user = belongs_to.register_account(®ister_schema).await.unwrap(); + let relationships = user + .get_mutual_relationships(&other_user.object.id.to_string()) + .await + .unwrap(); + println!("{:?}", relationships.unwrap()); +} From c2d46196c80ad3104c561b436cf9fc32f18bc380 Mon Sep 17 00:00:00 2001 From: Flori Weber Date: Thu, 15 Jun 2023 19:00:47 +0200 Subject: [PATCH 2/3] Implement send_friend_request --- src/api/users/relationships.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/api/users/relationships.rs b/src/api/users/relationships.rs index d286ef6..ede39f0 100644 --- a/src/api/users/relationships.rs +++ b/src/api/users/relationships.rs @@ -1,6 +1,12 @@ use reqwest::Client; +use serde_json::to_string; -use crate::{api::deserialize_response, errors::ChorusLibError, instance::UserMeta, types}; +use crate::{ + api::{deserialize_response, handle_request_as_option}, + errors::ChorusLibError, + instance::UserMeta, + types, +}; impl UserMeta { /// Retrieves the mutual relationships between the authenticated user and the specified user. @@ -30,4 +36,24 @@ impl UserMeta { ) .await } + + /// Sends a friend request to a user. + /// + /// # Arguments + /// + /// * `schema` - A [`FriendRequestSendSchema`] struct that holds the information about the friend request to be sent. + /// + /// # Returns + /// This function returns an [`Option`] that holds a [`ChorusLibError`] if the request fails. + pub async fn send_friend_request( + &mut self, + schema: types::FriendRequestSendSchema, + ) -> Option { + let belongs_to = self.belongs_to.borrow(); + let url = format!("{}/users/@me/relationships/", belongs_to.urls.get_api()); + drop(belongs_to); + let body = to_string(&schema).unwrap(); + let request = Client::new().post(url).bearer_auth(self.token()).body(body); + handle_request_as_option(request, self, crate::api::limits::LimitType::Global).await + } } From 2e6e9ad59d29613a9656464bd5e9799913f7becd Mon Sep 17 00:00:00 2001 From: Flori Weber Date: Thu, 15 Jun 2023 19:01:01 +0200 Subject: [PATCH 3/3] Add FriendRequestSendSchema --- src/types/entities/relationship.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/types/entities/relationship.rs b/src/types/entities/relationship.rs index b965907..fb8acee 100644 --- a/src/types/entities/relationship.rs +++ b/src/types/entities/relationship.rs @@ -30,3 +30,9 @@ pub enum RelationshipType { Friends = 1, None = 0, } + +#[derive(Deserialize, Serialize, Debug)] +pub struct FriendRequestSendSchema { + pub username: String, + pub discriminator: Option, +}