Merge pull request #38 from heikkari/13-24

This commit is contained in:
Paul Makles
2021-08-16 13:52:42 +01:00
committed by GitHub
33 changed files with 148 additions and 117 deletions

View File

@@ -15,6 +15,7 @@ pub enum WebSocketError {
InvalidSession, InvalidSession,
OnboardingNotFinished, OnboardingNotFinished,
AlreadyAuthenticated, AlreadyAuthenticated,
MalformedData { msg: String },
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]

View File

@@ -74,7 +74,20 @@ async fn accept(stream: TcpStream) {
let incoming = read.try_for_each(async move |msg| { let incoming = read.try_for_each(async move |msg| {
let mutex = mutex_generator(); let mutex = mutex_generator();
if let Message::Text(text) = msg { if let Message::Text(text) = msg {
if let Ok(notification) = serde_json::from_str::<ServerboundNotification>(&text) { let maybe_decoded = serde_json::from_str::<ServerboundNotification>(&text);
// If serde fails to decode the data, return a `MalformedData` error
if let Err(why) = maybe_decoded {
send(ClientboundNotification::Error(
WebSocketError::MalformedData {
msg: why.to_string()
}
));
return Ok(());
}
if let Ok(notification) = maybe_decoded {
match notification { match notification {
ServerboundNotification::Authenticate(auth) => { ServerboundNotification::Authenticate(auth) => {
{ {

View File

@@ -1,11 +1,11 @@
use crate::database::*; use crate::database::*;
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, EmptyResponse, Result};
use mongodb::bson::doc; use mongodb::bson::doc;
#[delete("/<target>")] #[delete("/<target>")]
pub async fn delete_bot(user: User, target: Ref) -> Result<()> { pub async fn delete_bot(user: User, target: Ref) -> Result<EmptyResponse> {
let bot = target.fetch_bot().await?; let bot = target.fetch_bot().await?;
if bot.owner != user.id { if bot.owner != user.id {
return Err(Error::MissingPermission); return Err(Error::MissingPermission);
@@ -58,6 +58,6 @@ pub async fn delete_bot(user: User, target: Ref) -> Result<()> {
with: "bot", with: "bot",
operation: "delete_one" operation: "delete_one"
})?; })?;
Ok(()) Ok(EmptyResponse {})
} }

View File

@@ -1,5 +1,5 @@
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use crate::{database::*, notifications::events::RemoveBotField}; use crate::{database::*, notifications::events::RemoveBotField};
use mongodb::bson::doc; use mongodb::bson::doc;
@@ -25,7 +25,7 @@ pub struct Data {
} }
#[patch("/<target>", data = "<data>")] #[patch("/<target>", data = "<data>")]
pub async fn edit_bot(user: User, target: Ref, data: Json<Data>) -> Result<()> { pub async fn edit_bot(user: User, target: Ref, data: Json<Data>) -> Result<EmptyResponse> {
let data = data.into_inner(); let data = data.into_inner();
data.validate() data.validate()
.map_err(|error| Error::FailedValidation { error })?; .map_err(|error| Error::FailedValidation { error })?;
@@ -112,5 +112,5 @@ pub async fn edit_bot(user: User, target: Ref, data: Json<Data>) -> Result<()> {
})?; })?;
} }
Ok(()) Ok(EmptyResponse {})
} }

View File

@@ -1,5 +1,5 @@
use crate::database::*; use crate::database::*;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use rocket::serde::json::Json; use rocket::serde::json::Json;
use serde::Deserialize; use serde::Deserialize;
@@ -22,7 +22,7 @@ pub enum Destination {
} }
#[post("/<target>/invite", data = "<dest>")] #[post("/<target>/invite", data = "<dest>")]
pub async fn invite_bot(user: User, target: Ref, dest: Json<Destination>) -> Result<()> { pub async fn invite_bot(user: User, target: Ref, dest: Json<Destination>) -> Result<EmptyResponse> {
let bot = target.fetch_bot().await?; let bot = target.fetch_bot().await?;
if !bot.public { if !bot.public {
@@ -39,13 +39,13 @@ pub async fn invite_bot(user: User, target: Ref, dest: Json<Destination>) -> Res
.with_server(&server) .with_server(&server)
.for_server() .for_server()
.await?; .await?;
if !perm.get_manage_server() { if !perm.get_manage_server() {
Err(Error::MissingPermission)? Err(Error::MissingPermission)?
} }
server.join_member(&bot.id).await?; server.join_member(&bot.id).await?;
Ok(()) Ok(EmptyResponse {})
} }
Destination::Group(GroupId { group }) => { Destination::Group(GroupId { group }) => {
let channel = Ref::from(group)?.fetch_channel().await?; let channel = Ref::from(group)?.fetch_channel().await?;
@@ -54,7 +54,7 @@ pub async fn invite_bot(user: User, target: Ref, dest: Json<Destination>) -> Res
.with_channel(&channel) .with_channel(&channel)
.for_channel() .for_channel()
.await?; .await?;
if !perm.get_invite_others() { if !perm.get_invite_others() {
Err(Error::MissingPermission)? Err(Error::MissingPermission)?
} }

View File

@@ -1,16 +1,16 @@
use crate::database::*; use crate::database::*;
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc; use mongodb::bson::doc;
use mongodb::options::UpdateOptions; use mongodb::options::UpdateOptions;
#[put("/<target>/ack/<message>")] #[put("/<target>/ack/<message>")]
pub async fn req(user: User, target: Ref, message: Ref) -> Result<()> { pub async fn req(user: User, target: Ref, message: Ref) -> Result<EmptyResponse> {
if user.bot.is_some() { if user.bot.is_some() {
return Err(Error::IsBot) return Err(Error::IsBot)
} }
let target = target.fetch_channel().await?; let target = target.fetch_channel().await?;
let perm = permissions::PermissionCalculator::new(&user) let perm = permissions::PermissionCalculator::new(&user)
@@ -52,5 +52,5 @@ pub async fn req(user: User, target: Ref, message: Ref) -> Result<()> {
} }
.publish(user.id); .publish(user.id);
Ok(()) Ok(EmptyResponse {})
} }

View File

@@ -1,10 +1,10 @@
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use crate::{database::*, notifications::events::ClientboundNotification}; use crate::{database::*, notifications::events::ClientboundNotification};
use mongodb::bson::doc; use mongodb::bson::doc;
#[delete("/<target>")] #[delete("/<target>")]
pub async fn req(user: User, target: Ref) -> Result<()> { pub async fn req(user: User, target: Ref) -> Result<EmptyResponse> {
let target = target.fetch_channel().await?; let target = target.fetch_channel().await?;
let perm = permissions::PermissionCalculator::new(&user) let perm = permissions::PermissionCalculator::new(&user)
@@ -37,7 +37,7 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
with: "channel", with: "channel",
})?; })?;
Ok(()) Ok(EmptyResponse {})
} }
Channel::Group { Channel::Group {
id, id,
@@ -103,7 +103,7 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
.await .await
.ok(); .ok();
Ok(()) Ok(EmptyResponse {})
} }
Channel::TextChannel { .. } | Channel::TextChannel { .. } |
Channel::VoiceChannel { .. } => { Channel::VoiceChannel { .. } => {

View File

@@ -1,5 +1,5 @@
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use crate::{database::*, notifications::events::RemoveChannelField}; use crate::{database::*, notifications::events::RemoveChannelField};
use mongodb::bson::{doc, to_document}; use mongodb::bson::{doc, to_document};
@@ -21,7 +21,7 @@ pub struct Data {
} }
#[patch("/<target>", data = "<data>")] #[patch("/<target>", data = "<data>")]
pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> { pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<EmptyResponse> {
let data = data.into_inner(); let data = data.into_inner();
data.validate() data.validate()
.map_err(|error| Error::FailedValidation { error })?; .map_err(|error| Error::FailedValidation { error })?;
@@ -31,7 +31,7 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
&& data.icon.is_none() && data.icon.is_none()
&& data.remove.is_none() && data.remove.is_none()
{ {
return Ok(()); return Ok(EmptyResponse {});
} }
let target = target.fetch_channel().await?; let target = target.fetch_channel().await?;
@@ -146,7 +146,7 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
} }
} }
Ok(()) Ok(EmptyResponse {})
} }
_ => Err(Error::InvalidOperation), _ => Err(Error::InvalidOperation),
} }

