Greenfield: Manage notifications (#6058)

Prerequisite for btcpayserver/app#12.
This commit is contained in:
d11n
2024-06-25 20:01:11 +02:00
committed by GitHub
parent e0c5ac5271
commit feffbbd980
7 changed files with 306 additions and 3 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants;
@@ -23,12 +24,16 @@ namespace BTCPayServer.Controllers.Greenfield
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly NotificationManager _notificationManager;
private readonly IEnumerable<INotificationHandler> _notificationHandlers;
public GreenfieldNotificationsController(UserManager<ApplicationUser> userManager,
NotificationManager notificationManager)
public GreenfieldNotificationsController(
UserManager<ApplicationUser> userManager,
NotificationManager notificationManager,
IEnumerable<INotificationHandler> notificationHandlers)
{
_userManager = userManager;
_notificationManager = notificationManager;
_notificationHandlers = notificationHandlers;
}
[Authorize(Policy = Policies.CanViewNotificationsForUser,
@@ -95,6 +100,37 @@ namespace BTCPayServer.Controllers.Greenfield
return Ok();
}
[Authorize(Policy = Policies.CanManageNotificationsForUser, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/users/me/notification-settings")]
public async Task<IActionResult> GetNotificationSettings()
{
var user = await _userManager.GetUserAsync(User);
var model = GetNotificationSettingsData(user);
return Ok(model);
}
[Authorize(Policy = Policies.CanManageNotificationsForUser, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpPut("~/api/v1/users/me/notification-settings")]
public async Task<IActionResult> UpdateNotificationSettings(UpdateNotificationSettingsRequest request)
{
var user = await _userManager.GetUserAsync(User);
if (request.Disabled.Contains("all"))
{
user.DisabledNotifications = "all";
}
else
{
var disabled = _notificationHandlers
.SelectMany(handler => handler.Meta.Select(tuple => tuple.identifier))
.Where(id => request.Disabled.Contains(id)).ToList();
user.DisabledNotifications = disabled.Any() ? string.Join(';', disabled) + ";" : string.Empty;
}
await _userManager.UpdateAsync(user);
var model = GetNotificationSettingsData(user);
return Ok(model);
}
private NotificationData ToModel(NotificationViewModel entity)
{
@@ -113,5 +149,19 @@ namespace BTCPayServer.Controllers.Greenfield
{
return this.CreateAPIError(404, "notification-not-found", "The notification was not found");
}
private NotificationSettingsData GetNotificationSettingsData(ApplicationUser user)
{
var disabledAll = user.DisabledNotifications == "all";
var disabledNotifications = user.DisabledNotifications?.Split(';', StringSplitOptions.RemoveEmptyEntries).ToList() ?? [];
var notifications = _notificationHandlers.SelectMany(handler => handler.Meta.Select(tuple =>
new NotificationSettingsItemData
{
Identifier = tuple.identifier,
Name = tuple.name,
Enabled = !disabledAll && !disabledNotifications.Contains(tuple.identifier, StringComparer.InvariantCultureIgnoreCase)
})).ToList();
return new NotificationSettingsData { Notifications = notifications };
}
}
}