diff --git a/BTCPayServer/Hosting/BTCPayServerServices.cs b/BTCPayServer/Hosting/BTCPayServerServices.cs index d8be48b88..1810bcf0f 100644 --- a/BTCPayServer/Hosting/BTCPayServerServices.cs +++ b/BTCPayServer/Hosting/BTCPayServerServices.cs @@ -38,6 +38,7 @@ using BTCPayServer.Logging; using BTCPayServer.HostedServices; using Meziantou.AspNetCore.BundleTagHelpers; using System.Security.Claims; +using BTCPayServer.Hubs; using BTCPayServer.Payments.Changelly; using BTCPayServer.Security; using Microsoft.AspNetCore.Mvc.ModelBinding; @@ -73,6 +74,7 @@ namespace BTCPayServer.Hosting services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); + services.TryAddSingleton(); services.TryAddSingleton(o => { var opts = o.GetRequiredService(); diff --git a/BTCPayServer/Hubs/CrowdfundHub.cs b/BTCPayServer/Hubs/CrowdfundHub.cs index 1e7ccaa47..23be12ddb 100644 --- a/BTCPayServer/Hubs/CrowdfundHub.cs +++ b/BTCPayServer/Hubs/CrowdfundHub.cs @@ -14,17 +14,18 @@ using BTCPayServer.Services.Rates; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Primitives; namespace BTCPayServer.Hubs { public class CrowdfundHub: Hub { - private readonly AppsPublicController _AppsPublicController; + private readonly IServiceProvider _ServiceProvider; - public CrowdfundHub(AppsPublicController appsPublicController) + public CrowdfundHub(IServiceProvider serviceProvider) { - _AppsPublicController = appsPublicController; + _ServiceProvider = serviceProvider; } public async Task ListenToCrowdfundApp(string appId) { @@ -42,11 +43,16 @@ namespace BTCPayServer.Hubs } - public async Task CreateInvoice(ContributeToCrowdfund model) + public async Task CreateInvoice(ContributeToCrowdfund model) { - model.RedirectToCheckout = false; - var result = await _AppsPublicController.ContributeToCrowdfund(Context.Items["app"].ToString(), model); - return (result as OkObjectResult)?.Value.ToString(); + using (var scope = _ServiceProvider.CreateScope()) + { + var controller = scope.ServiceProvider.GetService(); + model.RedirectToCheckout = false; + var result = await controller.ContributeToCrowdfund(Context.Items["app"].ToString(), model); + await Clients.Caller.SendCoreAsync("InvoiceCreated", new[] {(result as OkObjectResult)?.Value.ToString()}); + } + } public async Task PaymentReceived() diff --git a/BTCPayServer/wwwroot/crowdfund/app.js b/BTCPayServer/wwwroot/crowdfund/app.js index 24294235e..1df250cc0 100644 --- a/BTCPayServer/wwwroot/crowdfund/app.js +++ b/BTCPayServer/wwwroot/crowdfund/app.js @@ -1,4 +1,5 @@ var app = null; +var eventAggregator = new Vue(); window.onload = function (ev) { diff --git a/BTCPayServer/wwwroot/crowdfund/services/listener.js b/BTCPayServer/wwwroot/crowdfund/services/listener.js index 8e414b214..b15015a51 100644 --- a/BTCPayServer/wwwroot/crowdfund/services/listener.js +++ b/BTCPayServer/wwwroot/crowdfund/services/listener.js @@ -1,41 +1,41 @@ var hubListener = function(){ - - var statuses = { - DISCONNECTED: "disconnected", - CONNECTED: "connected", - CONNECTING: "connecting" - }; - var status = "disconnected"; - - - var connection = new signalR.HubConnectionBuilder().withUrl("/crowdfundHub").build(); + + var connection = new signalR.HubConnectionBuilder().withUrl("/apps/crowdfund/hub").build(); connection.onclose(function(){ - this.status = statuses.DISCONNECTED; + eventAggregator.$emit("connection-lost"); console.error("Connection was closed. Attempting reconnect in 2s"); setTimeout(connect, 2000); }); - + connection.on("InvoiceCreated", function(invoiceId){ + eventAggregator.$emit("invoice-created", invoiceId); + }); function connect(){ - status = statuses.CONNECTING; + + eventAggregator.$emit("connection-pending"); connection .start() .then(function(){ - this.status = statuses.CONNECTED; + connection.invoke("ListenToCrowdfundApp", srvModel.appId); + }) .catch(function (err) { - this.status = statuses.DISCONNECTED; + eventAggregator.$emit("connection-failed"); console.error("Could not connect to backend. Retrying in 2s", err ); setTimeout(connect, 2000); }); } + + + eventAggregator.$on("contribute", function(model){ + connection.invoke("CreateInvoice", model); + }); + return { - statuses: statuses, - status: status, connect: connect }; }();