Start work on notifications from client, cargo fmt

This commit is contained in:
Paul Makles
2020-12-30 11:36:32 +00:00
parent af56f5e2d8
commit f39bc07bb9
34 changed files with 341 additions and 264 deletions

View File

@@ -1,6 +1,9 @@
use serde::{Deserialize, Serialize};
use crate::{database::get_collection, util::result::{Error, Result}};
use crate::{
database::get_collection,
util::result::{Error, Result},
};
use mongodb::bson::to_document;
use serde::{Deserialize, Serialize};
/*#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LastMessage {
@@ -38,7 +41,7 @@ pub enum Channel {
SavedMessages {
#[serde(rename = "_id")]
id: String,
user: String
user: String,
},
DirectMessage {
#[serde(rename = "_id")]
@@ -53,20 +56,25 @@ pub enum Channel {
owner: String,
description: String,
recipients: Vec<String>,
}
},
}
impl Channel {
pub async fn save(&self) -> Result<()> {
get_collection("channels")
.insert_one(
to_document(&self)
.map_err(|_| Error::DatabaseError { operation: "to_bson", with: "channel" })?,
None
to_document(&self).map_err(|_| Error::DatabaseError {
operation: "to_bson",
with: "channel",
})?,
None,
)
.await
.map_err(|_| Error::DatabaseError { operation: "insert_one", with: "channel" })?;
.map_err(|_| Error::DatabaseError {
operation: "insert_one",
with: "channel",
})?;
Ok(())
}
}

View File

@@ -1,9 +1,9 @@
mod channel;
mod message;
mod guild;
mod message;
mod user;
pub use channel::*;
pub use message::*;
pub use guild::*;
pub use message::*;
pub use user::*;

View File

@@ -1,10 +1,10 @@
use crate::database::get_collection;
use crate::database::guards::reference::Ref;
use mongodb::bson::{doc, from_bson, Bson};
use rauth::auth::Session;
use rocket::http::Status;
use serde::{Deserialize, Serialize};
use crate::database::guards::reference::Ref;
use crate::database::get_collection;
use rocket::request::{self, FromRequest, Outcome, Request};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum RelationshipStatus {
@@ -14,7 +14,7 @@ pub enum RelationshipStatus {
Outgoing,
Incoming,
Blocked,
BlockedOther
BlockedOther,
}
#[derive(Serialize, Deserialize, Debug)]
@@ -44,24 +44,29 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
.find_one(
doc! {
"_id": &session.user_id
}, None
},
None,
)
.await {
.await
{
if let Some(doc) = result {
Outcome::Success(
from_bson(Bson::Document(doc)).unwrap()
)
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))
Outcome::Failure((
Status::InternalServerError,
rauth::util::Error::DatabaseError,
))
}
}
}
impl User {
pub fn as_ref(&self) -> Ref {
Ref { id: self.id.to_string() }
Ref {
id: self.id.to_string(),
}
}
}

View File

@@ -1,5 +1,5 @@
pub mod user;
pub mod reference;
pub mod user;
/*
// ! FIXME

View File

@@ -1,10 +1,10 @@
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 crate::database::get_collection;
use crate::util::result::{Error, Result};
use mongodb::bson::{doc, from_bson, Bson};
use rocket::http::RawStr;
use rocket::request::FromParam;
use serde::{Deserialize, Serialize};
use validator::Validate;
#[derive(Validate, Serialize, Deserialize)]
@@ -24,15 +24,20 @@ impl Ref {
doc! {
"_id": &self.id
},
None
None,
)
.await
.map_err(|_| Error::DatabaseError { operation: "find_one", with: "user" })?
.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" })?
from_bson(Bson::Document(doc)).map_err(|_| Error::DatabaseError {
operation: "from_bson",
with: "user",
})?,
)
}
}

View File

@@ -0,0 +1 @@

View File

@@ -10,28 +10,28 @@ pub async fn create_database() {
let db = get_db();
db.create_collection("users", None)
.await
.expect("Failed to create users collection.");
.await
.expect("Failed to create users collection.");
db.create_collection("channels", None)
.await
.expect("Failed to create channels collection.");
.await
.expect("Failed to create channels collection.");
db.create_collection("guilds", None)
.await
.expect("Failed to create guilds collection.");
.await
.expect("Failed to create guilds collection.");
db.create_collection("members", None)
.await
.expect("Failed to create members collection.");
.await
.expect("Failed to create members collection.");
db.create_collection("messages", None)
.await
.expect("Failed to create messages collection.");
.await
.expect("Failed to create messages collection.");
db.create_collection("migrations", None)
.await
.expect("Failed to create migrations collection.");
.await
.expect("Failed to create migrations collection.");
db.create_collection(
"pubsub",
@@ -60,21 +60,21 @@ pub async fn create_database() {
}
]
},
None
None,
)
.await
.expect("Failed to create username index.");
db.collection("migrations")
.insert_one(
doc! {
"_id": 0,
"revision": LATEST_REVISION
},
None,
)
.await
.expect("Failed to save migration info.");
.insert_one(
doc! {
"_id": 0,
"revision": LATEST_REVISION
},
None,
)
.await
.expect("Failed to save migration info.");
info!("Created database.");
}

View File

@@ -1,10 +1,10 @@
use super::super::{get_db, get_collection};
use super::super::{get_collection, get_db};
use crate::rocket::futures::StreamExt;
use log::info;
use mongodb::bson::{doc, from_bson, Bson};
use mongodb::options::FindOptions;
use serde::{Deserialize, Serialize};
use crate::rocket::futures::StreamExt;
use mongodb::bson::{doc, from_bson, Bson};
#[derive(Serialize, Deserialize)]
struct MigrationInfo {
@@ -86,7 +86,7 @@ pub async fn run_migrations(revision: i32) -> i32 {
.get_str("_id")
.expect("Failed to get channel id.")
.to_string();
let gid = channel
.get_str("guild")
.expect("Failed to get guild id.")
@@ -124,27 +124,28 @@ pub async fn run_migrations(revision: i32) -> i32 {
if revision <= 2 {
info!("Running migration [revision 2]: Add username index to users.");
get_db().run_command(
doc! {
"createIndexes": "users",
"indexes": [
{
"key": {
"username": 1
},
"name": "username",
"unique": true,
"collation": {
"locale": "en",
"strength": 2
get_db()
.run_command(
doc! {
"createIndexes": "users",
"indexes": [
{
"key": {
"username": 1
},
"name": "username",
"unique": true,
"collation": {
"locale": "en",
"strength": 2
}
}
}
]
},
None
)
.await
.expect("Failed to create username index.");
]
},
None,
)
.await
.expect("Failed to create username index.");
}
// Reminder to update LATEST_REVISION when adding new migrations.

View File

@@ -26,7 +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;
pub mod migrations;
pub mod permissions;

View File

@@ -7,7 +7,7 @@ use std::ops;
pub enum UserPermission {
Access = 1,
SendMessage = 2,
Invite = 4
Invite = 4,
}
bitfield! {
@@ -40,9 +40,7 @@ pub fn get_relationship(a: &User, b: &Ref) -> RelationshipStatus {
}
if let Some(relations) = &a.relations {
if let Some(relationship) = relations
.iter()
.find(|x| x.id == b.id) {
if let Some(relationship) = relations.iter().find(|x| x.id == b.id) {
return relationship.status.clone();
}
}