mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 05:26:59 +00:00
49 lines
1.4 KiB
Rust
49 lines
1.4 KiB
Rust
use mongodb::bson::{doc, from_bson, Bson};
|
|
use rauth::auth::Session;
|
|
use rocket::http::Status;
|
|
use serde::{Deserialize, Serialize};
|
|
use crate::database::get_collection;
|
|
use rocket::request::{self, FromRequest, Outcome, Request};
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct Relationship {
|
|
pub id: String,
|
|
pub status: u8,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct User {
|
|
#[serde(rename = "_id")]
|
|
pub id: String,
|
|
pub username: String,
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
pub relations: Option<Vec<Relationship>>,
|
|
}
|
|
|
|
#[rocket::async_trait]
|
|
impl<'a, 'r> FromRequest<'a, 'r> for User {
|
|
type Error = rauth::util::Error;
|
|
|
|
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
|
let session: Session = try_outcome!(request.guard::<Session>().await);
|
|
|
|
if let Ok(result) = get_collection("users")
|
|
.find_one(
|
|
doc! {
|
|
"_id": &session.user_id
|
|
}, None
|
|
)
|
|
.await {
|
|
if let Some(doc) = result {
|
|
Outcome::Success(
|
|
from_bson(Bson::Document(doc)).unwrap()
|
|
)
|
|
} else {
|
|
Outcome::Failure((Status::Forbidden, rauth::util::Error::InvalidSession))
|
|
}
|
|
} else {
|
|
Outcome::Failure((Status::InternalServerError, rauth::util::Error::DatabaseError))
|
|
}
|
|
}
|
|
}
|