This commit is contained in:
ponzischeme89
2026-08-19 21:30:44 +12:00
parent 0782545013
commit 769fe01c84
24 changed files with 1131 additions and 500 deletions
+66
View File
@@ -229,6 +229,72 @@ func (s *Server) syncReturnNotifications(
}
}
// clearNotificationsResponse says how many of this viewer's notifications the gateway
// actually cleared. The television prints the figure back as its confirmation, so it must be
// what happened rather than what was asked for.
type clearNotificationsResponse struct {
Cleared int `json:"cleared"`
}
// handleClearNotifications empties one viewer's list in a single request.
//
// It exists because clearing from the user picker is a shortcut for somebody who does not
// want to go into the page at all, and a television looping the per-alert dismiss route
// could neither report a trustworthy count nor leave one line in the log an operator could
// read. The two rules worth preserving:
//
// - What it clears is what that viewer can *see*. filterStoredNotifications is what the
// list route already applies, so a summary their preferences have withdrawn is not
// quietly dismissed underneath them by a press aimed at the seven alerts on screen —
// and the count agrees with the badge that was showing.
// - Nothing to clear is a success, not an error. It answers 0 and says so, because the
// television disables the action on an empty list and a race with another set finishing
// the job first is not a failure anybody should be shown.
//
// clearableNotificationIDs is the rows a clear-all press may take: exactly the ones the
// list route would have shown this viewer, and nothing their preferences have withdrawn.
//
// Pure and separate from the handler so the one rule that matters here — a press aimed at
// what is on screen never reaches past it — is pinned by a test rather than by a database.
func clearableNotificationIDs(
notifications []store.UserNotification, prefs store.NotificationPreferences,
) []int64 {
visible := filterStoredNotifications(notifications, prefs)
ids := make([]int64, 0, len(visible))
for _, notification := range visible {
ids = append(ids, notification.ID)
}
return ids
}
func (s *Server) handleClearNotifications(
w http.ResponseWriter, r *http.Request, sess store.Session,
) {
log := s.loggerFor(r.Context())
prefs, err := s.notificationPreferencesFor(r.Context(), sess.EmbyUserID)
if err != nil {
log.Warn("notifications not cleared", "reason", "preferences unavailable", "error", err)
writeError(w, http.StatusInternalServerError, "could not load notification preferences")
return
}
notifications, err := s.store.UserNotifications(r.Context(), sess.EmbyUserID)
if err != nil {
log.Warn("notifications not cleared", "reason", "list unavailable", "error", err)
writeError(w, http.StatusInternalServerError, "could not load notifications")
return
}
ids := clearableNotificationIDs(notifications, prefs)
cleared, err := s.store.DismissNotifications(r.Context(), sess.EmbyUserID, ids)
if err != nil {
log.Warn("notifications not cleared", "reason", "write failed",
"requested", len(ids), "error", err)
writeError(w, http.StatusInternalServerError, "could not clear notifications")
return
}
log.Info("notifications cleared", "cleared", cleared, "source", "user-switcher")
writeJSON(w, http.StatusOK, clearNotificationsResponse{Cleared: cleared})
}
func (s *Server) handleNotificationAction(
w http.ResponseWriter, r *http.Request, sess store.Session,
) {