forked from jmug/stoatchat
Add a way to create channels.
This commit is contained in:
@@ -36,7 +36,6 @@ pub struct Guild {
|
|||||||
pub description: String,
|
pub description: String,
|
||||||
pub owner: String,
|
pub owner: String,
|
||||||
|
|
||||||
pub channels: Vec<String>,
|
|
||||||
pub invites: Vec<Invite>,
|
pub invites: Vec<Invite>,
|
||||||
pub bans: Vec<Ban>,
|
pub bans: Vec<Ban>,
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ pub struct GuildRef {
|
|||||||
pub description: String,
|
pub description: String,
|
||||||
pub owner: String,
|
pub owner: String,
|
||||||
|
|
||||||
pub channels: Vec<String>,
|
|
||||||
pub bans: Vec<Ban>,
|
pub bans: Vec<Ban>,
|
||||||
|
|
||||||
pub default_permissions: i32,
|
pub default_permissions: i32,
|
||||||
@@ -30,7 +29,6 @@ impl GuildRef {
|
|||||||
"name": 1,
|
"name": 1,
|
||||||
"description": 1,
|
"description": 1,
|
||||||
"owner": 1,
|
"owner": 1,
|
||||||
"channels": 1,
|
|
||||||
"bans": 1,
|
"bans": 1,
|
||||||
"default_permissions": 1
|
"default_permissions": 1
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -89,17 +89,11 @@ pub fn my_guilds(user: UserRef) -> Response {
|
|||||||
pub fn guild(user: UserRef, target: GuildRef) -> Option<Response> {
|
pub fn guild(user: UserRef, target: GuildRef) -> Option<Response> {
|
||||||
with_permissions!(user, target);
|
with_permissions!(user, target);
|
||||||
|
|
||||||
let mut targets = vec![];
|
|
||||||
for channel in target.channels {
|
|
||||||
targets.push(Bson::String(channel));
|
|
||||||
}
|
|
||||||
|
|
||||||
let col = database::get_collection("channels");
|
let col = database::get_collection("channels");
|
||||||
match col.find(
|
match col.find(
|
||||||
doc! {
|
doc! {
|
||||||
"_id": {
|
"type": 2,
|
||||||
"$in": targets,
|
"guild": &target.id,
|
||||||
}
|
|
||||||
},
|
},
|
||||||
None,
|
None,
|
||||||
) {
|
) {
|
||||||
@@ -112,7 +106,6 @@ pub fn guild(user: UserRef, target: GuildRef) -> Option<Response> {
|
|||||||
{
|
{
|
||||||
channels.push(json!({
|
channels.push(json!({
|
||||||
"id": channel.id,
|
"id": channel.id,
|
||||||
"last_message": channel.last_message,
|
|
||||||
"name": channel.name,
|
"name": channel.name,
|
||||||
"description": channel.description,
|
"description": channel.description,
|
||||||
}));
|
}));
|
||||||
@@ -134,6 +127,73 @@ pub fn guild(user: UserRef, target: GuildRef) -> Option<Response> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct CreateChannel {
|
||||||
|
nonce: String,
|
||||||
|
name: String,
|
||||||
|
description: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// create a new channel
|
||||||
|
#[post("/<target>/channels", data = "<info>")]
|
||||||
|
pub fn create_channel(
|
||||||
|
user: UserRef,
|
||||||
|
target: GuildRef,
|
||||||
|
info: Json<CreateChannel>,
|
||||||
|
) -> Option<Response> {
|
||||||
|
let (permissions, _) = with_permissions!(user, target);
|
||||||
|
|
||||||
|
if !permissions.get_manage_channels() {
|
||||||
|
return Some(Response::LackingPermission(Permission::ManageChannels));
|
||||||
|
}
|
||||||
|
|
||||||
|
let nonce: String = info.nonce.chars().take(32).collect();
|
||||||
|
let name: String = info.name.chars().take(32).collect();
|
||||||
|
let description: String = info
|
||||||
|
.description
|
||||||
|
.clone()
|
||||||
|
.unwrap_or(String::new())
|
||||||
|
.chars()
|
||||||
|
.take(255)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if let Ok(result) =
|
||||||
|
database::get_collection("channels").find_one(doc! { "nonce": &nonce }, None)
|
||||||
|
{
|
||||||
|
if result.is_some() {
|
||||||
|
return Some(Response::BadRequest(
|
||||||
|
json!({ "error": "Channel already created." }),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let id = Ulid::new().to_string();
|
||||||
|
if database::get_collection("channels")
|
||||||
|
.insert_one(
|
||||||
|
doc! {
|
||||||
|
"_id": &id,
|
||||||
|
"nonce": &nonce,
|
||||||
|
"type": 2,
|
||||||
|
"guild": &target.id,
|
||||||
|
"name": name,
|
||||||
|
"description": description,
|
||||||
|
},
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.is_ok()
|
||||||
|
{
|
||||||
|
Some(Response::Success(json!({ "id": &id })))
|
||||||
|
} else {
|
||||||
|
Some(Response::BadRequest(
|
||||||
|
json!({ "error": "Couldn't create channel." }),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Some(Response::BadRequest(
|
||||||
|
json!({ "error": "Failed to check if channel was made." }),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct CreateGuild {
|
pub struct CreateGuild {
|
||||||
name: String,
|
name: String,
|
||||||
@@ -176,6 +236,7 @@ pub fn create_guild(user: UserRef, info: Json<CreateGuild>) -> Response {
|
|||||||
"_id": channel_id.clone(),
|
"_id": channel_id.clone(),
|
||||||
"type": ChannelType::GUILDCHANNEL as u32,
|
"type": ChannelType::GUILDCHANNEL as u32,
|
||||||
"name": "general",
|
"name": "general",
|
||||||
|
"description": "",
|
||||||
"guild": id.clone(),
|
"guild": id.clone(),
|
||||||
},
|
},
|
||||||
None,
|
None,
|
||||||
@@ -212,9 +273,6 @@ pub fn create_guild(user: UserRef, info: Json<CreateGuild>) -> Response {
|
|||||||
"name": name,
|
"name": name,
|
||||||
"description": description,
|
"description": description,
|
||||||
"owner": &user.id,
|
"owner": &user.id,
|
||||||
"channels": [
|
|
||||||
&channel_id
|
|
||||||
],
|
|
||||||
"invites": [],
|
"invites": [],
|
||||||
"bans": [],
|
"bans": [],
|
||||||
"default_permissions": 51,
|
"default_permissions": 51,
|
||||||
@@ -416,7 +474,7 @@ pub fn unban_member(user: UserRef, target: GuildRef, other: String) -> Option<Re
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
doc! { }
|
doc! {},
|
||||||
)
|
)
|
||||||
.is_none()
|
.is_none()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -106,6 +106,7 @@ pub fn mount(rocket: Rocket) -> Rocket {
|
|||||||
routes![
|
routes![
|
||||||
guild::my_guilds,
|
guild::my_guilds,
|
||||||
guild::guild,
|
guild::guild,
|
||||||
|
guild::create_channel,
|
||||||
guild::create_guild,
|
guild::create_guild,
|
||||||
guild::fetch_members,
|
guild::fetch_members,
|
||||||
guild::fetch_member,
|
guild::fetch_member,
|
||||||
|
|||||||
Reference in New Issue
Block a user