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>
77 lines
2.7 KiB
C#
77 lines
2.7 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Yavsc.Abstract.Models.Messaging;
|
|
using Yavsc.Models;
|
|
using Yavsc.Models.Messaging;
|
|
using Yavsc.Server.Helpers;
|
|
|
|
namespace Yavsc.ApiControllers.Administration;
|
|
|
|
/// <summary>
|
|
/// Notifications dismissed by the authenticated user.
|
|
/// </summary>
|
|
[Authorize]
|
|
[ResourceUsageExempt]
|
|
[Produces("application/json")]
|
|
[Route(Constants.APIPrefix + "/" + Constants.NotificationsDismissPath)]
|
|
public sealed class DismissClicksApiController : Controller
|
|
{
|
|
private readonly ApplicationDbContext _dbContext;
|
|
|
|
public DismissClicksApiController(ApplicationDbContext dbContext)
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<List<DismissedNotification>>> GetDismissed()
|
|
{
|
|
var userId = User.GetUserId();
|
|
var dismissed = await _dbContext.DismissClicked
|
|
.AsNoTracking()
|
|
.Where(d => d.UserId == userId)
|
|
.OrderBy(d => d.NotificationId)
|
|
.Select(d => new DismissedNotification { NotificationId = d.NotificationId })
|
|
.ToListAsync();
|
|
return Ok(dismissed);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dismisses a notification for the current user. Idempotent.
|
|
/// </summary>
|
|
[HttpPost(Constants.NotificationsDismissClickRoute + "/{notificationId:long}")]
|
|
public async Task<ActionResult<DismissedNotification>> Click(long notificationId)
|
|
{
|
|
var userId = User.GetUserId();
|
|
if (!await _dbContext.Notification.AnyAsync(n => n.Id == notificationId))
|
|
return NotFound();
|
|
|
|
var exists = await _dbContext.DismissClicked
|
|
.AnyAsync(d => d.UserId == userId && d.NotificationId == notificationId);
|
|
if (!exists)
|
|
{
|
|
_dbContext.DismissClicked.Add(new DismissClicked { UserId = userId, NotificationId = notificationId });
|
|
await _dbContext.SaveChangesAsync(userId);
|
|
}
|
|
|
|
return Ok(new DismissedNotification { NotificationId = notificationId });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Restores a previously dismissed notification for the current user.
|
|
/// </summary>
|
|
[HttpDelete(Constants.NotificationsDismissClickRoute + "/{notificationId:long}")]
|
|
public async Task<IActionResult> Restore(long notificationId)
|
|
{
|
|
var userId = User.GetUserId();
|
|
var dismissed = await _dbContext.DismissClicked
|
|
.SingleOrDefaultAsync(d => d.UserId == userId && d.NotificationId == notificationId);
|
|
if (dismissed is null)
|
|
return NotFound();
|
|
|
|
_dbContext.DismissClicked.Remove(dismissed);
|
|
await _dbContext.SaveChangesAsync(userId);
|
|
return NoContent();
|
|
}
|
|
}
|