Chat subscribe fixes and status update

This commit is contained in:
2025-09-27 19:25:10 +08:00
parent 1075177511
commit 58a44e8af4
8 changed files with 292 additions and 15 deletions

View File

@@ -3,8 +3,11 @@ using DysonNetwork.Pass.Wallet;
using DysonNetwork.Shared.Cache;
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Shared.Stream;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Localization;
using NATS.Client.Core;
using NATS.Net;
using NodaTime;
using NodaTime.Extensions;
@@ -17,7 +20,8 @@ public class AccountEventService(
IStringLocalizer<Localization.AccountEventResource> localizer,
RingService.RingServiceClient pusher,
SubscriptionService subscriptions,
Pass.Leveling.ExperienceService experienceService
Pass.Leveling.ExperienceService experienceService,
INatsConnection nats
)
{
private static readonly Random Random = new();
@@ -37,6 +41,19 @@ public class AccountEventService(
cache.RemoveAsync(cacheKey);
}
private async Task BroadcastStatusUpdate(SnAccountStatus status)
{
await nats.PublishAsync(
AccountStatusUpdatedEvent.Type,
GrpcTypeHelper.ConvertObjectToByteString(new AccountStatusUpdatedEvent
{
AccountId = status.AccountId,
Status = status,
UpdatedAt = SystemClock.Instance.GetCurrentInstant()
}).ToByteArray()
);
}
public async Task<SnAccountStatus> GetStatus(Guid userId)
{
var cacheKey = $"{StatusCacheKey}{userId}";
@@ -158,6 +175,8 @@ public class AccountEventService(
db.AccountStatuses.Add(status);
await db.SaveChangesAsync();
await BroadcastStatusUpdate(status);
return status;
}
@@ -167,6 +186,7 @@ public class AccountEventService(
db.Update(status);
await db.SaveChangesAsync();
PurgeStatusCache(user.Id);
await BroadcastStatusUpdate(status);
}
private const int FortuneTipCount = 14; // This will be the max index for each type (positive/negative)