This commit is contained in:
ponzischeme89
2026-08-19 06:57:59 +12:00
parent 8c847c59b8
commit 2b43b9ef12
94 changed files with 6359 additions and 778 deletions
+51 -22
View File
@@ -7,6 +7,7 @@ import (
"strings"
"time"
"github.com/ponzischeme89/memby/server/internal/notify"
"github.com/ponzischeme89/memby/server/internal/sonarr"
"github.com/ponzischeme89/memby/server/internal/store"
)
@@ -95,21 +96,26 @@ func (s *Server) scanSonarrLifecycle(ctx context.Context) error {
}
preferences[user.ID] = prefs
}
if preferenceErrors[user.ID] || !prefs.Enabled || !prefs.SonarrAlerts {
continue
}
eventAt := change.Current.ObservedAt
sourceKey := fmt.Sprintf("show-added:%s:%d", change.Current.SeriesKey, change.HistoryID)
message := change.Current.Title + " was added to Sonarr."
if err := s.store.UpsertNotification(
ctx, user.ID, sourceKey, "show-added", "",
"Show added", message, &eventAt,
); err != nil {
s.log.Warn("Sonarr addition notification failed",
"user", user.ID, "show", change.Current.Title, "error", err)
notification := notify.Notification{
Kind: "show-added",
Source: notifySourceSonarrLifecycle,
UserID: user.ID,
Username: user.Username,
Title: "Show added",
Body: change.Current.Title + " was added to Sonarr.",
SourceKey: sourceKey,
EventAt: &eventAt,
Metadata: map[string]any{"series": change.Current.Title, "status": change.Current.Status},
}
if preferenceErrors[user.ID] || !prefs.Enabled || !prefs.SonarrAlerts {
s.declineUser(ctx, notification, sonarrDeclineReason(prefs, preferenceErrors[user.ID]))
continue
}
notifications++
if s.notifyUser(ctx, notification) {
notifications++
}
}
}
for _, change := range cancellations {
@@ -125,21 +131,26 @@ func (s *Server) scanSonarrLifecycle(ctx context.Context) error {
}
preferences[user.ID] = prefs
}
if preferenceErrors[user.ID] || !prefs.Enabled || !prefs.SonarrAlerts {
continue
}
eventAt := change.Current.ObservedAt
sourceKey := fmt.Sprintf("show-cancelled:%s:%d", change.Current.SeriesKey, change.HistoryID)
message := change.Current.Title + " is now listed as cancelled in Sonarr."
if err := s.store.UpsertNotification(
ctx, user.ID, sourceKey, "show-cancelled", "",
"Show cancelled", message, &eventAt,
); err != nil {
s.log.Warn("Sonarr cancellation notification failed",
"user", user.ID, "show", change.Current.Title, "error", err)
notification := notify.Notification{
Kind: "show-cancelled",
Source: notifySourceSonarrLifecycle,
UserID: user.ID,
Username: user.Username,
Title: "Show cancelled",
Body: change.Current.Title + " is now listed as cancelled in Sonarr.",
SourceKey: sourceKey,
EventAt: &eventAt,
Metadata: map[string]any{"series": change.Current.Title, "status": change.Current.Status},
}
if preferenceErrors[user.ID] || !prefs.Enabled || !prefs.SonarrAlerts {
s.declineUser(ctx, notification, sonarrDeclineReason(prefs, preferenceErrors[user.ID]))
continue
}
notifications++
if s.notifyUser(ctx, notification) {
notifications++
}
}
}
s.log.Info("Sonarr lifecycle scan complete",
@@ -176,3 +187,21 @@ func sonarrBecameCancelled(previous, current string) bool {
current == "cancelled" || current == "canceled"
return active && cancelled
}
// sonarrDeclineReason is the sentence the console prints beside a skipped row.
//
// The three refusals are genuinely different answers to "why was I not told", and a page
// that collapsed them into "skipped" would send an operator to change a setting that was
// never the problem. A preference that would not load is its own case: it is read as "not
// now" rather than as consent, and that is a fact about the gateway rather than about the
// viewer.
func sonarrDeclineReason(prefs store.NotificationPreferences, unreadable bool) string {
switch {
case unreadable:
return "this viewer's notification preferences could not be read"
case !prefs.Enabled:
return "this viewer has notifications switched off"
default:
return "this viewer has Sonarr alerts switched off"
}
}