Servers: Add route for creating channels.
Notifications: Subscribe to guild channels.
This commit is contained in:
@@ -108,6 +108,12 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Channel::TextChannel { .. } => unimplemented!()
|
||||
Channel::TextChannel { .. } => {
|
||||
if perm.get_manage_channel() {
|
||||
target.delete().await
|
||||
} else {
|
||||
Err(Error::MissingPermission)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ pub struct Data {
|
||||
|
||||
#[post("/create", data = "<info>")]
|
||||
pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
|
||||
let info = info.into_inner();
|
||||
info.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
@@ -62,12 +63,9 @@ pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
|
||||
let id = Ulid::new().to_string();
|
||||
let channel = Channel::Group {
|
||||
id,
|
||||
nonce: Some(info.nonce.clone()),
|
||||
name: info.name.clone(),
|
||||
description: info
|
||||
.description
|
||||
.clone()
|
||||
.unwrap_or_else(|| "A group.".to_string()),
|
||||
nonce: Some(info.nonce),
|
||||
name: info.name,
|
||||
description: info.description,
|
||||
owner: user.id,
|
||||
recipients: set.into_iter().collect::<Vec<String>>(),
|
||||
icon: None,
|
||||
|
||||
85
src/routes/servers/channel_create.rs
Normal file
85
src/routes/servers/channel_create.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
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,
|
||||
#[validate(length(min = 0, max = 1024))]
|
||||
description: Option<String>,
|
||||
// Maximum length of 36 allows both ULIDs and UUIDs.
|
||||
#[validate(length(min = 1, max = 36))]
|
||||
nonce: String,
|
||||
}
|
||||
|
||||
#[post("/<target>/channels", data = "<info>")]
|
||||
pub async fn req(user: User, target: Ref, info: Json<Data>) -> Result<JsonValue> {
|
||||
let info = info.into_inner();
|
||||
info.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
let target = target.fetch_server().await?;
|
||||
let perm = permissions::PermissionCalculator::new(&user)
|
||||
.with_server(&target)
|
||||
.for_server()
|
||||
.await?;
|
||||
|
||||
if !perm.get_manage_server() {
|
||||
Err(Error::MissingPermission)?
|
||||
}
|
||||
|
||||
if get_collection("channels")
|
||||
.find_one(
|
||||
doc! {
|
||||
"nonce": &info.nonce
|
||||
},
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "find_one",
|
||||
with: "channel",
|
||||
})?
|
||||
.is_some()
|
||||
{
|
||||
Err(Error::DuplicateNonce)?
|
||||
}
|
||||
|
||||
let id = Ulid::new().to_string();
|
||||
let channel = Channel::TextChannel {
|
||||
id: id.clone(),
|
||||
server: target.id.clone(),
|
||||
nonce: Some(info.nonce),
|
||||
|
||||
name: info.name,
|
||||
description: info.description,
|
||||
icon: None,
|
||||
};
|
||||
|
||||
channel.clone().publish().await?;
|
||||
get_collection("servers")
|
||||
.update_one(
|
||||
doc! {
|
||||
"_id": target.id
|
||||
},
|
||||
doc! {
|
||||
"$addToSet": {
|
||||
"channels": id
|
||||
}
|
||||
},
|
||||
None
|
||||
)
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError {
|
||||
operation: "update_one",
|
||||
with: "server",
|
||||
})?;
|
||||
|
||||
Ok(json!(channel))
|
||||
}
|
||||
@@ -4,6 +4,13 @@ mod server_create;
|
||||
mod server_delete;
|
||||
mod server_edit;
|
||||
|
||||
mod channel_create;
|
||||
|
||||
pub fn routes() -> Vec<Route> {
|
||||
routes![server_create::req, server_delete::req, server_edit::req]
|
||||
routes![
|
||||
server_create::req,
|
||||
server_delete::req,
|
||||
server_edit::req,
|
||||
channel_create::req
|
||||
]
|
||||
}
|
||||
|
||||
@@ -40,34 +40,13 @@ pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
|
||||
|
||||
let id = Ulid::new().to_string();
|
||||
let cid = Ulid::new().to_string();
|
||||
|
||||
Channel::TextChannel {
|
||||
id: cid.clone(),
|
||||
server: id.clone(),
|
||||
nonce: Some(info.nonce.clone()),
|
||||
name: "general".to_string(),
|
||||
description: None,
|
||||
icon: None,
|
||||
}.publish().await?;
|
||||
|
||||
let server = Server {
|
||||
id: id.clone(),
|
||||
nonce: Some(info.nonce.clone()),
|
||||
owner: user.id.clone(),
|
||||
|
||||
name: info.name.clone(),
|
||||
channels: vec![ cid ],
|
||||
|
||||
icon: None,
|
||||
banner: None,
|
||||
};
|
||||
|
||||
get_collection("server_members")
|
||||
.insert_one(
|
||||
doc! {
|
||||
"_id": {
|
||||
"server": id,
|
||||
"user": user.id
|
||||
"server": &id,
|
||||
"user": &user.id
|
||||
}
|
||||
},
|
||||
None,
|
||||
@@ -78,6 +57,29 @@ pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
|
||||
with: "server_members",
|
||||
})?;
|
||||
|
||||
Channel::TextChannel {
|
||||
id: cid.clone(),
|
||||
server: id.clone(),
|
||||
nonce: Some(info.nonce.clone()),
|
||||
name: "general".to_string(),
|
||||
description: None,
|
||||
icon: None,
|
||||
}
|
||||
.publish()
|
||||
.await?;
|
||||
|
||||
let server = Server {
|
||||
id: id.clone(),
|
||||
nonce: Some(info.nonce.clone()),
|
||||
owner: user.id.clone(),
|
||||
|
||||
name: info.name.clone(),
|
||||
channels: vec![cid],
|
||||
|
||||
icon: None,
|
||||
banner: None,
|
||||
};
|
||||
|
||||
server.clone().publish().await?;
|
||||
|
||||
Ok(json!(server))
|
||||
|
||||
Reference in New Issue
Block a user