Start development sprint 3.

This commit is contained in:
Paul Makles
2021-01-18 13:41:48 +00:00
parent a9f258de6b
commit af8731ac38
8 changed files with 75 additions and 45 deletions

2
Cargo.lock generated
View File

@@ -2273,7 +2273,7 @@ dependencies = [
[[package]]
name = "revolt"
version = "0.3.0-alpha"
version = "0.3.1"
dependencies = [
"async-std",
"async-tungstenite",

View File

@@ -1,6 +1,6 @@
[package]
name = "revolt"
version = "0.3.0-alpha"
version = "0.3.1"
authors = ["Paul Makles <paulmakles@gmail.com>"]
edition = "2018"

View File

@@ -1,39 +1,7 @@
use crate::{
database::get_collection,
util::result::{Error, Result},
};
use crate::database::*;
use mongodb::bson::to_document;
use serde::{Deserialize, Serialize};
/*#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LastMessage {
id: String,
user_id: String,
short_content: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Channel {
#[serde(rename = "_id")]
pub id: String,
#[serde(rename = "type")]
pub channel_type: u8,
// DM: whether the DM is active
pub active: Option<bool>,
// DM + GDM: last message in channel
pub last_message: Option<LastMessage>,
// DM + GDM: recipients for channel
pub recipients: Option<Vec<String>>,
// GDM: owner of group
pub owner: Option<String>,
// GUILD: channel parent
pub guild: Option<String>,
// GUILD + GDM: channel name
pub name: Option<String>,
// GUILD + GDM: channel description
pub description: Option<String>,
}*/
use crate::util::result::{Error, Result};
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")]

View File

@@ -1,11 +1,11 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use mongodb::bson::{doc, from_bson, Bson};
use validator::Validate;
use rocket::http::RawStr;
use rocket::request::FromParam;
use serde::{Deserialize, Serialize};
use validator::Validate;
use mongodb::bson::{doc, from_bson, Bson};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
#[derive(Validate, Serialize, Deserialize)]
pub struct Ref {
@@ -18,8 +18,8 @@ impl Ref {
Ok(Ref { id })
}
pub async fn fetch_user(&self) -> Result<User> {
let doc = get_collection("users")
pub async fn fetch<T: DeserializeOwned>(&self, collection: &'static str) -> Result<T> {
let doc = get_collection(&collection)
.find_one(
doc! {
"_id": &self.id
@@ -29,17 +29,25 @@ impl Ref {
.await
.map_err(|_| Error::DatabaseError {
operation: "find_one",
with: "user",
with: &collection,
})?
.ok_or_else(|| Error::UnknownUser)?;
Ok(
from_bson(Bson::Document(doc)).map_err(|_| Error::DatabaseError {
from_bson::<T>(Bson::Document(doc)).map_err(|_| Error::DatabaseError {
operation: "from_bson",
with: "user",
with: &collection,
})?,
)
}
pub async fn fetch_user(&self) -> Result<User> {
self.fetch("users").await
}
pub async fn fetch_channel(&self) -> Result<Channel> {
self.fetch("channels").await
}
}
impl User {

View File

@@ -0,0 +1,33 @@
use crate::database::*;
use num_enum::TryFromPrimitive;
use std::ops;
#[derive(Debug, PartialEq, Eq, TryFromPrimitive, Copy, Clone)]
#[repr(u32)]
pub enum ChannelPermission {
View = 1,
SendMessage = 2,
}
bitfield! {
pub struct ChannelPermissions(MSB0 [u32]);
u32;
pub get_view, _: 31;
pub get_send_message, _: 30;
}
impl_op_ex!(+ |a: &ChannelPermission, b: &ChannelPermission| -> u32 { *a as u32 | *b as u32 });
impl_op_ex_commutative!(+ |a: &u32, b: &ChannelPermission| -> u32 { *a | *b as u32 });
pub async fn calculate(user: &User, target: &Channel) -> ChannelPermissions<[u32; 1]> {
match target {
Channel::SavedMessages { user: owner, .. } => {
if &user.id == owner {
ChannelPermissions([ ChannelPermission::View + ChannelPermission::SendMessage ])
} else {
ChannelPermissions([ 0 ])
}
}
_ => unreachable!()
}
}

View File

@@ -1,3 +1,4 @@
pub mod user;
pub mod channel;
pub use user::get_relationship;

View File

@@ -0,0 +1,16 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use rocket_contrib::json::JsonValue;
#[get("/<target>")]
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
let target = target.fetch_channel().await?;
let perm = permissions::channel::calculate(&user, &target).await;
if !perm.get_view() {
Err(Error::LabelMe)?
}
Ok(json!(target))
}

View File

@@ -1,5 +1,9 @@
use rocket::Route;
mod fetch_channel;
pub fn routes() -> Vec<Route> {
routes![]
routes![
fetch_channel::req
]
}