View File

@@ -1,10 +1,10 @@
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use crate::database::*; use crate::database::*;
use mongodb::bson::doc; use mongodb::bson::doc;
#[put("/<target>/recipients/<member>")] #[put("/<target>/recipients/<member>")]
pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> { pub async fn req(user: User, target: Ref, member: Ref) -> Result<EmptyResponse> {
if get_relationship(&user, &member.id) != RelationshipStatus::Friend { if get_relationship(&user, &member.id) != RelationshipStatus::Friend {
Err(Error::NotFriends)? Err(Error::NotFriends)?
} }
@@ -19,5 +19,6 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
Err(Error::MissingPermission)? Err(Error::MissingPermission)?
} }
channel.add_to_group(member.id, user.id).await channel.add_to_group(member.id, user.id).await;
Ok(EmptyResponse {})
} }

View File

@@ -1,10 +1,10 @@
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use crate::{database::*, notifications::events::ClientboundNotification}; use crate::{database::*, notifications::events::ClientboundNotification};
use mongodb::bson::doc; use mongodb::bson::doc;
#[delete("/<target>/recipients/<member>")] #[delete("/<target>/recipients/<member>")]
pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> { pub async fn req(user: User, target: Ref, member: Ref) -> Result<EmptyResponse> {
if &user.id == &member.id { if &user.id == &member.id {
Err(Error::CannotRemoveYourself)? Err(Error::CannotRemoveYourself)?
} }
@@ -59,7 +59,7 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
.await .await
.ok(); .ok();
Ok(()) Ok(EmptyResponse {})
} else { } else {
Err(Error::InvalidOperation) Err(Error::InvalidOperation)
} }

