2024-07-10 17:12:22 +02:00
|
|
|
#nullable enable
|
2020-12-11 15:11:08 +01:00
|
|
|
using System;
|
2024-06-25 20:01:11 +02:00
|
|
|
using System.Collections.Generic;
|
2020-12-11 15:11:08 +01:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Abstractions.Constants;
|
|
|
|
using BTCPayServer.Abstractions.Contracts;
|
2022-02-24 09:00:44 +01:00
|
|
|
using BTCPayServer.Abstractions.Extensions;
|
2020-12-11 15:11:08 +01:00
|
|
|
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;
|
|
|
|
|
2022-01-14 13:05:23 +09:00
|
|
|
namespace BTCPayServer.Controllers.Greenfield
|
2020-12-11 15:11:08 +01:00
|
|
|
{
|
|
|
|
[ApiController]
|
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[EnableCors(CorsPolicies.All)]
|
2022-01-07 12:17:59 +09:00
|
|
|
public class GreenfieldNotificationsController : ControllerBase
|
2020-12-11 15:11:08 +01:00
|
|
|
{
|
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
|
|
private readonly NotificationManager _notificationManager;
|
2024-06-25 20:01:11 +02:00
|
|
|
private readonly IEnumerable<INotificationHandler> _notificationHandlers;
|
2020-12-11 15:11:08 +01:00
|
|
|
|
2024-06-25 20:01:11 +02:00
|
|
|
public GreenfieldNotificationsController(
|
|
|
|
UserManager<ApplicationUser> userManager,
|
|
|
|
NotificationManager notificationManager,
|
|
|
|
IEnumerable<INotificationHandler> notificationHandlers)
|
2020-12-11 15:11:08 +01:00
|
|
|
{
|
|
|
|
_userManager = userManager;
|
|
|
|
_notificationManager = notificationManager;
|
2024-06-25 20:01:11 +02:00
|
|
|
_notificationHandlers = notificationHandlers;
|
2020-12-11 15:11:08 +01:00
|
|
|
}
|
|
|
|
|
2024-07-10 17:12:22 +02:00
|
|
|
[Authorize(Policy = Policies.CanViewNotificationsForUser, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2020-12-11 15:11:08 +01:00
|
|
|
[HttpGet("~/api/v1/users/me/notifications")]
|
2024-07-10 17:12:22 +02:00
|
|
|
public async Task<IActionResult> GetNotifications(bool? seen = null, [FromQuery] int? skip = null, [FromQuery] int? take = null, [FromQuery] string[]? storeId = null)
|
2020-12-11 15:11:08 +01:00
|
|
|
{
|
2024-07-10 17:12:22 +02:00
|
|
|
var items = await _notificationManager.GetNotifications(new NotificationsQuery
|
2020-12-11 15:11:08 +01:00
|
|
|
{
|
2021-12-31 16:59:02 +09:00
|
|
|
Seen = seen,
|
|
|
|
UserId = _userManager.GetUserId(User),
|
|
|
|
Skip = skip,
|
2024-07-10 17:12:22 +02:00
|
|
|
Take = take,
|
|
|
|
StoreIds = storeId,
|
2020-12-11 15:11:08 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return Ok(items.Items.Select(ToModel));
|
|
|
|
}
|
|
|
|
|
2024-07-10 17:12:22 +02:00
|
|
|
[Authorize(Policy = Policies.CanViewNotificationsForUser, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2020-12-11 15:11:08 +01:00
|
|
|
[HttpGet("~/api/v1/users/me/notifications/{id}")]
|
|
|
|
public async Task<IActionResult> GetNotification(string id)
|
|
|
|
{
|
2024-07-10 17:12:22 +02:00
|
|
|
var items = await _notificationManager.GetNotifications(new NotificationsQuery
|
2020-12-11 15:11:08 +01:00
|
|
|
{
|
2024-07-10 17:12:22 +02:00
|
|
|
Ids = [id],
|
2021-12-31 16:59:02 +09:00
|
|
|
UserId = _userManager.GetUserId(User)
|
2020-12-11 15:11:08 +01:00
|
|
|
});
|
|
|
|
|
2024-07-10 17:12:22 +02:00
|
|
|
return items.Count == 0 ? NotificationNotFound() : Ok(ToModel(items.Items.First()));
|
2020-12-11 15:11:08 +01:00
|
|
|
}
|
|
|
|
|
2024-07-10 17:12:22 +02:00
|
|
|
[Authorize(Policy = Policies.CanManageNotificationsForUser, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2020-12-11 15:11:08 +01:00
|
|
|
[HttpPut("~/api/v1/users/me/notifications/{id}")]
|
|
|
|
public async Task<IActionResult> UpdateNotification(string id, UpdateNotification request)
|
|
|
|
{
|
|
|
|
var items = await _notificationManager.ToggleSeen(
|
2024-07-10 17:12:22 +02:00
|
|
|
new NotificationsQuery { Ids = [id], UserId = _userManager.GetUserId(User) }, request.Seen);
|
2020-12-11 15:11:08 +01:00
|
|
|
|
2024-07-10 17:12:22 +02:00
|
|
|
return items.Count == 0 ? NotificationNotFound() : Ok(ToModel(items.First()));
|
2020-12-11 15:11:08 +01:00
|
|
|
}
|
|
|
|
|
2024-07-10 17:12:22 +02:00
|
|
|
[Authorize(Policy = Policies.CanManageNotificationsForUser, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2020-12-11 15:11:08 +01:00
|
|
|
[HttpDelete("~/api/v1/users/me/notifications/{id}")]
|
|
|
|
public async Task<IActionResult> DeleteNotification(string id)
|
|
|
|
{
|
2024-07-10 17:12:22 +02:00
|
|
|
await _notificationManager.Remove(new NotificationsQuery
|
2020-12-11 15:11:08 +01:00
|
|
|
{
|
2024-07-10 17:12:22 +02:00
|
|
|
Ids = [id],
|
2021-12-31 16:59:02 +09:00
|
|
|
UserId = _userManager.GetUserId(User)
|
2020-12-11 15:11:08 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
}
|
2024-07-10 17:12:22 +02:00
|
|
|
|
2024-06-25 20:01:11 +02:00
|
|
|
[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);
|
|
|
|
}
|
2020-12-11 15:11:08 +01:00
|
|
|
|
2024-07-10 17:12:22 +02:00
|
|
|
private static NotificationData ToModel(NotificationViewModel entity)
|
2020-12-11 15:11:08 +01:00
|
|
|
{
|
2023-02-21 03:06:27 +01:00
|
|
|
return new NotificationData
|
2020-12-11 15:11:08 +01:00
|
|
|
{
|
|
|
|
Id = entity.Id,
|
2023-02-21 03:06:27 +01:00
|
|
|
Identifier = entity.Identifier,
|
|
|
|
Type = entity.Type,
|
2020-12-11 15:11:08 +01:00
|
|
|
CreatedTime = entity.Created,
|
|
|
|
Body = entity.Body,
|
2024-07-10 17:12:22 +02:00
|
|
|
StoreId = entity.StoreId,
|
2020-12-11 15:11:08 +01:00
|
|
|
Seen = entity.Seen,
|
|
|
|
Link = string.IsNullOrEmpty(entity.ActionLink) ? null : new Uri(entity.ActionLink)
|
|
|
|
};
|
|
|
|
}
|
2024-07-10 17:12:22 +02:00
|
|
|
|
2021-12-23 05:32:08 +01:00
|
|
|
private IActionResult NotificationNotFound()
|
|
|
|
{
|
|
|
|
return this.CreateAPIError(404, "notification-not-found", "The notification was not found");
|
|
|
|
}
|
2024-06-25 20:01:11 +02:00
|
|
|
|
|
|
|
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 };
|
|
|
|
}
|
2020-12-11 15:11:08 +01:00
|
|
|
}
|
|
|
|
}
|