mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-03-03 09:29:10 +01:00
Removing obsolete NotificationDbSaver hosted service
This commit is contained in:
parent
b19298f633
commit
741b93dc04
4 changed files with 25 additions and 46 deletions
|
@ -201,7 +201,6 @@ namespace BTCPayServer.Hosting
|
|||
|
||||
services.AddSingleton<ChangellyClientProvider>();
|
||||
|
||||
services.AddSingleton<IHostedService, NotificationDbSaver>();
|
||||
services.AddSingleton<NotificationManager>();
|
||||
services.AddScoped<NotificationSender>();
|
||||
|
||||
|
|
|
@ -2,48 +2,15 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Data;
|
||||
using BTCPayServer.Events;
|
||||
using BTCPayServer.Models.NotificationViewModels;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
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
|
||||
{
|
||||
private readonly ApplicationDbContextFactory _factory;
|
||||
|
@ -64,7 +31,7 @@ namespace BTCPayServer.HostedServices
|
|||
|
||||
if (_memoryCache.TryGetValue<NotificationSummaryViewModel>(userId, out var obj))
|
||||
return obj;
|
||||
|
||||
|
||||
var resp = FetchNotificationsFromDb(userId);
|
||||
_memoryCache.Set(userId, resp, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMilliseconds(_cacheExpiryMs)));
|
||||
|
|
@ -11,6 +11,7 @@ namespace BTCPayServer.Services.Notifications
|
|||
{
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly EventAggregator _eventAggregator;
|
||||
private readonly ApplicationDbContextFactory _ContextFactory;
|
||||
|
||||
public NotificationSender(UserManager<ApplicationUser> userManager, EventAggregator eventAggregator)
|
||||
{
|
||||
|
@ -22,16 +23,28 @@ namespace BTCPayServer.Services.Notifications
|
|||
{
|
||||
var admins = await _userManager.GetUsersInRoleAsync(Roles.ServerAdmin);
|
||||
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,
|
||||
Notification = new NewVersionNotification
|
||||
{
|
||||
Version = version
|
||||
}
|
||||
};
|
||||
|
||||
_eventAggregator.Publish(evt);
|
||||
Notification = notif
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
@inject BTCPayServer.HostedServices.NotificationManager notificationManager
|
||||
@inject BTCPayServer.Services.Notifications.NotificationManager notificationManager
|
||||
|
||||
@{
|
||||
var notificationModel = notificationManager.GetSummaryNotifications(User);
|
||||
|
|
Loading…
Add table
Reference in a new issue