♻️ Better event broadcast status changes

This commit is contained in:
2026-01-02 15:08:02 +08:00
parent c588b6f234
commit eeb583d78d
2 changed files with 100 additions and 54 deletions

View File

@@ -25,9 +25,10 @@ public class AccountEventService(
{ {
private static readonly Random Random = new(); private static readonly Random Random = new();
private const string StatusCacheKey = "account:status:"; private const string StatusCacheKey = "account:status:";
private const string PreviousStatusCacheKey = "account:status:prev:";
private const string ActivityCacheKey = "account:activities:"; private const string ActivityCacheKey = "account:activities:";
public async Task<bool> GetAccountIsConnected(Guid userId) private async Task<bool> GetAccountIsConnected(Guid userId)
{ {
var resp = await pusher.GetWebsocketConnectionStatusAsync( var resp = await pusher.GetWebsocketConnectionStatusAsync(
new GetWebsocketConnectionStatusRequest { UserId = userId.ToString() } new GetWebsocketConnectionStatusRequest { UserId = userId.ToString() }
@@ -49,6 +50,8 @@ public class AccountEventService(
{ {
var cacheKey = $"{StatusCacheKey}{userId}"; var cacheKey = $"{StatusCacheKey}{userId}";
cache.RemoveAsync(cacheKey); cache.RemoveAsync(cacheKey);
var prevCacheKey = $"{PreviousStatusCacheKey}{userId}";
cache.RemoveAsync(prevCacheKey);
} }
public void PurgeActivityCache(Guid userId) public void PurgeActivityCache(Guid userId)
@@ -70,51 +73,71 @@ public class AccountEventService(
); );
} }
private static bool StatusesEqual(SnAccountStatus a, SnAccountStatus b)
{
return a.Attitude == b.Attitude &&
a.IsOnline == b.IsOnline &&
a.IsCustomized == b.IsCustomized &&
a.Label == b.Label &&
a.IsInvisible == b.IsInvisible &&
a.IsNotDisturb == b.IsNotDisturb;
}
public async Task<SnAccountStatus> GetStatus(Guid userId) public async Task<SnAccountStatus> GetStatus(Guid userId)
{ {
var cacheKey = $"{StatusCacheKey}{userId}"; var cacheKey = $"{StatusCacheKey}{userId}";
var cachedStatus = await cache.GetAsync<SnAccountStatus>(cacheKey); var cachedStatus = await cache.GetAsync<SnAccountStatus>(cacheKey);
SnAccountStatus status;
if (cachedStatus is not null) if (cachedStatus is not null)
{ {
cachedStatus!.IsOnline = !cachedStatus.IsInvisible && await GetAccountIsConnected(userId); cachedStatus!.IsOnline = !cachedStatus.IsInvisible && await GetAccountIsConnected(userId);
return cachedStatus; status = cachedStatus;
} }
else
var now = SystemClock.Instance.GetCurrentInstant();
var status = await db.AccountStatuses
.Where(e => e.AccountId == userId)
.Where(e => e.ClearedAt == null || e.ClearedAt > now)
.OrderByDescending(e => e.CreatedAt)
.FirstOrDefaultAsync();
var isOnline = await GetAccountIsConnected(userId);
if (status is not null)
{ {
status.IsOnline = !status.IsInvisible && isOnline; var now = SystemClock.Instance.GetCurrentInstant();
await cache.SetWithGroupsAsync(cacheKey, status, [$"{AccountService.AccountCachePrefix}{status.AccountId}"], status = await db.AccountStatuses
TimeSpan.FromMinutes(5)); .Where(e => e.AccountId == userId)
return status; .Where(e => e.ClearedAt == null || e.ClearedAt > now)
} .OrderByDescending(e => e.CreatedAt)
.FirstOrDefaultAsync();
if (isOnline) var isOnline = await GetAccountIsConnected(userId);
{ if (status is not null)
return new SnAccountStatus
{ {
Attitude = Shared.Models.StatusAttitude.Neutral, status.IsOnline = !status.IsInvisible && isOnline;
IsOnline = true, await cache.SetWithGroupsAsync(cacheKey, status, [$"{AccountService.AccountCachePrefix}{status.AccountId}"],
IsCustomized = false, TimeSpan.FromMinutes(5));
Label = "Online", }
AccountId = userId, else
}; {
if (isOnline)
{
status = new SnAccountStatus
{
Attitude = Shared.Models.StatusAttitude.Neutral,
IsOnline = true,
IsCustomized = false,
Label = "Online",
AccountId = userId,
};
}
else
{
status = new SnAccountStatus
{
Attitude = Shared.Models.StatusAttitude.Neutral,
IsOnline = false,
IsCustomized = false,
Label = "Offline",
AccountId = userId,
};
}
}
} }
return new SnAccountStatus await cache.SetAsync($"{PreviousStatusCacheKey}{userId}", status, TimeSpan.FromMinutes(5));
{
Attitude = Shared.Models.StatusAttitude.Neutral, return status;
IsOnline = false,
IsCustomized = false,
Label = "Offline",
AccountId = userId,
};
} }
public async Task<Dictionary<Guid, SnAccountStatus>> GetStatuses(List<Guid> userIds) public async Task<Dictionary<Guid, SnAccountStatus>> GetStatuses(List<Guid> userIds)

View File

@@ -1,6 +1,7 @@
using System.Text.Json; using System.Text.Json;
using DysonNetwork.Pass.Account; using DysonNetwork.Pass.Account;
using DysonNetwork.Pass.Wallet; using DysonNetwork.Pass.Wallet;
using DysonNetwork.Shared.Cache;
using DysonNetwork.Shared.Models; using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto; using DysonNetwork.Shared.Proto;
using DysonNetwork.Shared.Queue; using DysonNetwork.Shared.Queue;
@@ -18,6 +19,16 @@ public class BroadcastEventHandler(
IServiceProvider serviceProvider IServiceProvider serviceProvider
) : BackgroundService ) : BackgroundService
{ {
private static bool StatusesEqual(SnAccountStatus a, SnAccountStatus b)
{
return a.Attitude == b.Attitude &&
a.IsOnline == b.IsOnline &&
a.IsCustomized == b.IsCustomized &&
a.Label == b.Label &&
a.IsInvisible == b.IsInvisible &&
a.IsNotDisturb == b.IsNotDisturb;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
var paymentTask = HandlePaymentEventsAsync(stoppingToken); var paymentTask = HandlePaymentEventsAsync(stoppingToken);
@@ -159,26 +170,33 @@ public class BroadcastEventHandler(
{ {
var evt = var evt =
GrpcTypeHelper.ConvertByteStringToObject<WebSocketConnectedEvent>(ByteString.CopyFrom(msg.Data)); GrpcTypeHelper.ConvertByteStringToObject<WebSocketConnectedEvent>(ByteString.CopyFrom(msg.Data));
if (evt is null) continue;
logger.LogInformation("Received WebSocket connected event for user {AccountId}, device {DeviceId}", logger.LogInformation("Received WebSocket connected event for user {AccountId}, device {DeviceId}",
evt.AccountId, evt.DeviceId); evt.AccountId, evt.DeviceId);
await using var scope = serviceProvider.CreateAsyncScope(); await using var scope = serviceProvider.CreateAsyncScope();
var accountEventService = scope.ServiceProvider.GetRequiredService<AccountEventService>(); var accountEventService = scope.ServiceProvider.GetRequiredService<AccountEventService>();
var cache = scope.ServiceProvider.GetRequiredService<ICacheService>();
var previous = await cache.GetAsync<SnAccountStatus>($"account:status:prev:{evt.AccountId}");
var status = await accountEventService.GetStatus(evt.AccountId); var status = await accountEventService.GetStatus(evt.AccountId);
await nats.PublishAsync( if (previous != null && !StatusesEqual(previous, status))
AccountStatusUpdatedEvent.Type, {
ByteString.CopyFromUtf8(JsonSerializer.Serialize(new AccountStatusUpdatedEvent await nats.PublishAsync(
{ AccountStatusUpdatedEvent.Type,
AccountId = evt.AccountId, ByteString.CopyFromUtf8(JsonSerializer.Serialize(new AccountStatusUpdatedEvent
Status = status, {
UpdatedAt = SystemClock.Instance.GetCurrentInstant() AccountId = evt.AccountId,
}, GrpcTypeHelper.SerializerOptionsWithoutIgnore)).ToByteArray() Status = status,
); UpdatedAt = SystemClock.Instance.GetCurrentInstant()
}, GrpcTypeHelper.SerializerOptionsWithoutIgnore)).ToByteArray(),
cancellationToken: stoppingToken
);
}
logger.LogInformation("Broadcasted status update for user {AccountId}", evt.AccountId); logger.LogInformation("Handled status update for user {AccountId} on connect", evt.AccountId);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -204,20 +222,25 @@ public class BroadcastEventHandler(
await using var scope = serviceProvider.CreateAsyncScope(); await using var scope = serviceProvider.CreateAsyncScope();
var accountEventService = scope.ServiceProvider.GetRequiredService<AccountEventService>(); var accountEventService = scope.ServiceProvider.GetRequiredService<AccountEventService>();
var cache = scope.ServiceProvider.GetRequiredService<ICacheService>();
var previous = await cache.GetAsync<SnAccountStatus>($"account:status:prev:{evt.AccountId}");
var status = await accountEventService.GetStatus(evt.AccountId); var status = await accountEventService.GetStatus(evt.AccountId);
await nats.PublishAsync( if (previous != null && !StatusesEqual(previous, status))
AccountStatusUpdatedEvent.Type, {
ByteString.CopyFromUtf8(JsonSerializer.Serialize(new AccountStatusUpdatedEvent await nats.PublishAsync(
{ AccountStatusUpdatedEvent.Type,
AccountId = evt.AccountId, ByteString.CopyFromUtf8(JsonSerializer.Serialize(new AccountStatusUpdatedEvent
Status = status, {
UpdatedAt = SystemClock.Instance.GetCurrentInstant() AccountId = evt.AccountId,
}, GrpcTypeHelper.SerializerOptionsWithoutIgnore)).ToByteArray() Status = status,
); UpdatedAt = SystemClock.Instance.GetCurrentInstant()
}, GrpcTypeHelper.SerializerOptionsWithoutIgnore)).ToByteArray()
);
}
logger.LogInformation("Broadcasted status update for user {AccountId}", evt.AccountId); logger.LogInformation("Handled status update for user {AccountId} on disconnect", evt.AccountId);
} }
catch (Exception ex) catch (Exception ex)
{ {