View File

@@ -1,10 +1,10 @@
use crate::database::*; use crate::database::*;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc; use mongodb::bson::doc;
#[delete("/<target>/messages/<msg>")] #[delete("/<target>/messages/<msg>")]
pub async fn req(user: User, target: Ref, msg: Ref) -> Result<()> { pub async fn req(user: User, target: Ref, msg: Ref) -> Result<EmptyResponse> {
let channel = target.fetch_channel().await?; let channel = target.fetch_channel().await?;
channel.has_messaging()?; channel.has_messaging()?;
@@ -24,5 +24,6 @@ pub async fn req(user: User, target: Ref, msg: Ref) -> Result<()> {
} }
} }
message.delete().await message.delete().await;
Ok(EmptyResponse {})
} }

View File

@@ -1,5 +1,5 @@
use crate::database::*; use crate::database::*;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use chrono::Utc; use chrono::Utc;
use mongodb::bson::{doc, Bson, DateTime, Document}; use mongodb::bson::{doc, Bson, DateTime, Document};
@@ -14,7 +14,7 @@ pub struct Data {
} }
#[patch("/<target>/messages/<msg>", data = "<edit>")] #[patch("/<target>/messages/<msg>", data = "<edit>")]
pub async fn req(user: User, target: Ref, msg: Ref, edit: Json<Data>) -> Result<()> { pub async fn req(user: User, target: Ref, msg: Ref, edit: Json<Data>) -> Result<EmptyResponse> {
edit.validate() edit.validate()
.map_err(|error| Error::FailedValidation { error })?; .map_err(|error| Error::FailedValidation { error })?;
@@ -73,5 +73,6 @@ pub async fn req(user: User, target: Ref, msg: Ref, edit: Json<Data>) -> Result<
with: "message", with: "message",
})?; })?;
message.publish_update(update).await message.publish_update(update).await;
Ok(EmptyResponse {})
} }

View File

@@ -6,7 +6,7 @@ use validator::Contains;
use crate::database::*; use crate::database::*;
use crate::database::permissions::channel::ChannelPermission; use crate::database::permissions::channel::ChannelPermission;
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Data { pub struct Data {
@@ -14,7 +14,7 @@ pub struct Data {
} }
#[put("/<target>/permissions/<role>", data = "<data>", rank = 2)] #[put("/<target>/permissions/<role>", data = "<data>", rank = 2)]
pub async fn req(user: User, target: Ref, role: String, data: Json<Data>) -> Result<()> { pub async fn req(user: User, target: Ref, role: String, data: Json<Data>) -> Result<EmptyResponse> {
let target = target.fetch_channel().await?; let target = target.fetch_channel().await?;
match target { match target {
@@ -25,7 +25,7 @@ pub async fn req(user: User, target: Ref, role: String, data: Json<Data>) -> Res
.with_server(&target) .with_server(&target)
.for_server() .for_server()
.await?; .await?;
if !perm.get_manage_roles() { if !perm.get_manage_roles() {
return Err(Error::MissingPermission); return Err(Error::MissingPermission);
} }
@@ -33,9 +33,9 @@ pub async fn req(user: User, target: Ref, role: String, data: Json<Data>) -> Res
if !target.roles.has_element(&role) { if !target.roles.has_element(&role) {
return Err(Error::NotFound); return Err(Error::NotFound);
} }
let permissions: u32 = ChannelPermission::View as u32 | data.permissions; let permissions: u32 = ChannelPermission::View as u32 | data.permissions;
get_collection("channels") get_collection("channels")
.update_one( .update_one(
doc! { "_id": &id }, doc! { "_id": &id },
@@ -51,7 +51,7 @@ pub async fn req(user: User, target: Ref, role: String, data: Json<Data>) -> Res
operation: "update_one", operation: "update_one",
with: "channel" with: "channel"
})?; })?;
role_permissions.insert(role, permissions as i32); role_permissions.insert(role, permissions as i32);
ClientboundNotification::ChannelUpdate { ClientboundNotification::ChannelUpdate {
id: id.clone(), id: id.clone(),
@@ -62,7 +62,7 @@ pub async fn req(user: User, target: Ref, role: String, data: Json<Data>) -> Res
} }
.publish(id); .publish(id);
Ok(()) Ok(EmptyResponse {})
} }
_ => Err(Error::InvalidOperation) _ => Err(Error::InvalidOperation)
} }

