mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-22 22:25:28 +01:00
Foundation for events to propagate notifications
This commit is contained in:
parent
0cacfa140b
commit
6ddb20d022
6 changed files with 121 additions and 0 deletions
18
BTCPayServer.Data/Data/NotificationData.cs
Normal file
18
BTCPayServer.Data/Data/NotificationData.cs
Normal file
|
@ -0,0 +1,18 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BTCPayServer.Data.Data
|
||||
{
|
||||
public class NotificationData
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public DateTimeOffset Created { get; set; }
|
||||
public string ApplicationUserId { get; set; }
|
||||
public string NotificationType { get; set; }
|
||||
public bool Seen { get; set; }
|
||||
public byte[] Blob { get; set; }
|
||||
}
|
||||
}
|
28
BTCPayServer/Events/NotificationEventBase.cs
Normal file
28
BTCPayServer/Events/NotificationEventBase.cs
Normal file
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Data.Data;
|
||||
using ExchangeSharp;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace BTCPayServer.Events
|
||||
{
|
||||
public abstract class NotificationEventBase
|
||||
{
|
||||
public NotificationData ToData()
|
||||
{
|
||||
var obj = JsonConvert.SerializeObject(this);
|
||||
|
||||
var data = new NotificationData
|
||||
{
|
||||
Created = DateTimeOffset.UtcNow,
|
||||
NotificationType = GetType().Name,
|
||||
Blob = obj.ToBytesUTF8(),
|
||||
Seen = false
|
||||
};
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
14
BTCPayServer/Events/Notifications/NewVersionNotification.cs
Normal file
14
BTCPayServer/Events/Notifications/NewVersionNotification.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Data.Data;
|
||||
|
||||
namespace BTCPayServer.Events.Notifications
|
||||
{
|
||||
public class NewVersionNotification : NotificationEventBase
|
||||
{
|
||||
public string Version { get; set; }
|
||||
}
|
||||
}
|
19
BTCPayServer/Events/Notifications/NotificationsHelperExt.cs
Normal file
19
BTCPayServer/Events/Notifications/NotificationsHelperExt.cs
Normal file
|
@ -0,0 +1,19 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BTCPayServer.Events.Notifications
|
||||
{
|
||||
public static class NotificationsHelperExt
|
||||
{
|
||||
public static void NoticeNewVersion(this EventAggregator aggr, string version)
|
||||
{
|
||||
aggr.Publish(new NewVersionNotification
|
||||
{
|
||||
Version = version
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
40
BTCPayServer/HostedServices/NotificationDbSaver.cs
Normal file
40
BTCPayServer/HostedServices/NotificationDbSaver.cs
Normal file
|
@ -0,0 +1,40 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Data.Data;
|
||||
using BTCPayServer.Events;
|
||||
using BTCPayServer.Events.Notifications;
|
||||
|
||||
namespace BTCPayServer.HostedServices
|
||||
{
|
||||
public class NotificationDbSaver : EventHostedServiceBase
|
||||
{
|
||||
public NotificationDbSaver(EventAggregator eventAggregator) : base(eventAggregator) { }
|
||||
|
||||
protected override void SubscribeToEvents()
|
||||
{
|
||||
Subscribe<NewVersionNotification>();
|
||||
base.SubscribeToEvents();
|
||||
}
|
||||
|
||||
public static List<NotificationData> Notif = new List<NotificationData>();
|
||||
|
||||
protected override Task ProcessEvent(object evt, CancellationToken cancellationToken)
|
||||
{
|
||||
if (evt is NewVersionNotification)
|
||||
{
|
||||
var data = (evt as NewVersionNotification).ToData();
|
||||
|
||||
//var userIds = new[] { "rockstar", "nicolas", "kukkie", "pavel" };
|
||||
//foreach (var uid in userIds)
|
||||
data.Id = Guid.NewGuid().ToString();
|
||||
Notif.Add(data);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -200,6 +200,8 @@ namespace BTCPayServer.Hosting
|
|||
|
||||
services.AddSingleton<ChangellyClientProvider>();
|
||||
|
||||
services.AddSingleton<IHostedService, NotificationDbSaver>();
|
||||
|
||||
services.AddSingleton<IHostedService, NBXplorerWaiters>();
|
||||
services.AddSingleton<IHostedService, InvoiceNotificationManager>();
|
||||
services.AddSingleton<IHostedService, InvoiceWatcher>();
|
||||
|
|
Loading…
Add table
Reference in a new issue