Skip to content
Snippets Groups Projects
Commit 1713ad05 authored by insert's avatar insert
Browse files

Servers: Add route for creating channels.

Notifications: Subscribe to guild channels.
parent f32f4472
No related merge requests found
......@@ -42,7 +42,8 @@ pub enum Channel {
name: String,
owner: String,
description: String,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
recipients: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
......@@ -58,10 +59,11 @@ pub enum Channel {
nonce: Option<String>,
name: String,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
icon: Option<File>,
}
},
}
impl Channel {
......
use futures::StreamExt;
use hive_pubsub::PubSub;
use mongodb::bson::{doc, Document};
use rauth::auth::Session;
use mongodb::bson::{Document, doc};
use serde::{Deserialize, Serialize};
use rocket_contrib::json::JsonValue;
use serde::{Deserialize, Serialize};
use super::hive::{get_hive, subscribe_if_exists};
use crate::{database::*, util::result::{Error, Result}};
use crate::{
database::*,
util::result::{Error, Result},
};
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "error")]
......
use crate::database::*;
use crate::util::result::Error;
use super::hive::get_hive;
use futures::StreamExt;
use hive_pubsub::PubSub;
use mongodb::bson::doc;
use mongodb::bson::Document;
use mongodb::options::FindOptions;
pub async fn generate_subscriptions(user: &User) -> Result<(), String> {
......@@ -16,6 +18,44 @@ pub async fn generate_subscriptions(user: &User) -> Result<(), String> {
}
}
let server_ids = user
.fetch_server_ids()
.await
.map_err(|_| "Failed to fetch memberships.".to_string())?;
let channel_ids = get_collection("servers")
.find(
doc! {
"_id": {
"$in": &server_ids
}
},
None,
)
.await
.map_err(|_| "Failed to fetch servers.".to_string())?
.filter_map(async move |s| s.ok())
.collect::<Vec<Document>>()
.await
.into_iter()
.filter_map(|x| {
x.get_array("channels").ok().map(|v| {
v.into_iter()
.filter_map(|x| x.as_str().map(|x| x.to_string()))
.collect::<Vec<String>>()
})
})
.flatten()
.collect::<Vec<String>>();
for id in server_ids {
hive.subscribe(user.id.clone(), id)?;
}
for id in channel_ids {
hive.subscribe(user.id.clone(), id)?;
}
let mut cursor = get_collection("channels")
.find(
doc! {
......@@ -45,14 +85,5 @@ pub async fn generate_subscriptions(user: &User) -> Result<(), String> {
}
}
let server_ids = user
.fetch_server_ids()
.await
.map_err(|_| "Failed to fetch memberships.".to_string())?;
for id in server_ids {
hive.subscribe(user.id.clone(), id)?;
}
Ok(())
}
......@@ -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,
......
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,7 +40,23 @@ pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
let id = Ulid::new().to_string();
let cid = Ulid::new().to_string();
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",
})?;
Channel::TextChannel {
id: cid.clone(),
server: id.clone(),
......@@ -48,7 +64,9 @@ pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
name: "general".to_string(),
description: None,
icon: None,
}.publish().await?;
}
.publish()
.await?;
let server = Server {
id: id.clone(),
......@@ -56,28 +74,12 @@ pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> {
owner: user.id.clone(),
name: info.name.clone(),
channels: vec![ cid ],
channels: vec![cid],
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))
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment