From be11e138f032ec306f9c732b4a3a3cee47740edc Mon Sep 17 00:00:00 2001 From: Kat <30480654+Metraberryy@users.noreply.github.com> Date: Fri, 14 Jun 2024 19:27:18 -0700 Subject: [PATCH] Implement the ability for moderators to delete all scores/comments by a user (#1027) * Implement delete all scores/comments * Fix formatting in AdminUserController.cs * Move logging out of loop * Batch delete scores based on UserId * Batch update comments instead of using a foreach * Use html entity instead of apostrophe character * Confirm before deleting all comments/scores * Remove unnecessary database.SaveChanges --- .../Controllers/Admin/AdminUserController.cs | 52 ++++++++++++++++++- .../Pages/UserPage.cshtml | 12 +++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/ProjectLighthouse.Servers.Website/Controllers/Admin/AdminUserController.cs b/ProjectLighthouse.Servers.Website/Controllers/Admin/AdminUserController.cs index d4fdfaf8..55db420c 100644 --- a/ProjectLighthouse.Servers.Website/Controllers/Admin/AdminUserController.cs +++ b/ProjectLighthouse.Servers.Website/Controllers/Admin/AdminUserController.cs @@ -1,7 +1,9 @@ #nullable enable using LBPUnion.ProjectLighthouse.Database; +using LBPUnion.ProjectLighthouse.Extensions; using LBPUnion.ProjectLighthouse.Files; using LBPUnion.ProjectLighthouse.Logging; +using LBPUnion.ProjectLighthouse.Types.Entities.Level; using LBPUnion.ProjectLighthouse.Types.Entities.Profile; using LBPUnion.ProjectLighthouse.Types.Entities.Token; using LBPUnion.ProjectLighthouse.Types.Logging; @@ -27,7 +29,8 @@ public class AdminUserController : ControllerBase /// Resets the user's earth decorations to a blank state. Useful for users who abuse audio for example. /// [HttpGet("wipePlanets")] - public async Task WipePlanets([FromRoute] int id) { + public async Task WipePlanets([FromRoute] int id) + { UserEntity? user = this.database.UserFromWebRequest(this.Request); if (user == null || !user.IsModerator) return this.NotFound(); @@ -91,6 +94,53 @@ public class AdminUserController : ControllerBase return this.Redirect($"/user/{targetedUser.UserId}"); } + + /// + /// Deletes every comment by the user. Useful in case of mass spam + /// + [HttpGet("wipeComments")] + public async Task WipeComments([FromRoute] int id) + { + UserEntity? user = this.database.UserFromWebRequest(this.Request); + if (user == null || !user.IsModerator) return this.NotFound(); + + UserEntity? targetedUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id); + if (targetedUser == null) return this.NotFound(); + + // Find every comment by the user, then set the deletion info on them + await this.database.Comments.Where(c => c.PosterUserId == targetedUser.UserId) + .ExecuteUpdateAsync(s => + s.SetProperty(c => c.Deleted, true) + .SetProperty(c => c.DeletedBy, user.Username) + .SetProperty(c => c.DeletedType, "moderator")); + Logger.Success($"Deleted comments for {targetedUser.Username} (id:{targetedUser.UserId})", LogArea.Admin); + + await this.database.SendNotification(targetedUser.UserId, + "Your comments have been deleted by a moderator."); + + return this.Redirect($"/user/{targetedUser.UserId}"); + } + + /// + /// Deletes every score from the user. Useful in the case where a user cheated a ton of scores + /// + [HttpGet("wipeScores")] + public async Task WipeScores([FromRoute] int id) + { + UserEntity? user = this.database.UserFromWebRequest(this.Request); + if (user == null || !user.IsModerator) return this.NotFound(); + + UserEntity? targetedUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id); + if (targetedUser == null) return this.NotFound(); + + // Find and delete every score uploaded by the target user + await this.database.Scores.Where(c => c.UserId == targetedUser.UserId).ExecuteDeleteAsync(); + Logger.Success($"Deleted scores for {targetedUser.Username} (id:{targetedUser.UserId})", LogArea.Admin); + + await this.database.SendNotification(targetedUser.UserId, "Your scores have been deleted by a moderator."); + + return this.Redirect($"/user/{targetedUser.UserId}"); + } /// /// Forces the email verification of a user. diff --git a/ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml b/ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml index 33a07af1..98c23fcd 100644 --- a/ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml +++ b/ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml @@ -324,6 +324,18 @@ else Wipe Earth Decorations + + + + Wipe User's Comments + + + + + Wipe User's Scores + @if (!Model.CommentsDisabledByModerator) {