Files
minstrel/internal/library/notify.go
T
bvandeusenandClaude Opus 5.5 e1f270362d
release / go (push) Successful in 2m7s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m30s
release / android (push) Successful in 4m39s
release / Build signed APK (push) Successful in 5m10s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
chore(release): everything that ships points at GitHub and GHCR (#7897)
- Module path: github.com/Fabled-Sword/minstrel, in go.mod, every
  import and the Dockerfile's version ldflags.
- buildinfo.ProjectURL, the contact outside services see in Minstrel's
  User-Agent: https://github.com/Fabled-Sword/minstrel.
- README's compose example pulls ghcr.io/fabled-sword/minstrel:latest.
- docs/security.md sends problem reports to the GitHub issues.
- The demo-library stack builds Minstrel from the checkout, since :dev
  is not published to GHCR.
- hygiene.go's same-site example uses example.com names.

The workbench CI (.gitea/, ci/, export-ignored) still builds and
publishes :dev to the private registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 19:28:16 -05:00

119 lines
3.7 KiB
Go

package library
import (
"context"
"sync"
"github.com/jackc/pgx/v5/pgtype"
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
"github.com/Fabled-Sword/minstrel/internal/notifications"
)
// Package-level notifier for library-health notices to admins: a
// scan that failed, tracks gone missing, new duplicates. Set once at startup
// beside SetEventBus, for the same reason the bus is package-level. Nil, as in
// tests that never set it, means nobody is told.
var (
notifierMu sync.RWMutex
notifier *notifications.Notifier
)
// SetNotifier wires the notifications inbox into the library package.
func SetNotifier(n *notifications.Notifier) {
notifierMu.Lock()
defer notifierMu.Unlock()
notifier = n
}
// notifyAdmins tells every admin about a library-health event. These kinds
// coalesce, so a burst is one unread notice with a running count.
func notifyAdmins(ctx context.Context, kind notifications.Kind, p notifications.Payload) {
notifierMu.RLock()
n := notifier
notifierMu.RUnlock()
n.NotifyLogged(ctx, kind, notifications.ToAdmins(pgtype.UUID{}), p.Map())
}
// notifyItems is how many names a notice carries; the notifications package
// keeps the newest ten across a coalesced burst.
const notifyItems = 10
// announceNewAlbums claims the albums no scan has announced yet and tells
// every user about the ones that are new music. Called when a scan or a
// watcher batch has finished adding files, so an album is announced whole.
// The first import into an empty library is claimed without a notice.
func (s *Scanner) announceNewAlbums(ctx context.Context) {
if ctx.Err() != nil {
return
}
q := dbq.New(s.pool)
announcedBefore, err := q.AnyAlbumAnnounced(ctx)
if err != nil {
s.logger.Warn("library scan: reading announced albums failed", "err", err)
return
}
rows, err := q.ClaimUnannouncedAlbums(ctx)
if err != nil {
s.logger.Warn("library scan: claiming new albums failed", "err", err)
return
}
if !announcedBefore {
return
}
var p notifications.Payload
for _, r := range rows {
if !r.Arrived {
continue
}
p.Count++
if len(p.Albums) < notifications.AlbumsKept {
p.Albums = append(p.Albums, notifications.Arrival{
Artist: r.ArtistName,
Title: r.Title,
ArtistID: r.ArtistID.String(),
AlbumID: r.ID.String(),
})
}
}
if p.Count == 0 {
return
}
notifierMu.RLock()
n := notifier
notifierMu.RUnlock()
n.NotifyLogged(ctx, notifications.KindNewMusic, notifications.ToEveryone(), p.Map())
}
// notifyStillMissing tells admins which of the tracks a scan marked missing
// are still gone after it adopted moves and folded replacements. A marked
// track the scan healed is not news: reporting it would make a quality
// upgrade read as lost music.
func (s *Scanner) notifyStillMissing(ctx context.Context, marked []pgtype.UUID) {
if len(marked) == 0 || ctx.Err() != nil {
return
}
rows, err := dbq.New(s.pool).ListStillMissingTrackLabels(ctx, marked)
if err != nil {
s.logger.Warn("library scan: listing missing tracks for the notice failed", "err", err)
return
}
if len(rows) == 0 {
return
}
items := make([]string, 0, min(len(rows), notifyItems))
for _, r := range rows[:min(len(rows), notifyItems)] {
items = append(items, notifications.TrackLabel(r.ArtistName, r.Title))
}
notifyAdmins(ctx, notifications.KindTracksMissing, notifications.Payload{Count: int64(len(rows)), Items: items})
}
// notifyScanFinished tells admins a scan run ended in error. A scan cut short
// by shutdown is not a failure anyone needs telling about.
func notifyScanFinished(ctx context.Context, errMsg string) {
if errMsg == "" || ctx.Err() != nil {
return
}
notifyAdmins(ctx, notifications.KindScanFailed, notifications.Payload{Count: 1, Detail: errMsg})
}