mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 13:36:59 +00:00
Add extra guards, begin working on channels.
This commit is contained in:
@@ -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
58
src/guards/channel.rs
Normal 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
2
src/guards/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod auth;
|
||||
pub mod channel;
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::auth::User;
|
||||
use crate::guards::auth::User;
|
||||
use crate::database;
|
||||
use crate::routes::channel;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user