View File

@@ -5,7 +5,7 @@ use serde::{Serialize, Deserialize};
use crate::database::*; use crate::database::*;
use crate::database::permissions::channel::{ ChannelPermission, DEFAULT_PERMISSION_DM }; use crate::database::permissions::channel::{ ChannelPermission, DEFAULT_PERMISSION_DM };
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Data { pub struct Data {
@@ -13,14 +13,14 @@ pub struct Data {
} }
#[put("/<target>/permissions/default", data = "<data>", rank = 1)] #[put("/<target>/permissions/default", data = "<data>", rank = 1)]
pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> { pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<EmptyResponse> {
let target = target.fetch_channel().await?; let target = target.fetch_channel().await?;
match target { match target {
Channel::Group { id, owner, .. } => { Channel::Group { id, owner, .. } => {
if user.id == owner { if user.id == owner {
let permissions: u32 = ChannelPermission::View as u32 | (data.permissions & *DEFAULT_PERMISSION_DM); let permissions: u32 = ChannelPermission::View as u32 | (data.permissions & *DEFAULT_PERMISSION_DM);
get_collection("channels") get_collection("channels")
.update_one( .update_one(
doc! { "_id": &id }, doc! { "_id": &id },
@@ -36,7 +36,7 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
operation: "update_one", operation: "update_one",
with: "channel" with: "channel"
})?; })?;
ClientboundNotification::ChannelUpdate { ClientboundNotification::ChannelUpdate {
id: id.clone(), id: id.clone(),
data: json!({ data: json!({
@@ -45,8 +45,8 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
clear: None clear: None
} }
.publish(id); .publish(id);
Ok(()) Ok(EmptyResponse {})
} else { } else {
Err(Error::MissingPermission) Err(Error::MissingPermission)
} }
@@ -58,13 +58,13 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
.with_server(&target) .with_server(&target)
.for_server() .for_server()
.await?; .await?;
if !perm.get_manage_roles() { if !perm.get_manage_roles() {
return Err(Error::MissingPermission); return Err(Error::MissingPermission);
} }
let permissions: u32 = ChannelPermission::View as u32 | data.permissions; let permissions: u32 = ChannelPermission::View as u32 | data.permissions;
get_collection("channels") get_collection("channels")
.update_one( .update_one(
doc! { "_id": &id }, doc! { "_id": &id },
@@ -80,7 +80,7 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
operation: "update_one", operation: "update_one",
with: "channel" with: "channel"
})?; })?;
ClientboundNotification::ChannelUpdate { ClientboundNotification::ChannelUpdate {
id: id.clone(), id: id.clone(),
data: json!({ data: json!({
@@ -90,7 +90,7 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
} }
.publish(id); .publish(id);
Ok(()) Ok(EmptyResponse {})
} }
_ => Err(Error::InvalidOperation) _ => Err(Error::InvalidOperation)
} }

View File

@@ -1,8 +1,8 @@
use crate::database::*; use crate::database::*;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
#[delete("/<target>")] #[delete("/<target>")]
pub async fn req(user: User, target: Ref) -> Result<()> { pub async fn req(user: User, target: Ref) -> Result<EmptyResponse> {
let target = target.fetch_invite().await?; let target = target.fetch_invite().await?;
if target.creator() == &user.id { if target.creator() == &user.id {
@@ -20,7 +20,8 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
return Err(Error::MissingPermission); return Err(Error::MissingPermission);
} }
target.delete().await target.delete().await;
Ok(EmptyResponse {})
} }
_ => unreachable!(), _ => unreachable!(),
} }

View File

@@ -1,5 +1,5 @@
use crate::database::*; use crate::database::*;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc; use mongodb::bson::doc;
use rauth::auth::Session; use rauth::auth::Session;
@@ -19,7 +19,7 @@ pub struct Data {
} }
#[post("/complete", data = "<data>")] #[post("/complete", data = "<data>")]
pub async fn req(session: Session, user: Option<User>, data: Json<Data>) -> Result<()> { pub async fn req(session: Session, user: Option<User>, data: Json<Data>) -> Result<EmptyResponse> {
if user.is_some() { if user.is_some() {
Err(Error::AlreadyOnboarded)? Err(Error::AlreadyOnboarded)?
} }
@@ -45,5 +45,5 @@ pub async fn req(session: Session, user: Option<User>, data: Json<Data>) -> Resu
with: "user", with: "user",
})?; })?;
Ok(()) Ok(EmptyResponse {})
} }

