forked from jmug/stoatchat
New perm concept, reference, and adding routes.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
/*#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct LastMessage {
|
||||
id: String,
|
||||
user_id: String,
|
||||
@@ -28,4 +28,23 @@ pub struct Channel {
|
||||
pub name: Option<String>,
|
||||
// GUILD + GDM: channel description
|
||||
pub description: Option<String>,
|
||||
}*/
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum Channel {
|
||||
DirectMessage {
|
||||
#[serde(rename = "_id")]
|
||||
id: String,
|
||||
active: bool,
|
||||
recipients: Vec<String>,
|
||||
},
|
||||
Group {
|
||||
#[serde(rename = "_id")]
|
||||
id: String,
|
||||
name: String,
|
||||
owner: String,
|
||||
description: String,
|
||||
recipients: Vec<String>,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
// use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
/*#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct MemberCompositeKey {
|
||||
pub guild: String,
|
||||
pub user: String,
|
||||
@@ -40,4 +40,4 @@ pub struct Guild {
|
||||
pub bans: Vec<Ban>,
|
||||
|
||||
pub default_permissions: u32,
|
||||
}
|
||||
}*/
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use mongodb::bson::DateTime;
|
||||
use serde::{Deserialize, Serialize};
|
||||
// use mongodb::bson::DateTime;
|
||||
// use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
/*#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct PreviousEntry {
|
||||
pub content: String,
|
||||
pub time: DateTime,
|
||||
@@ -19,4 +19,4 @@ pub struct Message {
|
||||
pub edited: Option<DateTime>,
|
||||
|
||||
pub previous_content: Vec<PreviousEntry>,
|
||||
}
|
||||
}*/
|
||||
|
||||
@@ -5,17 +5,18 @@ use serde::{Deserialize, Serialize};
|
||||
use crate::database::get_collection;
|
||||
use rocket::request::{self, FromRequest, Outcome, Request};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Relationship {
|
||||
pub id: String,
|
||||
pub status: u8,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[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>>,
|
||||
}
|
||||
|
||||
@@ -43,35 +44,5 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
|
||||
} else {
|
||||
Outcome::Failure((Status::InternalServerError, rauth::util::Error::DatabaseError))
|
||||
}
|
||||
|
||||
/*Outcome::Success(
|
||||
User {
|
||||
id: "gaming".to_string(),
|
||||
username: None,
|
||||
relations: None
|
||||
}
|
||||
)*/
|
||||
|
||||
/*match (
|
||||
request.managed_state::<Auth>(),
|
||||
header_user_id,
|
||||
header_session_token,
|
||||
) {
|
||||
(Some(auth), Some(user_id), Some(session_token)) => {
|
||||
let session = Session {
|
||||
id: None,
|
||||
user_id,
|
||||
session_token,
|
||||
};
|
||||
|
||||
if let Ok(session) = auth.verify_session(session).await {
|
||||
Outcome::Success(session)
|
||||
} else {
|
||||
Outcome::Failure((Status::Forbidden, Error::InvalidSession))
|
||||
}
|
||||
}
|
||||
(None, _, _) => Outcome::Failure((Status::InternalServerError, Error::InternalError)),
|
||||
(_, _, _) => Outcome::Failure((Status::Forbidden, Error::MissingHeaders)),
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
use mongodb::bson::{doc, from_bson, Bson};
|
||||
use crate::util::result::{Error, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::database::get_collection;
|
||||
use crate::database::entities::*;
|
||||
use rocket::request::FromParam;
|
||||
use rocket::http::RawStr;
|
||||
use validator::Validate;
|
||||
|
||||
#[derive(Validate, Serialize, Deserialize)]
|
||||
pub struct Ref {
|
||||
#[validate(length(min = 26, max = 26))]
|
||||
id: String,
|
||||
}
|
||||
|
||||
impl Ref {
|
||||
pub fn from(id: String) -> Result<Ref> {
|
||||
Ok(Ref { id })
|
||||
}
|
||||
|
||||
pub async fn fetch_user(self) -> Result<User> {
|
||||
let doc = get_collection("users")
|
||||
.find_one(
|
||||
doc! {
|
||||
"_id": self.id
|
||||
},
|
||||
None
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "find_one", with: "user" })?
|
||||
.ok_or_else(|| Error::UnknownUser)?;
|
||||
|
||||
Ok(
|
||||
from_bson(Bson::Document(doc))
|
||||
.map_err(|_| Error::DatabaseError { operation: "from_bson", with: "user" })?
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r> FromParam<'r> for Ref {
|
||||
type Error = &'r RawStr;
|
||||
|
||||
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
|
||||
if let Ok(result) = Ref::from(param.to_string()) {
|
||||
if result.validate().is_ok() {
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
|
||||
Err(param)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ pub fn get_collection(collection: &str) -> Collection {
|
||||
get_db().collection(collection)
|
||||
}
|
||||
|
||||
pub mod permissions;
|
||||
pub mod migrations;
|
||||
pub mod entities;
|
||||
pub mod guards;
|
||||
|
||||
32
src/database/permissions/mod.rs
Normal file
32
src/database/permissions/mod.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use crate::database::entities::User;
|
||||
use num_enum::TryFromPrimitive;
|
||||
use std::ops;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, TryFromPrimitive, Copy, Clone)]
|
||||
#[repr(u32)]
|
||||
pub enum UserPermission {
|
||||
Access = 1,
|
||||
SendMessage = 2,
|
||||
Invite = 4
|
||||
}
|
||||
|
||||
bitfield! {
|
||||
pub struct UserPermissions(MSB0 [u32]);
|
||||
u32;
|
||||
pub get_access, _: 31;
|
||||
pub get_send_message, _: 30;
|
||||
pub get_invite, _: 29;
|
||||
}
|
||||
|
||||
impl_op_ex!(+ |a: &UserPermission, b: &UserPermission| -> u32 { *a + *b });
|
||||
impl_op_ex!(+ |a: &u32, b: &UserPermission| -> u32 { *a + *b });
|
||||
|
||||
pub async fn temp_calc_perm(_user: &User, _target: &User) -> UserPermissions<[u32; 1]> {
|
||||
// if friends; Access + Message + Invite
|
||||
// if mutually know each other:
|
||||
// and has DMs from users enabled -> Access + Message
|
||||
// otherwise -> Access
|
||||
// otherwise; None
|
||||
|
||||
UserPermissions([UserPermission::Access + UserPermission::SendMessage + UserPermission::Invite])
|
||||
}
|
||||
Reference in New Issue
Block a user