mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-30 18:04:00 +00:00
Keep notification counts and stopwatches in sync when tabs open or reconnect, closing the gap behind [this flake](https://github.com/go-gitea/gitea/actions/runs/31312525323/job/93242429394). 1. Send current notification counts and stopwatch state on each WebSocket connection, replacing the separate state fetches tabs made on every connect. 2. Tabs joining an already open connection fetch current state once, since its snapshot went out before they attached. 3. Send events queued during the handshake before the snapshot, so older events can't roll it back. 4. Wait for the Redis SUBSCRIBE acknowledgement, so the snapshot read after subscribing can't miss an event.
33 lines
997 B
Go
33 lines
997 B
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package websocket
|
|
|
|
import (
|
|
"context"
|
|
|
|
activities_model "gitea.dev/models/activities"
|
|
"gitea.dev/models/db"
|
|
"gitea.dev/modules/log"
|
|
)
|
|
|
|
const eventNotificationCount = "notification-count" // keep in sync with web_src/js/types.ts
|
|
|
|
func (n *wsNotifier) NotificationCountChange(ctx context.Context, userID int64) {
|
|
publishUserEvent(userID, func() []byte { return notificationCountEvent(ctx, userID) })
|
|
}
|
|
|
|
func notificationCountEvent(ctx context.Context, userID int64) []byte {
|
|
count, err := db.Count[activities_model.Notification](ctx, activities_model.FindNotificationOptions{
|
|
UserID: userID,
|
|
Status: []activities_model.NotificationStatus{activities_model.NotificationStatusUnread},
|
|
})
|
|
if err != nil {
|
|
log.Error("websocket: count notifications for user %d: %v", userID, err)
|
|
return nil
|
|
}
|
|
return makeEventMessage(eventNotificationCount, "", struct {
|
|
Count int64 `json:"count"`
|
|
}{count})
|
|
}
|