feat: don't send seen notifications

This commit is contained in:
CyberL1 2024-06-12 10:06:48 +02:00
parent afb6a4730c
commit 483bafcbf2
7 changed files with 656 additions and 4 deletions

View File

@ -15,5 +15,6 @@ namespace Minecraft_Realms_Emulator.Data
public DbSet<Slot> Slots { get; set; }
public DbSet<Template> Templates { get; set; }
public DbSet<Notification> Notifications { get; set; }
public DbSet<SeenNotification> SeenNotifications { get; set; }
}
}

View File

@ -0,0 +1,9 @@
namespace Minecraft_Realms_Emulator.Entities
{
public class SeenNotification
{
public int Id { get; set; }
public string PlayerUUID { get; set; } = null!;
public string NotificationUUID { get; set; } = null!;
}
}

View File

@ -0,0 +1,486 @@
// <auto-generated />
using System;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Minecraft_Realms_Emulator.Data;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace Minecraft_Realms_Emulator.Migrations
{
[DbContext(typeof(DataContext))]
[Migration("20240612072111_SeenNotifications")]
partial class SeenNotifications
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.5")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Backup", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("BackupId")
.IsRequired()
.HasColumnType("text");
b.Property<long>("LastModifiedDate")
.HasColumnType("bigint");
b.Property<JsonDocument>("Metadata")
.IsRequired()
.HasColumnType("jsonb");
b.Property<int>("Size")
.HasColumnType("integer");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId");
b.ToTable("Backups");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Configuration", b =>
{
b.Property<string>("Key")
.HasColumnType("text");
b.Property<object>("Value")
.IsRequired()
.HasColumnType("jsonb");
b.HasKey("Key");
b.ToTable("Configuration");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Connection", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("PendingUpdate")
.HasColumnType("boolean");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId");
b.ToTable("Connections");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Invite", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<DateTime>("Date")
.HasColumnType("timestamp with time zone");
b.Property<string>("InvitationId")
.IsRequired()
.HasColumnType("text");
b.Property<string>("RecipeintUUID")
.IsRequired()
.HasColumnType("text");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId");
b.ToTable("Invites");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Notification", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<JsonDocument>("ButtonText")
.HasColumnType("jsonb");
b.Property<bool>("Dismissable")
.HasColumnType("boolean");
b.Property<string>("Image")
.HasColumnType("text");
b.Property<JsonDocument>("Message")
.IsRequired()
.HasColumnType("jsonb");
b.Property<string>("NotificationUuid")
.IsRequired()
.HasColumnType("text");
b.Property<JsonDocument>("Title")
.HasColumnType("jsonb");
b.Property<string>("Type")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Url")
.HasColumnType("text");
b.Property<JsonDocument>("UrlButton")
.HasColumnType("jsonb");
b.HasKey("Id");
b.ToTable("Notifications");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Player", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<bool>("Accepted")
.HasColumnType("boolean");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("Online")
.HasColumnType("boolean");
b.Property<bool>("Operator")
.HasColumnType("boolean");
b.Property<string>("Permission")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Uuid")
.IsRequired()
.HasColumnType("text");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId");
b.ToTable("Players");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.SeenNotification", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("NotificationUUID")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PlayerUUID")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("SeenNotifications");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Slot", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<bool>("CommandBlocks")
.HasColumnType("boolean");
b.Property<int>("Difficulty")
.HasColumnType("integer");
b.Property<bool>("ForceGameMode")
.HasColumnType("boolean");
b.Property<int>("GameMode")
.HasColumnType("integer");
b.Property<bool>("Pvp")
.HasColumnType("boolean");
b.Property<int>("SlotId")
.HasColumnType("integer");
b.Property<string>("SlotName")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("SpawnAnimals")
.HasColumnType("boolean");
b.Property<bool>("SpawnMonsters")
.HasColumnType("boolean");
b.Property<bool>("SpawnNPCs")
.HasColumnType("boolean");
b.Property<int>("SpawnProtection")
.HasColumnType("integer");
b.Property<string>("Version")
.IsRequired()
.HasColumnType("text");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId");
b.ToTable("Slots");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Subscription", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<DateTime>("StartDate")
.HasColumnType("timestamp with time zone");
b.Property<string>("SubscriptionType")
.IsRequired()
.HasColumnType("text");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId")
.IsUnique();
b.ToTable("Subscriptions");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Template", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Author")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Image")
.HasColumnType("text");
b.Property<string>("Link")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.Property<string>("RecommendedPlayers")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Trailer")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Type")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Version")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Templates");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("ActiveSlot")
.HasColumnType("integer");
b.Property<int>("MaxPlayers")
.HasColumnType("integer");
b.Property<bool>("Member")
.HasColumnType("boolean");
b.Property<int?>("MinigameId")
.HasColumnType("integer");
b.Property<string>("MinigameImage")
.HasColumnType("text");
b.Property<string>("MinigameName")
.HasColumnType("text");
b.Property<string>("Motd")
.HasColumnType("text");
b.Property<string>("Name")
.HasColumnType("text");
b.Property<string>("Owner")
.HasColumnType("text");
b.Property<string>("OwnerUUID")
.HasColumnType("text");
b.Property<string>("State")
.IsRequired()
.HasColumnType("text");
b.Property<string>("WorldType")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Worlds");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Backup", b =>
{
b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World")
.WithMany()
.HasForeignKey("WorldId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("World");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Connection", b =>
{
b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World")
.WithMany()
.HasForeignKey("WorldId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("World");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Invite", b =>
{
b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World")
.WithMany()
.HasForeignKey("WorldId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("World");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Player", b =>
{
b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World")
.WithMany("Players")
.HasForeignKey("WorldId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("World");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Slot", b =>
{
b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World")
.WithMany("Slots")
.HasForeignKey("WorldId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("World");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Subscription", b =>
{
b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World")
.WithOne("Subscription")
.HasForeignKey("Minecraft_Realms_Emulator.Entities.Subscription", "WorldId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("World");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b =>
{
b.Navigation("Players");
b.Navigation("Slots");
b.Navigation("Subscription");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace Minecraft_Realms_Emulator.Migrations
{
/// <inheritdoc />
public partial class SeenNotifications : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "SeenNotifications",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
PlayerUUID = table.Column<string>(type: "text", nullable: false),
NotificationUUID = table.Column<string>(type: "text", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_SeenNotifications", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "SeenNotifications");
}
}
}

View File

@ -205,6 +205,27 @@ namespace Minecraft_Realms_Emulator.Migrations
b.ToTable("Players");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.SeenNotification", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("NotificationUUID")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PlayerUUID")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("SeenNotifications");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Slot", b =>
{
b.Property<int>("Id")

View File

@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using Minecraft_Realms_Emulator.Attributes;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Entities;
using Minecraft_Realms_Emulator.Responses;
namespace Minecraft_Realms_Emulator.Modes.External
@ -20,15 +21,25 @@ namespace Minecraft_Realms_Emulator.Modes.External
[HttpGet]
public ActionResult<NotificationsResponse> GetNotifications()
{
string cookie = Request.Headers.Cookie;
string playerUUID = cookie.Split(";")[0].Split(":")[2];
List<NotificationResponse> notifications = [];
foreach (var notification in _context.Notifications)
foreach (var notification in _context.Notifications.ToList())
{
var seen = _context.SeenNotifications.Any(n => n.PlayerUUID == playerUUID && n.NotificationUUID == notification.NotificationUuid);
if (seen)
{
continue;
}
NotificationResponse notificationResponse = new()
{
NotificationUuid = notification.NotificationUuid,
Dismissable = notification.Dismissable,
Seen = false,
Seen = seen,
Type = notification.Type,
Title = notification.Title,
Message = notification.Message,
@ -48,5 +59,44 @@ namespace Minecraft_Realms_Emulator.Modes.External
return Ok(notificationsResponse);
}
[HttpPost("seen")]
public ActionResult Seen(string[] notificationIds)
{
string cookie = Request.Headers.Cookie;
string playerUUID = cookie.Split(";")[0].Split(":")[2];
if (notificationIds.Length == 0)
{
ErrorResponse errorResponse = new()
{
ErrorCode = 400,
ErrorMsg = "Nothing to mark as seen"
};
return BadRequest(errorResponse);
}
foreach (var notificationId in notificationIds)
{
SeenNotification notificationSeen = new()
{
PlayerUUID = playerUUID,
NotificationUUID = notificationId
};
_context.SeenNotifications.Add(notificationSeen);
}
_context.SaveChanges();
return NoContent();
}
[HttpPost("dismiss")]
public ActionResult Dismiss(string[] notificationIds)
{
return NoContent();
}
}
}

View File

@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using Minecraft_Realms_Emulator.Attributes;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Entities;
using Minecraft_Realms_Emulator.Responses;
namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
@ -20,15 +21,25 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
[HttpGet]
public ActionResult<NotificationsResponse> GetNotifications()
{
string cookie = Request.Headers.Cookie;
string playerUUID = cookie.Split(";")[0].Split(":")[2];
List<NotificationResponse> notifications = [];
foreach (var notification in _context.Notifications)
foreach (var notification in _context.Notifications.ToList())
{
var seen = _context.SeenNotifications.Any(n => n.PlayerUUID == playerUUID && n.NotificationUUID == notification.NotificationUuid);
if (seen)
{
continue;
}
NotificationResponse notificationResponse = new()
{
NotificationUuid = notification.NotificationUuid,
Dismissable = notification.Dismissable,
Seen = false,
Seen = seen,
Type = notification.Type,
Title = notification.Title,
Message = notification.Message,
@ -48,5 +59,43 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
return Ok(notificationsResponse);
}
[HttpPost("seen")]
public ActionResult Seen(string[] notificationIds)
{
string cookie = Request.Headers.Cookie;
string playerUUID = cookie.Split(";")[0].Split(":")[2];
if (notificationIds.Length == 0) {
ErrorResponse errorResponse = new()
{
ErrorCode = 400,
ErrorMsg = "Nothing to mark as seen"
};
return BadRequest(errorResponse);
}
foreach (var notificationId in notificationIds)
{
SeenNotification notificationSeen = new()
{
PlayerUUID = playerUUID,
NotificationUUID = notificationId
};
_context.SeenNotifications.Add(notificationSeen);
}
_context.SaveChanges();
return NoContent();
}
[HttpPost("dismiss")]
public ActionResult Dismiss(string[] notificationIds)
{
return NoContent();
}
}
}