Add extra guards, begin working on channels.

This commit is contained in:
Paul Makles
2020-01-26 15:20:32 +00:00
parent 0e5211e705
commit 955e482dae
8 changed files with 118 additions and 4 deletions

View File

@@ -33,7 +33,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
let result = col.find_one(doc! { "access_token": key }, None).unwrap();
if let Some(user) = result {
Outcome::Success(User(
Outcome::Success(User (
Ulid::from_string(user.get_str("_id").unwrap()).unwrap(),
user.get_str("username").unwrap().to_string(),
user
@@ -55,7 +55,7 @@ impl<'r> FromParam<'r> for User {
let result = col.find_one(doc! { "_id": param.to_string() }, None).unwrap();
if let Some(user) = result {
Ok(User(
Ok(User (
Ulid::from_string(user.get_str("_id").unwrap()).unwrap(),
user.get_str("username").unwrap().to_string(),
user

58
src/guards/channel.rs Normal file
View File

@@ -0,0 +1,58 @@
use rocket::Outcome;
use rocket::http::{ Status, RawStr };
use rocket::request::{ self, Request, FromRequest, FromParam };
use bson::{ bson, doc, ordered::OrderedDocument };
use std::convert::TryFrom;
use ulid::Ulid;
use crate::database;
use crate::routes::channel::ChannelType;
pub struct Channel (
pub Ulid,
pub ChannelType,
pub OrderedDocument,
);
pub struct Message (
pub Ulid,
pub OrderedDocument,
);
impl<'r> FromParam<'r> for Channel {
type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
let col = database::get_db().collection("channels");
let result = col.find_one(doc! { "_id": param.to_string() }, None).unwrap();
if let Some(channel) = result {
Ok(Channel (
Ulid::from_string(channel.get_str("_id").unwrap()).unwrap(),
ChannelType::try_from(channel.get_i32("username").unwrap() as usize).unwrap(),
channel
))
} else {
Err(param)
}
}
}
impl<'r> FromParam<'r> for Message {
type Error = &'r RawStr;
fn from_param(param: &'r RawStr) -> Result<Self, Self::Error> {
let col = database::get_db().collection("messages");
let result = col.find_one(doc! { "_id": param.to_string() }, None).unwrap();
if let Some(message) = result {
Ok(Message (
Ulid::from_string(message.get_str("_id").unwrap()).unwrap(),
message
))
} else {
Err(param)
}
}
}

2
src/guards/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod auth;
pub mod channel;

View File

@@ -3,9 +3,9 @@
#[macro_use] extern crate rocket_contrib;
pub mod database;
pub mod guards;
pub mod routes;
pub mod email;
pub mod auth;
use dotenv;

View File

@@ -1,3 +1,15 @@
use crate::guards::auth::User;
use crate::database;
use rocket_contrib::json::{ Json, JsonValue };
use serde::{ Serialize, Deserialize };
use mongodb::options::FindOptions;
use num_enum::TryFromPrimitive;
use bson::{ bson, doc };
use ulid::Ulid;
#[derive(Debug, TryFromPrimitive)]
#[repr(usize)]
pub enum ChannelType {
DM = 0,
GROUP_DM = 1,

View File

@@ -1,4 +1,4 @@
use crate::auth::User;
use crate::guards::auth::User;
use crate::database;
use crate::routes::channel;