View File

@@ -4,7 +4,7 @@ use crate::util::result::{Error, Result};
use mongodb::bson::{doc, to_document}; use mongodb::bson::{doc, to_document};
use rauth::auth::Session; use rauth::auth::Session;
use rocket::serde::json::Json; use rocket::serde::json::Json;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize, EmptyResponse};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Subscription { pub struct Subscription {
@@ -14,7 +14,7 @@ pub struct Subscription {
} }
#[post("/subscribe", data = "<data>")] #[post("/subscribe", data = "<data>")]
pub async fn req(session: Session, data: Json<Subscription>) -> Result<()> { pub async fn req(session: Session, data: Json<Subscription>) -> Result<EmptyResponse> {
let data = data.into_inner(); let data = data.into_inner();
get_collection("accounts") get_collection("accounts")
.update_one( .update_one(
@@ -33,5 +33,5 @@ pub async fn req(session: Session, data: Json<Subscription>) -> Result<()> {
.await .await
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "account" })?; .map_err(|_| Error::DatabaseError { operation: "update_one", with: "account" })?;
Ok(()) Ok(EmptyResponse {})
} }

View File

@@ -1,11 +1,11 @@
use crate::database::*; use crate::database::*;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc; use mongodb::bson::doc;
use rauth::auth::Session; use rauth::auth::Session;
#[post("/unsubscribe")] #[post("/unsubscribe")]
pub async fn req(session: Session) -> Result<()> { pub async fn req(session: Session) -> Result<EmptyResponse> {
get_collection("accounts") get_collection("accounts")
.update_one( .update_one(
doc! { doc! {
@@ -25,5 +25,5 @@ pub async fn req(session: Session) -> Result<()> {
with: "subscription", with: "subscription",
})?; })?;
Ok(()) Ok(EmptyResponse {})
} }

View File

@@ -1,5 +1,5 @@
use crate::database::*; use crate::database::*;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc; use mongodb::bson::doc;
use rocket::serde::json::Json; use rocket::serde::json::Json;
@@ -13,7 +13,7 @@ pub struct Data {
} }
#[put("/<server>/bans/<target>", data = "<data>")] #[put("/<server>/bans/<target>", data = "<data>")]
pub async fn req(user: User, server: Ref, target: Ref, data: Json<Data>) -> Result<()> { pub async fn req(user: User, server: Ref, target: Ref, data: Json<Data>) -> Result<EmptyResponse> {
let data = data.into_inner(); let data = data.into_inner();
data.validate() data.validate()
.map_err(|error| Error::FailedValidation { error })?; .map_err(|error| Error::FailedValidation { error })?;
@@ -57,5 +57,6 @@ pub async fn req(user: User, server: Ref, target: Ref, data: Json<Data>) -> Resu
with: "server_ban", with: "server_ban",
})?; })?;
server.remove_member(&target.id, RemoveMember::Ban).await server.remove_member(&target.id, RemoveMember::Ban).await;
Ok(EmptyResponse {})
} }

View File

@@ -1,10 +1,10 @@
use crate::database::*; use crate::database::*;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc; use mongodb::bson::doc;
#[delete("/<server>/bans/<target>")] #[delete("/<server>/bans/<target>")]
pub async fn req(user: User, server: Ref, target: Ref) -> Result<()> { pub async fn req(user: User, server: Ref, target: Ref) -> Result<EmptyResponse> {
let server = server.fetch_server().await?; let server = server.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user) let perm = permissions::PermissionCalculator::new(&user)
@@ -39,5 +39,5 @@ pub async fn req(user: User, server: Ref, target: Ref) -> Result<()> {
with: "server_ban", with: "server_ban",
})?; })?;
Ok(()) Ok(EmptyResponse {})
} }

View File

