mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-17 05:54:26 +01:00
GreenField: Notifications API (#2055)
* GreenField: Notifications API This refactors notifications so that we dont have a bunch of duplicated direct access to db contexts in controllers and then introduces new endpoints to fetch/toggle seen/remove notifications of the current user. * add tests + docs * fix test * pr changes * fix permission json
This commit is contained in:
105
BTCPayServer/Controllers/GreenField/NotificationsController.cs
Normal file
105
BTCPayServer/Controllers/GreenField/NotificationsController.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Abstractions.Constants;
|
||||
using BTCPayServer.Abstractions.Contracts;
|
||||
using BTCPayServer.Client;
|
||||
using BTCPayServer.Client.Models;
|
||||
using BTCPayServer.Data;
|
||||
using BTCPayServer.Services.Notifications;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Cors;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NotificationData = BTCPayServer.Client.Models.NotificationData;
|
||||
|
||||
namespace BTCPayServer.Controllers.GreenField
|
||||
{
|
||||
[ApiController]
|
||||
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
||||
[EnableCors(CorsPolicies.All)]
|
||||
public class NotificationsController : ControllerBase
|
||||
{
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly NotificationManager _notificationManager;
|
||||
|
||||
public NotificationsController(UserManager<ApplicationUser> userManager,
|
||||
NotificationManager notificationManager)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_notificationManager = notificationManager;
|
||||
}
|
||||
|
||||
[Authorize(Policy = Policies.CanViewNotificationsForUser,
|
||||
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
||||
[HttpGet("~/api/v1/users/me/notifications")]
|
||||
public async Task<IActionResult> GetNotifications(bool? seen = null)
|
||||
{
|
||||
var items = await _notificationManager.GetNotifications(new NotificationsQuery()
|
||||
{
|
||||
Seen = seen, UserId = _userManager.GetUserId(User)
|
||||
});
|
||||
|
||||
return Ok(items.Items.Select(ToModel));
|
||||
}
|
||||
|
||||
[Authorize(Policy = Policies.CanViewNotificationsForUser,
|
||||
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
||||
[HttpGet("~/api/v1/users/me/notifications/{id}")]
|
||||
public async Task<IActionResult> GetNotification(string id)
|
||||
{
|
||||
var items = await _notificationManager.GetNotifications(new NotificationsQuery()
|
||||
{
|
||||
Ids = new[] {id}, UserId = _userManager.GetUserId(User)
|
||||
});
|
||||
|
||||
if (items.Count == 0)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return Ok(ToModel(items.Items.First()));
|
||||
}
|
||||
|
||||
[Authorize(Policy = Policies.CanManageNotificationsForUser,
|
||||
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
||||
[HttpPut("~/api/v1/users/me/notifications/{id}")]
|
||||
public async Task<IActionResult> UpdateNotification(string id, UpdateNotification request)
|
||||
{
|
||||
var items = await _notificationManager.ToggleSeen(
|
||||
new NotificationsQuery() {Ids = new[] {id}, UserId = _userManager.GetUserId(User)}, request.Seen);
|
||||
|
||||
if (items.Count == 0)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return Ok(ToModel(items.First()));
|
||||
}
|
||||
|
||||
[Authorize(Policy = Policies.CanManageNotificationsForUser,
|
||||
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
||||
[HttpDelete("~/api/v1/users/me/notifications/{id}")]
|
||||
public async Task<IActionResult> DeleteNotification(string id)
|
||||
{
|
||||
await _notificationManager.Remove(new NotificationsQuery()
|
||||
{
|
||||
Ids = new[] {id}, UserId = _userManager.GetUserId(User)
|
||||
});
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
private NotificationData ToModel(NotificationViewModel entity)
|
||||
{
|
||||
return new NotificationData()
|
||||
{
|
||||
Id = entity.Id,
|
||||
CreatedTime = entity.Created,
|
||||
Body = entity.Body,
|
||||
Seen = entity.Seen,
|
||||
Link = string.IsNullOrEmpty(entity.ActionLink) ? null : new Uri(entity.ActionLink)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user