2026-06-11 14:24:52 -04:00
|
|
|
package notifications
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"log/slog"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const availabilityDetectTimeout = 2 * time.Minute
|
|
|
|
|
|
|
|
|
|
// AvailabilityDetector turns completed ingest runs into episode_availability
|
|
|
|
|
// facts and release events. It runs after matching/reconcile is complete so
|
|
|
|
|
// a release is tied to an actual resolved episode, and it never blocks or
|
|
|
|
|
// fails the ingest itself.
|
|
|
|
|
type AvailabilityDetector struct {
|
|
|
|
|
releases *ReleaseRepository
|
|
|
|
|
settings *Settings
|
|
|
|
|
logger *slog.Logger
|
|
|
|
|
// nudge wakes the fanout worker after new release events land; may be nil.
|
|
|
|
|
nudge func()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewAvailabilityDetector creates an AvailabilityDetector.
|
|
|
|
|
func NewAvailabilityDetector(releases *ReleaseRepository, settings *Settings) *AvailabilityDetector {
|
|
|
|
|
return &AvailabilityDetector{
|
|
|
|
|
releases: releases,
|
|
|
|
|
settings: settings,
|
|
|
|
|
logger: slog.Default().With("component", "notifications.availability"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetFanoutNudge wires the fanout worker wake signal.
|
|
|
|
|
func (d *AvailabilityDetector) SetFanoutNudge(nudge func()) {
|
|
|
|
|
if d != nil {
|
|
|
|
|
d.nudge = nudge
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-11 23:04:35 -04:00
|
|
|
// AvailabilityKinds selects which content kinds an ingest scope covers.
|
|
|
|
|
// Each kind keeps its own seed marker and silent-seeding semantics.
|
|
|
|
|
type AvailabilityKinds struct {
|
2026-07-02 11:42:28 -04:00
|
|
|
Episodes bool
|
|
|
|
|
Movies bool
|
|
|
|
|
Audiobooks bool
|
|
|
|
|
Ebooks bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Any reports whether at least one kind is selected.
|
|
|
|
|
func (k AvailabilityKinds) Any() bool {
|
|
|
|
|
return k.Episodes || k.Movies || k.Audiobooks || k.Ebooks
|
2026-06-11 23:04:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// availabilityKindOps abstracts the per-kind recording calls so episode and
|
|
|
|
|
// movie passes share one detection flow; seed state is kind-keyed in the
|
|
|
|
|
// repository itself.
|
|
|
|
|
type availabilityKindOps struct {
|
|
|
|
|
kind string
|
|
|
|
|
recordForLibrary func(ctx context.Context, libraryID int, emitEvents bool) (int, int, error)
|
|
|
|
|
recordForPaths func(ctx context.Context, libraryID int, scopePaths []string, emitEvents bool) (int, int, error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HandleIngestCompleted records newly available content for a completed
|
2026-06-11 14:24:52 -04:00
|
|
|
// ingest scope. fullLibrary distinguishes whole-library scans (set-based
|
|
|
|
|
// detection, and the scan that seeds a new library) from subtree/file scans
|
|
|
|
|
// (path-bounded detection).
|
|
|
|
|
//
|
|
|
|
|
// Seeding semantics: a library without a seed marker records availability
|
|
|
|
|
// silently — "newly available" means newly released to this server, not newly
|
|
|
|
|
// seen by the notifications feature. The marker is written when a full scan
|
|
|
|
|
// completes successfully, so the next scan onward emits release events.
|
2026-06-11 23:04:35 -04:00
|
|
|
func (d *AvailabilityDetector) HandleIngestCompleted(ctx context.Context, libraryID int, fullLibrary bool, scopePaths []string, kinds AvailabilityKinds) {
|
2026-06-11 14:24:52 -04:00
|
|
|
if d == nil || d.releases == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// The scan context is done once the scan finishes; detection runs on its
|
|
|
|
|
// own deadline so cancellation of the parent does not drop availability.
|
|
|
|
|
detectCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), availabilityDetectTimeout)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
2026-06-11 23:04:35 -04:00
|
|
|
if kinds.Episodes {
|
|
|
|
|
d.runKind(detectCtx, libraryID, fullLibrary, scopePaths, availabilityKindOps{
|
|
|
|
|
kind: EventKindEpisode,
|
|
|
|
|
recordForLibrary: d.releases.RecordAvailabilityForLibrary,
|
|
|
|
|
recordForPaths: d.releases.RecordAvailabilityForPaths,
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-07-02 11:42:28 -04:00
|
|
|
for _, k := range flatItemKinds {
|
|
|
|
|
if k.Selected(kinds) {
|
|
|
|
|
d.runKind(detectCtx, libraryID, fullLibrary, scopePaths, d.flatKindOps(k))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// flatKindOps binds one flat item kind's registry entry to the shared
|
|
|
|
|
// detection flow.
|
|
|
|
|
func (d *AvailabilityDetector) flatKindOps(k flatItemKind) availabilityKindOps {
|
|
|
|
|
return availabilityKindOps{
|
|
|
|
|
kind: k.Kind,
|
|
|
|
|
recordForLibrary: func(ctx context.Context, libraryID int, emitEvents bool) (int, int, error) {
|
|
|
|
|
return d.releases.RecordItemAvailabilityForLibrary(ctx, k, libraryID, emitEvents)
|
|
|
|
|
},
|
|
|
|
|
recordForPaths: func(ctx context.Context, libraryID int, scopePaths []string, emitEvents bool) (int, int, error) {
|
|
|
|
|
return d.releases.RecordItemAvailabilityForPaths(ctx, k, libraryID, scopePaths, emitEvents)
|
|
|
|
|
},
|
2026-06-11 23:04:35 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *AvailabilityDetector) runKind(ctx context.Context, libraryID int, fullLibrary bool, scopePaths []string, ops availabilityKindOps) {
|
|
|
|
|
seeded, err := d.releases.IsContentSeeded(ctx, libraryID, ops.kind)
|
2026-06-11 14:24:52 -04:00
|
|
|
if err != nil {
|
2026-07-09 20:53:52 +08:00
|
|
|
d.logger.WarnContext(ctx, "seed state lookup failed",
|
2026-06-11 23:04:35 -04:00
|
|
|
"library_id", libraryID, "kind", ops.kind, "error", err)
|
2026-06-11 14:24:52 -04:00
|
|
|
return
|
|
|
|
|
}
|
2026-06-11 23:04:35 -04:00
|
|
|
emitEvents := seeded && d.settings.ReleaseEventsEnabled(ctx)
|
2026-06-11 14:24:52 -04:00
|
|
|
|
|
|
|
|
var inserted, events int
|
|
|
|
|
if fullLibrary {
|
2026-06-11 23:04:35 -04:00
|
|
|
inserted, events, err = ops.recordForLibrary(ctx, libraryID, emitEvents)
|
2026-06-11 14:24:52 -04:00
|
|
|
} else if seeded {
|
2026-06-11 23:04:35 -04:00
|
|
|
inserted, events, err = ops.recordForPaths(ctx, libraryID, scopePaths, emitEvents)
|
2026-06-11 14:24:52 -04:00
|
|
|
} else {
|
|
|
|
|
// Subtree/file ingest on an unseeded library: record silently but do
|
|
|
|
|
// not seed-mark — only a successful full scan proves the back catalog
|
|
|
|
|
// has been captured.
|
2026-06-11 23:04:35 -04:00
|
|
|
inserted, events, err = ops.recordForPaths(ctx, libraryID, scopePaths, false)
|
2026-06-11 14:24:52 -04:00
|
|
|
}
|
|
|
|
|
if err != nil {
|
2026-07-09 20:53:52 +08:00
|
|
|
d.logger.WarnContext(ctx, "availability detection failed",
|
2026-06-11 23:04:35 -04:00
|
|
|
"library_id", libraryID, "kind", ops.kind, "error", err)
|
2026-06-11 14:24:52 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if fullLibrary && !seeded {
|
2026-06-11 23:04:35 -04:00
|
|
|
if err := d.releases.MarkContentSeeded(ctx, libraryID, ops.kind); err != nil {
|
2026-07-09 20:53:52 +08:00
|
|
|
d.logger.WarnContext(ctx, "seed marker write failed",
|
2026-06-11 23:04:35 -04:00
|
|
|
"library_id", libraryID, "kind", ops.kind, "error", err)
|
2026-06-11 14:24:52 -04:00
|
|
|
} else {
|
2026-07-09 20:53:52 +08:00
|
|
|
d.logger.InfoContext(ctx, "library availability seeded",
|
2026-06-11 23:04:35 -04:00
|
|
|
"library_id", libraryID, "kind", ops.kind, "availability_rows", inserted)
|
2026-06-11 14:24:52 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if inserted > 0 || events > 0 {
|
2026-07-09 20:53:52 +08:00
|
|
|
d.logger.InfoContext(ctx, "availability recorded",
|
2026-06-11 14:24:52 -04:00
|
|
|
"library_id", libraryID,
|
2026-06-11 23:04:35 -04:00
|
|
|
"kind", ops.kind,
|
2026-06-11 14:24:52 -04:00
|
|
|
"full_library", fullLibrary,
|
|
|
|
|
"availability_rows", inserted,
|
|
|
|
|
"release_events", events,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
if events > 0 && d.nudge != nil {
|
|
|
|
|
d.nudge()
|
|
|
|
|
}
|
|
|
|
|
}
|