@@ -1,7 +1,7 @@
use std::collections::HashSet; use std::collections::HashSet;
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use crate::{database::*, notifications::events::RemoveMemberField}; use crate::{database::*, notifications::events::RemoveMemberField};
use mongodb::bson::{doc, to_document}; use mongodb::bson::{doc, to_document};
@@ -19,7 +19,7 @@ pub struct Data {
} }
#[patch("/<server>/members/<target>", data = "<data>")] #[patch("/<server>/members/<target>", data = "<data>")]
pub async fn req(user: User, server: Ref, target: String, data: Json<Data>) -> Result<()> { pub async fn req(user: User, server: Ref, target: String, data: Json<Data>) -> Result<EmptyResponse> {
let data = data.into_inner(); let data = data.into_inner();
data.validate() data.validate()
.map_err(|error| Error::FailedValidation { error })?; .map_err(|error| Error::FailedValidation { error })?;
@@ -154,5 +154,5 @@ pub async fn req(user: User, server: Ref, target: String, data: Json<Data>) -> R
} }
} }
Ok(()) Ok(EmptyResponse {})
} }

View File

@@ -1,10 +1,10 @@
use crate::database::*; use crate::database::*;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc; use mongodb::bson::doc;
#[delete("/<target>/members/<member>")] #[delete("/<target>/members/<member>")]
pub async fn req(user: User, target: Ref, member: String) -> Result<()> { pub async fn req(user: User, target: Ref, member: String) -> Result<EmptyResponse> {
let target = target.fetch_server().await?; let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user) let perm = permissions::PermissionCalculator::new(&user)
@@ -27,5 +27,7 @@ pub async fn req(user: User, target: Ref, member: String) -> Result<()> {
target target
.remove_member(&member.id.user, RemoveMember::Kick) .remove_member(&member.id.user, RemoveMember::Kick)
.await .await;
Ok(EmptyResponse {})
} }

View File

@@ -6,7 +6,7 @@ use crate::database::*;
use crate::database::permissions::channel::ChannelPermission; use crate::database::permissions::channel::ChannelPermission;
use crate::database::permissions::server::ServerPermission; use crate::database::permissions::server::ServerPermission;
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Values { pub struct Values {
@@ -20,7 +20,7 @@ pub struct Data {
} }
#[put("/<target>/permissions/<role_id>", data = "<data>", rank = 2)] #[put("/<target>/permissions/<role_id>", data = "<data>", rank = 2)]
pub async fn req(user: User, target: Ref, role_id: String, data: Json<Data>) -> Result<()> { pub async fn req(user: User, target: Ref, role_id: String, data: Json<Data>) -> Result<EmptyResponse> {
let target = target.fetch_server().await?; let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user) let perm = permissions::PermissionCalculator::new(&user)
@@ -38,7 +38,7 @@ pub async fn req(user: User, target: Ref, role_id: String, data: Json<Data>) ->
let server_permissions: u32 = ServerPermission::View as u32 | data.permissions.server; let server_permissions: u32 = ServerPermission::View as u32 | data.permissions.server;
let channel_permissions: u32 = ChannelPermission::View as u32 | data.permissions.channel; let channel_permissions: u32 = ChannelPermission::View as u32 | data.permissions.channel;
get_collection("servers") get_collection("servers")
.update_one( .update_one(
doc! { "_id": &target.id }, doc! { "_id": &target.id },
@@ -57,7 +57,7 @@ pub async fn req(user: User, target: Ref, role_id: String, data: Json<Data>) ->
operation: "update_one", operation: "update_one",
with: "server" with: "server"
})?; })?;
ClientboundNotification::ServerRoleUpdate { ClientboundNotification::ServerRoleUpdate {
id: target.id.clone(), id: target.id.clone(),
role_id, role_id,
@@ -71,5 +71,5 @@ pub async fn req(user: User, target: Ref, role_id: String, data: Json<Data>) ->
} }
.publish(target.id); .publish(target.id);
Ok(()) Ok(EmptyResponse)
} }

View File

