Merge remote-tracking branch 'revoltchat/master' into webhooks

This commit is contained in:
Zomatree
2023-04-18 19:50:04 +01:00
6 changed files with 47 additions and 19 deletions

View File

@@ -10,7 +10,7 @@ use crate::{
Channel,
},
tasks::{ack::AckEvent, process_embeds},
Database, Error, OverrideField, Result, types::push::MessageAuthor, web::idempotency::IdempotencyKey, Ref,
Database, Error, OverrideField, Result, types::push::MessageAuthor, web::idempotency::IdempotencyKey, Ref, variables::delta::{MAX_ATTACHMENT_COUNT, MAX_REPLY_COUNT},
};
impl Channel {
@@ -458,8 +458,8 @@ impl Channel {
// Verify replies are valid.
let mut replies = HashSet::new();
if let Some(entries) = data.replies {
if entries.len() > 5 {
return Err(Error::TooManyReplies);
if entries.len() > *MAX_REPLY_COUNT {
return Err(Error::TooManyReplies { max: *MAX_REPLY_COUNT });
}
for Reply { id, mention } in entries {
@@ -498,9 +498,8 @@ impl Channel {
// Add attachments to message.
let mut attachments = vec![];
if let Some(ids) = &data.attachments {
// ! FIXME: move this to app config
if ids.len() > 5 {
return Err(Error::TooManyAttachments);
if ids.len() > *MAX_ATTACHMENT_COUNT {
return Err(Error::TooManyAttachments { max: *MAX_ATTACHMENT_COUNT} );
}
for attachment_id in ids {

View File

@@ -3,7 +3,6 @@ use revolt_rocket_okapi::revolt_okapi::openapi3;
use rocket::{
http::{ContentType, Status},
response::{self, Responder},
serde::json::serde_json::json,
Request, Response,
};
use schemars::schema::Schema;
@@ -39,8 +38,15 @@ pub enum Error {
UnknownMessage,
CannotEditMessage,
CannotJoinCall,
TooManyAttachments,
TooManyReplies,
TooManyAttachments {
max: usize
},
TooManyReplies {
max: usize
},
TooManyChannels {
max: usize
},
EmptyMessage,
PayloadTooLarge,
CannotRemoveYourself,
@@ -57,7 +63,12 @@ pub enum Error {
TooManyServers {
max: usize,
},
TooManyEmoji,
TooManyEmoji {
max: usize
},
TooManyRoles {
max: usize
},
// ? Bot related errors
ReachedMaximumBots,
@@ -151,8 +162,8 @@ impl<'r> Responder<'r, 'static> for Error {
Error::UnknownAttachment => Status::BadRequest,
Error::CannotEditMessage => Status::Forbidden,
Error::CannotJoinCall => Status::BadRequest,
Error::TooManyAttachments => Status::BadRequest,
Error::TooManyReplies => Status::BadRequest,
Error::TooManyAttachments { .. } => Status::BadRequest,
Error::TooManyReplies { .. } => Status::BadRequest,
Error::EmptyMessage => Status::UnprocessableEntity,
Error::PayloadTooLarge => Status::UnprocessableEntity,
Error::CannotRemoveYourself => Status::BadRequest,
@@ -163,8 +174,11 @@ impl<'r> Responder<'r, 'static> for Error {
Error::UnknownServer => Status::NotFound,
Error::InvalidRole => Status::NotFound,
Error::Banned => Status::Forbidden,
Error::TooManyServers { .. } => Status::Forbidden,
Error::TooManyEmoji => Status::BadRequest,
Error::TooManyServers { .. } => Status::BadRequest,
Error::TooManyEmoji { .. } => Status::BadRequest,
Error::TooManyChannels { .. } => Status::BadRequest,
Error::TooManyRoles { .. } => Status::BadRequest,
Error::ReachedMaximumBots => Status::BadRequest,
Error::IsBot => Status::BadRequest,
@@ -193,7 +207,7 @@ impl<'r> Responder<'r, 'static> for Error {
};
// Serialize the error data structure into JSON.
let string = json!(self).to_string();
let string = serde_json::to_string(&self).unwrap();
// Build and send the request.
Response::build()

View File

@@ -44,6 +44,12 @@ pub static MAX_GROUP_SIZE: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_GROUP
pub static MAX_BOT_COUNT: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_BOT_COUNT").unwrap_or_else(|_| "5".to_string()).parse().unwrap());
pub static MAX_EMBED_COUNT: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_EMBED_COUNT").unwrap_or_else(|_| "5".to_string()).parse().unwrap());
pub static MAX_SERVER_COUNT: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_SERVER_COUNT").unwrap_or_else(|_| "100".to_string()).parse().unwrap());
pub static MAX_CHANNEL_COUNT: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_CHANNEL_COUNT").unwrap_or_else(|_| "200".to_string()).parse().unwrap());
pub static MAX_ROLE_COUNT: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_ROLE_COUNT").unwrap_or_else(|_| "200".to_string()).parse().unwrap());
pub static MAX_EMOJI_COUNT: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_EMOJI_COUNT").unwrap_or_else(|_| "100".to_string()).parse().unwrap());
pub static MAX_ATTACHMENT_COUNT: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_ATTACHMENT_COUNT").unwrap_or_else(|_| "5".to_string()).parse().unwrap());
pub static MAX_REPLY_COUNT: Lazy<usize> = Lazy::new(|| env::var("REVOLT_MAX_REPLY_COUNT").unwrap_or_else(|_| "5".to_string()).parse().unwrap());
pub static EARLY_ADOPTER_BADGE: Lazy<i64> = Lazy::new(|| env::var("REVOLT_EARLY_ADOPTER_BADGE").unwrap_or_else(|_| "0".to_string()).parse().unwrap());
pub fn preflight_checks() {