ProjectLighthouse/ProjectLighthouse.Servers.Website/Controllers/Moderator/ModerationSlotController.cs
sudokoko aea66b4a74
Implement in-game and website notifications (#932)
* Implement notifications logic, basic calls, and admin command

* Remove unnecessary code

* Add ability to stack notifications and return manually created XML

* Remove test that is no longer needed and is causing failures

* Apply suggestions from code review

* Merge notifications with existing announcements page

* Order notifications by descending ID instead of ascending ID

* Move notification send task to moderation options under user

Also restyles the buttons to line up next to each other like in the slot pages.

* Style/position fixes with granted slots/notification partials

* Fix incorrect form POST route

* Prevent notification text area from breaking out of container

* Actually use builder result for notification text

* Minor restructuring of the notifications page

* Add notifications for team picks, publish issues, and moderation

* Mark notifications as dismissed instead of deleting them

* Add XMLdoc to SendNotification method

* Fix incorrect URL in announcements webhook

* Remove unnecessary inline style from granted slots partial

* Apply suggestions from code review

* Apply first batch of suggestions from code review

* Apply second batch of suggestions from code review

* Change notification icon depending on if user has unread notifications

* Show unread notification icon if there is an announcement posted

* Remove "potential" wording from definitive fixes in error docs

* Remove "Error code:" from publish notifications

* Send notification if user tries to unlock a mod-locked level

* Change notification timestamp format to include date

* Add clarification to level mod-lock notification message

* Change team pick notifications to moderation notifications

Apparently the MMPick type doesn't show a visual notification.

* Apply suggestions from code review

* Add obsolete to notification types that display nothing in-game

* Remove unused imports and remove icon switch case in favor of bell icon

* Last minute fixes

* Send notification upon earth wipe and clarify moderation case notifications

* Add check for empty/too long notification text
2023-10-29 20:27:41 +00:00

105 lines
4.1 KiB
C#

using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace LBPUnion.ProjectLighthouse.Servers.Website.Controllers.Moderator;
[ApiController]
[Route("moderation/slot/{id:int}")]
public class ModerationSlotController : ControllerBase
{
private readonly DatabaseContext database;
public ModerationSlotController(DatabaseContext database)
{
this.database = database;
}
[HttpGet("teamPick")]
public async Task<IActionResult> TeamPick([FromRoute] int id)
{
UserEntity? user = this.database.UserFromWebRequest(this.Request);
if (user == null || !user.IsModerator) return this.StatusCode(403);
SlotEntity? slot = await this.database.Slots.Include(s => s.Creator).FirstOrDefaultAsync(s => s.SlotId == id);
if (slot == null) return this.NotFound();
slot.TeamPick = true;
// Send webhook with slot.Name and slot.Creator.Username
await WebhookHelper.SendWebhook("New Team Pick!", $"The level [**{slot.Name}**]({ServerConfiguration.Instance.ExternalUrl}/slot/{slot.SlotId}) by **{slot.Creator?.Username}** has been team picked");
// Send a notification to the creator
await this.database.SendNotification(slot.CreatorId,
$"Your level, {slot.Name}, has been team picked!");
await this.database.SaveChangesAsync();
return this.Redirect("~/slot/" + id);
}
[HttpGet("removeTeamPick")]
public async Task<IActionResult> RemoveTeamPick([FromRoute] int id)
{
UserEntity? user = this.database.UserFromWebRequest(this.Request);
if (user == null || !user.IsModerator) return this.StatusCode(403);
SlotEntity? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == id);
if (slot == null) return this.NotFound();
slot.TeamPick = false;
// Send a notification to the creator
await this.database.SendNotification(slot.CreatorId,
$"Your level, {slot.Name}, is no longer team picked.");
await this.database.SaveChangesAsync();
return this.Redirect("~/slot/" + id);
}
[HttpGet("delete")]
public async Task<IActionResult> DeleteLevel([FromRoute] int id)
{
UserEntity? user = this.database.UserFromWebRequest(this.Request);
if (user == null || !user.IsModerator) return this.StatusCode(403);
SlotEntity? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == id);
if (slot == null) return this.Ok();
// Send a notification to the creator
await this.database.SendNotification(slot.CreatorId,
$"Your level, {slot.Name}, has been deleted by a moderator.");
await this.database.RemoveSlot(slot);
return this.Redirect("~/slots/0");
}
[HttpGet("flag")]
public async Task<IActionResult> FlagLevel([FromRoute] int id)
{
UserEntity? user = this.database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect($"~/slot/{id}");
SlotEntity? slot = await this.database.Slots.Include(s => s.Creator).FirstOrDefaultAsync(s => s.SlotId == id);
if (slot == null) return this.BadRequest();
if (slot.CreatorId == user.UserId) return this.Redirect($"~/slot/{slot.SlotId}");
string externalUrl = ServerConfiguration.Instance.ExternalUrl;
await WebhookHelper.SendWebhook(title: "New duplicate level flag",
description: @$"Level [**{slot.Name}**]({externalUrl}/slot/{slot.SlotId}) has been flagged as a duplicate level.
> **Reporter:** [{user.Username}]({externalUrl}/user/{user.UserId})
> **Offender:** [{slot.Creator!.Username}]({externalUrl}/user/{slot.CreatorId})
> **Level Hash:** {slot.RootLevel}",
dest: WebhookHelper.WebhookDestination.Moderation);
return this.Redirect($"~/slot/{slot.SlotId}");
}
}