diff --git a/BTCPayServer.Tests/CrowdfundTests.cs b/BTCPayServer.Tests/CrowdfundTests.cs index 23449ff89..3cd78f402 100644 --- a/BTCPayServer.Tests/CrowdfundTests.cs +++ b/BTCPayServer.Tests/CrowdfundTests.cs @@ -5,6 +5,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; using BTCPayServer.Controllers; +using BTCPayServer.Crowdfund; using BTCPayServer.Data; using BTCPayServer.Events; using BTCPayServer.Hubs; diff --git a/BTCPayServer/Controllers/AppsPublicController.cs b/BTCPayServer/Controllers/AppsPublicController.cs index f3a06ec49..966e781d6 100644 --- a/BTCPayServer/Controllers/AppsPublicController.cs +++ b/BTCPayServer/Controllers/AppsPublicController.cs @@ -6,6 +6,7 @@ using System.IO; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; +using BTCPayServer.Crowdfund; using BTCPayServer.Data; using BTCPayServer.Filters; using BTCPayServer.Hubs; diff --git a/BTCPayServer/Crowdfund/CrowdfundHub.cs b/BTCPayServer/Crowdfund/CrowdfundHub.cs index a2e00ebec..a030ed361 100644 --- a/BTCPayServer/Crowdfund/CrowdfundHub.cs +++ b/BTCPayServer/Crowdfund/CrowdfundHub.cs @@ -1,3 +1,4 @@ +using System; using System.Threading.Tasks; using BTCPayServer.Controllers; using BTCPayServer.Models.AppViewModels; @@ -35,20 +36,30 @@ namespace BTCPayServer.Hubs { model.RedirectToCheckout = false; _AppsPublicController.ControllerContext.HttpContext = Context.GetHttpContext(); - var result = await _AppsPublicController.ContributeToCrowdfund(Context.Items["app"].ToString(), model); - switch (result) + try { - case OkObjectResult okObjectResult: - await Clients.Caller.SendCoreAsync(InvoiceCreated, new[] {okObjectResult.Value.ToString()}); - break; - case ObjectResult objectResult: - await Clients.Caller.SendCoreAsync(InvoiceError, new[] {objectResult.Value}); - break; - default: - await Clients.Caller.SendCoreAsync(InvoiceError, System.Array.Empty()); - break; + + var result = + await _AppsPublicController.ContributeToCrowdfund(Context.Items["app"].ToString(), model); + switch (result) + { + case OkObjectResult okObjectResult: + await Clients.Caller.SendCoreAsync(InvoiceCreated, new[] {okObjectResult.Value.ToString()}); + break; + case ObjectResult objectResult: + await Clients.Caller.SendCoreAsync(InvoiceError, new[] {objectResult.Value}); + break; + default: + await Clients.Caller.SendCoreAsync(InvoiceError, System.Array.Empty()); + break; + } } - + catch (Exception) + { + await Clients.Caller.SendCoreAsync(InvoiceError, System.Array.Empty()); + + } + } } diff --git a/BTCPayServer/Crowdfund/CrowdfundHubStreamer.cs b/BTCPayServer/Crowdfund/CrowdfundHubStreamer.cs index 7e1f69105..9e7741488 100644 --- a/BTCPayServer/Crowdfund/CrowdfundHubStreamer.cs +++ b/BTCPayServer/Crowdfund/CrowdfundHubStreamer.cs @@ -2,11 +2,11 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; -using System.Threading; using System.Threading.Tasks; using BTCPayServer.Controllers; using BTCPayServer.Data; using BTCPayServer.Events; +using BTCPayServer.Hubs; using BTCPayServer.Models.AppViewModels; using BTCPayServer.Payments; using BTCPayServer.Rating; @@ -16,14 +16,11 @@ using BTCPayServer.Services.Rates; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Primitives; using NBitcoin; -using YamlDotNet.Core; -namespace BTCPayServer.Hubs +namespace BTCPayServer.Crowdfund { - public class - CrowdfundHubStreamer + public class CrowdfundHubStreamer: IDisposable { public const string CrowdfundInvoiceOrderIdPrefix = "crowdfund-app_"; private readonly EventAggregator _EventAggregator; @@ -37,7 +34,9 @@ namespace BTCPayServer.Hubs private readonly ConcurrentDictionary _QuickAppInvoiceLookup = new ConcurrentDictionary(); - + + private List _Subscriptions; + public CrowdfundHubStreamer(EventAggregator eventAggregator, IHubContext hubContext, IMemoryCache memoryCache, @@ -116,13 +115,15 @@ namespace BTCPayServer.Hubs private void SubscribeToEvents() { - - _EventAggregator.Subscribe(OnInvoiceEvent); - _EventAggregator.Subscribe(updated => + _Subscriptions = new List() { - UpdateLookup(updated.AppId, updated.StoreId, updated.Settings); - InvalidateCacheForApp(updated.AppId); - }); + _EventAggregator.Subscribe(OnInvoiceEvent), + _EventAggregator.Subscribe(updated => + { + UpdateLookup(updated.AppId, updated.StoreId, updated.Settings); + InvalidateCacheForApp(updated.AppId); + }) + }; } private string GetCacheKey(string appId) @@ -152,7 +153,7 @@ namespace BTCPayServer.Hubs Enum.GetName(typeof(PaymentTypes), invoiceEvent.Payment.GetPaymentMethodId().PaymentType) } ); - + _Logger.LogInformation($"App {quickLookup.appId}: Received Payment"); InvalidateCacheForApp(quickLookup.appId); break; case InvoiceEvent.Created: @@ -361,5 +362,10 @@ namespace BTCPayServer.Hubs StartDate = startDate }); } + + public void Dispose() + { + _Subscriptions.ForEach(subscription => subscription.Dispose()); + } } } diff --git a/BTCPayServer/Hosting/BTCPayServerServices.cs b/BTCPayServer/Hosting/BTCPayServerServices.cs index e2f05e477..518e9c96a 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.Crowdfund; using BTCPayServer.Hubs; using BTCPayServer.Payments.Changelly; using BTCPayServer.Payments.Lightning; diff --git a/BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml b/BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml index 895ec8f3d..1af73a633 100644 --- a/BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml +++ b/BTCPayServer/Views/Apps/UpdateCrowdfund.cshtml @@ -1,4 +1,5 @@ -@using BTCPayServer.Hubs +@using BTCPayServer.Crowdfund +@using BTCPayServer.Hubs @addTagHelper *, Meziantou.AspNetCore.BundleTagHelpers @model UpdateCrowdfundViewModel @{ diff --git a/BTCPayServer/Views/AppsPublic/Crowdfund/VueCrowdfund.cshtml b/BTCPayServer/Views/AppsPublic/Crowdfund/VueCrowdfund.cshtml index 38d206846..3c2142371 100644 --- a/BTCPayServer/Views/AppsPublic/Crowdfund/VueCrowdfund.cshtml +++ b/BTCPayServer/Views/AppsPublic/Crowdfund/VueCrowdfund.cshtml @@ -1,41 +1,48 @@
- -
+ +

- {{srvModel.title}} - + {{srvModel.title}} + Starts {{startDateRelativeTime}} - + Ends {{endDateRelativeTime}} - + Currently Active!

- - {{srvModel.targetAmount}} {{targetCurrency}} - + {{srvModel.targetAmount}} {{targetCurrency}} + Dynamic - Hardcap Goal - Softcap Goal + Hardcap Goal + Softcap Goal
-
+
- -
-
+
{{ raisedAmount }} {{targetCurrency}}
Raised
-
+
{{ percentageRaisedAmount }}%
Of Goal
-
+
{{srvModel.info.totalContributors}}
Contributors
-
+
{{endDiff}}
Left
- +
  • {{started? "Started" : "Starts"}} {{startDate}} @@ -78,13 +83,13 @@
-
+
{{startDiff}}
Left to start
- +
  • {{started? "Started" : "Starts"}} {{startDate}} @@ -95,13 +100,13 @@
-
+
Campaign
not active
- +
  • {{started? "Started" : "Starts"}} {{startDate}} @@ -111,14 +116,10 @@
- - - -
- +
  • @@ -126,37 +127,34 @@
- + Goal resets every {{srvModel.resetEveryAmount}} {{srvModel.resetEvery}} {{srvModel.resetEveryAmount>1?'s': ''}} -
+
-
- - -

{{srvModel.tagline}}

+
+

{{srvModel.tagline}}

-
- - - +
+