forked from jmug/stoatchat
Servers: Core objects required. Includes creation. Bump version to 0.5.0.
This commit is contained in:
@@ -1,5 +0,0 @@
|
||||
use rocket::Route;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![]
|
||||
}
|
||||
@@ -3,12 +3,12 @@ pub use rocket::response::Redirect;
|
||||
use rocket::Rocket;
|
||||
|
||||
mod channels;
|
||||
mod guild;
|
||||
mod onboard;
|
||||
mod push;
|
||||
mod root;
|
||||
mod users;
|
||||
mod servers;
|
||||
mod sync;
|
||||
mod users;
|
||||
|
||||
pub fn mount(rocket: Rocket) -> Rocket {
|
||||
rocket
|
||||
@@ -16,7 +16,7 @@ pub fn mount(rocket: Rocket) -> Rocket {
|
||||
.mount("/onboard", onboard::routes())
|
||||
.mount("/users", users::routes())
|
||||
.mount("/channels", channels::routes())
|
||||
.mount("/guild", guild::routes())
|
||||
.mount("/servers", servers::routes())
|
||||
.mount("/push", push::routes())
|
||||
.mount("/sync", sync::routes())
|
||||
}
|
||||
|
||||
8
src/routes/servers/mod.rs
Normal file
8
src/routes/servers/mod.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use rocket::Route;
|
||||
|
||||
mod server_create;
|
||||
mod server_delete;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![server_create::req, server_delete::req]
|
||||
}
|
||||
73
src/routes/servers/server_create.rs
Normal file
73
src/routes/servers/server_create.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use rocket_contrib::json::{Json, JsonValue};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use ulid::Ulid;
|
||||
use validator::Validate;
|
||||
|
||||
#[derive(Validate, Serialize, Deserialize)]
|
||||
pub struct Data {
|
||||
#[validate(length(min = 1, max = 32))]
|
||||
name: String,
|
||||
// Maximum length of 36 allows both ULIDs and UUIDs.
|
||||
#[validate(length(min = 1, max = 36))]
|
||||
nonce: String,
|
||||
}
|
||||
|
||||
#[post("/create", data = "<info>")]
|
||||
pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
|
||||
info.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
if get_collection("servers")
|
||||
.find_one(
|
||||
doc! {
|
||||
"nonce": &info.nonce
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "find_one",
|
||||
with: "server",
|
||||
})?
|
||||
.is_some()
|
||||
{
|
||||
Err(Error::DuplicateNonce)?
|
||||
}
|
||||
|
||||
let id = Ulid::new().to_string();
|
||||
let server = Server {
|
||||
id: id.clone(),
|
||||
nonce: Some(info.nonce.clone()),
|
||||
owner: user.id.clone(),
|
||||
|
||||
name: info.name.clone(),
|
||||
channels: vec![],
|
||||
|
||||
icon: None,
|
||||
banner: None,
|
||||
};
|
||||
|
||||
get_collection("server_members")
|
||||
.insert_one(
|
||||
doc! {
|
||||
"_id": {
|
||||
"server": id,
|
||||
"user": user.id
|
||||
}
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "insert_one",
|
||||
with: "server_members",
|
||||
})?;
|
||||
|
||||
server.clone().publish().await?;
|
||||
|
||||
Ok(json!(server))
|
||||
}
|
||||
19
src/routes/servers/server_delete.rs
Normal file
19
src/routes/servers/server_delete.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::database::*;
|
||||
|
||||
use mongodb::bson::doc;
|
||||
|
||||
#[delete("/<target>")]
|
||||
pub async fn req(user: User, target: Ref) -> Result<()> {
|
||||
let target = target.fetch_server().await?;
|
||||
|
||||
/*let perm = permissions::PermissionCalculator::new(&user)
|
||||
.with_channel(&target)
|
||||
.for_channel()
|
||||
.await?;
|
||||
if !perm.get_view() {
|
||||
Err(Error::MissingPermission)?
|
||||
}*/
|
||||
|
||||
target.delete().await
|
||||
}
|
||||
@@ -2,13 +2,13 @@ use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use mongodb::options::FindOneOptions;
|
||||
use rocket_contrib::json::{Json, JsonValue};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Options {
|
||||
keys: Vec<String>
|
||||
keys: Vec<String>,
|
||||
}
|
||||
|
||||
#[post("/settings/fetch", data = "<options>")]
|
||||
@@ -27,14 +27,16 @@ pub async fn req(user: User, options: Json<Options>) -> Result<JsonValue> {
|
||||
doc! {
|
||||
"_id": user.id
|
||||
},
|
||||
FindOneOptions::builder()
|
||||
.projection(projection)
|
||||
.build()
|
||||
FindOneOptions::builder().projection(projection).build(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "find_one", with: "user_settings" })? {
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "find_one",
|
||||
with: "user_settings",
|
||||
})?
|
||||
{
|
||||
Ok(json!(doc))
|
||||
} else {
|
||||
Ok(json!({ }))
|
||||
Ok(json!({}))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,5 @@ mod get_settings;
|
||||
mod set_settings;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![
|
||||
get_settings::req,
|
||||
set_settings::req
|
||||
]
|
||||
routes![get_settings::req, set_settings::req]
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@ use crate::notifications::events::ClientboundNotification;
|
||||
use crate::util::result::{Error, Result};
|
||||
|
||||
use chrono::prelude::*;
|
||||
use rocket::request::Form;
|
||||
use std::collections::HashMap;
|
||||
use rocket_contrib::json::Json;
|
||||
use mongodb::bson::{doc, to_bson};
|
||||
use serde::{Serialize, Deserialize};
|
||||
use mongodb::options::UpdateOptions;
|
||||
use rocket::request::Form;
|
||||
use rocket_contrib::json::Json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
|
||||
type Data = HashMap<String, String>;
|
||||
|
||||
@@ -35,10 +35,10 @@ pub async fn req(user: User, data: Json<Data>, options: Form<Options>) -> Result
|
||||
for (key, data) in &data {
|
||||
set.insert(
|
||||
key.clone(),
|
||||
vec! [
|
||||
vec![
|
||||
to_bson(×tamp).unwrap(),
|
||||
to_bson(&data.clone()).unwrap()
|
||||
]
|
||||
to_bson(&data.clone()).unwrap(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -51,19 +51,20 @@ pub async fn req(user: User, data: Json<Data>, options: Form<Options>) -> Result
|
||||
doc! {
|
||||
"$set": &set
|
||||
},
|
||||
UpdateOptions::builder()
|
||||
.upsert(true)
|
||||
.build()
|
||||
UpdateOptions::builder().upsert(true).build(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "user_settings" })?;
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
with: "user_settings",
|
||||
})?;
|
||||
}
|
||||
|
||||
ClientboundNotification::UserSettingsUpdate {
|
||||
id: user.id.clone(),
|
||||
update: json!(set)
|
||||
update: json!(set),
|
||||
}
|
||||
.publish(user.id);
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user