Files
Gitea/services/websocket/notification.go
T
silverwind 7308c5fb97 enhance(websocket): improve client synchronization (#38847)
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.
2026-10-29 05:18:17 +00:00

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})
}