Skip to content

Commit

Permalink
Mark notifications as dismissed instead of deleting them
Browse files Browse the repository at this point in the history
  • Loading branch information
sudokoko committed Oct 27, 2023
1 parent 8d96b9a commit cdc742a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,24 @@ public async Task<IActionResult> Notification()
{
GameTokenEntity token = this.GetToken();

List<NotificationEntity> notifications = await this.database.Notifications
IQueryable<NotificationEntity> notificationQuery = this.database.Notifications
.Where(n => n.UserId == token.UserId)
.OrderByDescending(n => n.Id)
.ToListAsync();
.Where(n => n.IsDismissed == false)
.OrderByDescending(n => n.Id);

// We don't need to do any more work if there are no unconverted notifications to begin with.
if (notifications.Count == 0) return this.Ok();
if (!await notificationQuery.AnyAsync()) return this.Ok();

StringBuilder builder = new();

notifications.ForEach(n =>
await notificationQuery.ForEachAsync(n =>
{
builder.AppendLine(LighthouseSerializer.Serialize(this.HttpContext.RequestServices,
NotificationEntity.ConvertToGame(n)));
n.IsDismissed = true;
});
await this.database.SaveChangesAsync();

this.database.Notifications.RemoveRange(notifications);
return this.Ok(new LbpCustomXml
{
Content = builder.ToString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public async Task<IActionResult> OnGet()

this.Notifications = await this.Database.Notifications
.Where(n => n.UserId == this.User.UserId)
.Where(n => n.IsDismissed == false)
.Include(n => n.User)
.OrderByDescending(n => n.Id)
.ToListAsync();
Expand Down Expand Up @@ -94,11 +95,13 @@ public async Task<IActionResult> OnPostDelete(string type, int id)
{
NotificationEntity? notification = await this.Database.Notifications
.Where(n => n.Id == id)
.Where(n => n.IsDismissed == false)
.FirstOrDefaultAsync();

if (notification == null || notification.UserId != user.UserId) return this.BadRequest();

this.Database.Notifications.Remove(notification);
notification.IsDismissed = true;

await this.Database.SaveChangesAsync();

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace ProjectLighthouse.Migrations
{
[DbContext(typeof(DatabaseContext))]
[Migration("20231026150354_AddNotificationEntity")]
[Migration("20231027215257_AddNotificationEntity")]
public partial class AddNotificationEntity : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
Expand All @@ -22,7 +22,8 @@ protected override void Up(MigrationBuilder migrationBuilder)
UserId = table.Column<int>(type: "int", nullable: false),
Type = table.Column<int>(type: "int", nullable: false),
Text = table.Column<string>(type: "longtext", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4")
.Annotation("MySql:CharSet", "utf8mb4"),
IsDismissed = table.Column<bool>(type: "tinyint(1)", nullable: false)
},
constraints: table =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.ValueGeneratedOnAdd()
.HasColumnType("int");

b.Property<bool>("IsDismissed")
.HasColumnType("tinyint(1)");

b.Property<string>("Text")
.HasColumnType("longtext");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class NotificationEntity

public string Text { get; set; } = "";

public bool IsDismissed { get; set; } = false;

public static GameNotification ConvertToGame(NotificationEntity notification) => new()
{
Type = notification.Type,
Expand Down

0 comments on commit cdc742a

Please sign in to comment.