btcpayserver/BTCPayServer/Controllers/UINotificationsController.cs

239 lines
8.2 KiB
C#
Raw Normal View History

2020-06-29 04:44:35 +02:00
using System;
using System.Linq;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants;
using BTCPayServer.Client;
using BTCPayServer.Data;
using BTCPayServer.Filters;
using BTCPayServer.Models.NotificationViewModels;
using BTCPayServer.Security;
using BTCPayServer.Services;
using BTCPayServer.Services.Notifications;
using BTCPayServer.Services.Notifications.Blobs;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
namespace BTCPayServer.Controllers
{
[BitpayAPIConstraint(false)]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanViewNotificationsForUser)]
2022-01-14 12:16:28 +01:00
[Route("notifications/{action:lowercase=Index}")]
2022-01-07 04:32:00 +01:00
public class UINotificationsController : Controller
{
private readonly BTCPayServerEnvironment _env;
private readonly NotificationSender _notificationSender;
private readonly UserManager<ApplicationUser> _userManager;
2020-06-16 16:29:25 +02:00
private readonly NotificationManager _notificationManager;
private readonly EventAggregator _eventAggregator;
2022-01-07 04:32:00 +01:00
public UINotificationsController(BTCPayServerEnvironment env,
2020-06-16 16:29:25 +02:00
NotificationSender notificationSender,
UserManager<ApplicationUser> userManager,
2020-06-28 10:55:27 +02:00
NotificationManager notificationManager,
EventAggregator eventAggregator)
{
_env = env;
_notificationSender = notificationSender;
_userManager = userManager;
2020-06-16 16:29:25 +02:00
_notificationManager = notificationManager;
_eventAggregator = eventAggregator;
}
[HttpGet]
2020-06-26 13:52:39 +02:00
public IActionResult GetNotificationDropdownUI()
{
return ViewComponent("Notifications", new { appearance = "Dropdown" });
}
2021-12-31 08:59:02 +01:00
[HttpGet]
public async Task<IActionResult> SubscribeUpdates(CancellationToken cancellationToken)
{
if (!HttpContext.WebSockets.IsWebSocketRequest)
{
return BadRequest();
}
var websocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
var userId = _userManager.GetUserId(User);
var websocketHelper = new WebSocketHelper(websocket);
IEventAggregatorSubscription subscription = null;
try
{
subscription = _eventAggregator.SubscribeAsync<UserNotificationsUpdatedEvent>(async evt =>
{
if (evt.UserId == userId)
{
await websocketHelper.Send("update");
}
});
2020-06-26 11:54:18 +02:00
await websocketHelper.NextMessageAsync(cancellationToken);
}
catch (OperationCanceledException)
2020-06-26 11:54:18 +02:00
{
// ignored
}
catch (WebSocketException)
{
2020-06-26 11:54:18 +02:00
}
finally
{
subscription?.Dispose();
await websocketHelper.DisposeAsync(CancellationToken.None);
}
2020-06-28 10:55:27 +02:00
return new EmptyResult();
}
#if DEBUG
[HttpGet]
public async Task<IActionResult> GenerateJunk(int x = 100, bool admin = true)
{
for (int i = 0; i < x; i++)
{
await _notificationSender.SendNotification(
admin ? (NotificationScope)new AdminScope() : new UserScope(_userManager.GetUserId(User)),
new JunkNotification());
}
return RedirectToAction("Index");
}
#endif
[HttpGet]
public async Task<IActionResult> Index(int skip = 0, int count = 50, int timezoneOffset = 0)
{
2020-06-15 06:04:36 +02:00
if (!ValidUserClaim(out var userId))
2022-01-07 04:32:00 +01:00
return RedirectToAction("Index", "UIHome");
var res = await _notificationManager.GetNotifications(new NotificationsQuery()
{
2021-12-31 08:59:02 +01:00
Skip = skip,
Take = count,
UserId = userId
});
2021-12-31 08:59:02 +01:00
var model = new IndexViewModel() { Skip = skip, Count = count, Items = res.Items, Total = res.Count };
return View(model);
}
[HttpGet]
2020-06-15 07:53:12 +02:00
public async Task<IActionResult> Generate(string version)
{
if (_env.NetworkType != NBitcoin.ChainName.Regtest)
return NotFound();
await _notificationSender.SendNotification(new AdminScope(), new NewVersionNotification(version));
return RedirectToAction(nameof(Index));
}
[HttpPost]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanManageNotificationsForUser)]
public async Task<IActionResult> FlipRead(string id)
{
2020-06-15 06:04:36 +02:00
if (ValidUserClaim(out var userId))
{
2021-12-31 08:59:02 +01:00
await _notificationManager.ToggleSeen(new NotificationsQuery() { Ids = new[] { id }, UserId = userId }, null);
return RedirectToAction(nameof(Index));
}
return BadRequest();
}
2020-05-29 05:57:18 +02:00
[HttpGet]
public async Task<IActionResult> NotificationPassThrough(string id)
{
if (ValidUserClaim(out var userId))
{
var items = await
_notificationManager.ToggleSeen(new NotificationsQuery()
{
2021-12-31 08:59:02 +01:00
Ids = new[] { id },
UserId = userId
}, true);
2021-12-31 08:59:02 +01:00
var link = items.FirstOrDefault()?.ActionLink ?? "";
if (string.IsNullOrEmpty(link))
{
return RedirectToAction(nameof(Index));
}
return Redirect(link);
}
return NotFound();
}
2021-12-31 08:59:02 +01:00
[HttpPost]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanManageNotificationsForUser)]
public async Task<IActionResult> MassAction(string command, string[] selectedItems)
2020-05-29 05:57:18 +02:00
{
if (!ValidUserClaim(out var userId))
{
return NotFound();
}
if (command.StartsWith("flip-individual", StringComparison.InvariantCulture))
{
var id = command.Split(":")[1];
return await FlipRead(id);
}
if (selectedItems != null)
{
switch (command)
{
case "delete":
await _notificationManager.Remove(new NotificationsQuery()
{
2021-12-31 08:59:02 +01:00
UserId = userId,
Ids = selectedItems
});
break;
case "mark-seen":
await _notificationManager.ToggleSeen(new NotificationsQuery()
{
2021-12-31 08:59:02 +01:00
UserId = userId,
Ids = selectedItems,
Seen = false
}, true);
break;
case "mark-unseen":
await _notificationManager.ToggleSeen(new NotificationsQuery()
{
2021-12-31 08:59:02 +01:00
UserId = userId,
Ids = selectedItems,
Seen = true
}, false);
break;
}
return RedirectToAction(nameof(Index));
}
2020-05-29 05:57:18 +02:00
return RedirectToAction(nameof(Index));
}
[HttpPost]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanManageNotificationsForUser)]
public async Task<IActionResult> MarkAllAsSeen(string returnUrl)
{
if (!ValidUserClaim(out var userId))
{
return NotFound();
}
2021-12-31 08:59:02 +01:00
await _notificationManager.ToggleSeen(new NotificationsQuery() { Seen = false, UserId = userId }, true);
return Redirect(returnUrl);
}
2020-06-15 06:04:36 +02:00
private bool ValidUserClaim(out string userId)
{
userId = _userManager.GetUserId(User);
return userId != null;
}
}
}