Files
Gitea/services/websocket/notification.go
T
silverwind b60da6d876 enhance(websocket): improve client synchronization (#38847) (#40340)
Backport of https://github.com/go-gitea/gitea/pull/38847, which fixes
the stopwatch push flake in
https://github.com/go-gitea/gitea/actions/runs/43200377380/job/131122930118.

The stopwatch e2e test keeps its v28 flow, since main reworked it in a
separate change.

Assisted-by: Claude Code:claude-opus-5-5
2026-11-12 14:00:15 +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})
}