Generalizing saving to database and registration of NotificationEventBase classes

This commit is contained in:
rockstardev 2020-06-11 00:24:21 -05:00
parent b6e3ad9325
commit 73588ea22b
2 changed files with 19 additions and 4 deletions

View File

@ -7,7 +7,7 @@ using BTCPayServer.Data;
using ExchangeSharp;
using Newtonsoft.Json;
namespace BTCPayServer.Events
namespace BTCPayServer.Events.Notifications
{
public abstract class NotificationEventBase
{

View File

@ -28,15 +28,30 @@ namespace BTCPayServer.HostedServices
protected override void SubscribeToEvents()
{
Subscribe<NewVersionNotification>();
SubscribeAllChildrenOfNotificationEventBase();
base.SubscribeToEvents();
}
// subscribe all children of NotificationEventBase
public void SubscribeAllChildrenOfNotificationEventBase()
{
var method = this.GetType().GetMethod(nameof(SubscribeHelper));
var notificationTypes = this.GetType().Assembly.GetTypes().Where(a => typeof(NotificationEventBase).IsAssignableFrom(a));
foreach (var notif in notificationTypes)
{
var generic = method.MakeGenericMethod(notif);
generic.Invoke(this, null);
}
}
// we need publicly accessible method for reflection invoke
public void SubscribeHelper<T>() => base.Subscribe<T>();
protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken)
{
if (evt is NewVersionNotification)
if (evt is NotificationEventBase)
{
var data = (evt as NewVersionNotification).ToData();
var data = (evt as NotificationEventBase).ToData();
var admins = await _UserManager.GetUsersInRoleAsync(Roles.ServerAdmin);