diff --git a/DysonNetwork.Sphere/Account/NotificationController.cs b/DysonNetwork.Sphere/Account/NotificationController.cs index 5f97ae9..06f6ec2 100644 --- a/DysonNetwork.Sphere/Account/NotificationController.cs +++ b/DysonNetwork.Sphere/Account/NotificationController.cs @@ -11,14 +11,30 @@ namespace DysonNetwork.Sphere.Account; [Route("/notifications")] public class NotificationController(AppDatabase db, NotificationService nty) : ControllerBase { - [HttpGet] + [HttpGet("count")] [Authorize] - public async Task>> ListNotifications([FromQuery] int offset = 0, - [FromQuery] int take = 20) + public async Task> CountUnreadNotifications() { HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue); - var currentUser = currentUserValue as Account; - if (currentUser == null) return Unauthorized(); + if (currentUserValue is not Account currentUser) return Unauthorized(); + + var count = await db.Notifications + .Where(s => s.AccountId == currentUser.Id && s.ViewedAt == null) + .CountAsync(); + return Ok(count); + } + + [HttpGet] + [Authorize] + public async Task>> ListNotifications( + [FromQuery] int offset = 0, + // The page size set to 5 is to avoid the client pulled the notification + // but didn't render it in the screen-viewable region. + [FromQuery] int take = 5 + ) + { + HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue); + if (currentUserValue is not Account currentUser) return Unauthorized(); var totalCount = await db.Notifications .Where(s => s.AccountId == currentUser.Id) @@ -31,6 +47,7 @@ public class NotificationController(AppDatabase db, NotificationService nty) : C .ToListAsync(); Response.Headers["X-Total"] = totalCount.ToString(); + await nty.MarkNotificationsViewed(notifications); return Ok(notifications); } diff --git a/DysonNetwork.Sphere/Account/NotificationService.cs b/DysonNetwork.Sphere/Account/NotificationService.cs index 99ebe1b..8b18c44 100644 --- a/DysonNetwork.Sphere/Account/NotificationService.cs +++ b/DysonNetwork.Sphere/Account/NotificationService.cs @@ -140,6 +140,18 @@ public class NotificationService await Task.WhenAll(tasks); } + public async Task MarkNotificationsViewed(ICollection notifications) + { + var now = SystemClock.Instance.GetCurrentInstant(); + var id = notifications.Where(n => n.ViewedAt == null).Select(n => n.Id).ToList(); + if (id.Count == 0) return; + + await _db.Notifications + .Where(n => id.Contains(n.Id)) + .ExecuteUpdateAsync(s => s.SetProperty(n => n.ViewedAt, now) + ); + } + private async Task _PushSingleNotification(Notification notification, NotificationPushSubscription subscription) { switch (subscription.Provider)