@@ -6,7 +6,7 @@ use crate::database::*;
use crate::database::permissions::channel::ChannelPermission; use crate::database::permissions::channel::ChannelPermission;
use crate::database::permissions::server::ServerPermission; use crate::database::permissions::server::ServerPermission;
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Values { pub struct Values {
@@ -20,7 +20,7 @@ pub struct Data {
} }
#[put("/<target>/permissions/default", data = "<data>", rank = 1)] #[put("/<target>/permissions/default", data = "<data>", rank = 1)]
pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> { pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<EmptyResponse> {
let target = target.fetch_server().await?; let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user) let perm = permissions::PermissionCalculator::new(&user)
@@ -34,7 +34,7 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
let server_permissions: u32 = ServerPermission::View as u32 | data.permissions.server; let server_permissions: u32 = ServerPermission::View as u32 | data.permissions.server;
let channel_permissions: u32 = ChannelPermission::View as u32 | data.permissions.channel; let channel_permissions: u32 = ChannelPermission::View as u32 | data.permissions.channel;
get_collection("servers") get_collection("servers")
.update_one( .update_one(
doc! { "_id": &target.id }, doc! { "_id": &target.id },
@@ -53,7 +53,7 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
operation: "update_one", operation: "update_one",
with: "server" with: "server"
})?; })?;
ClientboundNotification::ServerUpdate { ClientboundNotification::ServerUpdate {
id: target.id.clone(), id: target.id.clone(),
data: json!({ data: json!({
@@ -66,5 +66,5 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
} }
.publish(target.id); .publish(target.id);
Ok(()) Ok(EmptyResponse {})
} }

View File

@@ -1,11 +1,11 @@
use crate::database::*; use crate::database::*;
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc; use mongodb::bson::doc;
#[delete("/<target>/roles/<role_id>")] #[delete("/<target>/roles/<role_id>")]
pub async fn req(user: User, target: Ref, role_id: String) -> Result<()> { pub async fn req(user: User, target: Ref, role_id: String) -> Result<EmptyResponse> {
let target = target.fetch_server().await?; let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user) let perm = permissions::PermissionCalculator::new(&user)
@@ -70,12 +70,12 @@ pub async fn req(user: User, target: Ref, role_id: String) -> Result<()> {
operation: "update_many", operation: "update_many",
with: "server_members" with: "server_members"
})?; })?;
ClientboundNotification::ServerRoleDelete { ClientboundNotification::ServerRoleDelete {
id: target.id.clone(), id: target.id.clone(),
role_id role_id
} }
.publish(target.id); .publish(target.id);
Ok(()) Ok(EmptyResponse {})
} }

View File

@@ -1,5 +1,5 @@
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use crate::{database::*, notifications::events::RemoveRoleField}; use crate::{database::*, notifications::events::RemoveRoleField};
use mongodb::bson::doc; use mongodb::bson::doc;
@@ -19,7 +19,7 @@ pub struct Data {
} }
#[patch("/<target>/roles/<role_id>", data = "<data>")] #[patch("/<target>/roles/<role_id>", data = "<data>")]
pub async fn req(user: User, target: Ref, role_id: String, data: Json<Data>) -> Result<()> { pub async fn req(user: User, target: Ref, role_id: String, data: Json<Data>) -> Result<EmptyResponse> {
let data = data.into_inner(); let data = data.into_inner();
data.validate() data.validate()
.map_err(|error| Error::FailedValidation { error })?; .map_err(|error| Error::FailedValidation { error })?;
@@ -106,5 +106,5 @@ pub async fn req(user: User, target: Ref, role_id: String, data: Json<Data>) ->
} }
.publish(target.id.clone()); .publish(target.id.clone());
Ok(()) Ok(EmptyResponse {})
} }

View File

@@ -1,12 +1,12 @@
use crate::database::*; use crate::database::*;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
#[put("/<target>/ack")] #[put("/<target>/ack")]
pub async fn req(user: User, target: Ref) -> Result<()> { pub async fn req(user: User, target: Ref) -> Result<EmptyResponse> {
if user.bot.is_some() { if user.bot.is_some() {
return Err(Error::IsBot) return Err(Error::IsBot)
} }
let target = target.fetch_server().await?; let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user) let perm = permissions::PermissionCalculator::new(&user)
@@ -18,5 +18,6 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
Err(Error::MissingPermission)? Err(Error::MissingPermission)?
} }
target.mark_as_read(&user.id).await target.mark_as_read(&user.id).await;
Ok(EmptyResponse {})
} }

View File

@@ -1,10 +1,10 @@
use crate::database::*; use crate::database::*;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc; use mongodb::bson::doc;
#[delete("/<target>")] #[delete("/<target>")]
pub async fn req(user: User, target: Ref) -> Result<()> { pub async fn req(user: User, target: Ref) -> Result<EmptyResponse> {
let target = target.fetch_server().await?; let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user) let perm = permissions::PermissionCalculator::new(&user)
.with_server(&target) .with_server(&target)

View File

@@ -1,5 +1,5 @@
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use crate::{database::*, notifications::events::RemoveServerField}; use crate::{database::*, notifications::events::RemoveServerField};
use mongodb::bson::{doc, to_bson, to_document}; use mongodb::bson::{doc, to_bson, to_document};
@@ -21,14 +21,14 @@ pub struct Data {
} }
#[patch("/<target>", data = "<data>")] #[patch("/<target>", data = "<data>")]
pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> { pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<EmptyResponse> {
let data = data.into_inner(); let data = data.into_inner();
data.validate() data.validate()
.map_err(|error| Error::FailedValidation { error })?; .map_err(|error| Error::FailedValidation { error })?;
if data.name.is_none() && data.description.is_none() && data.icon.is_none() && data.banner.is_none() && data.remove.is_none() && data.categories.is_none() && data.system_messages.is_none() if data.name.is_none() && data.description.is_none() && data.icon.is_none() && data.banner.is_none() && data.remove.is_none() && data.categories.is_none() && data.system_messages.is_none()
{ {
return Ok(()); return Ok(EmptyResponse {});
} }
let target = target.fetch_server().await?; let target = target.fetch_server().await?;
@@ -145,5 +145,5 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
} }
} }
Ok(()) Ok(EmptyResponse {})
} }

