Persist private alerts at 50, 75, 90, 95 and 99 percent of net consumption, reporting only the highest crossed threshold and rearming after consumption decreases. Keep notification reading and dismissal accessible at quota. Add crossing, concurrency, rollback and privacy tests. Verified 229 API and 238 server tests pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
51 lines
1.7 KiB
C#
51 lines
1.7 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Yavsc.Abstract.Models.Messaging;
|
|
using Yavsc.Models;
|
|
using Yavsc.Server.Helpers;
|
|
|
|
namespace Yavsc.ApiControllers.Administration;
|
|
|
|
[Produces("application/json")]
|
|
[ResourceUsageExempt]
|
|
[Route(Constants.APIPrefix + "/" + Constants.NotificationsPath)]
|
|
public sealed class NotificationsApiController : Controller
|
|
{
|
|
private readonly ApplicationDbContext _dbContext;
|
|
|
|
public NotificationsApiController(ApplicationDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
[HttpGet]
|
|
[HttpGet(Constants.NotificationsCurrentUserRoute)]
|
|
[AllowAnonymous]
|
|
public async Task<ActionResult<List<Notification>>> GetCurrentUserNotifications()
|
|
{
|
|
var currentUserId = User.GetUserId();
|
|
var isAdmin = User.IsInRole(Constants.AdminName);
|
|
|
|
var dismissedIds = string.IsNullOrWhiteSpace(currentUserId)
|
|
? new HashSet<long>()
|
|
: await _dbContext.DismissClicked
|
|
.AsNoTracking()
|
|
.Where(d => d.UserId == currentUserId)
|
|
.Select(d => d.NotificationId)
|
|
.ToHashSetAsync();
|
|
|
|
var notifications = await _dbContext.Notification
|
|
.AsNoTracking()
|
|
.Where(n => !dismissedIds.Contains(n.Id))
|
|
.Where(n =>
|
|
string.IsNullOrWhiteSpace(n.Target)
|
|
|| (n.Target != null && n.Target.StartsWith("pub/"))
|
|
|| (!string.IsNullOrWhiteSpace(currentUserId) && n.Target == $"user/{currentUserId}")
|
|
|| (isAdmin && n.Target == "administration"))
|
|
.OrderByDescending(n => n.Id)
|
|
.ToListAsync();
|
|
|
|
return Ok(notifications);
|
|
}
|
|
}
|