2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2020-05-28 16:19:02 -05:00
|
|
|
using System.Linq;
|
2021-10-05 14:29:49 +09:00
|
|
|
using System.Net.WebSockets;
|
2020-06-24 10:23:16 +02:00
|
|
|
using System.Threading;
|
2020-05-28 16:19:02 -05:00
|
|
|
using System.Threading.Tasks;
|
2020-11-17 13:46:23 +01:00
|
|
|
using BTCPayServer.Abstractions.Constants;
|
2021-12-31 08:36:38 +01:00
|
|
|
using BTCPayServer.Client;
|
2020-05-28 21:54:26 -05:00
|
|
|
using BTCPayServer.Data;
|
2020-05-28 16:19:02 -05:00
|
|
|
using BTCPayServer.Filters;
|
|
|
|
using BTCPayServer.Models.NotificationViewModels;
|
|
|
|
using BTCPayServer.Security;
|
2020-06-15 17:06:38 +09:00
|
|
|
using BTCPayServer.Services;
|
2020-06-14 23:47:11 -05:00
|
|
|
using BTCPayServer.Services.Notifications;
|
2020-06-15 16:45:29 +09:00
|
|
|
using BTCPayServer.Services.Notifications.Blobs;
|
2020-05-28 16:19:02 -05:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2020-06-11 17:58:50 -05:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
2020-05-28 16:19:02 -05:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
{
|
|
|
|
[BitpayAPIConstraint(false)]
|
2021-12-31 08:36:38 +01:00
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanViewNotificationsForUser)]
|
2022-01-14 20:16:28 +09:00
|
|
|
[Route("notifications/{action:lowercase=Index}")]
|
2022-01-07 12:32:00 +09:00
|
|
|
public class UINotificationsController : Controller
|
2020-05-28 16:19:02 -05:00
|
|
|
{
|
2020-06-11 17:58:50 -05:00
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
2020-06-16 23:29:25 +09:00
|
|
|
private readonly NotificationManager _notificationManager;
|
2020-05-28 16:19:02 -05:00
|
|
|
|
2023-09-18 10:55:05 +09:00
|
|
|
public UINotificationsController(
|
2020-06-16 23:29:25 +09:00
|
|
|
UserManager<ApplicationUser> userManager,
|
2023-09-18 10:55:05 +09:00
|
|
|
NotificationManager notificationManager)
|
2020-05-28 16:19:02 -05:00
|
|
|
{
|
2020-06-11 17:58:50 -05:00
|
|
|
_userManager = userManager;
|
2020-06-16 23:29:25 +09:00
|
|
|
_notificationManager = notificationManager;
|
2020-06-24 10:23:16 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 16:19:02 -05: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 16:19:02 -05:00
|
|
|
{
|
2020-06-14 23:04:36 -05:00
|
|
|
if (!ValidUserClaim(out var userId))
|
2022-01-07 12:32:00 +09:00
|
|
|
return RedirectToAction("Index", "UIHome");
|
2020-05-28 16:19:02 -05:00
|
|
|
|
2020-12-11 15:11:08 +01:00
|
|
|
var res = await _notificationManager.GetNotifications(new NotificationsQuery()
|
2020-05-28 16:19:02 -05:00
|
|
|
{
|
2021-12-31 16:59:02 +09:00
|
|
|
Skip = skip,
|
|
|
|
Take = count,
|
|
|
|
UserId = userId
|
2020-12-11 15:11:08 +01:00
|
|
|
});
|
|
|
|
|
2021-12-31 16:59:02 +09:00
|
|
|
var model = new IndexViewModel() { Skip = skip, Count = count, Items = res.Items, Total = res.Count };
|
2020-05-28 21:54:26 -05:00
|
|
|
|
2020-05-28 16:19:02 -05:00
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
|
2020-06-10 23:41:18 -05:00
|
|
|
[HttpPost]
|
2021-12-31 08:36:38 +01:00
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanManageNotificationsForUser)]
|
2020-06-10 23:41:18 -05:00
|
|
|
public async Task<IActionResult> FlipRead(string id)
|
2020-05-28 22:32:31 -05:00
|
|
|
{
|
2020-06-14 23:04:36 -05:00
|
|
|
if (ValidUserClaim(out var userId))
|
2020-06-11 00:06:53 -05:00
|
|
|
{
|
2021-12-31 16:59:02 +09: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 00:06:53 -05:00
|
|
|
}
|
2020-05-28 22:32:31 -05:00
|
|
|
|
2020-06-23 03:06:02 +02:00
|
|
|
return BadRequest();
|
2020-05-28 22:32:31 -05:00
|
|
|
}
|
2020-05-28 22:57:18 -05: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()
|
|
|
|
{
|
2021-12-31 16:59:02 +09:00
|
|
|
Ids = new[] { id },
|
|
|
|
UserId = userId
|
2020-12-11 15:11:08 +01:00
|
|
|
}, true);
|
2021-12-31 16:59:02 +09:00
|
|
|
|
2020-12-11 15:11:08 +01:00
|
|
|
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();
|
|
|
|
}
|
2021-12-31 16:59:02 +09:00
|
|
|
|
2020-06-10 22:41:52 -05:00
|
|
|
[HttpPost]
|
2021-12-31 08:36:38 +01:00
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanManageNotificationsForUser)]
|
2020-06-13 19:19:21 -05:00
|
|
|
public async Task<IActionResult> MassAction(string command, string[] selectedItems)
|
2020-05-28 22:57:18 -05: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-13 19:19:21 -05:00
|
|
|
if (selectedItems != null)
|
2020-06-10 23:41:18 -05:00
|
|
|
{
|
2020-06-23 03:06:02 +02:00
|
|
|
switch (command)
|
2020-06-10 23:41:18 -05:00
|
|
|
{
|
2020-06-23 03:06:02 +02:00
|
|
|
case "delete":
|
2020-12-11 15:11:08 +01:00
|
|
|
await _notificationManager.Remove(new NotificationsQuery()
|
|
|
|
{
|
2021-12-31 16:59:02 +09:00
|
|
|
UserId = userId,
|
|
|
|
Ids = selectedItems
|
2020-12-11 15:11:08 +01:00
|
|
|
});
|
2020-06-10 23:41:18 -05: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
|
|
|
{
|
2021-12-31 16:59:02 +09:00
|
|
|
UserId = userId,
|
|
|
|
Ids = selectedItems,
|
|
|
|
Seen = false
|
2020-12-11 15:11:08 +01:00
|
|
|
}, 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
|
|
|
{
|
2021-12-31 16:59:02 +09:00
|
|
|
UserId = userId,
|
|
|
|
Ids = selectedItems,
|
|
|
|
Seen = true
|
2020-12-11 15:11:08 +01:00
|
|
|
}, false);
|
2020-06-23 03:06:02 +02:00
|
|
|
break;
|
2020-06-10 23:41:18 -05:00
|
|
|
}
|
2020-06-23 03:06:02 +02:00
|
|
|
return RedirectToAction(nameof(Index));
|
2020-06-10 23:41:18 -05:00
|
|
|
}
|
2020-05-28 22:57:18 -05:00
|
|
|
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
}
|
2020-06-11 00:06:53 -05:00
|
|
|
|
2020-12-11 21:14:50 -08:00
|
|
|
[HttpPost]
|
2021-12-31 08:36:38 +01:00
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanManageNotificationsForUser)]
|
2020-12-11 21:14:50 -08:00
|
|
|
public async Task<IActionResult> MarkAllAsSeen(string returnUrl)
|
|
|
|
{
|
|
|
|
if (!ValidUserClaim(out var userId))
|
|
|
|
{
|
|
|
|
return NotFound();
|
|
|
|
}
|
2021-12-31 16:59:02 +09:00
|
|
|
await _notificationManager.ToggleSeen(new NotificationsQuery() { Seen = false, UserId = userId }, true);
|
2022-05-13 10:26:20 +09:00
|
|
|
return LocalRedirect(returnUrl);
|
2020-12-11 21:14:50 -08:00
|
|
|
}
|
|
|
|
|
2020-06-14 23:04:36 -05:00
|
|
|
private bool ValidUserClaim(out string userId)
|
2020-06-11 00:06:53 -05:00
|
|
|
{
|
2020-06-11 17:58:50 -05:00
|
|
|
userId = _userManager.GetUserId(User);
|
|
|
|
return userId != null;
|
2020-06-11 00:06:53 -05:00
|
|
|
}
|
2020-05-28 16:19:02 -05:00
|
|
|
}
|
|
|
|
}
|