Restrict bots from creating servers, groups, acking messages, and making friends.

This commit is contained in:
Paul
2021-08-11 20:16:11 +01:00
parent 084d71f050
commit 93610ebbc3
16 changed files with 65 additions and 5 deletions

View File

@@ -7,6 +7,10 @@ use mongodb::options::UpdateOptions;
#[put("/<target>/ack/<message>")]
pub async fn req(user: User, target: Ref, message: Ref) -> Result<()> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let target = target.fetch_channel().await?;
let perm = permissions::PermissionCalculator::new(&user)

View File

@@ -24,6 +24,10 @@ pub struct Data {
#[post("/create", data = "<info>")]
pub async fn req(user: User, info: Json<Data>) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let info = info.into_inner();
info.validate()
.map_err(|error| Error::FailedValidation { error })?;

View File

@@ -1,10 +1,14 @@
use crate::database::*;
use crate::util::result::Result;
use crate::util::result::{Error, Result};
use rocket::serde::json::Value;
#[post("/<target>")]
pub async fn req(user: User, target: Ref) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let target = target.fetch_invite().await?;
match target {

View File

@@ -3,6 +3,10 @@ use crate::util::result::{Error, Result};
#[put("/<target>/ack")]
pub async fn req(user: User, target: Ref) -> Result<()> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let target = target.fetch_server().await?;
let perm = permissions::PermissionCalculator::new(&user)

View File

@@ -22,6 +22,10 @@ pub struct Data {
#[post("/create", data = "<info>")]
pub async fn req(user: User, info: Json<Data>) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let info = info.into_inner();
info.validate()
.map_err(|error| Error::FailedValidation { error })?;

View File

@@ -13,6 +13,10 @@ pub struct Options {
#[post("/settings/fetch", data = "<options>")]
pub async fn req(user: User, options: Json<Options>) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let options = options.into_inner();
let mut projection = doc! {
"_id": 0,

View File

@@ -6,5 +6,9 @@ use rocket::serde::json::Value;
#[get("/unreads")]
pub async fn req(user: User) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
Ok(json!(User::fetch_unreads(&user.id).await?))
}

View File

@@ -19,6 +19,10 @@ pub struct Options {
#[post("/settings/set?<options..>", data = "<data>")]
pub async fn req(user: User, data: Json<Data>, options: Options) -> Result<()> {
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 {

View File

@@ -9,6 +9,10 @@ use rocket::serde::json::Value;
#[put("/<username>/friend")]
pub async fn req(user: User, username: String) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let col = get_collection("users");
let doc = col
.find_one(

View File

@@ -8,8 +8,11 @@ use rocket::serde::json::Value;
#[put("/<target>/block")]
pub async fn req(user: User, target: Ref) -> Result<Value> {
let col = get_collection("users");
if user.bot.is_some() {
return Err(Error::IsBot)
}
let col = get_collection("users");
let target = target.fetch_user().await?;
match get_relationship(&user, &target.id) {

View File

@@ -32,6 +32,10 @@ pub async fn req(
data: Json<Data>,
_ignore_id: String,
) -> Result<()> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
data.validate()
.map_err(|error| Error::FailedValidation { error })?;

View File

@@ -1,9 +1,13 @@
use crate::database::*;
use crate::util::result::Result;
use crate::util::result::{Error, Result};
use rocket::serde::json::Value;
#[get("/<target>/relationship")]
pub async fn req(user: User, target: Ref) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
Ok(json!({ "status": get_relationship(&user, &target.id) }))
}

View File

@@ -1,10 +1,14 @@
use crate::database::*;
use crate::util::result::Result;
use crate::util::result::{Error, Result};
use rocket::serde::json::Value;
#[get("/relationships")]
pub async fn req(user: User) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
Ok(if let Some(vec) = user.relations {
json!(vec)
} else {

View File

@@ -8,8 +8,11 @@ use rocket::serde::json::Value;
#[delete("/<target>/friend")]
pub async fn req(user: User, target: Ref) -> Result<Value> {
let col = get_collection("users");
if user.bot.is_some() {
return Err(Error::IsBot)
}
let col = get_collection("users");
let target = target.fetch_user().await?;
match get_relationship(&user, &target.id) {

View File

@@ -8,6 +8,10 @@ use rocket::serde::json::Value;
#[delete("/<target>/block")]
pub async fn req(user: User, target: Ref) -> Result<Value> {
if user.bot.is_some() {
return Err(Error::IsBot)
}
let col = get_collection("users");
let target = target.fetch_user().await?;

View File

@@ -46,6 +46,7 @@ pub enum Error {
// ? Bot related errors.
ReachedMaximumBots,
IsBot,
// ? General errors.
TooManyIds,
@@ -102,6 +103,7 @@ impl<'r> Responder<'r, 'static> for Error {
Error::Banned => Status::Forbidden,
Error::ReachedMaximumBots => Status::BadRequest,
Error::IsBot => Status::BadRequest,
Error::FailedValidation { .. } => Status::UnprocessableEntity,
Error::DatabaseError { .. } => Status::InternalServerError,