Foundation for events to propagate notifications

This commit is contained in:
rockstardev 2020-05-27 18:41:00 -05:00
parent 0cacfa140b
commit 6ddb20d022
6 changed files with 121 additions and 0 deletions

View 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; }
}
}

View 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;
}
}
}

View 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; }
}
}

View 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
});
}
}
}

View 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;
}
}
}

View file

@ -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>();