mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2025-06-26 19:46:36 +02:00
chore: Fix more complicated clippy warnings
This commit is contained in:
parent
a173f7c091
commit
ae3858163f
3 changed files with 53 additions and 41 deletions
|
@ -73,9 +73,8 @@ pub(super) async fn purge_sync_tokens(&self, room: OwnedRoomOrAliasId) -> Result
|
||||||
let room_id = self.services.rooms.alias.resolve(&room).await?;
|
let room_id = self.services.rooms.alias.resolve(&room).await?;
|
||||||
|
|
||||||
// Delete all tokens for this room using the service method
|
// Delete all tokens for this room using the service method
|
||||||
let deleted_count = match self.services.rooms.user.delete_room_tokens(&room_id).await {
|
let Ok(deleted_count) = self.services.rooms.user.delete_room_tokens(&room_id).await else {
|
||||||
| Ok(count) => count,
|
return Err!("Failed to delete sync tokens for room {}", room_id);
|
||||||
| Err(_) => return Err!("Failed to delete sync tokens for room {}", room_id),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
self.write_str(&format!(
|
self.write_str(&format!(
|
||||||
|
@ -84,12 +83,23 @@ pub(super) async fn purge_sync_tokens(&self, room: OwnedRoomOrAliasId) -> Result
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Target options for room purging
|
||||||
|
#[derive(Default, Debug, clap::ValueEnum, Clone)]
|
||||||
|
pub(crate) enum RoomTargetOption {
|
||||||
|
#[default]
|
||||||
|
/// Target all rooms
|
||||||
|
All,
|
||||||
|
/// Target only disabled rooms
|
||||||
|
DisabledOnly,
|
||||||
|
/// Target only banned rooms
|
||||||
|
BannedOnly,
|
||||||
|
}
|
||||||
|
|
||||||
#[admin_command]
|
#[admin_command]
|
||||||
pub(super) async fn purge_empty_room_tokens(
|
pub(super) async fn purge_empty_room_tokens(
|
||||||
&self,
|
&self,
|
||||||
yes: bool,
|
yes: bool,
|
||||||
target_disabled: bool,
|
target_option: Option<RoomTargetOption>,
|
||||||
target_banned: bool,
|
|
||||||
dry_run: bool,
|
dry_run: bool,
|
||||||
) -> Result {
|
) -> Result {
|
||||||
use conduwuit::{debug, info};
|
use conduwuit::{debug, info};
|
||||||
|
@ -103,11 +113,13 @@ pub(super) async fn purge_empty_room_tokens(
|
||||||
|
|
||||||
let mode = if dry_run { "Simulating" } else { "Starting" };
|
let mode = if dry_run { "Simulating" } else { "Starting" };
|
||||||
|
|
||||||
let mut total_rooms_processed = 0;
|
// strictly, we should check if these reach the max value after the loop and
|
||||||
let mut empty_rooms_processed = 0;
|
// warn the user that the count is too large
|
||||||
let mut total_tokens_deleted = 0;
|
let mut total_rooms_processed: usize = 0;
|
||||||
let mut error_count = 0;
|
let mut empty_rooms_processed: u32 = 0;
|
||||||
let mut skipped_rooms = 0;
|
let mut total_tokens_deleted: usize = 0;
|
||||||
|
let mut error_count: u32 = 0;
|
||||||
|
let mut skipped_rooms: u32 = 0;
|
||||||
|
|
||||||
info!("{} purge of sync tokens for rooms with no local users", mode);
|
info!("{} purge of sync tokens for rooms with no local users", mode);
|
||||||
|
|
||||||
|
@ -125,17 +137,24 @@ pub(super) async fn purge_empty_room_tokens(
|
||||||
// Filter rooms based on options
|
// Filter rooms based on options
|
||||||
let mut rooms = Vec::new();
|
let mut rooms = Vec::new();
|
||||||
for room_id in all_rooms {
|
for room_id in all_rooms {
|
||||||
// Filter rooms based on targeting options
|
if let Some(target) = &target_option {
|
||||||
let is_disabled = self.services.rooms.metadata.is_disabled(room_id).await;
|
match target {
|
||||||
let is_banned = self.services.rooms.metadata.is_banned(room_id).await;
|
| RoomTargetOption::DisabledOnly => {
|
||||||
|
if !self.services.rooms.metadata.is_disabled(room_id).await {
|
||||||
// If targeting specific types of rooms, only include matching rooms
|
debug!("Skipping room {} as it's not disabled", room_id);
|
||||||
if (target_disabled || target_banned)
|
skipped_rooms = skipped_rooms.saturating_add(1);
|
||||||
&& !((target_disabled && is_disabled) || (target_banned && is_banned))
|
continue;
|
||||||
{
|
}
|
||||||
debug!("Skipping room {} as it doesn't match targeting criteria", room_id);
|
},
|
||||||
skipped_rooms += 1;
|
| RoomTargetOption::BannedOnly => {
|
||||||
continue;
|
if !self.services.rooms.metadata.is_banned(room_id).await {
|
||||||
|
debug!("Skipping room {} as it's not banned", room_id);
|
||||||
|
skipped_rooms = skipped_rooms.saturating_add(1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
| RoomTargetOption::All => {},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rooms.push(room_id);
|
rooms.push(room_id);
|
||||||
|
@ -150,7 +169,7 @@ pub(super) async fn purge_empty_room_tokens(
|
||||||
|
|
||||||
// Process each room
|
// Process each room
|
||||||
for room_id in rooms {
|
for room_id in rooms {
|
||||||
total_rooms_processed += 1;
|
total_rooms_processed = total_rooms_processed.saturating_add(1);
|
||||||
|
|
||||||
// Count local users in this room
|
// Count local users in this room
|
||||||
let local_users_count = self
|
let local_users_count = self
|
||||||
|
@ -163,7 +182,7 @@ pub(super) async fn purge_empty_room_tokens(
|
||||||
|
|
||||||
// Only process rooms with no local users
|
// Only process rooms with no local users
|
||||||
if local_users_count == 0 {
|
if local_users_count == 0 {
|
||||||
empty_rooms_processed += 1;
|
empty_rooms_processed = empty_rooms_processed.saturating_add(1);
|
||||||
|
|
||||||
// In dry run mode, just count what would be deleted, don't actually delete
|
// In dry run mode, just count what would be deleted, don't actually delete
|
||||||
debug!(
|
debug!(
|
||||||
|
@ -182,13 +201,13 @@ pub(super) async fn purge_empty_room_tokens(
|
||||||
| Ok(count) =>
|
| Ok(count) =>
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
debug!("Would delete {} sync tokens for room {}", count, room_id);
|
debug!("Would delete {} sync tokens for room {}", count, room_id);
|
||||||
total_tokens_deleted += count;
|
total_tokens_deleted = total_tokens_deleted.saturating_add(count);
|
||||||
} else {
|
} else {
|
||||||
debug!("No sync tokens found for room {}", room_id);
|
debug!("No sync tokens found for room {}", room_id);
|
||||||
},
|
},
|
||||||
| Err(e) => {
|
| Err(e) => {
|
||||||
debug!("Error counting sync tokens for room {}: {:?}", room_id, e);
|
debug!("Error counting sync tokens for room {}: {:?}", room_id, e);
|
||||||
error_count += 1;
|
error_count = error_count.saturating_add(1);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -197,13 +216,13 @@ pub(super) async fn purge_empty_room_tokens(
|
||||||
| Ok(count) =>
|
| Ok(count) =>
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
debug!("Deleted {} sync tokens for room {}", count, room_id);
|
debug!("Deleted {} sync tokens for room {}", count, room_id);
|
||||||
total_tokens_deleted += count;
|
total_tokens_deleted = total_tokens_deleted.saturating_add(count);
|
||||||
} else {
|
} else {
|
||||||
debug!("No sync tokens found for room {}", room_id);
|
debug!("No sync tokens found for room {}", room_id);
|
||||||
},
|
},
|
||||||
| Err(e) => {
|
| Err(e) => {
|
||||||
debug!("Error purging sync tokens for room {}: {:?}", room_id, e);
|
debug!("Error purging sync tokens for room {}: {:?}", room_id, e);
|
||||||
error_count += 1;
|
error_count = error_count.saturating_add(1);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ mod info;
|
||||||
mod moderation;
|
mod moderation;
|
||||||
|
|
||||||
use clap::Subcommand;
|
use clap::Subcommand;
|
||||||
|
use commands::RoomTargetOption;
|
||||||
use conduwuit::Result;
|
use conduwuit::Result;
|
||||||
use ruma::{OwnedRoomId, OwnedRoomOrAliasId};
|
use ruma::{OwnedRoomId, OwnedRoomOrAliasId};
|
||||||
|
|
||||||
|
@ -66,21 +67,15 @@ pub(super) enum RoomCommand {
|
||||||
|
|
||||||
/// - Delete sync tokens for all rooms that have no local users
|
/// - Delete sync tokens for all rooms that have no local users
|
||||||
///
|
///
|
||||||
/// By default, processes all empty rooms. You can use --target-disabled
|
/// By default, processes all empty rooms.
|
||||||
/// and/or --target-banned to exclusively process rooms matching those
|
|
||||||
/// conditions.
|
|
||||||
PurgeEmptyRoomTokens {
|
PurgeEmptyRoomTokens {
|
||||||
/// Confirm you want to delete tokens from potentially many rooms
|
/// Confirm you want to delete tokens from potentially many rooms
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
yes: bool,
|
yes: bool,
|
||||||
|
|
||||||
/// Only purge rooms that have federation disabled
|
/// Target specific room types
|
||||||
#[arg(long)]
|
#[arg(long, value_enum)]
|
||||||
target_disabled: bool,
|
target_option: Option<RoomTargetOption>,
|
||||||
|
|
||||||
/// Only purge rooms that have been banned
|
|
||||||
#[arg(long)]
|
|
||||||
target_banned: bool,
|
|
||||||
|
|
||||||
/// Perform a dry run without actually deleting any tokens
|
/// Perform a dry run without actually deleting any tokens
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
|
|
|
@ -166,9 +166,6 @@ pub async fn delete_room_tokens(&self, room_id: &RoomId) -> Result<usize> {
|
||||||
// short ID
|
// short ID
|
||||||
let prefix = &[shortroomid];
|
let prefix = &[shortroomid];
|
||||||
|
|
||||||
// Get all keys with this room prefix
|
|
||||||
let mut count = 0;
|
|
||||||
|
|
||||||
// Collect all keys into a Vec first, then delete them
|
// Collect all keys into a Vec first, then delete them
|
||||||
let keys = self
|
let keys = self
|
||||||
.db
|
.db
|
||||||
|
@ -184,8 +181,9 @@ pub async fn delete_room_tokens(&self, room_id: &RoomId) -> Result<usize> {
|
||||||
// Delete each key individually
|
// Delete each key individually
|
||||||
for key in &keys {
|
for key in &keys {
|
||||||
self.db.roomsynctoken_shortstatehash.del(key);
|
self.db.roomsynctoken_shortstatehash.del(key);
|
||||||
count += 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let count = keys.len();
|
||||||
|
|
||||||
Ok(count)
|
Ok(count)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue