From 2024972832498ac83965a150dbd02a8628377549 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 23 Nov 2025 03:02:51 +0800 Subject: [PATCH] :bug: Trying to fix Pass service issues --- .../Startup/BroadcastEventHandler.cs | 8 +-- DysonNetwork.Shared/Cache/CacheService.cs | 1 + .../Data/ByteStringConverter.cs | 50 +++++++++++++++++++ 3 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 DysonNetwork.Shared/Data/ByteStringConverter.cs diff --git a/DysonNetwork.Pass/Startup/BroadcastEventHandler.cs b/DysonNetwork.Pass/Startup/BroadcastEventHandler.cs index 1ac2a92..60e349c 100644 --- a/DysonNetwork.Pass/Startup/BroadcastEventHandler.cs +++ b/DysonNetwork.Pass/Startup/BroadcastEventHandler.cs @@ -170,12 +170,12 @@ public class BroadcastEventHandler( await nats.PublishAsync( AccountStatusUpdatedEvent.Type, - GrpcTypeHelper.ConvertObjectToByteString(new AccountStatusUpdatedEvent + ByteString.CopyFromUtf8(JsonSerializer.Serialize(new AccountStatusUpdatedEvent { AccountId = evt.AccountId, Status = status, UpdatedAt = SystemClock.Instance.GetCurrentInstant() - }).ToByteArray() + }, GrpcTypeHelper.SerializerOptionsWithIgnore)).ToByteArray() ); logger.LogInformation("Broadcasted status update for user {AccountId}", evt.AccountId); @@ -209,12 +209,12 @@ public class BroadcastEventHandler( await nats.PublishAsync( AccountStatusUpdatedEvent.Type, - GrpcTypeHelper.ConvertObjectToByteString(new AccountStatusUpdatedEvent + ByteString.CopyFromUtf8(JsonSerializer.Serialize(new AccountStatusUpdatedEvent { AccountId = evt.AccountId, Status = status, UpdatedAt = SystemClock.Instance.GetCurrentInstant() - }).ToByteArray() + }, GrpcTypeHelper.SerializerOptionsWithIgnore)).ToByteArray() ); logger.LogInformation("Broadcasted status update for user {AccountId}", evt.AccountId); diff --git a/DysonNetwork.Shared/Cache/CacheService.cs b/DysonNetwork.Shared/Cache/CacheService.cs index a35a7a6..fbcac9a 100644 --- a/DysonNetwork.Shared/Cache/CacheService.cs +++ b/DysonNetwork.Shared/Cache/CacheService.cs @@ -211,6 +211,7 @@ public class CacheServiceRedis : ICacheService }, ReferenceHandler = ReferenceHandler.Preserve, NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals, + Converters = { new ByteStringConverter() } }; _jsonOptions.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb); _jsonOptions.PropertyNameCaseInsensitive = true; diff --git a/DysonNetwork.Shared/Data/ByteStringConverter.cs b/DysonNetwork.Shared/Data/ByteStringConverter.cs new file mode 100644 index 0000000..93c0151 --- /dev/null +++ b/DysonNetwork.Shared/Data/ByteStringConverter.cs @@ -0,0 +1,50 @@ +using System; +using System.Text.Json; +using System.Text.Json.Serialization; +using Google.Protobuf; + +namespace DysonNetwork.Shared.Data; + +public class ByteStringConverter : JsonConverter +{ + public override ByteString Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + if (reader.TokenType == JsonTokenType.Null) + { + return ByteString.Empty; + } + + if (reader.TokenType == JsonTokenType.String) + { + return ByteString.FromBase64(reader.GetString()); + } + + //This is a temporary workaround to fix the issue that the old data in the cache is not in base64 format. + if (reader.TokenType == JsonTokenType.StartObject) + { + var value = string.Empty; + while (reader.Read()) + { + if (reader.TokenType == JsonTokenType.EndObject) + { + return ByteString.CopyFromUtf8(value); + } + + if (reader.TokenType == JsonTokenType.PropertyName && reader.GetString() == "$values") + { + if (reader.Read() && reader.TokenType == JsonTokenType.String) + { + value = reader.GetString(); + } + } + } + } + + throw new JsonException("Expected a string or an object with a '$values' property."); + } + + public override void Write(Utf8JsonWriter writer, ByteString value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToBase64()); + } +}