Enhancement: Routes with no body should return 204 No Content.

This commit is contained in:
heikkari
2021-08-16 15:34:44 +03:00
parent 48d2fbe5ae
commit 721ca3a4f5
30 changed files with 124 additions and 116 deletions

View File

@@ -1,16 +1,16 @@
use crate::database::*;
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc;
use mongodb::options::UpdateOptions;
#[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() {
return Err(Error::IsBot)
}
let target = target.fetch_channel().await?;
let perm = permissions::PermissionCalculator::new(&user)
@@ -52,5 +52,5 @@ pub async fn req(user: User, target: Ref, message: Ref) -> Result<()> {
}
.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 mongodb::bson::doc;
#[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 perm = permissions::PermissionCalculator::new(&user)
@@ -37,7 +37,7 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
with: "channel",
})?;
Ok(())
Ok(EmptyResponse {})
}
Channel::Group {
id,
@@ -103,7 +103,7 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
.await
.ok();
Ok(())
Ok(EmptyResponse {})
}
Channel::TextChannel { .. } |
Channel::VoiceChannel { .. } => {

View File

@@ -1,5 +1,5 @@
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 mongodb::bson::{doc, to_document};
@@ -21,7 +21,7 @@ pub struct 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();
data.validate()
.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.remove.is_none()
{
return Ok(());
return Ok(EmptyResponse {});
}
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),
}

View File

@@ -1,10 +1,10 @@
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
use crate::database::*;
use mongodb::bson::doc;
#[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 {
Err(Error::NotFriends)?
}
@@ -19,5 +19,6 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
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 mongodb::bson::doc;
#[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 {
Err(Error::CannotRemoveYourself)?
}
@@ -59,7 +59,7 @@ pub async fn req(user: User, target: Ref, member: Ref) -> Result<()> {
.await
.ok();
Ok(())
Ok(EmptyResponse {})
} else {
Err(Error::InvalidOperation)
}

View File

@@ -1,10 +1,10 @@
use crate::database::*;
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
use mongodb::bson::doc;
#[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?;
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::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
use chrono::Utc;
use mongodb::bson::{doc, Bson, DateTime, Document};
@@ -14,7 +14,7 @@ pub struct Data {
}
#[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()
.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",
})?;
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::permissions::channel::ChannelPermission;
use crate::notifications::events::ClientboundNotification;
use crate::util::result::{Error, Result};
use crate::util::result::{Error, Result, EmptyResponse};
#[derive(Serialize, Deserialize)]
pub struct Data {
@@ -14,7 +14,7 @@ pub struct Data {
}
#[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?;
match target {
@@ -25,7 +25,7 @@ pub async fn req(user: User, target: Ref, role: String, data: Json<Data>) -> Res
.with_server(&target)
.for_server()
.await?;
if !perm.get_manage_roles() {
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) {
return Err(Error::NotFound);
}
let permissions: u32 = ChannelPermission::View as u32 | data.permissions;
get_collection("channels")
.update_one(
doc! { "_id": &id },
@@ -51,7 +51,7 @@ pub async fn req(user: User, target: Ref, role: String, data: Json<Data>) -> Res
operation: "update_one",
with: "channel"
})?;
role_permissions.insert(role, permissions as i32);
ClientboundNotification::ChannelUpdate {
id: id.clone(),
@@ -62,7 +62,7 @@ pub async fn req(user: User, target: Ref, role: String, data: Json<Data>) -> Res
}
.publish(id);
Ok(())
Ok(EmptyResponse {})
}
_ => Err(Error::InvalidOperation)
}

View File

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