Fix scrobble error spam in the logs.

Relates to #2831 and #2975
This commit is contained in:
Deluan 2024-04-26 16:59:14 -04:00
parent 18143fa5a1
commit f5df948eb1
4 changed files with 44 additions and 15 deletions

View file

@ -6,6 +6,7 @@ import (
"time"
. "github.com/Masterminds/squirrel"
"github.com/google/uuid"
"github.com/navidrome/navidrome/model"
"github.com/pocketbase/dbx"
)
@ -37,6 +38,7 @@ func (r *scrobbleBufferRepository) UserIDs(service string) ([]string, error) {
func (r *scrobbleBufferRepository) Enqueue(service, userId, mediaFileId string, playTime time.Time) error {
ins := Insert(r.tableName).SetMap(map[string]interface{}{
"id": uuid.NewString(),
"user_id": userId,
"service": service,
"media_file_id": mediaFileId,
@ -48,7 +50,8 @@ func (r *scrobbleBufferRepository) Enqueue(service, userId, mediaFileId string,
}
func (r *scrobbleBufferRepository) Next(service string, userId string) (*model.ScrobbleEntry, error) {
sql := Select().Columns("s.*, m.*").
// 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{
@ -57,24 +60,20 @@ func (r *scrobbleBufferRepository) Next(service string, userId string) (*model.S
}).
OrderBy("play_time", "s.rowid").Limit(1)
res := model.ScrobbleEntries{}
// TODO Rewrite queryOne to use QueryRows, to workaround the recursive embedded structs issue
err := r.queryAll(sql, &res)
if errors.Is(err, model.ErrNotFound) || len(res) == 0 {
res := &model.ScrobbleEntry{}
err := r.queryOne(sql, res)
if errors.Is(err, model.ErrNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &res[0], nil
res.MediaFile.ID = res.MediaFileID
return res, nil
}
func (r *scrobbleBufferRepository) Dequeue(entry *model.ScrobbleEntry) error {
return r.delete(And{
Eq{"service": entry.Service},
Eq{"media_file_id": entry.MediaFile.ID},
Eq{"play_time": entry.PlayTime},
})
return r.delete(Eq{"id": entry.ID})
}
func (r *scrobbleBufferRepository) Length() (int64, error) {