Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the ability for moderators to delete all scores/comments by a user #1027

Merged
merged 8 commits into from
Jun 15, 2024
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -91,6 +93,63 @@ await this.database.SendNotification(targetedUser.UserId,

return this.Redirect($"/user/{targetedUser.UserId}");
}

/// <summary>
/// Deletes every comment by the user. Useful in case of mass spam
/// </summary>
[HttpGet("wipeComments")]
public async Task<IActionResult> WipeComments([FromRoute] int id) {
Metraberryy marked this conversation as resolved.
Show resolved Hide resolved
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();

// Get every comment posted by the target user
List<CommentEntity> comments = await this.database.Comments
.Where(c => c.Poster == targetedUser)
Metraberryy marked this conversation as resolved.
Show resolved Hide resolved
.ToListAsync();

// Loop through and delete all of the comments
foreach (CommentEntity comment in comments)
{
comment.Deleted = true;
comment.DeletedBy = user.Username;
comment.DeletedType = "moderator";
Logger.Success($"Deleted comments for {targetedUser.Username} (id:{targetedUser.UserId})",
Metraberryy marked this conversation as resolved.
Show resolved Hide resolved
LogArea.Admin);
}

await this.database.SendNotification(targetedUser.UserId,
"Your comments have been deleted by a moderator.");

await this.database.SaveChangesAsync();
Metraberryy marked this conversation as resolved.
Show resolved Hide resolved

return this.Redirect($"/user/{targetedUser.UserId}");
}

/// <summary>
/// Deletes every score from the user. Useful in the case where a user cheated a ton of scores
/// </summary>
[HttpGet("wipeScores")]
public async Task<IActionResult> 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.RemoveWhere(c => c.User == targetedUser);
Metraberryy marked this conversation as resolved.
Show resolved Hide resolved
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.");

await this.database.SaveChangesAsync();

return this.Redirect($"/user/{targetedUser.UserId}");
}

/// <summary>
/// Forces the email verification of a user.
Expand Down
10 changes: 10 additions & 0 deletions ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,16 @@ else
<i class="trash alternate icon"></i>
<span>Wipe Earth Decorations</span>
</a>

<a class="ui red button" href="/moderation/user/@Model.ProfileUser.UserId/wipeComments">
<i class="trash alternate icon"></i>
<span>Wipe User's Comments</span>
</a>

<a class="ui red button" href="/moderation/user/@Model.ProfileUser.UserId/wipeScores">
<i class="trash alternate icon"></i>
<span>Wipe User's Scores</span>
</a>

@if (!Model.CommentsDisabledByModerator)
{
Expand Down
Loading