Simplify server events

This commit is contained in:
Dennis Reimann 2024-07-12 07:56:50 +02:00
parent e1888a038c
commit 5b7dafe142
No known key found for this signature in database
GPG key ID: 5009E1797F03F8D0
3 changed files with 19 additions and 26 deletions

View file

@ -46,23 +46,12 @@ public interface IBTCPayAppHubServer
Task SendPaymentUpdate(string identifier, LightningPayment lightningPayment);
}
public interface IServerEvent
{
public string Type { get; }
}
public interface IServerEvent<T> : IServerEvent
{
public T? Event { get; }
}
public class ServerEvent(string type) : IServerEvent
public class ServerEvent(string type)
{
public string Type { get; } = type;
}
public class ServerEvent<T>(string type, T? evt = default) : ServerEvent(type), IServerEvent<T>
{
public T? Event { get; } = evt;
public string? StoreId { get; init; }
public string? UserId { get; init; }
public string? InvoiceId { get; init; }
}
public record TxResp(long Confirmations, long? Height, decimal BalanceChange, DateTimeOffset Timestamp, string TransactionId)

View file

@ -78,50 +78,54 @@ public class BTCPayAppState : IHostedService
private async Task InvoiceChangedEvent(InvoiceEvent arg)
{
await _hubContext.Clients.Group(arg.Invoice.StoreId).NotifyServerEvent(new ServerEvent<InvoiceEvent>("invoice-updated", arg));
await _hubContext.Clients.Group(arg.Invoice.StoreId).NotifyServerEvent(new ServerEvent("invoice-updated") { StoreId = arg.Invoice.StoreId, InvoiceId = arg.InvoiceId });
}
private async Task UserNotificationsUpdatedEvent(UserNotificationsUpdatedEvent arg)
{
await _hubContext.Clients.Group(arg.UserId).NotifyServerEvent(new ServerEvent<UserNotificationsUpdatedEvent>("notifications-updated", arg));
await _hubContext.Clients.Group(arg.UserId).NotifyServerEvent(new ServerEvent("notifications-updated") { UserId = arg.UserId });
}
private async Task StoreCreatedEvent(StoreCreatedEvent arg)
{
await _hubContext.Clients.Group(arg.StoreId).NotifyServerEvent(new ServerEvent<StoreCreatedEvent>("store-created", arg));
await _hubContext.Clients.Group(arg.StoreId).NotifyServerEvent(new ServerEvent("store-created") { StoreId = arg.StoreId });
}
private async Task StoreUpdatedEvent(StoreUpdatedEvent arg)
{
await _hubContext.Clients.Group(arg.StoreId).NotifyServerEvent(new ServerEvent<StoreUpdatedEvent>("store-updated", arg));
await _hubContext.Clients.Group(arg.StoreId).NotifyServerEvent(new ServerEvent("store-updated") { StoreId = arg.StoreId });
}
private async Task StoreRemovedEvent(StoreRemovedEvent arg)
{
await _hubContext.Clients.Group(arg.StoreId).NotifyServerEvent(new ServerEvent<StoreRemovedEvent>("store-removed", arg));
await _hubContext.Clients.Group(arg.StoreId).NotifyServerEvent(new ServerEvent("store-removed") { StoreId = arg.StoreId });
}
private async Task StoreUserAddedEvent(UserStoreAddedEvent arg)
{
var ev = new ServerEvent<UserStoreAddedEvent>("user-store-added", arg);
var ev = new ServerEvent("user-store-added") { StoreId = arg.StoreId, UserId = arg.UserId };
await _hubContext.Clients.Group(arg.StoreId).NotifyServerEvent(ev);
await _hubContext.Clients.Group(arg.UserId).NotifyServerEvent(ev);
//await _hubContext.Clients.User(arg.UserId) .AddToGroupAsync(Context.ConnectionId, arg.StoreId);
// TODO: Add user to store group - how to get the connectionId?
//await _hubContext.Groups.AddToGroupAsync(connectionId, arg.StoreId);
}
private async Task StoreUserUpdatedEvent(UserStoreUpdatedEvent arg)
{
var ev = new ServerEvent<UserStoreUpdatedEvent>("user-store-updated", arg);
var ev = new ServerEvent("user-store-updated") { StoreId = arg.StoreId, UserId = arg.UserId };
await _hubContext.Clients.Group(arg.StoreId).NotifyServerEvent(ev);
await _hubContext.Clients.Group(arg.UserId).NotifyServerEvent(ev);
}
private async Task StoreUserRemovedEvent(UserStoreRemovedEvent arg)
{
var ev = new ServerEvent<UserStoreRemovedEvent>("user-store-removed", arg);
var ev = new ServerEvent("user-store-removed") { StoreId = arg.StoreId, UserId = arg.UserId };
await _hubContext.Clients.Group(arg.StoreId).NotifyServerEvent(ev);
await _hubContext.Clients.Group(arg.UserId).NotifyServerEvent(ev);
// await Groups.RemoveFromGroupAsync(Context.ConnectionId, arg.UserId);
// TODO: Remove user from store group - how to get the connectionId?
//await _hubContext.Groups.RemoveFromGroupAsync(connectionId, arg.UserId);
}
private string _nodeInfo = string.Empty;

View file

@ -411,8 +411,8 @@ namespace BTCPayServer.Services.Stores
ctx.Add(storeData);
ctx.Add(userStore);
await ctx.SaveChangesAsync();
_eventAggregator.Publish(new StoreCreatedEvent(storeData));
_eventAggregator.Publish(new UserStoreAddedEvent(storeData.Id, userStore.ApplicationUserId));
_eventAggregator.Publish(new StoreCreatedEvent(storeData));
}
public async Task<WebhookData[]> GetWebhooks(string storeId)