Servers: Add edit route. Ability to set icon / banner.

This commit is contained in:
Paul
2021-06-01 17:42:23 +01:00
parent bff72fa6c3
commit 6bb4501c52
9 changed files with 181 additions and 32 deletions

View File

@@ -1,4 +1,5 @@
use futures::StreamExt;
use mongodb::bson::Document;
use mongodb::options::{Collation, FindOneOptions};
use mongodb::{
bson::{doc, from_document},
@@ -223,4 +224,31 @@ impl User {
Ok(users)
}
/// Utility function to get all the server IDs the user is in.
pub async fn fetch_server_ids(&self) -> Result<Vec<String>> {
Ok(get_collection("server_members")
.find(
doc! {
"_id.user": &self.id
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find",
with: "server_members",
})?
.filter_map(async move |s| s.ok())
.collect::<Vec<Document>>()
.await
.into_iter()
.filter_map(|x| {
x.get_document("_id")
.ok()
.map(|i| i.get_str("server").ok().map(|x| x.to_string()))
})
.flatten()
.collect::<Vec<String>>())
}
}

View File

@@ -1,6 +1,7 @@
pub use crate::database::*;
pub mod channel;
pub mod server;
pub mod user;
pub use user::get_relationship;

View File

@@ -1,4 +1,3 @@
use crate::database::*;
use crate::util::result::Result;
use super::PermissionCalculator;
@@ -10,6 +9,7 @@ use std::ops;
#[repr(u32)]
pub enum ServerPermission {
View = 1,
ManageServer = 8,
}
impl_op_ex!(+ |a: &ServerPermission, b: &ServerPermission| -> u32 { *a as u32 | *b as u32 });
@@ -19,6 +19,7 @@ bitfield! {
pub struct ServerPermissions(MSB0 [u32]);
u32;
pub get_view, _: 31;
pub get_manage_server, _: 28;
}
impl<'a> PermissionCalculator<'a> {
@@ -29,14 +30,14 @@ impl<'a> PermissionCalculator<'a> {
unreachable!()
};
if &self.perspective.id == server.owner {
if self.perspective.id == server.owner {
Ok(u32::MAX)
} else {
Ok(ServerPermission::View as u32)
}
}
pub async fn for_server(self) -> Result<ChannelPermissions<[u32; 1]>> {
pub async fn for_server(self) -> Result<ServerPermissions<[u32; 1]>> {
Ok(ServerPermissions([self.calculate_server().await?]))
}
}

View File

@@ -6,7 +6,7 @@ use crate::{
util::result::{Error, Result},
};
use futures::StreamExt;
use mongodb::bson::{doc, from_document, Document};
use mongodb::bson::{doc, from_document};
pub async fn generate_ready(mut user: User) -> Result<ClientboundNotification> {
let mut user_ids: HashSet<String> = HashSet::new();
@@ -19,30 +19,7 @@ pub async fn generate_ready(mut user: User) -> Result<ClientboundNotification> {
);
}
let server_ids = get_collection("server_members")
.find(
doc! {
"_id.user": &user.id
},
None,
)
.await
.map_err(|_| Error::DatabaseError {
operation: "find",
with: "server_members",
})?
.filter_map(async move |s| s.ok())
.collect::<Vec<Document>>()
.await
.into_iter()
.filter_map(|x| {
x.get_document("_id")
.ok()
.map(|i| i.get_str("server").ok().map(|x| x.to_string()))
})
.flatten()
.collect::<Vec<String>>();
let server_ids = user.fetch_server_ids().await?;
let mut cursor = get_collection("servers")
.find(
doc! {

View File

@@ -45,7 +45,14 @@ pub async fn generate_subscriptions(user: &User) -> Result<(), String> {
}
}
// ! FIXME: fetch memberships for servers
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(())
}

View File

@@ -69,7 +69,7 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
if let Some(attachment_id) = &data.icon {
let attachment =
File::find_and_use(&attachment_id, "icons", "object", &user.id).await?;
File::find_and_use(&attachment_id, "icons", "object", target.id()).await?;
set.insert(
"icon",
to_document(&attachment).map_err(|_| Error::DatabaseError {

View File

@@ -2,7 +2,8 @@ use rocket::Route;
mod server_create;
mod server_delete;
mod server_edit;
pub fn routes() -> Vec<Route> {
routes![server_create::req, server_delete::req]
routes![server_create::req, server_delete::req, server_edit::req]
}

View File

@@ -1,5 +1,5 @@
use crate::util::result::{Error, Result};
use crate::database::*;
use crate::util::result::{Error, Result};
use mongodb::bson::doc;
@@ -15,5 +15,8 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
Err(Error::MissingPermission)?
}*/
// ! FIXME: either delete server if owner
// ! OR leave server if member
target.delete().await
}

View File

@@ -0,0 +1,131 @@
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result};
use crate::{database::*, notifications::events::RemoveServerField};
use mongodb::bson::{doc, to_document};
use rocket_contrib::json::Json;
use serde::{Deserialize, Serialize};
use validator::Validate;
#[derive(Validate, Serialize, Deserialize)]
pub struct Data {
#[validate(length(min = 1, max = 32))]
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
icon: Option<String>,
banner: Option<String>,
remove: Option<RemoveServerField>,
}
#[patch("/<target>", data = "<data>")]
pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
let data = data.into_inner();
data.validate()
.map_err(|error| Error::FailedValidation { error })?;
if data.name.is_none() && data.icon.is_none() && data.banner.is_none() && data.remove.is_none()
{
return Ok(());
}
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)?
}
let mut set = doc! {};
let mut unset = doc! {};
let mut remove_icon = false;
let mut remove_banner = false;
if let Some(remove) = &data.remove {
match remove {
RemoveServerField::Icon => {
unset.insert("icon", 1);
remove_icon = true;
}
RemoveServerField::Banner => {
unset.insert("banner", 1);
remove_banner = true;
}
}
}
if let Some(name) = &data.name {
set.insert("name", name);
}
if let Some(attachment_id) = &data.icon {
let attachment = File::find_and_use(&attachment_id, "icons", "object", &target.id).await?;
set.insert(
"icon",
to_document(&attachment).map_err(|_| Error::DatabaseError {
operation: "to_document",
with: "attachment",
})?,
);
remove_icon = true;
}
if let Some(attachment_id) = &data.banner {
let attachment =
File::find_and_use(&attachment_id, "banners", "server", &target.id).await?;
set.insert(
"banner",
to_document(&attachment).map_err(|_| Error::DatabaseError {
operation: "to_document",
with: "attachment",
})?,
);
remove_banner = true;
}
let mut operations = doc! {};
if set.len() > 0 {
operations.insert("$set", &set);
}
if unset.len() > 0 {
operations.insert("$unset", unset);
}
if operations.len() > 0 {
get_collection("servers")
.update_one(doc! { "_id": &target.id }, operations, None)
.await
.map_err(|_| Error::DatabaseError {
operation: "update_one",
with: "server",
})?;
}
ClientboundNotification::ServerUpdate {
id: target.id.clone(),
data: json!(set),
clear: data.remove,
}
.publish(target.id.clone());
let Server { icon, banner, .. } = target;
if remove_icon {
if let Some(old_icon) = icon {
old_icon.delete().await?;
}
}
if remove_banner {
if let Some(old_banner) = banner {
old_banner.delete().await?;
}
}
Ok(())
}