feat: implement time based message sweep on user ban (#670)
* feat: implement time based message sweep on user ban - Adds `delete_message_seconds` (0 to 7 days in seconds) to the ban request payload. Signed-off-by: arsabutispik <ispik@ispik.dev> * refactor: pass ulid conversion to database Signed-off-by: arsabutispik <ispik@ispik.dev> * fix: use COL constant instead of hardcoded string in error mapper Signed-off-by: arsabutispik <ispik@ispik.dev> * refactor: broadcast bulk delete events during ban sweep Updates the `delete_messages_by_author_since` trait to return a HashMap of deleted message IDs grouped by channel. The MongoDB implementation now uses a two-step process: it first runs a projected `find` query to gather the target `_id` and `channel` fields, then executes the `delete_many` operation. This allows the ban route to loop through the affected channels and dispatch `EventV1::BulkMessageDelete` WebSocket events, ensuring that the swept messages are instantly removed from the UI for all connected clients. Signed-off-by: arsabutispik <ispik@ispik.dev> * refactor: optimize message deletion by using $group and aggregate Signed-off-by: arsabutispik <ispik@ispik.dev> * refactor: use with_type in query Signed-off-by: arsabutispik <ispik@ispik.dev> * refactor: abstract function to Message model and mark attachments as deleted Signed-off-by: arsabutispik <ispik@ispik.dev> --------- Signed-off-by: arsabutispik <ispik@ispik.dev>
This commit is contained in:
@@ -4,13 +4,16 @@ use revolt_database::{
|
||||
get_user_voice_channel_in_server, remove_user_from_voice_channel, UserVoiceChannel,
|
||||
VoiceClient,
|
||||
},
|
||||
Database, RemovalIntention, ServerBan, User,
|
||||
Database, Message, RemovalIntention, ServerBan, User,
|
||||
};
|
||||
use revolt_models::v0;
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
use revolt_database::events::client::EventV1;
|
||||
use revolt_permissions::{calculate_server_permissions, ChannelPermission};
|
||||
use revolt_result::{create_error, Result};
|
||||
use rocket::{serde::json::Json, State};
|
||||
use ulid::Ulid;
|
||||
use validator::Validate;
|
||||
|
||||
/// # Ban User
|
||||
@@ -73,7 +76,15 @@ pub async fn ban(
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
// We do this outside the member check so we can sweep hit-and-run spammers who already left.
|
||||
if let Some(seconds) = data.delete_message_seconds {
|
||||
if seconds > 0 {
|
||||
let threshold_time = SystemTime::now() - Duration::from_secs(seconds as u64);
|
||||
|
||||
Message::bulk_delete_by_author_since(db, &server.channels, target.id, threshold_time)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
ServerBan::create(db, &server, target.id, data.reason)
|
||||
.await
|
||||
.map(Into::into)
|
||||
|
||||
Reference in New Issue
Block a user