From b851b9f6e2ceb3d56a7a18a343f05e873b8dd774 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 24 Jan 2026 10:56:29 +0800 Subject: [PATCH] :sparkles: Shared project ring helper service --- .../DysonNetwork.Shared.csproj | 2 +- .../Registry/RemoteRingService.cs | 181 ++++++++++++++++++ .../Registry/ServiceInjectionHelper.cs | 1 + 3 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 DysonNetwork.Shared/Registry/RemoteRingService.cs diff --git a/DysonNetwork.Shared/DysonNetwork.Shared.csproj b/DysonNetwork.Shared/DysonNetwork.Shared.csproj index d48c4e15..fea40f7e 100644 --- a/DysonNetwork.Shared/DysonNetwork.Shared.csproj +++ b/DysonNetwork.Shared/DysonNetwork.Shared.csproj @@ -2,7 +2,7 @@ DysonNetwork.Shared - 1.0.0 + 1.0.1 LittleSheep Solsynth Shared project of the Solar Network server side software. diff --git a/DysonNetwork.Shared/Registry/RemoteRingService.cs b/DysonNetwork.Shared/Registry/RemoteRingService.cs new file mode 100644 index 00000000..4c4814f3 --- /dev/null +++ b/DysonNetwork.Shared/Registry/RemoteRingService.cs @@ -0,0 +1,181 @@ +using DysonNetwork.Shared.Proto; + +namespace DysonNetwork.Shared.Registry; + +public class RemoteRingService(RingService.RingServiceClient ring) +{ + public async Task SendEmail(string toName, string toAddress, string subject, string body) + { + var request = new SendEmailRequest + { + Email = new EmailMessage + { + ToName = toName, + ToAddress = toAddress, + Subject = subject, + Body = body + } + }; + await ring.SendEmailAsync(request); + } + + public async Task PushWebSocketPacket(string accountId, string type, byte[] data, string? errorMessage = null) + { + var request = new PushWebSocketPacketRequest + { + UserId = accountId, + Packet = new WebSocketPacket + { + Type = type, + Data = Google.Protobuf.ByteString.CopyFrom(data) + } + }; + + if (errorMessage != null) + request.Packet.ErrorMessage = errorMessage; + + await ring.PushWebSocketPacketAsync(request); + } + + public async Task PushWebSocketPacketToUsers(List userIds, string type, byte[] data, string? errorMessage = null) + { + var request = new PushWebSocketPacketToUsersRequest + { + Packet = new WebSocketPacket + { + Type = type, + Data = Google.Protobuf.ByteString.CopyFrom(data) + } + }; + request.UserIds.AddRange(userIds); + + if (errorMessage != null) + request.Packet.ErrorMessage = errorMessage; + + await ring.PushWebSocketPacketToUsersAsync(request); + } + + public async Task PushWebSocketPacketToDevice(string deviceId, string type, byte[] data, string? errorMessage = null) + { + var request = new PushWebSocketPacketToDeviceRequest + { + DeviceId = deviceId, + Packet = new WebSocketPacket + { + Type = type, + Data = Google.Protobuf.ByteString.CopyFrom(data) + } + }; + + if (errorMessage != null) + request.Packet.ErrorMessage = errorMessage; + + await ring.PushWebSocketPacketToDeviceAsync(request); + } + + public async Task PushWebSocketPacketToDevices(List deviceIds, string type, byte[] data, string? errorMessage = null) + { + var request = new PushWebSocketPacketToDevicesRequest + { + Packet = new WebSocketPacket + { + Type = type, + Data = Google.Protobuf.ByteString.CopyFrom(data) + } + }; + request.DeviceIds.AddRange(deviceIds); + + if (errorMessage != null) + request.Packet.ErrorMessage = errorMessage; + + await ring.PushWebSocketPacketToDevicesAsync(request); + } + + public async Task SendPushNotificationToUser(string userId, string topic, string title, string subtitle, string body, byte[]? meta = null, string? actionUri = null, bool isSilent = false, bool isSavable = false) + { + var request = new SendPushNotificationToUserRequest + { + UserId = userId, + Notification = new PushNotification + { + Topic = topic, + Title = title, + Subtitle = subtitle, + Body = body, + IsSilent = isSilent, + IsSavable = isSavable + } + }; + + if (meta != null) + request.Notification.Meta = Google.Protobuf.ByteString.CopyFrom(meta); + + if (actionUri != null) + request.Notification.ActionUri = actionUri; + + await ring.SendPushNotificationToUserAsync(request); + } + + public async Task SendPushNotificationToUsers(List userIds, string topic, string title, string subtitle, string body, byte[]? meta = null, string? actionUri = null, bool isSilent = false, bool isSavable = false) + { + var request = new SendPushNotificationToUsersRequest + { + Notification = new PushNotification + { + Topic = topic, + Title = title, + Subtitle = subtitle, + Body = body, + IsSilent = isSilent, + IsSavable = isSavable + } + }; + request.UserIds.AddRange(userIds); + + if (meta != null) + { + request.Notification.Meta = Google.Protobuf.ByteString.CopyFrom(meta); + } + + if (actionUri != null) + { + request.Notification.ActionUri = actionUri; + } + + await ring.SendPushNotificationToUsersAsync(request); + } + + public async Task UnsubscribePushNotifications(string deviceId) + { + var request = new UnsubscribePushNotificationsRequest + { + DeviceId = deviceId + }; + await ring.UnsubscribePushNotificationsAsync(request); + } + + public async Task GetWebsocketConnectionStatus(string deviceIdOrUserId, bool isUserId = false) + { + var request = new GetWebsocketConnectionStatusRequest(); + if (isUserId) + { + request.UserId = deviceIdOrUserId; + } + else + { + request.DeviceId = deviceIdOrUserId; + } + + var response = await ring.GetWebsocketConnectionStatusAsync(request); + return response.IsConnected; + } + + public async Task> GetWebsocketConnectionStatusBatch(List userIds) + { + var request = new GetWebsocketConnectionStatusBatchRequest(); + request.UsersId.AddRange(userIds); + + var response = await ring.GetWebsocketConnectionStatusBatchAsync(request); + return response.IsConnected.ToDictionary(); + } +} diff --git a/DysonNetwork.Shared/Registry/ServiceInjectionHelper.cs b/DysonNetwork.Shared/Registry/ServiceInjectionHelper.cs index 06114757..f96282a2 100644 --- a/DysonNetwork.Shared/Registry/ServiceInjectionHelper.cs +++ b/DysonNetwork.Shared/Registry/ServiceInjectionHelper.cs @@ -12,6 +12,7 @@ public static class ServiceInjectionHelper services.AddGrpcClientWithSharedChannel( "https://_grpc.ring", "RingService"); + services.AddSingleton(); return services; }