Trying to fix boardcast to upstream

This commit is contained in:
Creeper Lv
2026-06-01 18:58:36 +10:00
parent 9decde570f
commit 2920a67a61
3 changed files with 46 additions and 26 deletions
+24 -8
View File
@@ -211,7 +211,7 @@ public static class SyncEndpoints
});
// 5. Receive broadcast of changes (broadcasts single node updates)
routes.MapPost("/api/sync/broadcast", async (SyncPushItem item, HttpContext context, ServerDbContext db, PeerCache peerCache, RsaKeyManager rsaKeyManager, HttpClient httpClient) =>
routes.MapPost("/api/sync/broadcast", async (SyncPushItem item, HttpContext context, ServerDbContext db, PeerCache peerCache, RsaKeyManager rsaKeyManager, HttpClient httpClient, IServiceProvider serviceProvider) =>
{
if (!AuthHelper.IsServerTokenValid(context, peerCache))
{
@@ -273,13 +273,21 @@ public static class SyncEndpoints
{
await db.SaveChangesAsync();
// Relay broadcast to all peers (upstream + downstream) with loop prevention!
if (!string.IsNullOrEmpty(broadcastId))
{
_ = Task.Run(async () =>
{
var configuration = context.RequestServices.GetRequiredService<IConfiguration>();
await BroadcastNodeChangeAsync(item.Node.Uuid, db, peerCache, rsaKeyManager, configuration, httpClient, broadcastId);
try
{
using var scope = serviceProvider.CreateScope();
var backgroundDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>();
var backgroundConfig = scope.ServiceProvider.GetRequiredService<IConfiguration>();
await BroadcastNodeChangeAsync(item.Node.Uuid, backgroundDb, peerCache, rsaKeyManager, backgroundConfig, httpClient, broadcastId);
}
catch (Exception ex)
{
Console.WriteLine($"Error during background BroadcastNodeChangeAsync: {ex.Message}");
}
});
}
}
@@ -288,7 +296,7 @@ public static class SyncEndpoints
});
// Receive broadcast of user registration or password updates
routes.MapPost("/api/sync/broadcast-user", async (UserBroadcastPayload payload, HttpContext context, ServerDbContext db, PeerCache peerCache, RsaKeyManager rsaKeyManager, HttpClient httpClient) =>
routes.MapPost("/api/sync/broadcast-user", async (UserBroadcastPayload payload, HttpContext context, ServerDbContext db, PeerCache peerCache, RsaKeyManager rsaKeyManager, HttpClient httpClient, IServiceProvider serviceProvider) =>
{
if (!AuthHelper.IsServerTokenValid(context, peerCache))
{
@@ -326,13 +334,21 @@ public static class SyncEndpoints
{
await db.SaveChangesAsync();
// Relay the user broadcast to other peers
if (!string.IsNullOrEmpty(broadcastId))
{
_ = Task.Run(async () =>
{
var configuration = context.RequestServices.GetRequiredService<IConfiguration>();
await BroadcastUserChangeAsync(payload.User.Username, db, peerCache, rsaKeyManager, configuration, httpClient, broadcastId);
try
{
using var scope = serviceProvider.CreateScope();
var backgroundDb = scope.ServiceProvider.GetRequiredService<ServerDbContext>();
var backgroundConfig = scope.ServiceProvider.GetRequiredService<IConfiguration>();
await BroadcastUserChangeAsync(payload.User.Username, backgroundDb, peerCache, rsaKeyManager, backgroundConfig, httpClient, broadcastId);
}
catch (Exception ex)
{
Console.WriteLine($"Error during background BroadcastUserChangeAsync: {ex.Message}");
}
});
}
}