forked from jmug/stoatchat
Re-implement user relationships.
This commit is contained in:
@@ -2,14 +2,26 @@ use mongodb::bson::{doc, from_bson, Bson};
|
||||
use rauth::auth::Session;
|
||||
use rocket::http::Status;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::database::guards::reference::Ref;
|
||||
use crate::database::get_collection;
|
||||
use rocket::request::{self, FromRequest, Outcome, Request};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub enum RelationshipStatus {
|
||||
None,
|
||||
User,
|
||||
Friend,
|
||||
Outgoing,
|
||||
Incoming,
|
||||
Blocked,
|
||||
BlockedOther
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Relationship {
|
||||
#[serde(rename = "_id")]
|
||||
pub id: String,
|
||||
pub status: u8,
|
||||
pub status: RelationshipStatus,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
@@ -47,3 +59,9 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub fn as_ref(&self) -> Ref {
|
||||
Ref { id: self.id.to_string() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,11 +18,11 @@ impl Ref {
|
||||
Ok(Ref { id })
|
||||
}
|
||||
|
||||
pub async fn fetch_user(self) -> Result<User> {
|
||||
pub async fn fetch_user(&self) -> Result<User> {
|
||||
let doc = get_collection("users")
|
||||
.find_one(
|
||||
doc! {
|
||||
"_id": self.id
|
||||
"_id": &self.id
|
||||
},
|
||||
None
|
||||
)
|
||||
|
||||
@@ -30,3 +30,22 @@ pub async fn temp_calc_perm(_user: &User, _target: &User) -> UserPermissions<[u3
|
||||
|
||||
UserPermissions([UserPermission::Access + UserPermission::SendMessage + UserPermission::Invite])
|
||||
}
|
||||
|
||||
use crate::database::entities::RelationshipStatus;
|
||||
use crate::database::guards::reference::Ref;
|
||||
|
||||
pub fn get_relationship(a: &User, b: &Ref) -> RelationshipStatus {
|
||||
if a.id == b.id {
|
||||
return RelationshipStatus::Friend;
|
||||
}
|
||||
|
||||
if let Some(relations) = &a.relations {
|
||||
if let Some(relationship) = relations
|
||||
.iter()
|
||||
.find(|x| x.id == b.id) {
|
||||
return relationship.status.clone();
|
||||
}
|
||||
}
|
||||
|
||||
RelationshipStatus::None
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user