* docs(markers): design + implementation plans for multi-source markers & TheIntroDB contribution * fix(markers): TheIntroDB read-path correctness (TVDB, real confidence, best candidate) Honor TVDB ids in /media lookups (previously dropped — anime/TheTVDB-first libraries got no markers), decode and use the real per-segment confidence and submission_count instead of a hardcoded 0.9, and pick the most-submitted / highest-confidence candidate when several are returned. Adds httptest coverage for the introdb client and provider. Phase 1 of docs/superpowers/plans/2026-06-06-marker-sources-and-contribution-implementation.md Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(markers): multi-source dispatch, per-provider config, per-segment provenance Add marker_provider_config (per-provider fetch enable/priority + contribute gates, contribution off by default) and a cached ProviderConfigStore. Add Registry.FetchMerged: query all fetch-enabled providers concurrently and keep the best candidate per segment (submission_count, then confidence, then fetch priority), stamping each winning marker with its provider/algorithm. Thread per-segment provenance through MarkerUpdatePayload and scanner.MarkerUpdate (additive SegmentProvenance overrides) so a merged result writes correct per-segment provider/confidence/algorithm; the legacy shared columns keep a summary. The lazy-playback path now uses FetchMerged. With only TheIntroDB enabled, behavior is unchanged. Phase 2 of docs/superpowers/plans/2026-06-06-marker-sources-and-contribution-implementation.md Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(markers): TheIntroDB submission client, contribution audit, service engine Add a markers.Submitter capability and implement it on the introdb provider (POST /v3/submit, GET /v3/user/stats; key required, usage-limit aware, applies the null start/end conventions). Add the marker_contributions audit table and a value-hash-keyed ContributionStore for idempotency. Add ContributionService: resolves enabled submitter providers, gates eligibility (never re-submit online-sourced markers; auto runs require contribute_auto_local + scanner-intro above the per-provider confidence threshold), checks idempotency, submits, and records. Wired in main.go; no trigger yet (admin API and task follow). Phase 3 of docs/superpowers/plans/2026-06-06-marker-sources-and-contribution-implementation.md Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(api): admin marker editing, contribution, and provider config endpoints Add the RequireAdmin marker API: GET/PUT /admin/files/{id}/markers (read with provenance; manual upsert where a segment object sets and null clears), DELETE .../markers/{segment}, POST .../contribute and GET .../contributions, plus GET/PUT /admin/markers/providers[/{provider}] and a .../validate key-check returning user stats. Manual writes go through the priority-gated UpsertMarkers (source=manual) and notify live sessions; a new FileRepository.ClearMarkers nulls a segment's columns. Validation mirrors the contribution rules. Phase 4 of docs/superpowers/plans/2026-06-06-marker-sources-and-contribution-implementation.md Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(markers): daily auto-contribution task for local intro markers Add ContributeMarkersTask (daily 04:00, after local detection): when a provider has contribute_enabled + contribute_auto_local, page through episode files with a scanner intro marker at/above the provider's confidence threshold (new ContributionStore.CandidateLocalIntroFiles keyset query) and run them through ContributionService with Auto=true. No-op when no provider opts in; idempotent and resumable across runs. Phase 5 of docs/superpowers/plans/2026-06-06-marker-sources-and-contribution-implementation.md Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(intromarkers): refine chromaprint starts with dialogue cues * feat(markers): finish marker management backend * feat(web): add marker editing UI * feat(markers): use plugin marker providers * fix(markers): address PR review feedback * feat(player): show marker labels on seek hover * fix(markers): type nullable marker mutation params * feat(markers): audit marker edits and add permission --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
300 lines
9.0 KiB
Go
300 lines
9.0 KiB
Go
package markers
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Silo-Server/silo-server/internal/models"
|
|
)
|
|
|
|
// OutcomeStatus values for a contribution attempt, in addition to the provider
|
|
// SubmissionStatus* values.
|
|
const (
|
|
OutcomeStatusError = "error"
|
|
OutcomeStatusRateLimited = "rate_limited"
|
|
OutcomeStatusSkipped = "skipped"
|
|
)
|
|
|
|
// ContributeOptions scopes a contribution run.
|
|
type ContributeOptions struct {
|
|
// Provider restricts the run to a single provider id ("" = all enabled).
|
|
Provider string
|
|
// Segments restricts the run to specific kinds (nil = every present segment).
|
|
Segments []MarkerKind
|
|
// Auto marks a background run: a segment is only contributed when the
|
|
// provider has contribute_auto_local enabled and the segment is a local
|
|
// (scanner) intro at or above the provider's confidence threshold.
|
|
Auto bool
|
|
}
|
|
|
|
// ContributionOutcome is the per (provider, segment) result of a run.
|
|
type ContributionOutcome struct {
|
|
Provider string
|
|
Segment MarkerKind
|
|
Status string // pending | accepted | rejected | error | rate_limited | skipped
|
|
SubmissionID string
|
|
Reason string // skip reason or error message
|
|
RetryAfter time.Duration
|
|
}
|
|
|
|
// providerConfigReader is the read surface ContributionService needs from the
|
|
// provider config store (satisfied by *ProviderConfigStore).
|
|
type providerConfigReader interface {
|
|
Get(provider string) (ProviderConfig, bool)
|
|
}
|
|
|
|
// contributionRecorder is the audit surface ContributionService needs
|
|
// (satisfied by *ContributionStore).
|
|
type contributionRecorder interface {
|
|
AlreadySubmitted(ctx context.Context, fileID int, provider, segmentKind, contentHash string) (bool, error)
|
|
Record(ctx context.Context, row ContributionRow) error
|
|
}
|
|
|
|
// ContributionService submits a file's eligible markers to enabled submitter
|
|
// providers, idempotently and audited. Both the on-demand admin path and the
|
|
// daily auto-submission task funnel through ContributeFile.
|
|
type ContributionService struct {
|
|
registry *Registry
|
|
resolver ExternalIDResolver
|
|
config providerConfigReader
|
|
store contributionRecorder
|
|
logger *slog.Logger
|
|
}
|
|
|
|
// NewContributionService constructs the service.
|
|
func NewContributionService(registry *Registry, resolver ExternalIDResolver, config providerConfigReader, store contributionRecorder, logger *slog.Logger) *ContributionService {
|
|
if logger == nil {
|
|
logger = slog.Default()
|
|
}
|
|
return &ContributionService{registry: registry, resolver: resolver, config: config, store: store, logger: logger}
|
|
}
|
|
|
|
type segmentData struct {
|
|
kind MarkerKind
|
|
name string
|
|
start *float64
|
|
end *float64
|
|
source *string
|
|
confidence *float64
|
|
}
|
|
|
|
func fileSegments(file *models.MediaFile) []segmentData {
|
|
return []segmentData{
|
|
{MarkerKindIntro, "intro", file.IntroStart, file.IntroEnd, file.IntroMarkersSource, file.IntroMarkersConfidence},
|
|
{MarkerKindCredits, "credits", file.CreditsStart, file.CreditsEnd, file.CreditsMarkersSource, file.CreditsMarkersConfidence},
|
|
{MarkerKindRecap, "recap", file.RecapStart, file.RecapEnd, file.RecapMarkersSource, file.RecapMarkersConfidence},
|
|
{MarkerKindPreview, "preview", file.PreviewStart, file.PreviewEnd, file.PreviewMarkersSource, file.PreviewMarkersConfidence},
|
|
}
|
|
}
|
|
|
|
func (s *ContributionService) submitters() []Submitter {
|
|
if s == nil || s.registry == nil {
|
|
return nil
|
|
}
|
|
var out []Submitter
|
|
for _, p := range s.registry.Providers() {
|
|
if sub, ok := p.(Submitter); ok {
|
|
out = append(out, sub)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// ContributeFile submits the file's eligible markers and returns one outcome
|
|
// per (provider, segment) attempted (including explicit skips).
|
|
func (s *ContributionService) ContributeFile(ctx context.Context, file *models.MediaFile, opts ContributeOptions) ([]ContributionOutcome, error) {
|
|
if s == nil || file == nil || s.registry == nil || s.resolver == nil || s.config == nil || s.store == nil {
|
|
return nil, nil
|
|
}
|
|
ids, err := s.resolver.ResolveForFile(ctx, file)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !ids.HasAnyID() {
|
|
return nil, nil
|
|
}
|
|
|
|
want := func(k MarkerKind) bool {
|
|
if len(opts.Segments) == 0 {
|
|
return true
|
|
}
|
|
for _, kind := range opts.Segments {
|
|
if kind == k {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
var outcomes []ContributionOutcome
|
|
for _, sub := range s.submitters() {
|
|
providerID := sub.ID()
|
|
if opts.Provider != "" && opts.Provider != providerID {
|
|
continue
|
|
}
|
|
cfg, ok := s.config.Get(providerID)
|
|
if !ok || !cfg.ContributeEnabled {
|
|
continue
|
|
}
|
|
for _, seg := range fileSegments(file) {
|
|
if !want(seg.kind) {
|
|
continue
|
|
}
|
|
if outcome, attempted := s.contributeSegment(ctx, sub, providerID, cfg, file, ids, seg, opts.Auto); attempted {
|
|
outcomes = append(outcomes, outcome)
|
|
if outcome.Status == OutcomeStatusRateLimited {
|
|
return outcomes, nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return outcomes, nil
|
|
}
|
|
|
|
func (s *ContributionService) contributeSegment(
|
|
ctx context.Context,
|
|
sub Submitter,
|
|
providerID string,
|
|
cfg ProviderConfig,
|
|
file *models.MediaFile,
|
|
ids ExternalIDs,
|
|
seg segmentData,
|
|
auto bool,
|
|
) (ContributionOutcome, bool) {
|
|
if seg.start == nil || seg.end == nil {
|
|
return ContributionOutcome{}, false
|
|
}
|
|
source := ""
|
|
if seg.source != nil {
|
|
source = *seg.source
|
|
}
|
|
// Only ever contribute our own data; never round-trip online markers.
|
|
if source != models.MarkerSourceScanner && source != models.MarkerSourceManual {
|
|
return ContributionOutcome{}, false
|
|
}
|
|
if auto {
|
|
if !cfg.ContributeAutoLocal || source != models.MarkerSourceScanner || seg.kind != MarkerKindIntro {
|
|
return ContributionOutcome{}, false
|
|
}
|
|
confidence := 0.0
|
|
if seg.confidence != nil {
|
|
confidence = *seg.confidence
|
|
}
|
|
if confidence < cfg.ContributeMinConfidence {
|
|
return ContributionOutcome{}, false
|
|
}
|
|
}
|
|
if missing := missingRequiredExternalIDs(sub, ids); len(missing) > 0 {
|
|
return ContributionOutcome{
|
|
Provider: providerID,
|
|
Segment: seg.kind,
|
|
Status: OutcomeStatusSkipped,
|
|
Reason: strings.Join(missing, ",") + " id required",
|
|
}, true
|
|
}
|
|
|
|
startMs := int64(*seg.start * 1000)
|
|
endMs := int64(*seg.end * 1000)
|
|
durMs := int64(file.Duration) * 1000
|
|
hash := ContentHash(seg.name, &startMs, &endMs, &durMs, contributionTargetParts(ids)...)
|
|
|
|
already, err := s.store.AlreadySubmitted(ctx, file.ID, providerID, seg.name, hash)
|
|
if err != nil {
|
|
return ContributionOutcome{Provider: providerID, Segment: seg.kind, Status: OutcomeStatusError, Reason: err.Error()}, true
|
|
}
|
|
if already {
|
|
return ContributionOutcome{Provider: providerID, Segment: seg.kind, Status: OutcomeStatusSkipped, Reason: "already submitted"}, true
|
|
}
|
|
|
|
startDur := time.Duration(*seg.start * float64(time.Second))
|
|
endDur := time.Duration(*seg.end * float64(time.Second))
|
|
req := SubmissionRequest{
|
|
Kind: ids.Kind,
|
|
ExternalIDs: ids.AsRequestMap(),
|
|
SeasonNumber: ids.SeasonNumber,
|
|
EpisodeNumber: ids.EpisodeNumber,
|
|
Segment: seg.kind,
|
|
Start: &startDur,
|
|
End: &endDur,
|
|
Duration: time.Duration(file.Duration) * time.Second,
|
|
}
|
|
|
|
row := ContributionRow{
|
|
MediaFileID: file.ID,
|
|
Provider: providerID,
|
|
SegmentKind: seg.name,
|
|
Source: source,
|
|
SubmittedStartMs: &startMs,
|
|
SubmittedEndMs: &endMs,
|
|
VideoDurationMs: &durMs,
|
|
ContentHash: hash,
|
|
}
|
|
|
|
result, err := sub.SubmitMarker(ctx, req)
|
|
if err != nil {
|
|
msg := err.Error()
|
|
row.Status = OutcomeStatusError
|
|
row.Error = &msg
|
|
if recErr := s.store.Record(ctx, row); recErr != nil {
|
|
s.logger.Warn("record contribution error failed", "file_id", file.ID, "provider", providerID, "segment", seg.name, "error", recErr)
|
|
}
|
|
if after, ok := RetryAfter(err); ok {
|
|
return ContributionOutcome{Provider: providerID, Segment: seg.kind, Status: OutcomeStatusRateLimited, Reason: msg, RetryAfter: after}, true
|
|
}
|
|
return ContributionOutcome{Provider: providerID, Segment: seg.kind, Status: OutcomeStatusError, Reason: msg}, true
|
|
}
|
|
|
|
row.Status = result.Status
|
|
if row.Status == "" {
|
|
row.Status = SubmissionStatusPending
|
|
}
|
|
if result.ID != "" {
|
|
id := result.ID
|
|
row.SubmissionID = &id
|
|
}
|
|
if err := s.store.Record(ctx, row); err != nil {
|
|
s.logger.Warn("record contribution failed", "file_id", file.ID, "provider", providerID, "segment", seg.name, "error", err)
|
|
}
|
|
return ContributionOutcome{Provider: providerID, Segment: seg.kind, Status: row.Status, SubmissionID: result.ID}, true
|
|
}
|
|
|
|
func contributionTargetParts(ids ExternalIDs) []string {
|
|
return []string{
|
|
itemTypeName(ids.Kind),
|
|
ids.TmdbID,
|
|
ids.ImdbID,
|
|
ids.TvdbID,
|
|
intPart(ids.SeasonNumber),
|
|
intPart(ids.EpisodeNumber),
|
|
}
|
|
}
|
|
|
|
func intPart(v int) string {
|
|
if v == 0 {
|
|
return ""
|
|
}
|
|
return strconv.Itoa(v)
|
|
}
|
|
|
|
func missingRequiredExternalIDs(sub Submitter, ids ExternalIDs) []string {
|
|
reqProvider, ok := sub.(SubmissionRequirementProvider)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
reqs := reqProvider.SubmissionRequirements()
|
|
if len(reqs.RequiredExternalIDs) == 0 {
|
|
return nil
|
|
}
|
|
values := ids.AsRequestMap()
|
|
var missing []string
|
|
for _, key := range reqs.RequiredExternalIDs {
|
|
if strings.TrimSpace(values[key]) == "" {
|
|
missing = append(missing, key)
|
|
}
|
|
}
|
|
return missing
|
|
}
|