btcpayserver/BTCPayServer/Services/Apps/AppHub.cs

79 lines
2.7 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;
2020-06-28 10:55:27 +02:00
using Microsoft.AspNetCore.Builder;
2019-03-09 08:08:31 +01:00
using Microsoft.AspNetCore.Http;
2018-12-27 20:19:21 +01:00
using Microsoft.AspNetCore.Mvc;
2019-10-03 11:46:09 +02:00
using Microsoft.AspNetCore.Routing;
2020-06-28 10:55:27 +02:00
using Microsoft.AspNetCore.SignalR;
2018-12-22 15:02:16 +01:00
namespace BTCPayServer.Services.Apps
2018-12-22 15:02:16 +01:00
{
2020-06-28 10:55:27 +02:00
public class AppHub : Hub
2018-12-22 15:02:16 +01:00
{
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
public AppHub(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
{
2020-06-28 10:55:27 +02:00
model.RedirectToCheckout = false;
_AppsPublicController.ControllerContext.HttpContext = Context.GetHttpContext();
try
{
2019-01-11 10:52:21 +01:00
2020-06-28 10:55:27 +02:00
var result =
await _AppsPublicController.ContributeToCrowdfund(Context.Items["app"].ToString(), model, Context.ConnectionAborted);
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;
}
}
catch (Exception)
{
await Clients.Caller.SendCoreAsync(InvoiceError, System.Array.Empty<object>());
2019-01-11 10:52:21 +01:00
2020-06-28 10:55:27 +02:00
}
2019-01-11 10:52:21 +01:00
2018-12-27 20:19:21 +01:00
}
2018-12-28 23:57:39 +01:00
2019-03-09 08:08:31 +01:00
public static string GetHubPath(HttpRequest request)
{
return request.GetRelativePathOrAbsolute("/apps/hub");
}
2020-01-12 05:30:54 +01:00
2019-10-03 11:46:09 +02:00
public static void Register(IEndpointRouteBuilder route)
2019-03-09 08:08:31 +01:00
{
route.MapHub<AppHub>("/apps/hub");
}
2019-10-03 11:46:09 +02:00
2018-12-27 20:19:21 +01:00
}
2018-12-22 15:02:16 +01:00
}