View File

@@ -1,6 +1,6 @@
use crate::database::*; use crate::database::*;
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use chrono::prelude::*; use chrono::prelude::*;
use mongodb::bson::{doc, to_bson}; use mongodb::bson::{doc, to_bson};
@@ -18,11 +18,11 @@ pub struct Options {
} }
#[post("/settings/set?<options..>", data = "<data>")] #[post("/settings/set?<options..>", data = "<data>")]
pub async fn req(user: User, data: Json<Data>, options: Options) -> Result<()> { pub async fn req(user: User, data: Json<Data>, options: Options) -> Result<EmptyResponse> {
if user.bot.is_some() { if user.bot.is_some() {
return Err(Error::IsBot) return Err(Error::IsBot)
} }
let data = data.into_inner(); let data = data.into_inner();
let current_time = Utc::now().timestamp_millis(); let current_time = Utc::now().timestamp_millis();
let timestamp = if let Some(timestamp) = options.timestamp { let timestamp = if let Some(timestamp) = options.timestamp {
@@ -70,5 +70,5 @@ pub async fn req(user: User, data: Json<Data>, options: Options) -> Result<()> {
} }
.publish(user.id); .publish(user.id);
Ok(()) Ok(EmptyResponse {})
} }

View File

@@ -1,6 +1,6 @@
use crate::database::*; use crate::database::*;
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc; use mongodb::bson::doc;
use rauth::auth::{Auth, Session}; use rauth::auth::{Auth, Session};
@@ -31,11 +31,11 @@ pub async fn req(
user: User, user: User,
data: Json<Data>, data: Json<Data>,
_ignore_id: String, _ignore_id: String,
) -> Result<()> { ) -> Result<EmptyResponse> {
if user.bot.is_some() { if user.bot.is_some() {
return Err(Error::IsBot) return Err(Error::IsBot)
} }
data.validate() data.validate()
.map_err(|error| Error::FailedValidation { error })?; .map_err(|error| Error::FailedValidation { error })?;
@@ -69,5 +69,5 @@ pub async fn req(
} }
.publish_as_user(user.id.clone()); .publish_as_user(user.id.clone());
Ok(()) Ok(EmptyResponse {})
} }

View File

@@ -1,5 +1,5 @@
use crate::notifications::events::ClientboundNotification; use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result}; use crate::util::result::{Error, Result, EmptyResponse};
use crate::{database::*, notifications::events::RemoveUserField}; use crate::{database::*, notifications::events::RemoveUserField};
use mongodb::bson::{doc, to_document}; use mongodb::bson::{doc, to_document};
@@ -29,7 +29,7 @@ pub struct Data {
} }
#[patch("/<_ignore_id>", data = "<data>")] #[patch("/<_ignore_id>", data = "<data>")]
pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()> { pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<EmptyResponse> {
let mut data = data.into_inner(); let mut data = data.into_inner();
data.validate() data.validate()
@@ -40,7 +40,7 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()>
&& data.avatar.is_none() && data.avatar.is_none()
&& data.remove.is_none() && data.remove.is_none()
{ {
return Ok(()); return Ok(EmptyResponse {});
} }
let mut unset = doc! {}; let mut unset = doc! {};
@@ -152,5 +152,5 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()>
} }
} }
Ok(()) Ok(EmptyResponse {})
} }

View File

@@ -68,8 +68,17 @@ pub enum Error {
NoEffect, NoEffect,
} }
pub struct EmptyResponse;
pub type Result<T, E = Error> = std::result::Result<T, E>; pub type Result<T, E = Error> = std::result::Result<T, E>;
impl<'r> Responder<'r> for EmptyResponse {
fn respond_to(self, req: &Request) -> response::Result<'r> {
Response::build()
.status(rocket::http::Status { code: 204 })
.ok()
}
}
/// HTTP response builder for Error enum /// HTTP response builder for Error enum
impl<'r> Responder<'r, 'static> for Error { impl<'r> Responder<'r, 'static> for Error {
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> { fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {