diff --git a/DysonNetwork.Pusher/Notification/Notification.cs b/DysonNetwork.Pusher/Notification/Notification.cs index 47cbdee..322dca7 100644 --- a/DysonNetwork.Pusher/Notification/Notification.cs +++ b/DysonNetwork.Pusher/Notification/Notification.cs @@ -1,8 +1,6 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -using System.Text.Json.Serialization; using DysonNetwork.Shared.Data; -using DysonNetwork.Shared.Proto; using NodaTime; namespace DysonNetwork.Pusher.Notification; diff --git a/DysonNetwork.Pusher/Notification/NotificationController.cs b/DysonNetwork.Pusher/Notification/NotificationController.cs index 90551e2..8aac56f 100644 --- a/DysonNetwork.Pusher/Notification/NotificationController.cs +++ b/DysonNetwork.Pusher/Notification/NotificationController.cs @@ -5,7 +5,6 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using NodaTime; -using AccountService = DysonNetwork.Shared.Proto.AccountService; namespace DysonNetwork.Pusher.Notification; @@ -13,8 +12,8 @@ namespace DysonNetwork.Pusher.Notification; [Route("/api/notifications")] public class NotificationController( AppDatabase db, - PushService nty, - AccountService.AccountServiceClient accounts) : ControllerBase + PushService nty +) : ControllerBase { [HttpGet("count")] [Authorize] @@ -59,6 +58,18 @@ public class NotificationController( return Ok(notifications); } + [HttpPost("all/read")] + [Authorize] + public async Task MarkAllNotificationsViewed() + { + HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue); + if (currentUserValue is not Account currentUser) return Unauthorized(); + var accountId = Guid.Parse(currentUser.Id); + + await nty.MarkAllNotificationsViewed(accountId); + return Ok(); + } + public class PushNotificationSubscribeRequest { [MaxLength(4096)] public string DeviceToken { get; set; } = null!; @@ -112,10 +123,10 @@ public class NotificationController( public class NotificationRequest { - [Required] [MaxLength(1024)] public string Topic { get; set; } = null!; - [Required] [MaxLength(1024)] public string Title { get; set; } = null!; + [Required][MaxLength(1024)] public string Topic { get; set; } = null!; + [Required][MaxLength(1024)] public string Title { get; set; } = null!; [MaxLength(2048)] public string? Subtitle { get; set; } - [Required] [MaxLength(4096)] public string Content { get; set; } = null!; + [Required][MaxLength(4096)] public string Content { get; set; } = null!; public Dictionary? Meta { get; set; } public int Priority { get; set; } = 10; } @@ -149,4 +160,4 @@ public class NotificationController( ); return Ok(); } -} \ No newline at end of file +} diff --git a/DysonNetwork.Pusher/Notification/PushService.cs b/DysonNetwork.Pusher/Notification/PushService.cs index c277aca..4b4f011 100644 --- a/DysonNetwork.Pusher/Notification/PushService.cs +++ b/DysonNetwork.Pusher/Notification/PushService.cs @@ -173,6 +173,14 @@ public class PushService ); } + public async Task MarkAllNotificationsViewed(Guid accountId) + { + var now = SystemClock.Instance.GetCurrentInstant(); + await _db.Notifications + .Where(n => n.AccountId == accountId) + .ExecuteUpdateAsync(s => s.SetProperty(n => n.ViewedAt, now)); + } + public async Task SendNotificationBatch(Notification notification, List accounts, bool save = false) { if (save)