Removing obsolete NotificationDbSaver hosted service

This commit is contained in:
rockstardev 2020-06-15 01:22:09 -05:00
parent b19298f633
commit 741b93dc04
4 changed files with 25 additions and 46 deletions

View file

@ -201,7 +201,6 @@ namespace BTCPayServer.Hosting
services.AddSingleton<ChangellyClientProvider>(); services.AddSingleton<ChangellyClientProvider>();
services.AddSingleton<IHostedService, NotificationDbSaver>();
services.AddSingleton<NotificationManager>(); services.AddSingleton<NotificationManager>();
services.AddScoped<NotificationSender>(); services.AddScoped<NotificationSender>();

View file

@ -2,48 +2,15 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security.Claims; using System.Security.Claims;
using System.Threading; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Data; using BTCPayServer.Data;
using BTCPayServer.Events;
using BTCPayServer.Models.NotificationViewModels; using BTCPayServer.Models.NotificationViewModels;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
namespace BTCPayServer.HostedServices namespace BTCPayServer.Services.Notifications
{ {
public class NotificationDbSaver : EventHostedServiceBase
{
private readonly ApplicationDbContextFactory _ContextFactory;
public NotificationDbSaver(ApplicationDbContextFactory contextFactory,
EventAggregator eventAggregator) : base(eventAggregator)
{
_ContextFactory = contextFactory;
}
protected override void SubscribeToEvents()
{
Subscribe<NotificationEvent>();
base.SubscribeToEvents();
}
protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken)
{
var casted = (NotificationEvent)evt;
using (var db = _ContextFactory.CreateContext())
{
foreach (var uid in casted.ApplicationUserIds)
{
var data = casted.Notification.ToData(uid);
db.Notifications.Add(data);
}
await db.SaveChangesAsync();
}
}
}
public class NotificationManager public class NotificationManager
{ {
private readonly ApplicationDbContextFactory _factory; private readonly ApplicationDbContextFactory _factory;
@ -64,7 +31,7 @@ namespace BTCPayServer.HostedServices
if (_memoryCache.TryGetValue<NotificationSummaryViewModel>(userId, out var obj)) if (_memoryCache.TryGetValue<NotificationSummaryViewModel>(userId, out var obj))
return obj; return obj;
var resp = FetchNotificationsFromDb(userId); var resp = FetchNotificationsFromDb(userId);
_memoryCache.Set(userId, resp, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMilliseconds(_cacheExpiryMs))); _memoryCache.Set(userId, resp, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMilliseconds(_cacheExpiryMs)));

View file

@ -11,6 +11,7 @@ namespace BTCPayServer.Services.Notifications
{ {
private readonly UserManager<ApplicationUser> _userManager; private readonly UserManager<ApplicationUser> _userManager;
private readonly EventAggregator _eventAggregator; private readonly EventAggregator _eventAggregator;
private readonly ApplicationDbContextFactory _ContextFactory;
public NotificationSender(UserManager<ApplicationUser> userManager, EventAggregator eventAggregator) public NotificationSender(UserManager<ApplicationUser> userManager, EventAggregator eventAggregator)
{ {
@ -22,16 +23,28 @@ namespace BTCPayServer.Services.Notifications
{ {
var admins = await _userManager.GetUsersInRoleAsync(Roles.ServerAdmin); var admins = await _userManager.GetUsersInRoleAsync(Roles.ServerAdmin);
var adminUids = admins.Select(a => a.Id).ToArray(); var adminUids = admins.Select(a => a.Id).ToArray();
var evt = new NotificationEvent
var notif = new NewVersionNotification
{
Version = version
};
using (var db = _ContextFactory.CreateContext())
{
foreach (var uid in adminUids)
{
var data = notif.ToData(uid);
db.Notifications.Add(data);
}
await db.SaveChangesAsync();
}
// propagate notification
_eventAggregator.Publish(new NotificationEvent
{ {
ApplicationUserIds = adminUids, ApplicationUserIds = adminUids,
Notification = new NewVersionNotification Notification = notif
{ });
Version = version
}
};
_eventAggregator.Publish(evt);
} }
} }
} }

View file

@ -1,4 +1,4 @@
@inject BTCPayServer.HostedServices.NotificationManager notificationManager @inject BTCPayServer.Services.Notifications.NotificationManager notificationManager
@{ @{
var notificationModel = notificationManager.GetSummaryNotifications(User); var notificationModel = notificationManager.GetSummaryNotifications(User);