diff --git a/BTCPayServer/Controllers/NotificationsController.cs b/BTCPayServer/Controllers/NotificationsController.cs index aaf66ebc1..3736811bc 100644 --- a/BTCPayServer/Controllers/NotificationsController.cs +++ b/BTCPayServer/Controllers/NotificationsController.cs @@ -4,11 +4,13 @@ using System.Linq; using System.Security.Claims; using System.Text; using System.Threading.Tasks; +using BTCPayServer.Data; using BTCPayServer.Events.Notifications; using BTCPayServer.Filters; using BTCPayServer.HostedServices; using BTCPayServer.Models.NotificationViewModels; using BTCPayServer.Security; +using Google; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -18,10 +20,12 @@ namespace BTCPayServer.Controllers [Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)] public class NotificationsController : Controller { + private readonly ApplicationDbContext _db; private readonly EventAggregator _eventAggregator; - public NotificationsController(EventAggregator eventAggregator) + public NotificationsController(ApplicationDbContext db, EventAggregator eventAggregator) { + _db = db; _eventAggregator = eventAggregator; } @@ -35,13 +39,14 @@ namespace BTCPayServer.Controllers var userId = claimWithId.Value; var model = new IndexViewModel() { - Items = NotificationDbSaver.Notif + Items = _db.Notifications + .OrderByDescending(a => a.Created) .Skip(skip).Take(count) .Where(a => a.ApplicationUserId == userId) .Select(a => a.ViewModel()) .ToList() }; - model.Items = model.Items.OrderByDescending(a => a.Created).ToList(); + return View(model); } diff --git a/BTCPayServer/HostedServices/NotificationDbSaver.cs b/BTCPayServer/HostedServices/NotificationDbSaver.cs index cad4aea49..5b1dca523 100644 --- a/BTCPayServer/HostedServices/NotificationDbSaver.cs +++ b/BTCPayServer/HostedServices/NotificationDbSaver.cs @@ -13,15 +13,17 @@ namespace BTCPayServer.HostedServices { public class NotificationDbSaver : EventHostedServiceBase { - private readonly UserManager _userManager; + private readonly UserManager _UserManager; + private readonly ApplicationDbContextFactory _ContextFactory; public NotificationDbSaver(UserManager userManager, - EventAggregator eventAggregator) : base(eventAggregator) { - _userManager = userManager; + ApplicationDbContextFactory contextFactory, + EventAggregator eventAggregator) : base(eventAggregator) + { + _UserManager = userManager; + _ContextFactory = contextFactory; } - public static List Notif = new List(); - protected override void SubscribeToEvents() { Subscribe(); @@ -34,13 +36,19 @@ namespace BTCPayServer.HostedServices { var data = (evt as NewVersionNotification).ToData(); - var admins = await _userManager.GetUsersInRoleAsync(Roles.ServerAdmin); - foreach (var admin in admins) - { - data.Id = Guid.NewGuid().ToString(); - data.ApplicationUserId = admin.Id; + var admins = await _UserManager.GetUsersInRoleAsync(Roles.ServerAdmin); - Notif.Add(data); + using (var db = _ContextFactory.CreateContext()) + { + foreach (var admin in admins) + { + data.Id = Guid.NewGuid().ToString(); + data.ApplicationUserId = admin.Id; + + db.Notifications.Add(data); + } + + await db.SaveChangesAsync(); } } }