mirror of
https://github.com/navidrome/navidrome.git
synced 2026-07-09 17:18:45 +00:00
* fix(plugins): discard buffered scrobbles when a plugin is removed Scrobbles are buffered in the DB per service, keyed by the plugin name. When a plugin was removed (deleted from the plugins folder and detected by the sync), its pending buffer entries were left behind forever: the drain goroutine is stopped on the next scrobbler refresh, so the rows were never retried nor discarded. Worse, if a plugin with the same name was installed later, the stale entries would be drained into it - potentially a completely unrelated plugin that just reuses the name. Add a Discard(service) method to ScrobbleBufferRepository and call it from removePluginFromDB, right after the plugin record is deleted. Disabling a plugin intentionally keeps its buffered scrobbles, consistent with the buffer's purpose of surviving temporary outages, and transient unload/reload cycles during config updates are unaffected since they never delete the plugin record. * fix(plugins): don't wipe builtin scrobbler queues on plugin removal Buffer entries are keyed by service name only, and removePluginFromDB runs for any removed plugin file, so removing a plugin named e.g. lastfm.ndp - regardless of its capability - would discard the builtin Last.fm retry queue. Skip the discard when the plugin name is owned by a registered builtin scrobbler, exposed via a new scrobbler.IsBuiltinScrobbler helper. Reported by Codex review on the PR. Also drop the testBroker usage from the new removePluginFromDB spec: it is defined in manager_test.go which is excluded on Windows, breaking the Windows test build. sendPluginRefreshEvent is nil-safe, so no broker is needed.
104 lines
2.5 KiB
Go
104 lines
2.5 KiB
Go
package persistence
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
. "github.com/Masterminds/squirrel"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/model/id"
|
|
"github.com/pocketbase/dbx"
|
|
)
|
|
|
|
type scrobbleBufferRepository struct {
|
|
sqlRepository
|
|
}
|
|
|
|
type dbScrobbleBuffer struct {
|
|
dbMediaFile
|
|
*model.ScrobbleEntry `structs:",flatten"`
|
|
}
|
|
|
|
func (t *dbScrobbleBuffer) PostScan() error {
|
|
if err := t.dbMediaFile.PostScan(); err != nil {
|
|
return err
|
|
}
|
|
t.ScrobbleEntry.MediaFile = *t.dbMediaFile.MediaFile
|
|
t.ScrobbleEntry.MediaFile.ID = t.MediaFileID
|
|
return nil
|
|
}
|
|
|
|
func NewScrobbleBufferRepository(ctx context.Context, db dbx.Builder) model.ScrobbleBufferRepository {
|
|
r := &scrobbleBufferRepository{}
|
|
r.ctx = ctx
|
|
r.db = db
|
|
r.tableName = "scrobble_buffer"
|
|
return r
|
|
}
|
|
|
|
func (r *scrobbleBufferRepository) UserIDs(service string) ([]string, error) {
|
|
sql := Select().Columns("user_id").
|
|
From(r.tableName).
|
|
Where(And{
|
|
Eq{"service": service},
|
|
}).
|
|
GroupBy("user_id").
|
|
OrderBy("count(*)")
|
|
var userIds []string
|
|
err := r.queryAllSlice(sql, &userIds)
|
|
return userIds, err
|
|
}
|
|
|
|
func (r *scrobbleBufferRepository) Enqueue(service, userId, mediaFileId string, playTime time.Time) error {
|
|
ins := Insert(r.tableName).SetMap(map[string]any{
|
|
"id": id.NewRandom(),
|
|
"user_id": userId,
|
|
"service": service,
|
|
"media_file_id": mediaFileId,
|
|
"play_time": playTime,
|
|
"enqueue_time": time.Now(),
|
|
})
|
|
_, err := r.executeSQL(ins)
|
|
return err
|
|
}
|
|
|
|
func (r *scrobbleBufferRepository) Next(service string, userId string) (*model.ScrobbleEntry, error) {
|
|
// Put `s.*` last or else m.id overrides s.id
|
|
sql := Select().Columns("m.*, s.*").
|
|
From(r.tableName+" s").
|
|
LeftJoin("media_file m on m.id = s.media_file_id").
|
|
Where(And{
|
|
Eq{"service": service},
|
|
Eq{"user_id": userId},
|
|
}).
|
|
OrderBy("play_time", "s.rowid").Limit(1)
|
|
|
|
var res dbScrobbleBuffer
|
|
err := r.queryOne(sql, &res)
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
res.ScrobbleEntry.Participants, err = r.getParticipants(&res.ScrobbleEntry.MediaFile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return res.ScrobbleEntry, nil
|
|
}
|
|
|
|
func (r *scrobbleBufferRepository) Dequeue(entry *model.ScrobbleEntry) error {
|
|
return r.delete(Eq{"id": entry.ID})
|
|
}
|
|
|
|
func (r *scrobbleBufferRepository) Discard(service string) error {
|
|
return r.delete(Eq{"service": service})
|
|
}
|
|
|
|
func (r *scrobbleBufferRepository) Length() (int64, error) {
|
|
return r.count(Select())
|
|
}
|
|
|
|
var _ model.ScrobbleBufferRepository = (*scrobbleBufferRepository)(nil)
|