feat(desktop): report current user online clients
This commit is contained in:
@@ -27,6 +27,13 @@ func desktopClientAccountsPresenceKey(clientID uuid.UUID) string {
|
||||
return "desktop:presence:client_accounts:" + clientID.String()
|
||||
}
|
||||
|
||||
func desktopUserClientsPresenceKey(tenantID, workspaceID, userID int64) string {
|
||||
return "desktop:presence:user_clients:" +
|
||||
strconv.FormatInt(tenantID, 10) + ":" +
|
||||
strconv.FormatInt(workspaceID, 10) + ":" +
|
||||
strconv.FormatInt(userID, 10)
|
||||
}
|
||||
|
||||
func markDesktopClientPresent(ctx context.Context, redis *goredis.Client, clientID uuid.UUID) {
|
||||
if redis == nil {
|
||||
return
|
||||
@@ -35,6 +42,25 @@ func markDesktopClientPresent(ctx context.Context, redis *goredis.Client, client
|
||||
_ = redis.Set(ctx, desktopClientPresenceKey(clientID), time.Now().UTC().Format(time.RFC3339), desktopClientPresenceTTL).Err()
|
||||
}
|
||||
|
||||
func markDesktopClientPresentForUser(ctx context.Context, redis *goredis.Client, clientID uuid.UUID, tenantID, workspaceID, userID int64) {
|
||||
markDesktopClientPresent(ctx, redis, clientID)
|
||||
if redis == nil || clientID == uuid.Nil || tenantID == 0 || workspaceID == 0 || userID == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
staleBefore := strconv.FormatInt(now.Add(-desktopClientPresenceTTL).UnixMilli(), 10)
|
||||
key := desktopUserClientsPresenceKey(tenantID, workspaceID, userID)
|
||||
pipe := redis.Pipeline()
|
||||
pipe.ZRemRangeByScore(ctx, key, "-inf", staleBefore)
|
||||
pipe.ZAdd(ctx, key, goredis.Z{
|
||||
Score: float64(now.UnixMilli()),
|
||||
Member: clientID.String(),
|
||||
})
|
||||
pipe.Expire(ctx, key, desktopClientPresenceTTL*4)
|
||||
_, _ = pipe.Exec(ctx)
|
||||
}
|
||||
|
||||
func clearDesktopClientPresence(ctx context.Context, redis *goredis.Client, clientID uuid.UUID) {
|
||||
if redis == nil {
|
||||
return
|
||||
@@ -43,6 +69,20 @@ func clearDesktopClientPresence(ctx context.Context, redis *goredis.Client, clie
|
||||
_ = redis.Del(ctx, desktopClientPresenceKey(clientID)).Err()
|
||||
}
|
||||
|
||||
func clearDesktopClientPresenceStateForUser(ctx context.Context, redis *goredis.Client, clientID uuid.UUID, tenantID, workspaceID, userID int64) {
|
||||
clearDesktopClientPresenceState(ctx, redis, clientID)
|
||||
if redis == nil || clientID == uuid.Nil || tenantID == 0 || workspaceID == 0 || userID == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
staleBefore := strconv.FormatInt(time.Now().UTC().Add(-desktopClientPresenceTTL).UnixMilli(), 10)
|
||||
key := desktopUserClientsPresenceKey(tenantID, workspaceID, userID)
|
||||
pipe := redis.Pipeline()
|
||||
pipe.ZRem(ctx, key, clientID.String())
|
||||
pipe.ZRemRangeByScore(ctx, key, "-inf", staleBefore)
|
||||
_, _ = pipe.Exec(ctx)
|
||||
}
|
||||
|
||||
func loadDesktopClientPresence(ctx context.Context, redis *goredis.Client, clientIDs []uuid.UUID) map[uuid.UUID]bool {
|
||||
if redis == nil || len(clientIDs) == 0 {
|
||||
return nil
|
||||
@@ -75,6 +115,22 @@ func loadDesktopClientPresence(ctx context.Context, redis *goredis.Client, clien
|
||||
return result
|
||||
}
|
||||
|
||||
func loadDesktopUserOnlineClientCount(ctx context.Context, redis *goredis.Client, tenantID, workspaceID, userID int64, now time.Time) (int, bool) {
|
||||
if redis == nil || tenantID == 0 || workspaceID == 0 || userID == 0 {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
staleBefore := strconv.FormatInt(now.UTC().Add(-desktopClientPresenceTTL).UnixMilli(), 10)
|
||||
key := desktopUserClientsPresenceKey(tenantID, workspaceID, userID)
|
||||
pipe := redis.Pipeline()
|
||||
pipe.ZRemRangeByScore(ctx, key, "-inf", staleBefore)
|
||||
countCmd := pipe.ZCard(ctx, key)
|
||||
if _, err := pipe.Exec(ctx); err != nil && err != goredis.Nil {
|
||||
return 0, false
|
||||
}
|
||||
return int(countCmd.Val()), true
|
||||
}
|
||||
|
||||
func markDesktopAccountsPresent(ctx context.Context, redis *goredis.Client, clientID uuid.UUID, accountIDs []uuid.UUID) {
|
||||
if redis == nil {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user