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