Start work on notifications from client, cargo fmt
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use rocket::Route;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes! []
|
||||
routes![]
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use rocket::Route;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes! []
|
||||
routes![]
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@ pub use rocket::http::Status;
|
||||
pub use rocket::response::Redirect;
|
||||
use rocket::Rocket;
|
||||
|
||||
mod root;
|
||||
mod users;
|
||||
mod channels;
|
||||
mod guild;
|
||||
mod onboard;
|
||||
mod channels;
|
||||
mod root;
|
||||
mod users;
|
||||
|
||||
pub fn mount(rocket: Rocket) -> Rocket {
|
||||
rocket
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
use mongodb::options::{Collation, FindOneOptions};
|
||||
use crate::util::result::{Error, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::database::entities::User;
|
||||
use crate::database::get_collection;
|
||||
use rocket_contrib::json::Json;
|
||||
use rauth::auth::Session;
|
||||
use validator::Validate;
|
||||
use crate::util::result::{Error, Result};
|
||||
use mongodb::bson::doc;
|
||||
use mongodb::options::{Collation, FindOneOptions};
|
||||
use rauth::auth::Session;
|
||||
use regex::Regex;
|
||||
use rocket_contrib::json::Json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use validator::Validate;
|
||||
|
||||
lazy_static! {
|
||||
static ref RE_USERNAME: Regex = Regex::new(r"^[a-zA-Z0-9-_]+$").unwrap();
|
||||
@@ -16,7 +16,7 @@ lazy_static! {
|
||||
#[derive(Validate, Serialize, Deserialize)]
|
||||
pub struct Data {
|
||||
#[validate(length(min = 2, max = 32), regex = "RE_USERNAME")]
|
||||
username: String
|
||||
username: String,
|
||||
}
|
||||
|
||||
#[post("/complete", data = "<data>")]
|
||||
@@ -24,22 +24,27 @@ pub async fn req(session: Session, user: Option<User>, data: Json<Data>) -> Resu
|
||||
if user.is_some() {
|
||||
Err(Error::AlreadyOnboarded)?
|
||||
}
|
||||
|
||||
|
||||
data.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
let col = get_collection("users");
|
||||
if col.find_one(
|
||||
doc! {
|
||||
"username": &data.username
|
||||
},
|
||||
FindOneOptions::builder()
|
||||
.collation(Collation::builder().locale("en").strength(2).build())
|
||||
.build()
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "find_one", with: "user" })?
|
||||
.is_some() {
|
||||
if col
|
||||
.find_one(
|
||||
doc! {
|
||||
"username": &data.username
|
||||
},
|
||||
FindOneOptions::builder()
|
||||
.collation(Collation::builder().locale("en").strength(2).build())
|
||||
.build(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "find_one",
|
||||
with: "user",
|
||||
})?
|
||||
.is_some()
|
||||
{
|
||||
Err(Error::UsernameTaken)?
|
||||
}
|
||||
|
||||
@@ -48,10 +53,13 @@ pub async fn req(session: Session, user: Option<User>, data: Json<Data>) -> Resu
|
||||
"_id": session.user_id,
|
||||
"username": &data.username
|
||||
},
|
||||
None
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "insert_one", with: "user" })?;
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "insert_one",
|
||||
with: "user",
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use crate::database::entities::User;
|
||||
use rauth::auth::Session;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
#[get("/hello")]
|
||||
pub async fn req(_session: Session, user: Option<User>) -> JsonValue {
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
use rocket::Route;
|
||||
|
||||
mod hello;
|
||||
mod complete;
|
||||
mod hello;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes! [
|
||||
hello::req,
|
||||
complete::req
|
||||
]
|
||||
routes![hello::req, complete::req]
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::util::variables::{DISABLE_REGISTRATION, HCAPTCHA_SITEKEY, USE_EMAIL, USE_HCAPTCHA};
|
||||
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use mongodb::bson::doc;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
#[get("/")]
|
||||
pub async fn root() -> JsonValue {
|
||||
|
||||
@@ -1,8 +1,16 @@
|
||||
use crate::{database::{entities::{User, RelationshipStatus}, get_collection, guards::reference::Ref, permissions::get_relationship}, util::result::Error};
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use crate::util::result::Result;
|
||||
use mongodb::bson::doc;
|
||||
use crate::{
|
||||
database::{
|
||||
entities::{RelationshipStatus, User},
|
||||
get_collection,
|
||||
guards::reference::Ref,
|
||||
permissions::get_relationship,
|
||||
},
|
||||
util::result::Error,
|
||||
};
|
||||
use futures::try_join;
|
||||
use mongodb::bson::doc;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
#[put("/<target>/friend")]
|
||||
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
@@ -42,7 +50,10 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
)
|
||||
) {
|
||||
Ok(_) => Ok(json!({ "status": "Friend" })),
|
||||
Err(_) => Err(Error::DatabaseError { operation: "update_one", with: "user" })
|
||||
Err(_) => Err(Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
with: "user",
|
||||
}),
|
||||
}
|
||||
}
|
||||
RelationshipStatus::None => {
|
||||
@@ -77,7 +88,10 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
)
|
||||
) {
|
||||
Ok(_) => Ok(json!({ "status": "Outgoing" })),
|
||||
Err(_) => Err(Error::DatabaseError { operation: "update_one", with: "user" })
|
||||
Err(_) => Err(Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
with: "user",
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
use crate::{database::entities::RelationshipStatus, database::guards::reference::Ref, database::entities::User, database::permissions::get_relationship, util::result::Error, database::get_collection};
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use crate::util::result::Result;
|
||||
use mongodb::bson::doc;
|
||||
use crate::{
|
||||
database::entities::RelationshipStatus, database::entities::User, database::get_collection,
|
||||
database::guards::reference::Ref, database::permissions::get_relationship, util::result::Error,
|
||||
};
|
||||
use futures::try_join;
|
||||
use mongodb::bson::doc;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
#[put("/<target>/block")]
|
||||
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
let col = get_collection("users");
|
||||
|
||||
match get_relationship(&user, &target) {
|
||||
RelationshipStatus::User |
|
||||
RelationshipStatus::Blocked => Err(Error::NoEffect),
|
||||
RelationshipStatus::User | RelationshipStatus::Blocked => Err(Error::NoEffect),
|
||||
RelationshipStatus::BlockedOther => {
|
||||
col.update_one(
|
||||
doc! {
|
||||
@@ -22,13 +24,16 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
"relations.$.status": "Blocked"
|
||||
}
|
||||
},
|
||||
None
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "user" })?;
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
with: "user",
|
||||
})?;
|
||||
|
||||
Ok(json!({ "status": "Blocked" }))
|
||||
},
|
||||
}
|
||||
RelationshipStatus::None => {
|
||||
match try_join!(
|
||||
col.update_one(
|
||||
@@ -61,12 +66,15 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
)
|
||||
) {
|
||||
Ok(_) => Ok(json!({ "status": "Blocked" })),
|
||||
Err(_) => Err(Error::DatabaseError { operation: "update_one", with: "user" })
|
||||
Err(_) => Err(Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
with: "user",
|
||||
}),
|
||||
}
|
||||
},
|
||||
RelationshipStatus::Friend |
|
||||
RelationshipStatus::Incoming |
|
||||
RelationshipStatus::Outgoing => {
|
||||
}
|
||||
RelationshipStatus::Friend
|
||||
| RelationshipStatus::Incoming
|
||||
| RelationshipStatus::Outgoing => {
|
||||
match try_join!(
|
||||
col.update_one(
|
||||
doc! {
|
||||
@@ -94,7 +102,10 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
)
|
||||
) {
|
||||
Ok(_) => Ok(json!({ "status": "Blocked" })),
|
||||
Err(_) => Err(Error::DatabaseError { operation: "update_one", with: "user" })
|
||||
Err(_) => Err(Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
with: "user",
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use crate::database::entities::{Channel, User};
|
||||
use mongodb::bson::{Bson, doc, from_bson};
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::database::get_collection;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use crate::util::result::{Error, Result};
|
||||
use futures::StreamExt;
|
||||
use mongodb::bson::{doc, from_bson, Bson};
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
#[get("/dms")]
|
||||
pub async fn req(user: User) -> Result<JsonValue> {
|
||||
@@ -21,11 +21,14 @@ pub async fn req(user: User) -> Result<JsonValue> {
|
||||
],
|
||||
"recipients": user.id
|
||||
},
|
||||
None
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "find", with: "channels" })?;
|
||||
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "find",
|
||||
with: "channels",
|
||||
})?;
|
||||
|
||||
let mut channels = vec![];
|
||||
while let Some(result) = cursor.next().await {
|
||||
if let Ok(doc) = result {
|
||||
@@ -33,7 +36,5 @@ pub async fn req(user: User) -> Result<JsonValue> {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(json!(
|
||||
channels
|
||||
))
|
||||
Ok(json!(channels))
|
||||
}
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
use crate::database::{entities::User, guards::reference::Ref, permissions::get_relationship};
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use crate::util::result::Result;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
#[get("/<target>/relationship")]
|
||||
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
Ok(json!({
|
||||
"status": get_relationship(&user, &target)
|
||||
}))
|
||||
Ok(json!({ "status": get_relationship(&user, &target) }))
|
||||
}
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
use crate::database::entities::User;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use crate::util::result::Result;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
#[get("/relationships")]
|
||||
pub async fn req(user: User) -> Result<JsonValue> {
|
||||
Ok(
|
||||
if let Some(vec) = user.relations {
|
||||
json!(vec)
|
||||
} else {
|
||||
json!([])
|
||||
}
|
||||
)
|
||||
Ok(if let Some(vec) = user.relations {
|
||||
json!(vec)
|
||||
} else {
|
||||
json!([])
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::database::entities::User;
|
||||
use crate::database::guards::reference::Ref;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::database::entities::User;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
#[get("/<target>")]
|
||||
@@ -13,7 +13,7 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
if !perm.get_access() {
|
||||
Err(Error::LabelMe)?
|
||||
}
|
||||
|
||||
|
||||
// Only return user relationships if the target is the caller.
|
||||
target.relations = None;
|
||||
}
|
||||
|
||||
@@ -1,24 +1,22 @@
|
||||
use rocket::Route;
|
||||
|
||||
mod fetch_user;
|
||||
mod fetch_dms;
|
||||
mod open_dm;
|
||||
mod fetch_relationships;
|
||||
mod fetch_relationship;
|
||||
mod add_friend;
|
||||
mod remove_friend;
|
||||
mod block_user;
|
||||
mod fetch_dms;
|
||||
mod fetch_relationship;
|
||||
mod fetch_relationships;
|
||||
mod fetch_user;
|
||||
mod open_dm;
|
||||
mod remove_friend;
|
||||
mod unblock_user;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes! [
|
||||
routes![
|
||||
// User Information
|
||||
fetch_user::req,
|
||||
|
||||
// Direct Messaging
|
||||
fetch_dms::req,
|
||||
open_dm::req,
|
||||
|
||||
// Relationships
|
||||
fetch_relationships::req,
|
||||
fetch_relationship::req,
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use crate::database::entities::{Channel, User};
|
||||
use crate::database::get_collection;
|
||||
use crate::database::guards::reference::Ref;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::database::get_collection;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use mongodb::bson::doc;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use ulid::Ulid;
|
||||
|
||||
#[get("/<target>/dm")]
|
||||
@@ -25,25 +25,22 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
let existing_channel = get_collection("channels")
|
||||
.find_one(query, None)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "find_one", with: "channel" })?;
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "find_one",
|
||||
with: "channel",
|
||||
})?;
|
||||
|
||||
if let Some(doc) = existing_channel {
|
||||
Ok(json!(doc))
|
||||
} else {
|
||||
let id = Ulid::new().to_string();
|
||||
let channel = if user.id == target.id {
|
||||
Channel::SavedMessages {
|
||||
id,
|
||||
user: user.id
|
||||
}
|
||||
Channel::SavedMessages { id, user: user.id }
|
||||
} else {
|
||||
Channel::DirectMessage {
|
||||
id,
|
||||
active: false,
|
||||
recipients: vec! [
|
||||
user.id,
|
||||
target.id
|
||||
]
|
||||
recipients: vec![user.id, target.id],
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
use crate::{database::entities::RelationshipStatus, database::guards::reference::Ref, database::entities::User, database::permissions::get_relationship, util::result::Error, database::get_collection};
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use crate::util::result::Result;
|
||||
use mongodb::bson::doc;
|
||||
use crate::{
|
||||
database::entities::RelationshipStatus, database::entities::User, database::get_collection,
|
||||
database::guards::reference::Ref, database::permissions::get_relationship, util::result::Error,
|
||||
};
|
||||
use futures::try_join;
|
||||
use mongodb::bson::doc;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
#[delete("/<target>/friend")]
|
||||
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
let col = get_collection("users");
|
||||
|
||||
match get_relationship(&user, &target) {
|
||||
RelationshipStatus::Blocked |
|
||||
RelationshipStatus::BlockedOther |
|
||||
RelationshipStatus::User |
|
||||
RelationshipStatus::None => Err(Error::NoEffect),
|
||||
RelationshipStatus::Friend |
|
||||
RelationshipStatus::Outgoing |
|
||||
RelationshipStatus::Incoming => {
|
||||
RelationshipStatus::Blocked
|
||||
| RelationshipStatus::BlockedOther
|
||||
| RelationshipStatus::User
|
||||
| RelationshipStatus::None => Err(Error::NoEffect),
|
||||
RelationshipStatus::Friend
|
||||
| RelationshipStatus::Outgoing
|
||||
| RelationshipStatus::Incoming => {
|
||||
match try_join!(
|
||||
col.update_one(
|
||||
doc! {
|
||||
@@ -45,7 +48,10 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
)
|
||||
) {
|
||||
Ok(_) => Ok(json!({ "status": "None" })),
|
||||
Err(_) => Err(Error::DatabaseError { operation: "update_one", with: "user" })
|
||||
Err(_) => Err(Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
with: "user",
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,23 @@
|
||||
use crate::{database::entities::RelationshipStatus, database::guards::reference::Ref, database::entities::User, database::permissions::get_relationship, util::result::Error, database::get_collection};
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use crate::util::result::Result;
|
||||
use mongodb::bson::doc;
|
||||
use crate::{
|
||||
database::entities::RelationshipStatus, database::entities::User, database::get_collection,
|
||||
database::guards::reference::Ref, database::permissions::get_relationship, util::result::Error,
|
||||
};
|
||||
use futures::try_join;
|
||||
use mongodb::bson::doc;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
|
||||
#[delete("/<target>/block")]
|
||||
pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
let col = get_collection("users");
|
||||
|
||||
match get_relationship(&user, &target) {
|
||||
RelationshipStatus::None |
|
||||
RelationshipStatus::User |
|
||||
RelationshipStatus::BlockedOther |
|
||||
RelationshipStatus::Incoming |
|
||||
RelationshipStatus::Outgoing |
|
||||
RelationshipStatus::Friend => Err(Error::NoEffect),
|
||||
RelationshipStatus::None
|
||||
| RelationshipStatus::User
|
||||
| RelationshipStatus::BlockedOther
|
||||
| RelationshipStatus::Incoming
|
||||
| RelationshipStatus::Outgoing
|
||||
| RelationshipStatus::Friend => Err(Error::NoEffect),
|
||||
RelationshipStatus::Blocked => {
|
||||
match get_relationship(&target.fetch_user().await?, &user.as_ref()) {
|
||||
RelationshipStatus::Blocked => {
|
||||
@@ -28,13 +31,16 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
"relations.$.status": "BlockedOther"
|
||||
}
|
||||
},
|
||||
None
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "user" })?;
|
||||
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
with: "user",
|
||||
})?;
|
||||
|
||||
Ok(json!({ "status": "BlockedOther" }))
|
||||
},
|
||||
}
|
||||
RelationshipStatus::BlockedOther => {
|
||||
match try_join!(
|
||||
col.update_one(
|
||||
@@ -65,10 +71,13 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> {
|
||||
)
|
||||
) {
|
||||
Ok(_) => Ok(json!({ "status": "None" })),
|
||||
Err(_) => Err(Error::DatabaseError { operation: "update_one", with: "user" })
|
||||
Err(_) => Err(Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
with: "user",
|
||||
}),
|
||||
}
|
||||
},
|
||||
_ => Err(Error::InternalError)
|
||||
}
|
||||
_ => Err(Error::InternalError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user