2020-05-28 23:19:02 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-05-29 04:54:26 +02:00
|
|
|
|
using BTCPayServer.Data;
|
2020-05-28 23:19:02 +02:00
|
|
|
|
using BTCPayServer.Events.Notifications;
|
|
|
|
|
using BTCPayServer.Filters;
|
|
|
|
|
using BTCPayServer.HostedServices;
|
|
|
|
|
using BTCPayServer.Models.NotificationViewModels;
|
|
|
|
|
using BTCPayServer.Security;
|
2020-05-29 04:54:26 +02:00
|
|
|
|
using Google;
|
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)]
|
|
|
|
|
public class NotificationsController : Controller
|
|
|
|
|
{
|
2020-05-29 04:54:26 +02:00
|
|
|
|
private readonly ApplicationDbContext _db;
|
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-05-28 23:19:02 +02:00
|
|
|
|
|
2020-06-12 06:52:46 +02:00
|
|
|
|
public NotificationsController(ApplicationDbContext db, NotificationSender notificationSender, UserManager<ApplicationUser> userManager)
|
2020-05-28 23:19:02 +02:00
|
|
|
|
{
|
2020-05-29 04:54:26 +02:00
|
|
|
|
_db = db;
|
2020-06-12 06:52:46 +02:00
|
|
|
|
_notificationSender = notificationSender;
|
2020-06-12 00:58:50 +02:00
|
|
|
|
_userManager = userManager;
|
2020-05-28 23:19:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> Index(int skip = 0, int count = 50, int timezoneOffset = 0)
|
|
|
|
|
{
|
2020-06-12 00:58:50 +02:00
|
|
|
|
if (!validUserClaim(out var userId))
|
2020-05-28 23:19:02 +02:00
|
|
|
|
return RedirectToAction("Index", "Home");
|
|
|
|
|
|
|
|
|
|
var model = new IndexViewModel()
|
|
|
|
|
{
|
2020-05-29 04:54:26 +02:00
|
|
|
|
Items = _db.Notifications
|
|
|
|
|
.OrderByDescending(a => a.Created)
|
2020-05-28 23:19:02 +02:00
|
|
|
|
.Skip(skip).Take(count)
|
|
|
|
|
.Where(a => a.ApplicationUserId == userId)
|
|
|
|
|
.Select(a => a.ViewModel())
|
|
|
|
|
.ToList()
|
|
|
|
|
};
|
2020-05-29 04:54:26 +02:00
|
|
|
|
|
2020-05-28 23:19:02 +02:00
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> Generate()
|
|
|
|
|
{
|
2020-06-12 06:52:46 +02:00
|
|
|
|
await _notificationSender.NoticeNewVersionAsync("1.0.4.4");
|
2020-05-28 23:19:02 +02:00
|
|
|
|
// waiting for event handler to catch up
|
|
|
|
|
await Task.Delay(1000);
|
|
|
|
|
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-12 00:58:50 +02:00
|
|
|
|
if (validUserClaim(out var userId))
|
2020-06-11 07:06:53 +02:00
|
|
|
|
{
|
2020-06-12 00:58:50 +02:00
|
|
|
|
var notif = _db.Notifications.Single(a => a.Id == id && a.ApplicationUserId == userId);
|
2020-06-11 07:06:53 +02:00
|
|
|
|
notif.Seen = !notif.Seen;
|
|
|
|
|
await _db.SaveChangesAsync();
|
|
|
|
|
}
|
2020-05-29 05:32:31 +02:00
|
|
|
|
|
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
}
|
2020-05-29 05:57:18 +02:00
|
|
|
|
|
2020-06-11 05:41:52 +02:00
|
|
|
|
[HttpPost]
|
2020-06-11 06:41:18 +02:00
|
|
|
|
public async Task<IActionResult> MassAction(string command, string csvGuids)
|
2020-05-29 05:57:18 +02:00
|
|
|
|
{
|
2020-06-11 06:41:18 +02:00
|
|
|
|
List<string> parsedGuids = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
parsedGuids = csvGuids.Split(',').ToList();
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
2020-05-29 05:57:18 +02:00
|
|
|
|
|
2020-06-11 06:41:18 +02:00
|
|
|
|
if (parsedGuids != null)
|
|
|
|
|
{
|
2020-06-12 00:58:50 +02:00
|
|
|
|
if (command == "delete" && validUserClaim(out var userId))
|
2020-06-11 06:41:18 +02:00
|
|
|
|
{
|
2020-06-12 00:58:50 +02:00
|
|
|
|
var toRemove = _db.Notifications.Where(a => a.ApplicationUserId == userId && parsedGuids.Contains(a.Id));
|
2020-06-11 06:41:18 +02:00
|
|
|
|
_db.Notifications.RemoveRange(toRemove);
|
|
|
|
|
await _db.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-29 05:57:18 +02:00
|
|
|
|
|
|
|
|
|
return RedirectToAction(nameof(Index));
|
|
|
|
|
}
|
2020-06-11 07:06:53 +02:00
|
|
|
|
|
2020-06-12 00:58:50 +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
|
|
|
|
}
|
|
|
|
|
}
|