btcpayserver/BTCPayServer/Crowdfund/CrowdfundHub.cs

67 lines
2.4 KiB
C#
Raw Normal View History

2019-01-11 10:52:21 +01:00
using System;
2018-12-22 15:02:16 +01:00
using System.Threading.Tasks;
2018-12-27 20:19:21 +01:00
using BTCPayServer.Controllers;
using BTCPayServer.Models.AppViewModels;
using Microsoft.AspNetCore.Mvc;
2018-12-22 15:02:16 +01:00
using Microsoft.AspNetCore.SignalR;
2018-12-27 20:55:46 +01:00
using Microsoft.Extensions.DependencyInjection;
2018-12-22 15:02:16 +01:00
namespace BTCPayServer.Hubs
{
public class CrowdfundHub: Hub
{
2019-01-02 09:45:04 +01:00
public const string InvoiceCreated = "InvoiceCreated";
public const string PaymentReceived = "PaymentReceived";
public const string InfoUpdated = "InfoUpdated";
public const string InvoiceError = "InvoiceError";
2018-12-28 23:57:39 +01:00
private readonly AppsPublicController _AppsPublicController;
2018-12-27 20:19:21 +01:00
2018-12-28 23:57:39 +01:00
public CrowdfundHub(AppsPublicController appsPublicController)
2018-12-27 20:19:21 +01:00
{
2018-12-28 23:57:39 +01:00
_AppsPublicController = appsPublicController;
2018-12-27 20:19:21 +01:00
}
public async Task ListenToCrowdfundApp(string appId)
{
if (Context.Items.ContainsKey("app"))
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, Context.Items["app"].ToString());
Context.Items.Remove("app");
}
Context.Items.Add("app", appId);
await Groups.AddToGroupAsync(Context.ConnectionId, appId);
}
2018-12-27 20:55:46 +01:00
public async Task CreateInvoice(ContributeToCrowdfund model)
2018-12-27 20:19:21 +01:00
{
2018-12-27 20:55:46 +01:00
model.RedirectToCheckout = false;
2018-12-28 23:57:39 +01:00
_AppsPublicController.ControllerContext.HttpContext = Context.GetHttpContext();
2019-01-11 10:52:21 +01:00
try
{
2019-01-11 10:52:21 +01:00
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<object>());
break;
}
}
2019-01-11 10:52:21 +01:00
catch (Exception)
{
await Clients.Caller.SendCoreAsync(InvoiceError, System.Array.Empty<object>());
}
2018-12-27 20:19:21 +01:00
}
2018-12-28 23:57:39 +01:00
2018-12-27 20:19:21 +01:00
}
2018-12-22 15:02:16 +01:00
}