2020-05-27 18:41:00 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2020-05-28 22:48:09 -05:00
|
|
|
|
using System.Security.Claims;
|
2020-05-27 18:41:00 -05:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-05-28 15:15:24 -05:00
|
|
|
|
using BTCPayServer.Data;
|
2020-05-27 18:41:00 -05:00
|
|
|
|
using BTCPayServer.Events;
|
|
|
|
|
using BTCPayServer.Events.Notifications;
|
2020-06-10 18:55:31 -05:00
|
|
|
|
using BTCPayServer.Models.NotificationViewModels;
|
2020-05-28 16:19:02 -05:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2020-05-27 18:41:00 -05:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.HostedServices
|
|
|
|
|
{
|
|
|
|
|
public class NotificationDbSaver : EventHostedServiceBase
|
|
|
|
|
{
|
2020-05-28 21:54:26 -05:00
|
|
|
|
private readonly UserManager<ApplicationUser> _UserManager;
|
|
|
|
|
private readonly ApplicationDbContextFactory _ContextFactory;
|
2020-05-28 16:19:02 -05:00
|
|
|
|
|
|
|
|
|
public NotificationDbSaver(UserManager<ApplicationUser> userManager,
|
2020-05-28 21:54:26 -05:00
|
|
|
|
ApplicationDbContextFactory contextFactory,
|
|
|
|
|
EventAggregator eventAggregator) : base(eventAggregator)
|
|
|
|
|
{
|
|
|
|
|
_UserManager = userManager;
|
|
|
|
|
_ContextFactory = contextFactory;
|
2020-05-28 16:19:02 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-27 18:41:00 -05:00
|
|
|
|
protected override void SubscribeToEvents()
|
|
|
|
|
{
|
|
|
|
|
Subscribe<NewVersionNotification>();
|
|
|
|
|
base.SubscribeToEvents();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-28 16:19:02 -05:00
|
|
|
|
protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken)
|
2020-05-27 18:41:00 -05:00
|
|
|
|
{
|
|
|
|
|
if (evt is NewVersionNotification)
|
|
|
|
|
{
|
|
|
|
|
var data = (evt as NewVersionNotification).ToData();
|
|
|
|
|
|
2020-05-28 21:54:26 -05:00
|
|
|
|
var admins = await _UserManager.GetUsersInRoleAsync(Roles.ServerAdmin);
|
|
|
|
|
|
|
|
|
|
using (var db = _ContextFactory.CreateContext())
|
2020-05-28 16:19:02 -05:00
|
|
|
|
{
|
2020-05-28 21:54:26 -05:00
|
|
|
|
foreach (var admin in admins)
|
|
|
|
|
{
|
|
|
|
|
data.Id = Guid.NewGuid().ToString();
|
|
|
|
|
data.ApplicationUserId = admin.Id;
|
|
|
|
|
|
|
|
|
|
db.Notifications.Add(data);
|
|
|
|
|
}
|
2020-05-27 18:41:00 -05:00
|
|
|
|
|
2020-05-28 21:54:26 -05:00
|
|
|
|
await db.SaveChangesAsync();
|
2020-05-28 16:19:02 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-27 18:41:00 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-28 22:48:09 -05:00
|
|
|
|
|
|
|
|
|
public class NotificationManager
|
|
|
|
|
{
|
|
|
|
|
private readonly ApplicationDbContext _db;
|
|
|
|
|
|
|
|
|
|
public NotificationManager(ApplicationDbContext db)
|
|
|
|
|
{
|
|
|
|
|
_db = db;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-10 18:55:31 -05:00
|
|
|
|
public NotificationSummaryViewModel GetSummaryNotifications(ClaimsPrincipal user)
|
2020-05-28 22:48:09 -05:00
|
|
|
|
{
|
2020-06-10 18:55:31 -05:00
|
|
|
|
var resp = new NotificationSummaryViewModel();
|
2020-05-28 22:48:09 -05:00
|
|
|
|
var claimWithId = user.Claims.SingleOrDefault(a => a.Type == ClaimTypes.NameIdentifier);
|
|
|
|
|
|
|
|
|
|
// TODO: Soft caching in order not to pound database too much
|
2020-06-10 18:55:31 -05:00
|
|
|
|
resp.UnseenCount = _db.Notifications
|
2020-05-28 22:48:09 -05:00
|
|
|
|
.Where(a => a.ApplicationUserId == claimWithId.Value && !a.Seen)
|
|
|
|
|
.Count();
|
2020-06-10 18:55:31 -05:00
|
|
|
|
|
|
|
|
|
if (resp.UnseenCount > 0)
|
|
|
|
|
{
|
|
|
|
|
resp.Last5 = _db.Notifications
|
|
|
|
|
.Where(a => a.ApplicationUserId == claimWithId.Value && !a.Seen)
|
|
|
|
|
.OrderByDescending(a => a.Created)
|
|
|
|
|
.Take(5)
|
|
|
|
|
.Select(a => a.ViewModel())
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
resp.Last5 = new List<NotificationViewModel>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resp;
|
2020-05-28 22:48:09 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-10 18:55:31 -05:00
|
|
|
|
|
|
|
|
|
public class NotificationSummaryViewModel
|
|
|
|
|
{
|
|
|
|
|
public int UnseenCount { get; set; }
|
|
|
|
|
public List<NotificationViewModel> Last5 { get; set; }
|
|
|
|
|
}
|
2020-05-27 18:41:00 -05:00
|
|
|
|
}
|