Merge pull request #106 from polyphony-chat/feature/relationships

Feature/relationships
This commit is contained in:
Flori 2023-06-14 22:55:25 +02:00 committed by GitHub
commit f95641a0fa
6 changed files with 46 additions and 3 deletions

View File

@ -25,7 +25,7 @@ impl Message {
message: &mut MessageSendSchema,
files: Option<Vec<PartialDiscordFileAttachment>>,
) -> Result<Message, crate::errors::ChorusLibError> {
let belongs_to = user.belongs_to.borrow_mut();
let belongs_to = user.belongs_to.borrow();
let url_api = belongs_to.urls.get_api().to_string();
drop(belongs_to);

View File

@ -1,3 +1,5 @@
pub use relationships::*;
pub use users::*;
pub mod relationships;
pub mod users;

View File

@ -0,0 +1,33 @@
use reqwest::Client;
use crate::{api::deserialize_response, errors::ChorusLibError, instance::UserMeta, types};
impl UserMeta {
/// Retrieves the mutual relationships between the authenticated user and the specified user.
///
/// # Arguments
///
/// * `user_id` - A string slice that holds the ID of the user to retrieve the mutual relationships with.
///
/// # Returns
/// This function returns a [`Option<Vec<Result<PublicUser, ChorusLibError>>>`].
pub async fn get_mutual_relationships(
&mut self,
user_id: &str,
) -> Result<Option<Vec<types::PublicUser>>, ChorusLibError> {
let belongs_to = self.belongs_to.borrow();
let url = format!(
"{}/users/{}/relationships/",
belongs_to.urls.get_api(),
user_id
);
drop(belongs_to);
let request = Client::new().get(url).bearer_auth(self.token());
deserialize_response::<Option<Vec<types::PublicUser>>>(
request,
self,
crate::api::limits::LimitType::Global,
)
.await
}
}

View File

@ -1,3 +1,4 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
@ -6,22 +7,26 @@ use crate::types::Snowflake;
use super::PublicUser;
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
/// See https://docs.spacebar.chat/routes/#get-/users/@me/relationships/
/// See https://discord-userdoccers.vercel.app/resources/user#relationship-structure
pub struct Relationship {
pub id: Snowflake,
#[serde(rename = "type")]
pub relationship_type: RelationshipType,
pub nickname: Option<String>,
pub user: PublicUser,
pub since: Option<DateTime<Utc>>,
}
#[derive(Serialize_repr, Deserialize_repr, Debug, Clone, Default)]
#[repr(u8)]
/// See https://github.com/spacebarchat/server/blob/60394d8c43904ff17935d6edbbfb09ecd479570a/src/util/entities/Relationship.ts#L30
/// See https://discord-userdoccers.vercel.app/resources/user#relationship-type
pub enum RelationshipType {
Suggestion = 6,
Implicit = 5,
Outgoing = 4,
Incoming = 3,
Blocked = 2,
#[default]
Friends = 1,
None = 0,
}

View File

@ -3,6 +3,7 @@ pub use auth::*;
pub use channel::*;
pub use guild::*;
pub use message::*;
pub use relationship::*;
pub use role::*;
pub use user::*;
@ -11,6 +12,7 @@ mod auth;
mod channel;
mod guild;
mod message;
mod relationship;
mod role;
mod user;

View File

@ -0,0 +1 @@