From 73588ea22b1c9c8dd5e5f96bc26d53bc27c52c1d Mon Sep 17 00:00:00 2001 From: rockstardev Date: Thu, 11 Jun 2020 00:24:21 -0500 Subject: [PATCH] Generalizing saving to database and registration of NotificationEventBase classes --- .../NotificationEventBase.cs | 2 +- .../HostedServices/NotificationDbSaver.cs | 21 ++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) rename BTCPayServer/Events/{ => Notifications}/NotificationEventBase.cs (93%) diff --git a/BTCPayServer/Events/NotificationEventBase.cs b/BTCPayServer/Events/Notifications/NotificationEventBase.cs similarity index 93% rename from BTCPayServer/Events/NotificationEventBase.cs rename to BTCPayServer/Events/Notifications/NotificationEventBase.cs index 2f27848c3..aabb68cdf 100644 --- a/BTCPayServer/Events/NotificationEventBase.cs +++ b/BTCPayServer/Events/Notifications/NotificationEventBase.cs @@ -7,7 +7,7 @@ using BTCPayServer.Data; using ExchangeSharp; using Newtonsoft.Json; -namespace BTCPayServer.Events +namespace BTCPayServer.Events.Notifications { public abstract class NotificationEventBase { diff --git a/BTCPayServer/HostedServices/NotificationDbSaver.cs b/BTCPayServer/HostedServices/NotificationDbSaver.cs index 0077c17c5..7d18fbc8c 100644 --- a/BTCPayServer/HostedServices/NotificationDbSaver.cs +++ b/BTCPayServer/HostedServices/NotificationDbSaver.cs @@ -28,15 +28,30 @@ namespace BTCPayServer.HostedServices protected override void SubscribeToEvents() { - Subscribe(); + 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() => base.Subscribe(); + 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);