✨ Unread count notification APIs
This commit is contained in:
parent
793043aba2
commit
61f7764510
@ -11,14 +11,30 @@ namespace DysonNetwork.Sphere.Account;
|
|||||||
[Route("/notifications")]
|
[Route("/notifications")]
|
||||||
public class NotificationController(AppDatabase db, NotificationService nty) : ControllerBase
|
public class NotificationController(AppDatabase db, NotificationService nty) : ControllerBase
|
||||||
{
|
{
|
||||||
[HttpGet]
|
[HttpGet("count")]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
public async Task<ActionResult<List<Notification>>> ListNotifications([FromQuery] int offset = 0,
|
public async Task<ActionResult<int>> CountUnreadNotifications()
|
||||||
[FromQuery] int take = 20)
|
|
||||||
{
|
{
|
||||||
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
|
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
|
||||||
var currentUser = currentUserValue as Account;
|
if (currentUserValue is not Account currentUser) return Unauthorized();
|
||||||
if (currentUser == null) 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<ActionResult<List<Notification>>> 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
|
var totalCount = await db.Notifications
|
||||||
.Where(s => s.AccountId == currentUser.Id)
|
.Where(s => s.AccountId == currentUser.Id)
|
||||||
@ -31,6 +47,7 @@ public class NotificationController(AppDatabase db, NotificationService nty) : C
|
|||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
Response.Headers["X-Total"] = totalCount.ToString();
|
Response.Headers["X-Total"] = totalCount.ToString();
|
||||||
|
await nty.MarkNotificationsViewed(notifications);
|
||||||
|
|
||||||
return Ok(notifications);
|
return Ok(notifications);
|
||||||
}
|
}
|
||||||
|
@ -140,6 +140,18 @@ public class NotificationService
|
|||||||
await Task.WhenAll(tasks);
|
await Task.WhenAll(tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task MarkNotificationsViewed(ICollection<Notification> 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)
|
private async Task _PushSingleNotification(Notification notification, NotificationPushSubscription subscription)
|
||||||
{
|
{
|
||||||
switch (subscription.Provider)
|
switch (subscription.Provider)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user