feat: new-music notices come from the scanner; request notices and the original specs go
release / web (push) Failing after 1m29s
release / go (push) Successful in 1m55s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m22s
release / android (push) Successful in 4m57s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / Build signed APK (push) Canceled after 4m23s
release / web (push) Failing after 1m29s
release / go (push) Successful in 1m55s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m22s
release / android (push) Successful in 4m57s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / Build signed APK (push) Canceled after 4m23s
Request notifications (approved, declined, completed, pending) are removed
along with the request flow that produced them. A new_music kind takes the
"new music arrived" slot: when a scan or a watcher batch finishes, the
scanner claims every album not yet announced (albums.announced_at) and tells
every user about the ones that are new: an album with a track on disk and
no track older than itself. The first import into an empty library, and an
album formed only by retagging tracks already held, announce nothing. One
notice per scan, carrying up to 50 albums and the total; it is never
coalesced, so a notice already emailed can't quietly absorb later arrivals.
It feeds the daily summary email, grouped by artist. Defaults: inbox and
email on, phone off.
Migration 0103 deletes request-kind notification rows and prefs, swaps the
kind CHECKs to new_music, and adds albums.announced_at with every album
held marked announced, so the first scan after it announces nothing old.
Web and Android: labels, the Android "Requests" settings section becomes
"New music", the shade channel your_requests becomes new_music (the old one
is deleted at startup), and "/" opens Home in the app. TrackLabel moves to
the notifications package. docs/smart-music-{server,client}-spec.md are
deleted per the operator; an Android comment loses the word "exchanges".
Grimoire #7666, #7671.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -208,7 +208,7 @@ func ResolveDuplicatesWith(
|
||||
merged[g.id] = true
|
||||
res.Merged++
|
||||
if len(mergedNames) < notifyItems {
|
||||
mergedNames = append(mergedNames, TrackLabel(g.members[0].ArtistName, g.members[0].Title))
|
||||
mergedNames = append(mergedNames, notifications.TrackLabel(g.members[0].ArtistName, g.members[0].Title))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,7 +278,7 @@ func foldAlbumCopies(
|
||||
gone[i] = true
|
||||
n++
|
||||
if len(names) < notifyItems {
|
||||
names = append(names, TrackLabel(g.members[i].ArtistName, g.members[i].Title))
|
||||
names = append(names, notifications.TrackLabel(g.members[i].ArtistName, g.members[i].Title))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,7 +239,7 @@ func settleWrongSlots(
|
||||
}
|
||||
settled[g.id] = true
|
||||
for _, i := range losers {
|
||||
names = append(names, TrackLabel(g.members[i].ArtistName, g.members[i].Title))
|
||||
names = append(names, notifications.TrackLabel(g.members[i].ArtistName, g.members[i].Title))
|
||||
}
|
||||
}
|
||||
return removed, names
|
||||
@@ -563,7 +563,7 @@ func notifyNewForReview(ctx context.Context, groups []*resolveGroup, settled map
|
||||
}
|
||||
count++
|
||||
if len(items) < notifyItems {
|
||||
items = append(items, TrackLabel(g.members[0].ArtistName, g.members[0].Title))
|
||||
items = append(items, notifications.TrackLabel(g.members[0].ArtistName, g.members[0].Title))
|
||||
}
|
||||
}
|
||||
if count > 0 {
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/notifications"
|
||||
)
|
||||
|
||||
// newMusicNotices returns user's unread new_music notices' payloads.
|
||||
func newMusicNotices(t *testing.T, q *dbq.Queries, user dbq.User) []notifications.Payload {
|
||||
t.Helper()
|
||||
rows, err := q.ListNotifications(context.Background(), dbq.ListNotificationsParams{UserID: user.ID, PageLimit: 50})
|
||||
if err != nil {
|
||||
t.Fatalf("list notifications: %v", err)
|
||||
}
|
||||
var out []notifications.Payload
|
||||
for _, r := range rows {
|
||||
if r.ReadAt.Valid || r.Kind != string(notifications.KindNewMusic) {
|
||||
continue
|
||||
}
|
||||
var p notifications.Payload
|
||||
if err := json.Unmarshal(r.Payload, &p); err != nil {
|
||||
t.Fatalf("payload: %v", err)
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Every user hears about an album new to the library, once. The first import
|
||||
// into an empty library is the library being set up, and an album that only
|
||||
// formed because tracks already held were retagged into it is not new music.
|
||||
func TestAnnounceNewAlbums_TellsEveryoneOfEachNewAlbumOnce(t *testing.T) {
|
||||
pool := newPool(t)
|
||||
admin := notifyingAdmin(t, pool)
|
||||
ctx := context.Background()
|
||||
q := dbq.New(pool)
|
||||
listener, err := q.CreateUser(ctx, dbq.CreateUserParams{
|
||||
Username: dbtest.TestUserPrefix + "listener", PasswordHash: "x", ApiTokenHash: "listener",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := New(pool, slog.New(slog.NewTextHandler(io.Discard, nil)), nil, nil)
|
||||
dir := t.TempDir()
|
||||
|
||||
// First import: claimed, not announced.
|
||||
held, _, artist := seedTrack(t, pool, filepath.Join(dir, "a.mp3"))
|
||||
s.announceNewAlbums(ctx)
|
||||
if got := newMusicNotices(t, q, admin); len(got) != 0 {
|
||||
t.Fatalf("the first import was announced: %+v", got)
|
||||
}
|
||||
|
||||
// A new album with its own new track.
|
||||
arrived, err := q.UpsertAlbum(ctx, dbq.UpsertAlbumParams{Title: "Geogaddi", SortTitle: "Geogaddi", ArtistID: artist.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{
|
||||
Title: "Music Is Math", AlbumID: arrived.ID, ArtistID: artist.ID,
|
||||
DurationMs: 1000, FilePath: filepath.Join(dir, "b.mp3"), FileSize: 100, FileFormat: "mp3",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// And an album formed by retagging a track the library already held.
|
||||
retagged, err := q.UpsertAlbum(ctx, dbq.UpsertAlbumParams{Title: "Retitled", SortTitle: "Retitled", ArtistID: artist.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `UPDATE tracks SET album_id = $2 WHERE id = $1`, held.ID, retagged.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
s.announceNewAlbums(ctx)
|
||||
for _, u := range []dbq.User{admin, listener} {
|
||||
got := newMusicNotices(t, q, u)
|
||||
if len(got) != 1 || got[0].Count != 1 || len(got[0].Albums) != 1 ||
|
||||
got[0].Albums[0].Title != "Geogaddi" || got[0].Albums[0].AlbumID != arrived.ID.String() ||
|
||||
got[0].Albums[0].Artist != "Delete Test Artist" {
|
||||
t.Fatalf("%s's notices = %+v, want one naming Geogaddi alone", u.Username, got)
|
||||
}
|
||||
}
|
||||
|
||||
// Nothing new since: nothing more to say.
|
||||
s.announceNewAlbums(ctx)
|
||||
if got := newMusicNotices(t, q, listener); len(got) != 1 {
|
||||
t.Fatalf("an announced album was announced again: %+v", got)
|
||||
}
|
||||
}
|
||||
@@ -39,13 +39,50 @@ func notifyAdmins(ctx context.Context, kind notifications.Kind, p notifications.
|
||||
// keeps the newest ten across a coalesced burst.
|
||||
const notifyItems = 10
|
||||
|
||||
// TrackLabel names a track as a notice does: "Artist – Title", or the title
|
||||
// alone when the artist is unknown.
|
||||
func TrackLabel(artist, title string) string {
|
||||
if artist == "" {
|
||||
return title
|
||||
// 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
|
||||
}
|
||||
return artist + " – " + title
|
||||
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
|
||||
@@ -66,7 +103,7 @@ func (s *Scanner) notifyStillMissing(ctx context.Context, marked []pgtype.UUID)
|
||||
}
|
||||
items := make([]string, 0, min(len(rows), notifyItems))
|
||||
for _, r := range rows[:min(len(rows), notifyItems)] {
|
||||
items = append(items, TrackLabel(r.ArtistName, r.Title))
|
||||
items = append(items, notifications.TrackLabel(r.ArtistName, r.Title))
|
||||
}
|
||||
notifyAdmins(ctx, notifications.KindTracksMissing, notifications.Payload{Count: int64(len(rows)), Items: items})
|
||||
}
|
||||
|
||||
@@ -213,6 +213,7 @@ func (s *Scanner) Scan(ctx context.Context, progressCb func(Stats)) (Stats, erro
|
||||
// Told only now, so a file that moved or was upgraded, which this scan
|
||||
// marked and then healed, is not reported as lost.
|
||||
s.notifyStillMissing(ctx, stats.marked)
|
||||
s.announceNewAlbums(ctx)
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
@@ -509,6 +510,9 @@ func (s *Scanner) ScanFiles(ctx context.Context, paths []string) ([]pgtype.UUID,
|
||||
s.logger.Warn("library watch: missing-pair pass failed", "err", err)
|
||||
}
|
||||
}
|
||||
if stats.Added > 0 {
|
||||
s.announceNewAlbums(ctx)
|
||||
}
|
||||
return changed, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user