diff --git a/BTCPayServer/Events/Notifications/NewVersionNotification.cs b/BTCPayServer/Events/Notifications/NewVersionNotification.cs index 9449faf19..847370506 100644 --- a/BTCPayServer/Events/Notifications/NewVersionNotification.cs +++ b/BTCPayServer/Events/Notifications/NewVersionNotification.cs @@ -3,11 +3,30 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using BTCPayServer.Data; +using BTCPayServer.Models.NotificationViewModels; +using ExchangeSharp; +using Newtonsoft.Json; namespace BTCPayServer.Events.Notifications { public class NewVersionNotification : NotificationEventBase { public string Version { get; set; } + + public override NotificationViewModel ToViewModel(NotificationData data) + { + var casted = JsonConvert.DeserializeObject(data.Blob.ToStringFromUTF8()); + var obj = new NotificationViewModel + { + Id = data.Id, + Created = data.Created, + Body = $"New version {casted.Version} released!", + ActionLink = "https://github.com/btcpayserver/btcpayserver/releases/tag/v" + casted.Version, + Seen = data.Seen + }; + + return obj; + } } } diff --git a/BTCPayServer/Events/Notifications/NotificationEventBase.cs b/BTCPayServer/Events/Notifications/NotificationEventBase.cs index aabb68cdf..e9ffeeebd 100644 --- a/BTCPayServer/Events/Notifications/NotificationEventBase.cs +++ b/BTCPayServer/Events/Notifications/NotificationEventBase.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using BTCPayServer.Data; +using BTCPayServer.Models.NotificationViewModels; using ExchangeSharp; using Newtonsoft.Json; @@ -24,5 +25,7 @@ namespace BTCPayServer.Events.Notifications }; return data; } + + public abstract NotificationViewModel ToViewModel(NotificationData data); } } diff --git a/BTCPayServer/Models/NotificationViewModels/IndexViewModel.cs b/BTCPayServer/Models/NotificationViewModels/IndexViewModel.cs index 4483fb56f..74c487750 100644 --- a/BTCPayServer/Models/NotificationViewModels/IndexViewModel.cs +++ b/BTCPayServer/Models/NotificationViewModels/IndexViewModel.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; using BTCPayServer.Data; @@ -29,22 +30,12 @@ namespace BTCPayServer.Models.NotificationViewModels { public static NotificationViewModel ViewModel(this NotificationData data) { - if (data.NotificationType == nameof(NewVersionNotification)) - { - var casted = JsonConvert.DeserializeObject(data.Blob.ToStringFromUTF8()); - var obj = new NotificationViewModel - { - Id = data.Id, - Created = data.Created, - Body = $"New version {casted.Version} released!", - ActionLink = "https://github.com/btcpayserver/btcpayserver/releases/tag/v" + casted.Version, - Seen = data.Seen - }; + var baseType = typeof(NotificationEventBase); - return obj; - } + var typeName = baseType.FullName.Replace(nameof(NotificationEventBase), data.NotificationType, StringComparison.OrdinalIgnoreCase); + var instance = Activator.CreateInstance(baseType.Assembly.GetType(typeName)) as NotificationEventBase; - return null; + return instance.ToViewModel(data); } } }