forked from jmug/stoatchat
Merge pull request #38 from heikkari/13-24
This commit is contained in:
@@ -15,6 +15,7 @@ pub enum WebSocketError {
|
||||
InvalidSession,
|
||||
OnboardingNotFinished,
|
||||
AlreadyAuthenticated,
|
||||
MalformedData { msg: String },
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
|
||||
@@ -74,7 +74,20 @@ async fn accept(stream: TcpStream) {
|
||||
let incoming = read.try_for_each(async move |msg| {
|
||||
let mutex = mutex_generator();
|
||||
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 {
|
||||
ServerboundNotification::Authenticate(auth) => {
|
||||
{
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::database::*;
|
||||
use crate::notifications::events::ClientboundNotification;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::util::result::{Error, EmptyResponse, Result};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
|
||||
#[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?;
|
||||
if bot.owner != user.id {
|
||||
return Err(Error::MissingPermission);
|
||||
@@ -58,6 +58,6 @@ pub async fn delete_bot(user: User, target: Ref) -> Result<()> {
|
||||
with: "bot",
|
||||
operation: "delete_one"
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
|
||||
@@ -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::RemoveBotField};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
@@ -25,7 +25,7 @@ pub struct 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();
|
||||
data.validate()
|
||||
.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 {})
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::util::result::{Error, Result, EmptyResponse};
|
||||
|
||||
use rocket::serde::json::Json;
|
||||
use serde::Deserialize;
|
||||
@@ -22,7 +22,7 @@ pub enum Destination {
|
||||
}
|
||||
|
||||
#[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?;
|
||||
|
||||
if !bot.public {
|
||||
@@ -39,13 +39,13 @@ pub async fn invite_bot(user: User, target: Ref, dest: Json<Destination>) -> Res
|
||||
.with_server(&server)
|
||||
.for_server()
|
||||
.await?;
|
||||
|
||||
|
||||
if !perm.get_manage_server() {
|
||||
Err(Error::MissingPermission)?
|
||||
}
|
||||
|
||||
server.join_member(&bot.id).await?;
|
||||
Ok(())
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
Destination::Group(GroupId { group }) => {
|
||||
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)
|
||||
.for_channel()
|
||||
.await?;
|
||||
|
||||
|
||||
if !perm.get_invite_others() {
|
||||
Err(Error::MissingPermission)?
|
||||
}
|
||||
|
||||
@@ -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 {})
|
||||
}
|
||||
|
||||
@@ -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 { .. } => {
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
|
||||
@@ -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 {})
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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 {})
|
||||
}
|
||||
|
||||
@@ -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 {})
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::util::result::{Error, Result, EmptyResponse};
|
||||
|
||||
#[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?;
|
||||
|
||||
if target.creator() == &user.id {
|
||||
@@ -20,7 +20,8 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
|
||||
return Err(Error::MissingPermission);
|
||||
}
|
||||
|
||||
target.delete().await
|
||||
target.delete().await;
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::util::result::{Error, Result, EmptyResponse};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use rauth::auth::Session;
|
||||
@@ -19,7 +19,7 @@ pub struct 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() {
|
||||
Err(Error::AlreadyOnboarded)?
|
||||
}
|
||||
@@ -45,5 +45,5 @@ pub async fn req(session: Session, user: Option<User>, data: Json<Data>) -> Resu
|
||||
with: "user",
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use crate::util::result::{Error, Result};
|
||||
use mongodb::bson::{doc, to_document};
|
||||
use rauth::auth::Session;
|
||||
use rocket::serde::json::Json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::{Deserialize, Serialize, EmptyResponse};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Subscription {
|
||||
@@ -14,7 +14,7 @@ pub struct Subscription {
|
||||
}
|
||||
|
||||
#[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();
|
||||
get_collection("accounts")
|
||||
.update_one(
|
||||
@@ -33,5 +33,5 @@ pub async fn req(session: Session, data: Json<Subscription>) -> Result<()> {
|
||||
.await
|
||||
.map_err(|_| Error::DatabaseError { operation: "update_one", with: "account" })?;
|
||||
|
||||
Ok(())
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::util::result::{Error, Result, EmptyResponse};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use rauth::auth::Session;
|
||||
|
||||
#[post("/unsubscribe")]
|
||||
pub async fn req(session: Session) -> Result<()> {
|
||||
pub async fn req(session: Session) -> Result<EmptyResponse> {
|
||||
get_collection("accounts")
|
||||
.update_one(
|
||||
doc! {
|
||||
@@ -25,5 +25,5 @@ pub async fn req(session: Session) -> Result<()> {
|
||||
with: "subscription",
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::util::result::{Error, Result, EmptyResponse};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
use rocket::serde::json::Json;
|
||||
@@ -13,7 +13,7 @@ pub struct 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();
|
||||
data.validate()
|
||||
.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",
|
||||
})?;
|
||||
|
||||
server.remove_member(&target.id, RemoveMember::Ban).await
|
||||
server.remove_member(&target.id, RemoveMember::Ban).await;
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
|
||||
@@ -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("/<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 perm = permissions::PermissionCalculator::new(&user)
|
||||
@@ -39,5 +39,5 @@ pub async fn req(user: User, server: Ref, target: Ref) -> Result<()> {
|
||||
with: "server_ban",
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
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 mongodb::bson::{doc, to_document};
|
||||
@@ -19,7 +19,7 @@ pub struct 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();
|
||||
data.validate()
|
||||
.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 {})
|
||||
}
|
||||
|
||||
@@ -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>/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 perm = permissions::PermissionCalculator::new(&user)
|
||||
@@ -27,5 +27,7 @@ pub async fn req(user: User, target: Ref, member: String) -> Result<()> {
|
||||
|
||||
target
|
||||
.remove_member(&member.id.user, RemoveMember::Kick)
|
||||
.await
|
||||
.await;
|
||||
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::database::*;
|
||||
use crate::database::permissions::channel::ChannelPermission;
|
||||
use crate::database::permissions::server::ServerPermission;
|
||||
use crate::notifications::events::ClientboundNotification;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::util::result::{Error, Result, EmptyResponse};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Values {
|
||||
@@ -20,7 +20,7 @@ pub struct Data {
|
||||
}
|
||||
|
||||
#[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 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 channel_permissions: u32 = ChannelPermission::View as u32 | data.permissions.channel;
|
||||
|
||||
|
||||
get_collection("servers")
|
||||
.update_one(
|
||||
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",
|
||||
with: "server"
|
||||
})?;
|
||||
|
||||
|
||||
ClientboundNotification::ServerRoleUpdate {
|
||||
id: target.id.clone(),
|
||||
role_id,
|
||||
@@ -71,5 +71,5 @@ pub async fn req(user: User, target: Ref, role_id: String, data: Json<Data>) ->
|
||||
}
|
||||
.publish(target.id);
|
||||
|
||||
Ok(())
|
||||
Ok(EmptyResponse)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::database::*;
|
||||
use crate::database::permissions::channel::ChannelPermission;
|
||||
use crate::database::permissions::server::ServerPermission;
|
||||
use crate::notifications::events::ClientboundNotification;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::util::result::{Error, Result, EmptyResponse};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Values {
|
||||
@@ -20,7 +20,7 @@ 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_server().await?;
|
||||
|
||||
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 channel_permissions: u32 = ChannelPermission::View as u32 | data.permissions.channel;
|
||||
|
||||
|
||||
get_collection("servers")
|
||||
.update_one(
|
||||
doc! { "_id": &target.id },
|
||||
@@ -53,7 +53,7 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
|
||||
operation: "update_one",
|
||||
with: "server"
|
||||
})?;
|
||||
|
||||
|
||||
ClientboundNotification::ServerUpdate {
|
||||
id: target.id.clone(),
|
||||
data: json!({
|
||||
@@ -66,5 +66,5 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
|
||||
}
|
||||
.publish(target.id);
|
||||
|
||||
Ok(())
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
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;
|
||||
|
||||
#[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 perm = permissions::PermissionCalculator::new(&user)
|
||||
@@ -70,12 +70,12 @@ pub async fn req(user: User, target: Ref, role_id: String) -> Result<()> {
|
||||
operation: "update_many",
|
||||
with: "server_members"
|
||||
})?;
|
||||
|
||||
|
||||
ClientboundNotification::ServerRoleDelete {
|
||||
id: target.id.clone(),
|
||||
role_id
|
||||
}
|
||||
.publish(target.id);
|
||||
|
||||
Ok(())
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
|
||||
@@ -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::RemoveRoleField};
|
||||
|
||||
use mongodb::bson::doc;
|
||||
@@ -19,7 +19,7 @@ pub struct 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();
|
||||
data.validate()
|
||||
.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());
|
||||
|
||||
Ok(())
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
use crate::database::*;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::util::result::{Error, Result, EmptyResponse};
|
||||
|
||||
#[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() {
|
||||
return Err(Error::IsBot)
|
||||
}
|
||||
|
||||
|
||||
let target = target.fetch_server().await?;
|
||||
|
||||
let perm = permissions::PermissionCalculator::new(&user)
|
||||
@@ -18,5 +18,6 @@ pub async fn req(user: User, target: Ref) -> Result<()> {
|
||||
Err(Error::MissingPermission)?
|
||||
}
|
||||
|
||||
target.mark_as_read(&user.id).await
|
||||
target.mark_as_read(&user.id).await;
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
|
||||
@@ -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>")]
|
||||
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 perm = permissions::PermissionCalculator::new(&user)
|
||||
.with_server(&target)
|
||||
|
||||
@@ -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::RemoveServerField};
|
||||
|
||||
use mongodb::bson::{doc, to_bson, to_document};
|
||||
@@ -21,14 +21,14 @@ 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 })?;
|
||||
|
||||
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?;
|
||||
@@ -145,5 +145,5 @@ pub async fn req(user: User, target: Ref, data: Json<Data>) -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::database::*;
|
||||
use crate::notifications::events::ClientboundNotification;
|
||||
use crate::util::result::{Error, Result};
|
||||
use crate::util::result::{Error, Result, EmptyResponse};
|
||||
|
||||
use chrono::prelude::*;
|
||||
use mongodb::bson::{doc, to_bson};
|
||||
@@ -18,11 +18,11 @@ pub struct Options {
|
||||
}
|
||||
|
||||
#[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() {
|
||||
return Err(Error::IsBot)
|
||||
}
|
||||
|
||||
|
||||
let data = data.into_inner();
|
||||
let current_time = Utc::now().timestamp_millis();
|
||||
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);
|
||||
|
||||
Ok(())
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
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 rauth::auth::{Auth, Session};
|
||||
@@ -31,11 +31,11 @@ pub async fn req(
|
||||
user: User,
|
||||
data: Json<Data>,
|
||||
_ignore_id: String,
|
||||
) -> Result<()> {
|
||||
) -> Result<EmptyResponse> {
|
||||
if user.bot.is_some() {
|
||||
return Err(Error::IsBot)
|
||||
}
|
||||
|
||||
|
||||
data.validate()
|
||||
.map_err(|error| Error::FailedValidation { error })?;
|
||||
|
||||
@@ -69,5 +69,5 @@ pub async fn req(
|
||||
}
|
||||
.publish_as_user(user.id.clone());
|
||||
|
||||
Ok(())
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
|
||||
@@ -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::RemoveUserField};
|
||||
|
||||
use mongodb::bson::{doc, to_document};
|
||||
@@ -29,7 +29,7 @@ pub struct 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();
|
||||
|
||||
data.validate()
|
||||
@@ -40,7 +40,7 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()>
|
||||
&& data.avatar.is_none()
|
||||
&& data.remove.is_none()
|
||||
{
|
||||
return Ok(());
|
||||
return Ok(EmptyResponse {});
|
||||
}
|
||||
|
||||
let mut unset = doc! {};
|
||||
@@ -152,5 +152,5 @@ pub async fn req(user: User, data: Json<Data>, _ignore_id: String) -> Result<()>
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(EmptyResponse {})
|
||||
}
|
||||
|
||||
@@ -68,8 +68,17 @@ pub enum Error {
|
||||
NoEffect,
|
||||
}
|
||||
|
||||
pub struct EmptyResponse;
|
||||
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
|
||||
impl<'r> Responder<'r, 'static> for Error {
|
||||
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {
|
||||
|
||||
Reference in New Issue
Block a user