mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-29 00:18:39 +00:00
Refactor web-based comment deletion routine (#1079)
* Refactor web-based comment deletion routine * Apply suggestions from code review
This commit is contained in:
parent
f1fe542656
commit
a3022ff5b4
1 changed files with 31 additions and 25 deletions
|
@ -56,52 +56,58 @@ public class ModerationRemovalController : ControllerBase
|
||||||
UserEntity? user = this.database.UserFromWebRequest(this.Request);
|
UserEntity? user = this.database.UserFromWebRequest(this.Request);
|
||||||
if (user == null) return this.Redirect("~/login");
|
if (user == null) return this.Redirect("~/login");
|
||||||
|
|
||||||
CommentEntity? comment = await this.database.Comments
|
CommentEntity? comment = await this.database.Comments.Include(c => c.TargetUser)
|
||||||
.Include(c => c.TargetUser)
|
|
||||||
.Include(c => c.TargetSlot)
|
.Include(c => c.TargetSlot)
|
||||||
|
.ThenInclude(s => s!.Creator)
|
||||||
.FirstOrDefaultAsync(c => c.CommentId == commentId);
|
.FirstOrDefaultAsync(c => c.CommentId == commentId);
|
||||||
if (comment == null) return this.Redirect("~/404");
|
if (comment == null) return this.Redirect("~/404");
|
||||||
|
|
||||||
if (comment.Deleted) return this.Redirect(callbackUrl ?? "~/");
|
if (comment.Deleted) return this.Redirect(callbackUrl ?? "~/");
|
||||||
|
|
||||||
bool canDelete;
|
bool canDelete = comment.Type switch
|
||||||
switch (comment.Type)
|
|
||||||
{
|
{
|
||||||
case CommentType.Level:
|
CommentType.Level => user.UserId == comment.PosterUserId || user.UserId == comment.TargetSlot?.CreatorId,
|
||||||
int slotCreatorId = await this.database.Slots.Where(s => s.SlotId == comment.TargetSlotId)
|
CommentType.Profile => user.UserId == comment.PosterUserId || user.UserId == comment.TargetUserId,
|
||||||
.Select(s => s.CreatorId)
|
_ => throw new ArgumentOutOfRangeException(nameof(comment.Type),
|
||||||
.FirstOrDefaultAsync();
|
@"Comment type is not recognized (impossible)."),
|
||||||
canDelete = user.UserId == comment.PosterUserId || user.UserId == slotCreatorId;
|
};
|
||||||
break;
|
|
||||||
case CommentType.Profile:
|
|
||||||
canDelete = user.UserId == comment.PosterUserId || user.UserId == comment.TargetUserId;
|
|
||||||
break;
|
|
||||||
default: throw new ArgumentOutOfRangeException(nameof(commentId));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!canDelete && !user.IsModerator) return this.Redirect(callbackUrl ?? "~/");
|
if (!canDelete && !user.IsModerator) return this.Redirect(callbackUrl ?? "~/");
|
||||||
|
|
||||||
comment.Deleted = true;
|
comment.Deleted = true;
|
||||||
comment.DeletedBy = user.Username;
|
comment.DeletedBy = user.Username;
|
||||||
comment.DeletedType = !canDelete && user.IsModerator ? "moderator" : "user";
|
comment.DeletedType = !canDelete && user.IsModerator ? "moderator" : "user";
|
||||||
|
|
||||||
switch (comment.Type)
|
switch (comment.DeletedType)
|
||||||
{
|
{
|
||||||
case CommentType.Profile when comment.DeletedType == "moderator" && comment.TargetUser != null:
|
case "moderator":
|
||||||
{
|
{
|
||||||
await this.database.SendNotification(comment.PosterUserId,
|
string? notificationMessage = comment.Type switch
|
||||||
$"Your comment on {comment.TargetUser.Username}'s profile has been removed by a moderator.");
|
{
|
||||||
|
CommentType.Profile when comment.TargetUser != null =>
|
||||||
|
$"Your comment on {comment.TargetUser.Username}'s profile has been removed by a moderator.",
|
||||||
|
CommentType.Level when comment.TargetSlot != null =>
|
||||||
|
$"Your comment on level {comment.TargetSlot.Name} has been removed by a moderator.",
|
||||||
|
_ => null,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (notificationMessage != null)
|
||||||
|
await this.database.SendNotification(comment.PosterUserId, notificationMessage);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case CommentType.Level when comment.DeletedType == "moderator" && comment.TargetSlot != null:
|
case "user":
|
||||||
{
|
{
|
||||||
await this.database.SendNotification(comment.PosterUserId,
|
string? notificationMessage = comment.Type switch
|
||||||
$"Your comment on level {comment.TargetSlot.Name} has been removed by a moderator.");
|
{
|
||||||
|
CommentType.Profile when comment.TargetUser != null && user != comment.TargetUser =>
|
||||||
|
$"Your comment on {comment.TargetUser.Username}'s profile has been removed by the user.",
|
||||||
|
CommentType.Level when comment.TargetSlot != null && user != comment.TargetSlot.Creator =>
|
||||||
|
$"Your comment on level {comment.TargetSlot.Name} has been removed by the user.",
|
||||||
|
_ => null,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (notificationMessage != null)
|
||||||
|
await this.database.SendNotification(comment.PosterUserId, notificationMessage);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default: throw new ArgumentOutOfRangeException(nameof(comment.Type), @"Comment type is out of range.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.database.SaveChangesAsync();
|
await this.database.SaveChangesAsync();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue