Mark all notifications read

This commit is contained in:
2025-08-14 15:33:48 +08:00
parent b04b17c8ae
commit 27e6dde7c4
3 changed files with 26 additions and 9 deletions

View File

@@ -1,8 +1,6 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
using DysonNetwork.Shared.Data; using DysonNetwork.Shared.Data;
using DysonNetwork.Shared.Proto;
using NodaTime; using NodaTime;
namespace DysonNetwork.Pusher.Notification; namespace DysonNetwork.Pusher.Notification;

View File

@@ -5,7 +5,6 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NodaTime; using NodaTime;
using AccountService = DysonNetwork.Shared.Proto.AccountService;
namespace DysonNetwork.Pusher.Notification; namespace DysonNetwork.Pusher.Notification;
@@ -13,8 +12,8 @@ namespace DysonNetwork.Pusher.Notification;
[Route("/api/notifications")] [Route("/api/notifications")]
public class NotificationController( public class NotificationController(
AppDatabase db, AppDatabase db,
PushService nty, PushService nty
AccountService.AccountServiceClient accounts) : ControllerBase ) : ControllerBase
{ {
[HttpGet("count")] [HttpGet("count")]
[Authorize] [Authorize]
@@ -59,6 +58,18 @@ public class NotificationController(
return Ok(notifications); return Ok(notifications);
} }
[HttpPost("all/read")]
[Authorize]
public async Task<ActionResult> 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 public class PushNotificationSubscribeRequest
{ {
[MaxLength(4096)] public string DeviceToken { get; set; } = null!; [MaxLength(4096)] public string DeviceToken { get; set; } = null!;
@@ -112,10 +123,10 @@ public class NotificationController(
public class NotificationRequest public class NotificationRequest
{ {
[Required] [MaxLength(1024)] public string Topic { get; set; } = null!; [Required][MaxLength(1024)] public string Topic { get; set; } = null!;
[Required] [MaxLength(1024)] public string Title { get; set; } = null!; [Required][MaxLength(1024)] public string Title { get; set; } = null!;
[MaxLength(2048)] public string? Subtitle { get; set; } [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<string, object>? Meta { get; set; } public Dictionary<string, object>? Meta { get; set; }
public int Priority { get; set; } = 10; public int Priority { get; set; } = 10;
} }

View File

@@ -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<Guid> accounts, bool save = false) public async Task SendNotificationBatch(Notification notification, List<Guid> accounts, bool save = false)
{ {
if (save) if (save)