Skip to content

Commit

Permalink
feat(core): separate limits for new user accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
insertish committed Jun 25, 2024
1 parent e5eea26 commit 6ec8007
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 22 deletions.
3 changes: 1 addition & 2 deletions crates/core/config/Revolt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ webhooks_enabled = false

[features.limits.global]
group_size = 100
message_embeds = 5
message_replies = 5
message_reactions = 20
server_emoji = 100
Expand All @@ -61,7 +62,6 @@ outgoing_friend_requests = 5

bots = 2
message_length = 2000
message_embeds = 5
message_attachments = 5
servers = 100

Expand All @@ -77,7 +77,6 @@ outgoing_friend_requests = 10

bots = 5
message_length = 2000
message_embeds = 5
message_attachments = 5
servers = 100

Expand Down
2 changes: 1 addition & 1 deletion crates/core/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub struct Api {
#[derive(Deserialize, Debug, Clone)]
pub struct GlobalLimits {
pub group_size: usize,
pub message_embeds: usize,
pub message_replies: usize,
pub message_reactions: usize,
pub server_emoji: usize,
Expand All @@ -123,7 +124,6 @@ pub struct FeaturesLimits {
pub bots: usize,
pub message_length: usize,
pub message_attachments: usize,
pub message_embeds: usize,
pub servers: usize,

pub attachment_size: usize,
Expand Down
3 changes: 1 addition & 2 deletions crates/core/database/src/models/bots/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ impl Bot {
return Err(create_error!(IsBot));
}

let config = config().await;
if db.get_number_of_bots_by_user(&owner.id).await? >= config.features.limits.default.bots {
if db.get_number_of_bots_by_user(&owner.id).await? >= owner.limits().await.bots {
return Err(create_error!(ReachedMaximumBots));
}

Expand Down
14 changes: 8 additions & 6 deletions crates/core/database/src/models/messages/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashSet;

use indexmap::{IndexMap, IndexSet};
use iso8601_timestamp::Timestamp;
use revolt_config::config;
use revolt_config::{config, FeaturesLimits};
use revolt_models::v0::{
self, BulkMessageResponse, DataMessageSend, Embed, MessageAuthor, MessageSort, MessageWebhook,
PushNotification, ReplyIntent, SendableEmbed, Text, RE_MENTION,
Expand Down Expand Up @@ -207,11 +207,13 @@ impl Default for Message {
#[allow(clippy::disallowed_methods)]
impl Message {
/// Create message from API data
#[allow(clippy::too_many_arguments)]
pub async fn create_from_api(
db: &Database,
channel: Channel,
data: DataMessageSend,
author: MessageAuthor<'_>,
limits: FeaturesLimits,
mut idempotency: IdempotencyKey,
generate_embeds: bool,
allow_mentions: bool,
Expand All @@ -221,7 +223,7 @@ impl Message {
Message::validate_sum(
&data.content,
data.embeds.as_deref().unwrap_or_default(),
config.features.limits.default.message_length,
limits.message_length,
)?;

idempotency
Expand Down Expand Up @@ -320,20 +322,20 @@ impl Message {
if data
.attachments
.as_ref()
.is_some_and(|v| v.len() > config.features.limits.default.message_attachments)
.is_some_and(|v| v.len() > limits.message_attachments)
{
return Err(create_error!(TooManyAttachments {
max: config.features.limits.default.message_attachments,
max: limits.message_attachments,
}));
}

if data
.embeds
.as_ref()
.is_some_and(|v| v.len() > config.features.limits.default.message_embeds)
.is_some_and(|v| v.len() > config.features.limits.global.message_embeds)
{
return Err(create_error!(TooManyEmbeds {
max: config.features.limits.default.message_embeds,
max: config.features.limits.global.message_embeds,
}));
}

Expand Down
30 changes: 22 additions & 8 deletions crates/core/database/src/models/users/model.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{collections::HashSet, time::Duration};
use std::{collections::HashSet, str::FromStr, time::Duration};

use crate::{events::client::EventV1, Database, File, RatelimitEvent};

use once_cell::sync::Lazy;
use rand::seq::SliceRandom;
use revolt_config::config;
use revolt_config::{config, FeaturesLimits};
use revolt_models::v0;
use revolt_presence::filter_online;
use revolt_result::{create_error, Result};
Expand Down Expand Up @@ -197,6 +197,22 @@ impl User {
Ok(user)
}

/// Get limits for this user
pub async fn limits(&self) -> FeaturesLimits {
let config = config().await;
if ulid::Ulid::from_str(&self.id)
.expect("`ulid`")
.datetime()
.elapsed()
.expect("time went backwards")
<= Duration::from_secs(86400u64 * config.features.limits.global.new_user_days as u64)
{
config.features.limits.new_user
} else {
config.features.limits.default
}
}

/// Get the relationship with another user
pub fn relationship_with(&self, user_b: &str) -> RelationshipStatus {
if self.id == user_b {
Expand Down Expand Up @@ -235,12 +251,11 @@ impl User {

/// Check if this user can acquire another server
pub async fn can_acquire_server(&self, db: &Database) -> Result<()> {
let config = config().await;
if db.fetch_server_count(&self.id).await? <= config.features.limits.default.servers {
if db.fetch_server_count(&self.id).await? <= self.limits().await.servers {
Ok(())
} else {
Err(create_error!(TooManyServers {
max: config.features.limits.default.servers
max: self.limits().await.servers
}))
}
}
Expand Down Expand Up @@ -500,10 +515,9 @@ impl User {
.unwrap_or_default();

// If we're over the limit, don't allow creating more requests
let config = config().await;
if count >= config.features.limits.default.outgoing_friend_requests {
if count >= self.limits().await.outgoing_friend_requests {
return Err(create_error!(TooManyPendingFriendRequests {
max: config.features.limits.default.outgoing_friend_requests
max: self.limits().await.outgoing_friend_requests
}));
}

Expand Down
2 changes: 1 addition & 1 deletion crates/core/database/src/tasks/process_embeds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub async fn worker(db: Database) {
let embeds = generate(
task.content,
&config.hosts.january,
config.features.limits.default.message_embeds,
config.features.limits.global.message_embeds,
semaphore,
)
.await;
Expand Down
3 changes: 1 addition & 2 deletions crates/delta/src/routes/channels/message_edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ pub async fn edit(
})
})?;

let config = config().await;
Message::validate_sum(
&edit.content,
edit.embeds.as_deref().unwrap_or_default(),
config.features.limits.default.message_length,
user.limits().await.message_length,
)?;

// Ensure we have permissions to send a message
Expand Down
1 change: 1 addition & 0 deletions crates/delta/src/routes/channels/message_send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub async fn message_send(
channel,
data,
v0::MessageAuthor::User(&author),
user.limits().await,
idempotency,
permissions.has_channel_permission(ChannelPermission::SendEmbeds),
allow_mentions,
Expand Down
2 changes: 2 additions & 0 deletions crates/delta/src/routes/webhooks/webhook_execute.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use revolt_config::config;
use revolt_database::{
util::{idempotency::IdempotencyKey, reference::Reference},
Database, Message,
Expand Down Expand Up @@ -58,6 +59,7 @@ pub async fn webhook_execute(
channel,
data,
v0::MessageAuthor::Webhook(&webhook.into()),
config().await.features.limits.default,
idempotency,
true,
true,
Expand Down

0 comments on commit 6ec8007

Please sign in to comment.