This commit is contained in:
ponzischeme89
2026-08-12 08:25:15 +12:00
parent e30962245e
commit 4b31946635
28 changed files with 439 additions and 130 deletions
+47 -3
View File
@@ -58,12 +58,16 @@ func (s *Server) scanSonarrLifecycle(ctx context.Context) error {
return err
}
cancellations := make([]store.SonarrSeriesStatusChange, 0, len(changes))
additions := make([]store.SonarrSeriesStatusChange, 0, len(changes))
for _, change := range changes {
if sonarrBecameCancelled(change.PreviousStatus, change.Current.Status) {
switch sonarrLifecycleNotificationKind(change.PreviousStatus, change.Current.Status) {
case "show-added":
additions = append(additions, change)
case "show-cancelled":
cancellations = append(cancellations, change)
}
}
if len(cancellations) == 0 {
if len(cancellations) == 0 && len(additions) == 0 {
s.log.Info("Sonarr lifecycle scan complete", "series", len(observations), "changes", len(changes))
return nil
}
@@ -75,6 +79,36 @@ func (s *Server) scanSonarrLifecycle(ctx context.Context) error {
preferences := map[string]store.NotificationPreferences{}
preferenceErrors := map[string]bool{}
notifications := 0
for _, change := range additions {
for _, user := range users {
prefs, ok := preferences[user.ID]
if !ok && !preferenceErrors[user.ID] {
prefs, err = s.store.NotificationPreferences(ctx, user.ID)
if err != nil {
preferenceErrors[user.ID] = true
s.log.Warn("notification preferences unavailable during Sonarr lifecycle scan",
"user", user.ID, "error", err)
continue
}
preferences[user.ID] = prefs
}
if preferenceErrors[user.ID] || !prefs.Enabled {
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)
continue
}
notifications++
}
}
for _, change := range cancellations {
for _, user := range users {
prefs, ok := preferences[user.ID]
@@ -107,10 +141,20 @@ func (s *Server) scanSonarrLifecycle(ctx context.Context) error {
}
s.log.Info("Sonarr lifecycle scan complete",
"series", len(observations), "changes", len(changes),
"cancelled", len(cancellations), "notifications", notifications)
"added", len(additions), "cancelled", len(cancellations), "notifications", notifications)
return nil
}
func sonarrLifecycleNotificationKind(previous, current string) string {
if strings.TrimSpace(previous) == "" {
return "show-added"
}
if sonarrBecameCancelled(previous, current) {
return "show-cancelled"
}
return ""
}
func sonarrSeriesStatusKey(series sonarr.Series) string {
if series.TVDBID > 0 {
return "tvdb:" + strconv.Itoa(series.TVDBID)