♻️ Move the chat part of the Sphere service to the Messager service
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using DysonNetwork.Shared.Auth;
|
||||
using DysonNetwork.Shared.Http;
|
||||
using DysonNetwork.Sphere.Poll;
|
||||
using DysonNetwork.Sphere.Post;
|
||||
using DysonNetwork.Sphere.Publisher;
|
||||
using DysonNetwork.Sphere.Rewind;
|
||||
@@ -23,6 +24,7 @@ public static class ApplicationConfiguration
|
||||
|
||||
// Map gRPC services
|
||||
app.MapGrpcService<PostServiceGrpc>();
|
||||
app.MapGrpcService<PollServiceGrpc>();
|
||||
app.MapGrpcService<PublisherServiceGrpc>();
|
||||
app.MapGrpcService<SphereRewindServiceGrpc>();
|
||||
app.MapGrpcReflectionService();
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using DysonNetwork.Shared.Models;
|
||||
using DysonNetwork.Shared.Proto;
|
||||
using DysonNetwork.Shared.Queue;
|
||||
using DysonNetwork.Sphere.Chat;
|
||||
using DysonNetwork.Sphere.Post;
|
||||
using Google.Protobuf;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NATS.Client.Core;
|
||||
using NATS.Client.JetStream.Models;
|
||||
@@ -39,10 +35,8 @@ public class BroadcastEventHandler(
|
||||
{
|
||||
var paymentTask = HandlePaymentOrders(stoppingToken);
|
||||
var accountTask = HandleAccountDeletions(stoppingToken);
|
||||
var websocketTask = HandleWebSocketPackets(stoppingToken);
|
||||
var accountStatusTask = HandleAccountStatusUpdates(stoppingToken);
|
||||
|
||||
await Task.WhenAll(paymentTask, accountTask, websocketTask, accountStatusTask);
|
||||
await Task.WhenAll(paymentTask, accountTask);
|
||||
}
|
||||
|
||||
private async Task HandlePaymentOrders(CancellationToken stoppingToken)
|
||||
@@ -94,6 +88,7 @@ public class BroadcastEventHandler(
|
||||
}
|
||||
default:
|
||||
// ignore
|
||||
await msg.AckAsync(cancellationToken: stoppingToken);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -169,242 +164,6 @@ public class BroadcastEventHandler(
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleWebSocketPackets(CancellationToken stoppingToken)
|
||||
{
|
||||
await foreach (var msg in nats.SubscribeAsync<byte[]>(
|
||||
WebSocketPacketEvent.SubjectPrefix + "sphere", cancellationToken: stoppingToken))
|
||||
{
|
||||
logger.LogDebug("Handling websocket packet...");
|
||||
|
||||
try
|
||||
{
|
||||
var evt = JsonSerializer.Deserialize<WebSocketPacketEvent>(msg.Data, GrpcTypeHelper.SerializerOptions);
|
||||
if (evt == null) throw new ArgumentNullException(nameof(evt));
|
||||
var packet = WebSocketPacket.FromBytes(evt.PacketBytes);
|
||||
logger.LogInformation("Handling websocket packet... {Type}", packet.Type);
|
||||
switch (packet.Type)
|
||||
{
|
||||
case "messages.read":
|
||||
await HandleMessageRead(evt, packet);
|
||||
break;
|
||||
case "messages.typing":
|
||||
await HandleMessageTyping(evt, packet);
|
||||
break;
|
||||
case "messages.subscribe":
|
||||
await HandleMessageSubscribe(evt, packet);
|
||||
break;
|
||||
case "messages.unsubscribe":
|
||||
await HandleMessageUnsubscribe(evt, packet);
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error processing websocket packet");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleMessageRead(WebSocketPacketEvent evt, WebSocketPacket packet)
|
||||
{
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var cs = scope.ServiceProvider.GetRequiredService<ChatService>();
|
||||
var crs = scope.ServiceProvider.GetRequiredService<ChatRoomService>();
|
||||
|
||||
if (packet.Data == null)
|
||||
{
|
||||
await SendErrorResponse(evt, "Mark message as read requires you to provide the ChatRoomId");
|
||||
return;
|
||||
}
|
||||
|
||||
var requestData = packet.GetData<Chat.ChatController.MarkMessageReadRequest>();
|
||||
if (requestData == null)
|
||||
{
|
||||
await SendErrorResponse(evt, "Invalid request data");
|
||||
return;
|
||||
}
|
||||
|
||||
var sender = await crs.GetRoomMember(evt.AccountId, requestData.ChatRoomId);
|
||||
if (sender == null)
|
||||
{
|
||||
await SendErrorResponse(evt, "User is not a member of the chat room.");
|
||||
return;
|
||||
}
|
||||
|
||||
await cs.ReadChatRoomAsync(requestData.ChatRoomId, evt.AccountId);
|
||||
}
|
||||
|
||||
private async Task HandleMessageTyping(WebSocketPacketEvent evt, WebSocketPacket packet)
|
||||
{
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var crs = scope.ServiceProvider.GetRequiredService<ChatRoomService>();
|
||||
|
||||
if (packet.Data == null)
|
||||
{
|
||||
await SendErrorResponse(evt, "messages.typing requires you to provide the ChatRoomId");
|
||||
return;
|
||||
}
|
||||
|
||||
var requestData = packet.GetData<Chat.ChatController.ChatRoomWsUniversalRequest>();
|
||||
if (requestData == null)
|
||||
{
|
||||
await SendErrorResponse(evt, "Invalid request data");
|
||||
return;
|
||||
}
|
||||
|
||||
var sender = await crs.GetRoomMember(evt.AccountId, requestData.ChatRoomId);
|
||||
if (sender == null)
|
||||
{
|
||||
await SendErrorResponse(evt, "User is not a member of the chat room.");
|
||||
return;
|
||||
}
|
||||
|
||||
var responsePacket = new WebSocketPacket
|
||||
{
|
||||
Type = "messages.typing",
|
||||
Data = new
|
||||
{
|
||||
room_id = sender.ChatRoomId,
|
||||
sender_id = sender.Id,
|
||||
sender
|
||||
}
|
||||
};
|
||||
|
||||
// Broadcast typing indicator to subscribed room members only
|
||||
var subscribedMemberIds = await crs.GetSubscribedMembers(requestData.ChatRoomId);
|
||||
var roomMembers = await crs.ListRoomMembers(requestData.ChatRoomId);
|
||||
|
||||
// Filter to subscribed members excluding the current user
|
||||
var subscribedMembers = roomMembers
|
||||
.Where(m => subscribedMemberIds.Contains(m.Id) && m.AccountId != evt.AccountId)
|
||||
.Select(m => m.AccountId.ToString())
|
||||
.ToList();
|
||||
|
||||
if (subscribedMembers.Count > 0)
|
||||
{
|
||||
var respRequest = new PushWebSocketPacketToUsersRequest { Packet = responsePacket.ToProtoValue() };
|
||||
respRequest.UserIds.AddRange(subscribedMembers);
|
||||
await pusher.PushWebSocketPacketToUsersAsync(respRequest);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task HandleMessageSubscribe(WebSocketPacketEvent evt, WebSocketPacket packet)
|
||||
{
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var crs = scope.ServiceProvider.GetRequiredService<ChatRoomService>();
|
||||
|
||||
if (packet.Data == null)
|
||||
{
|
||||
await SendErrorResponse(evt, "messages.subscribe requires you to provide the ChatRoomId");
|
||||
return;
|
||||
}
|
||||
|
||||
var requestData = packet.GetData<Chat.ChatController.ChatRoomWsUniversalRequest>();
|
||||
if (requestData == null)
|
||||
{
|
||||
await SendErrorResponse(evt, "Invalid request data");
|
||||
return;
|
||||
}
|
||||
|
||||
var sender = await crs.GetRoomMember(evt.AccountId, requestData.ChatRoomId);
|
||||
if (sender == null)
|
||||
{
|
||||
await SendErrorResponse(evt, "User is not a member of the chat room.");
|
||||
return;
|
||||
}
|
||||
|
||||
await crs.SubscribeChatRoom(sender);
|
||||
}
|
||||
|
||||
private async Task HandleMessageUnsubscribe(WebSocketPacketEvent evt, WebSocketPacket packet)
|
||||
{
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var crs = scope.ServiceProvider.GetRequiredService<ChatRoomService>();
|
||||
|
||||
if (packet.Data == null)
|
||||
{
|
||||
await SendErrorResponse(evt, "messages.unsubscribe requires you to provide the ChatRoomId");
|
||||
return;
|
||||
}
|
||||
|
||||
var requestData = packet.GetData<Chat.ChatController.ChatRoomWsUniversalRequest>();
|
||||
if (requestData == null)
|
||||
{
|
||||
await SendErrorResponse(evt, "Invalid request data");
|
||||
return;
|
||||
}
|
||||
|
||||
var sender = await crs.GetRoomMember(evt.AccountId, requestData.ChatRoomId);
|
||||
if (sender == null)
|
||||
{
|
||||
await SendErrorResponse(evt, "User is not a member of the chat room.");
|
||||
return;
|
||||
}
|
||||
|
||||
await crs.UnsubscribeChatRoom(sender);
|
||||
}
|
||||
|
||||
private async Task HandleAccountStatusUpdates(CancellationToken stoppingToken)
|
||||
{
|
||||
await foreach (var msg in nats.SubscribeAsync<byte[]>(AccountStatusUpdatedEvent.Type, cancellationToken: stoppingToken))
|
||||
{
|
||||
try
|
||||
{
|
||||
var evt = GrpcTypeHelper.ConvertByteStringToObject<AccountStatusUpdatedEvent>(ByteString.CopyFrom(msg.Data));
|
||||
if (evt == null)
|
||||
continue;
|
||||
|
||||
logger.LogInformation("Account status updated: {AccountId}", evt.AccountId);
|
||||
|
||||
await using var scope = serviceProvider.CreateAsyncScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDatabase>();
|
||||
var chatRoomService = scope.ServiceProvider.GetRequiredService<ChatRoomService>();
|
||||
|
||||
// Get user's joined chat rooms
|
||||
var userRooms = await db.ChatMembers
|
||||
.Where(m => m.AccountId == evt.AccountId && m.JoinedAt != null && m.LeaveAt == null)
|
||||
.Select(m => m.ChatRoomId)
|
||||
.ToListAsync(cancellationToken: stoppingToken);
|
||||
|
||||
// Send WebSocket packet to subscribed users per room
|
||||
foreach (var roomId in userRooms)
|
||||
{
|
||||
var members = await chatRoomService.ListRoomMembers(roomId);
|
||||
var subscribedMemberIds = await chatRoomService.GetSubscribedMembers(roomId);
|
||||
var subscribedUsers = members
|
||||
.Where(m => subscribedMemberIds.Contains(m.Id))
|
||||
.Select(m => m.AccountId.ToString())
|
||||
.ToList();
|
||||
|
||||
if (subscribedUsers.Count == 0) continue;
|
||||
var packet = new WebSocketPacket
|
||||
{
|
||||
Type = "accounts.status.update",
|
||||
Data = new Dictionary<string, object>
|
||||
{
|
||||
["status"] = evt.Status,
|
||||
["chat_room_id"] = roomId
|
||||
}
|
||||
};
|
||||
|
||||
var request = new PushWebSocketPacketToUsersRequest
|
||||
{
|
||||
Packet = packet.ToProtoValue()
|
||||
};
|
||||
request.UserIds.AddRange(subscribedUsers);
|
||||
|
||||
await pusher.PushWebSocketPacketToUsersAsync(request, cancellationToken: stoppingToken);
|
||||
|
||||
logger.LogInformation("Sent status update for room {roomId} to {count} subscribed users", roomId, subscribedUsers.Count);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error processing AccountStatusUpdated");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SendErrorResponse(WebSocketPacketEvent evt, string message)
|
||||
{
|
||||
await pusher.PushWebSocketPacketToDeviceAsync(new PushWebSocketPacketToDeviceRequest
|
||||
|
||||
@@ -5,14 +5,11 @@ using DysonNetwork.Shared.Cache;
|
||||
using DysonNetwork.Shared.Geometry;
|
||||
using DysonNetwork.Sphere.ActivityPub;
|
||||
using DysonNetwork.Sphere.Autocompletion;
|
||||
using DysonNetwork.Sphere.Chat;
|
||||
using DysonNetwork.Sphere.Chat.Realtime;
|
||||
using DysonNetwork.Sphere.Discovery;
|
||||
using DysonNetwork.Sphere.Localization;
|
||||
using DysonNetwork.Sphere.Poll;
|
||||
using DysonNetwork.Sphere.Post;
|
||||
using DysonNetwork.Sphere.Publisher;
|
||||
using DysonNetwork.Sphere.Sticker;
|
||||
using DysonNetwork.Sphere.Timeline;
|
||||
using DysonNetwork.Sphere.Translation;
|
||||
using DysonNetwork.Sphere.WebReader;
|
||||
@@ -23,104 +20,101 @@ namespace DysonNetwork.Sphere.Startup;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddAppServices(this IServiceCollection services)
|
||||
extension(IServiceCollection services)
|
||||
{
|
||||
services.AddLocalization(options => options.ResourcesPath = "Resources");
|
||||
public IServiceCollection AddAppServices()
|
||||
{
|
||||
services.AddLocalization(options => options.ResourcesPath = "Resources");
|
||||
|
||||
services.AddDbContext<AppDatabase>();
|
||||
services.AddHttpContextAccessor();
|
||||
services.AddDbContext<AppDatabase>();
|
||||
services.AddHttpContextAccessor();
|
||||
|
||||
services.AddHttpClient();
|
||||
services.AddHttpClient();
|
||||
|
||||
services
|
||||
.AddControllers()
|
||||
.AddJsonOptions(options =>
|
||||
services
|
||||
.AddControllers()
|
||||
.AddJsonOptions(options =>
|
||||
{
|
||||
options.JsonSerializerOptions.NumberHandling =
|
||||
JsonNumberHandling.AllowNamedFloatingPointLiterals;
|
||||
options.JsonSerializerOptions.PropertyNamingPolicy =
|
||||
JsonNamingPolicy.SnakeCaseLower;
|
||||
|
||||
options.JsonSerializerOptions.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
|
||||
})
|
||||
.AddDataAnnotationsLocalization(options =>
|
||||
{
|
||||
options.DataAnnotationLocalizerProvider = (type, factory) =>
|
||||
factory.Create(typeof(SharedResource));
|
||||
});
|
||||
services.AddRazorPages();
|
||||
|
||||
services.AddGrpc(options =>
|
||||
{
|
||||
options.JsonSerializerOptions.NumberHandling =
|
||||
JsonNumberHandling.AllowNamedFloatingPointLiterals;
|
||||
options.JsonSerializerOptions.PropertyNamingPolicy =
|
||||
JsonNamingPolicy.SnakeCaseLower;
|
||||
|
||||
options.JsonSerializerOptions.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
|
||||
})
|
||||
.AddDataAnnotationsLocalization(options =>
|
||||
{
|
||||
options.DataAnnotationLocalizerProvider = (type, factory) =>
|
||||
factory.Create(typeof(SharedResource));
|
||||
options.EnableDetailedErrors = true;
|
||||
});
|
||||
services.AddRazorPages();
|
||||
services.AddGrpcReflection();
|
||||
|
||||
services.AddGrpc(options =>
|
||||
{
|
||||
options.EnableDetailedErrors = true;
|
||||
});
|
||||
services.AddGrpcReflection();
|
||||
services.Configure<RequestLocalizationOptions>(options =>
|
||||
{
|
||||
var supportedCultures = new[] { new CultureInfo("en-US"), new CultureInfo("zh-Hans") };
|
||||
|
||||
services.Configure<RequestLocalizationOptions>(options =>
|
||||
{
|
||||
var supportedCultures = new[] { new CultureInfo("en-US"), new CultureInfo("zh-Hans") };
|
||||
options.SupportedCultures = supportedCultures;
|
||||
options.SupportedUICultures = supportedCultures;
|
||||
});
|
||||
|
||||
options.SupportedCultures = supportedCultures;
|
||||
options.SupportedUICultures = supportedCultures;
|
||||
});
|
||||
services.AddHostedService<BroadcastEventHandler>();
|
||||
services.AddHostedService<ActivityPubDeliveryWorker>();
|
||||
|
||||
services.AddHostedService<BroadcastEventHandler>();
|
||||
services.AddHostedService<ActivityPubDeliveryWorker>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddAppAuthentication(this IServiceCollection services)
|
||||
{
|
||||
services.AddAuthorization();
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddAppFlushHandlers(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<FlushBufferService>();
|
||||
services.AddScoped<PostViewFlushHandler>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddAppBusinessServices(
|
||||
this IServiceCollection services,
|
||||
IConfiguration configuration
|
||||
)
|
||||
{
|
||||
services.Configure<GeoOptions>(configuration.GetSection("GeoIP"));
|
||||
services.Configure<ActivityPubDeliveryOptions>(configuration.GetSection("ActivityPubDelivery"));
|
||||
services.AddScoped<GeoService>();
|
||||
services.AddScoped<PublisherService>();
|
||||
services.AddScoped<PublisherSubscriptionService>();
|
||||
services.AddScoped<TimelineService>();
|
||||
services.AddScoped<PostService>();
|
||||
services.AddScoped<ChatRoomService>();
|
||||
services.AddScoped<ChatService>();
|
||||
services.AddScoped<StickerService>();
|
||||
services.AddScoped<IRealtimeService, LiveKitRealtimeService>();
|
||||
services.AddScoped<WebReaderService>();
|
||||
services.AddScoped<WebFeedService>();
|
||||
services.AddScoped<DiscoveryService>();
|
||||
services.AddScoped<PollService>();
|
||||
services.AddScoped<AutocompletionService>();
|
||||
services.AddScoped<ActivityPubKeyService>();
|
||||
services.AddScoped<ActivityPubSignatureService>();
|
||||
services.AddScoped<ActivityPubActivityHandler>();
|
||||
services.AddScoped<ActivityPubDeliveryService>();
|
||||
services.AddScoped<ActivityPubDiscoveryService>();
|
||||
services.AddScoped<ActivityPubObjectFactory>();
|
||||
services.AddSingleton<ActivityPubQueueService>();
|
||||
|
||||
var translationProvider = configuration["Translation:Provider"]?.ToLower();
|
||||
switch (translationProvider)
|
||||
{
|
||||
case "tencent":
|
||||
services.AddScoped<ITranslationProvider, TencentTranslation>();
|
||||
break;
|
||||
return services;
|
||||
}
|
||||
|
||||
return services;
|
||||
public IServiceCollection AddAppAuthentication()
|
||||
{
|
||||
services.AddAuthorization();
|
||||
return services;
|
||||
}
|
||||
|
||||
public IServiceCollection AddAppFlushHandlers()
|
||||
{
|
||||
services.AddSingleton<FlushBufferService>();
|
||||
services.AddScoped<PostViewFlushHandler>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public IServiceCollection AddAppBusinessServices(IConfiguration configuration
|
||||
)
|
||||
{
|
||||
services.Configure<GeoOptions>(configuration.GetSection("GeoIP"));
|
||||
services.Configure<ActivityPubDeliveryOptions>(configuration.GetSection("ActivityPubDelivery"));
|
||||
services.AddScoped<GeoService>();
|
||||
services.AddScoped<PublisherService>();
|
||||
services.AddScoped<PublisherSubscriptionService>();
|
||||
services.AddScoped<TimelineService>();
|
||||
services.AddScoped<PostService>();
|
||||
services.AddScoped<WebReaderService>();
|
||||
services.AddScoped<WebFeedService>();
|
||||
services.AddScoped<DiscoveryService>();
|
||||
services.AddScoped<PollService>();
|
||||
services.AddScoped<AutocompletionService>();
|
||||
services.AddScoped<ActivityPubKeyService>();
|
||||
services.AddScoped<ActivityPubSignatureService>();
|
||||
services.AddScoped<ActivityPubActivityHandler>();
|
||||
services.AddScoped<ActivityPubDeliveryService>();
|
||||
services.AddScoped<ActivityPubDiscoveryService>();
|
||||
services.AddScoped<ActivityPubObjectFactory>();
|
||||
services.AddSingleton<ActivityPubQueueService>();
|
||||
|
||||
var translationProvider = configuration["Translation:Provider"]?.ToLower();
|
||||
switch (translationProvider)
|
||||
{
|
||||
case "tencent":
|
||||
services.AddScoped<ITranslationProvider, TencentTranslation>();
|
||||
break;
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user