feat: Add slowmode functionality to text channels (#680)

* feat: Add slowmode functionality to text channels

Signed-off-by: arsabutispik <ispik@ispik.dev>

* fix: use atomic check-and-set to prevent spamming with scripts

Signed-off-by: arsabutispik <ispik@ispik.dev>

* feat: Add BypassSlowmode permission to channel permissions

Signed-off-by: arsabutispik <ispik@ispik.dev>

* refactor: Use set_options instead of manually building the command

Signed-off-by: arsabutispik <ispik@ispik.dev>

---------

Signed-off-by: arsabutispik <ispik@ispik.dev>
This commit is contained in:
İspik
2026-03-28 03:09:15 +03:00
committed by GitHub
parent a5cd08a655
commit 6107f242fd
9 changed files with 92 additions and 4 deletions

View File

@@ -41,6 +41,7 @@ pub async fn edit(
&& data.nsfw.is_none()
&& data.owner.is_none()
&& data.voice.is_none()
&& data.slowmode.is_none()
&& data.remove.is_empty()
{
return Ok(Json(channel.into()));
@@ -200,6 +201,7 @@ pub async fn edit(
icon,
nsfw,
voice,
slowmode,
..
} => {
if data.remove.contains(&v0::FieldsChannel::Icon) {
@@ -247,6 +249,11 @@ pub async fn edit(
*voice = Some(new_voice.clone().into());
partial.voice = Some(new_voice.into());
}
if let Some(new_slowmode) = data.slowmode {
*slowmode = Some(new_slowmode);
partial.slowmode = Some(new_slowmode);
}
}
_ => return Err(create_error!(InvalidOperation)),
};

View File

@@ -1,9 +1,10 @@
use chrono::{Duration, Utc};
use redis_kiss::{get_connection, redis, AsyncCommands};
use revolt_database::util::permissions::DatabasePermissionQuery;
use revolt_database::{
util::idempotency::IdempotencyKey, util::reference::Reference, Database, User,
};
use revolt_database::{Interactions, Message, AMQP};
use revolt_database::{Channel, Interactions, Message, AMQP};
use revolt_models::v0;
use revolt_permissions::PermissionQuery;
use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
@@ -57,6 +58,50 @@ pub async fn message_send(
permissions.throw_if_lacking_channel_permission(ChannelPermission::UploadFiles)?;
}
if !permissions.has_channel_permission(ChannelPermission::BypassSlowmode) {
if let Channel::TextChannel {
slowmode: Some(channel_slowmode),
id: channel_id,
..
} = &channel
{
if *channel_slowmode > 0 {
if let Ok(conn) = get_connection().await {
let mut conn = conn.into_inner();
let slowmode_key = format!("slowmode:{}:{}", user.id, channel_id);
// Atomic check-and-set: only set if absent and apply expiry in one command.
let set_result: Option<String> = conn
.set_options(
&slowmode_key,
"1", // The value doesn't matter, only the key's existence
redis::SetOptions::default()
.conditional_set(redis::ExistenceCheck::NX)
.with_expiration(redis::SetExpiry::EX(*channel_slowmode as usize)),
)
.await
.unwrap_or(None);
// If `set_result` is None, the `NX` condition failed because the key already exists.
// This means the user is currently in slowmode.
if set_result.is_none() {
// Fetch the remaining TTL to accurately populate the retry_after field
let ttl: i64 = conn.ttl(&slowmode_key).await.unwrap_or(0);
// Redis returns positive integers for valid TTLs
if ttl > 0 {
return Err(create_error!(InSlowmode {
retry_after: ttl as u64
}));
}
}
}
// If Redis connection fails, just skip the slowmode check
}
}
}
// Ensure interactions information is correct
if let Some(interactions) = &data.interactions {
let interactions: Interactions = interactions.clone().into();
@@ -186,6 +231,7 @@ mod test {
}),
last_message_id: None,
voice: None,
slowmode: None,
};
locked_channel
.update(&harness.db, partial, vec![])