btcpayserver/BTCPayServer/Controllers/UINotificationsController.cs

162 lines
5.5 KiB
C#
Raw Normal View History

2020-06-28 21:44:35 -05: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 20:16:28 +09:00
[Route("notifications/{action:lowercase=Index}")]
2022-01-07 12:32:00 +09:00
public class UINotificationsController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
2020-06-16 23:29:25 +09:00
private readonly NotificationManager _notificationManager;
public UINotificationsController(
2020-06-16 23:29:25 +09:00
UserManager<ApplicationUser> userManager,
NotificationManager notificationManager)
{
_userManager = userManager;
2020-06-16 23:29:25 +09:00
_notificationManager = notificationManager;
}
[HttpGet]
public async Task<IActionResult> Index(int skip = 0, int count = 50, int timezoneOffset = 0)
{
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");
var res = await _notificationManager.GetNotifications(new NotificationsQuery()
{
2021-12-31 16:59:02 +09:00
Skip = skip,
Take = count,
UserId = userId
});
2021-12-31 16:59:02 +09:00
var model = new IndexViewModel() { Skip = skip, Count = count, Items = res.Items, Total = res.Count };
return View(model);
}
[HttpPost]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanManageNotificationsForUser)]
public async Task<IActionResult> FlipRead(string id)
{
2020-06-14 23:04:36 -05:00
if (ValidUserClaim(out var userId))
{
2021-12-31 16:59:02 +09:00
await _notificationManager.ToggleSeen(new NotificationsQuery() { Ids = new[] { id }, UserId = userId }, null);
return RedirectToAction(nameof(Index));
}
return BadRequest();
}
2020-05-28 22:57:18 -05:00
[HttpGet]
public async Task<IActionResult> NotificationPassThrough(string id)
{
if (ValidUserClaim(out var userId))
{
var items = await
_notificationManager.ToggleSeen(new NotificationsQuery()
{
2021-12-31 16:59:02 +09:00
Ids = new[] { id },
UserId = userId
}, true);
2021-12-31 16:59:02 +09:00
var link = items.FirstOrDefault()?.ActionLink ?? "";
if (string.IsNullOrEmpty(link))
{
return RedirectToAction(nameof(Index));
}
return Redirect(link);
}
return NotFound();
}
2021-12-31 16:59:02 +09:00
[HttpPost]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanManageNotificationsForUser)]
public async Task<IActionResult> MassAction(string command, string[] selectedItems)
2020-05-28 22:57:18 -05: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 16:59:02 +09:00
UserId = userId,
Ids = selectedItems
});
break;
case "mark-seen":
await _notificationManager.ToggleSeen(new NotificationsQuery()
{
2021-12-31 16:59:02 +09:00
UserId = userId,
Ids = selectedItems,
Seen = false
}, true);
break;
case "mark-unseen":
await _notificationManager.ToggleSeen(new NotificationsQuery()
{
2021-12-31 16:59:02 +09:00
UserId = userId,
Ids = selectedItems,
Seen = true
}, false);
break;
}
return RedirectToAction(nameof(Index));
}
2020-05-28 22:57:18 -05: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 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-06-14 23:04:36 -05:00
private bool ValidUserClaim(out string userId)
{
userId = _userManager.GetUserId(User);
return userId != null;
}
}
}