btcpayserver/BTCPayServer/Services/Apps/AppHubStreamer.cs

63 lines
2.2 KiB
C#
Raw Normal View History

using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Controllers;
using BTCPayServer.Events;
using BTCPayServer.HostedServices;
2021-11-22 17:16:08 +09:00
using BTCPayServer.Logging;
using Microsoft.AspNetCore.SignalR;
namespace BTCPayServer.Services.Apps
{
public class AppHubStreamer : EventHostedServiceBase
{
private readonly AppService _appService;
private readonly IHubContext<AppHub> _HubContext;
2019-01-14 08:21:27 +01:00
public AppHubStreamer(EventAggregator eventAggregator,
IHubContext<AppHub> hubContext,
2021-11-22 17:16:08 +09:00
AppService appService,
Logs logs) : base(eventAggregator, logs)
{
_appService = appService;
_HubContext = hubContext;
2019-01-05 19:47:39 +01:00
}
protected override void SubscribeToEvents()
{
Subscribe<InvoiceEvent>();
2022-01-07 12:32:00 +09:00
Subscribe<UIAppsController.AppUpdated>();
}
2020-06-28 17:55:27 +09:00
protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken)
{
if (evt is InvoiceEvent invoiceEvent)
{
foreach (var appId in AppService.GetAppInternalTags(invoiceEvent.Invoice))
{
if (invoiceEvent.Name == InvoiceEvent.ReceivedPayment)
{
var data = invoiceEvent.Payment.GetCryptoPaymentData();
await _HubContext.Clients.Group(appId).SendCoreAsync(AppHub.PaymentReceived, new object[]
{
data.GetValue(),
invoiceEvent.Payment.GetCryptoCode(),
invoiceEvent.Payment.GetPaymentMethodId()?.PaymentType?.ToString()
}, cancellationToken);
}
await InfoUpdated(appId);
}
}
2022-01-07 12:32:00 +09:00
else if (evt is UIAppsController.AppUpdated app)
{
await InfoUpdated(app.AppId);
}
}
private async Task InfoUpdated(string appId)
{
var info = await _appService.GetAppInfo(appId);
await _HubContext.Clients.Group(appId).SendCoreAsync(AppHub.InfoUpdated, new object[] { info });
}
}
}