mirror of
https://github.com/navidrome/navidrome.git
synced 2025-07-18 01:11:44 +03:00
<think> I'm being asked to generate a Git commit message for the attached Git diff. I need to base this on: 1. The provided problem statement (PR description) 2. Recent commit messages for style guidance 3. The actual Git diff to understand what changed Looking at the Git diff: - In `persistence/mediafile_repository.go`, there's a formatting change where an alignment was fixed for the map definitions (added spaces to align keys and values) - There's a new key "recently_added" added to the sort mappings that uses `recentlyAddedSort()` - In `persistence/mediafile_repository_test.go`, there are whitespace changes and test additions for the recently_added functionality From the PR description, this is addressing issue #4046 where the `RecentlyAddedByModTime` config option wasn't applying to tracks in the "Recently Added" view, only to albums. From the recent commit messages, I see a simple descriptive style "Add recently_added sort option to mediafile repository" which is actually already describing the main change in this PR. Given this information, I should create a commit message that: 1. Clearly states what was changed 2. Is concise and to the point 3. Follows the style of previous commits The primary change in this diff is the addition of the "recently_added" sort option to mediafile repository, which extends the `RecentlyAddedByModTime` functionality to tracks. </think> Fix: Apply RecentlyAddedByModTime config to tracks in Recently Added view Co-authored-by: deluan <331353+deluan@users.noreply.github.com>
182 lines
5.7 KiB
Go
182 lines
5.7 KiB
Go
package persistence
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/model/id"
|
|
"github.com/navidrome/navidrome/model/request"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("MediaRepository", func() {
|
|
var mr model.MediaFileRepository
|
|
|
|
BeforeEach(func() {
|
|
ctx := log.NewContext(context.TODO())
|
|
ctx = request.WithUser(ctx, model.User{ID: "userid"})
|
|
mr = NewMediaFileRepository(ctx, GetDBXBuilder())
|
|
})
|
|
|
|
It("gets mediafile from the DB", func() {
|
|
actual, err := mr.Get("1004")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
actual.CreatedAt = time.Time{}
|
|
Expect(actual).To(Equal(&songAntenna))
|
|
})
|
|
|
|
It("returns ErrNotFound", func() {
|
|
_, err := mr.Get("56")
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
})
|
|
|
|
It("counts the number of mediafiles in the DB", func() {
|
|
Expect(mr.CountAll()).To(Equal(int64(4)))
|
|
})
|
|
|
|
It("checks existence of mediafiles in the DB", func() {
|
|
Expect(mr.Exists(songAntenna.ID)).To(BeTrue())
|
|
Expect(mr.Exists("666")).To(BeFalse())
|
|
})
|
|
|
|
It("delete tracks by id", func() {
|
|
newID := id.NewRandom()
|
|
Expect(mr.Put(&model.MediaFile{LibraryID: 1, ID: newID})).To(Succeed())
|
|
|
|
Expect(mr.Delete(newID)).To(Succeed())
|
|
|
|
_, err := mr.Get(newID)
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
})
|
|
|
|
It("deletes all missing files", func() {
|
|
new1 := model.MediaFile{ID: id.NewRandom(), LibraryID: 1}
|
|
new2 := model.MediaFile{ID: id.NewRandom(), LibraryID: 1}
|
|
Expect(mr.Put(&new1)).To(Succeed())
|
|
Expect(mr.Put(&new2)).To(Succeed())
|
|
Expect(mr.MarkMissing(true, &new1, &new2)).To(Succeed())
|
|
|
|
adminCtx := request.WithUser(log.NewContext(context.TODO()), model.User{ID: "userid", IsAdmin: true})
|
|
adminRepo := NewMediaFileRepository(adminCtx, GetDBXBuilder())
|
|
|
|
// Ensure the files are marked as missing and we have 2 of them
|
|
count, err := adminRepo.CountAll(model.QueryOptions{Filters: squirrel.Eq{"missing": true}})
|
|
Expect(count).To(BeNumerically("==", 2))
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
count, err = adminRepo.DeleteAllMissing()
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(count).To(BeNumerically("==", 2))
|
|
|
|
_, err = mr.Get(new1.ID)
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
_, err = mr.Get(new2.ID)
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
})
|
|
|
|
Context("Annotations", func() {
|
|
It("increments play count when the tracks does not have annotations", func() {
|
|
id := "incplay.firsttime"
|
|
Expect(mr.Put(&model.MediaFile{LibraryID: 1, ID: id})).To(BeNil())
|
|
playDate := time.Now()
|
|
Expect(mr.IncPlayCount(id, playDate)).To(BeNil())
|
|
|
|
mf, err := mr.Get(id)
|
|
Expect(err).To(BeNil())
|
|
|
|
Expect(mf.PlayDate.Unix()).To(Equal(playDate.Unix()))
|
|
Expect(mf.PlayCount).To(Equal(int64(1)))
|
|
})
|
|
|
|
It("preserves play date if and only if provided date is older", func() {
|
|
id := "incplay.playdate"
|
|
Expect(mr.Put(&model.MediaFile{LibraryID: 1, ID: id})).To(BeNil())
|
|
playDate := time.Now()
|
|
Expect(mr.IncPlayCount(id, playDate)).To(BeNil())
|
|
mf, err := mr.Get(id)
|
|
Expect(err).To(BeNil())
|
|
Expect(mf.PlayDate.Unix()).To(Equal(playDate.Unix()))
|
|
Expect(mf.PlayCount).To(Equal(int64(1)))
|
|
|
|
playDateLate := playDate.AddDate(0, 0, 1)
|
|
Expect(mr.IncPlayCount(id, playDateLate)).To(BeNil())
|
|
mf, err = mr.Get(id)
|
|
Expect(err).To(BeNil())
|
|
Expect(mf.PlayDate.Unix()).To(Equal(playDateLate.Unix()))
|
|
Expect(mf.PlayCount).To(Equal(int64(2)))
|
|
|
|
playDateEarly := playDate.AddDate(0, 0, -1)
|
|
Expect(mr.IncPlayCount(id, playDateEarly)).To(BeNil())
|
|
mf, err = mr.Get(id)
|
|
Expect(err).To(BeNil())
|
|
Expect(mf.PlayDate.Unix()).To(Equal(playDateLate.Unix()))
|
|
Expect(mf.PlayCount).To(Equal(int64(3)))
|
|
})
|
|
|
|
It("increments play count on newly starred items", func() {
|
|
id := "star.incplay"
|
|
Expect(mr.Put(&model.MediaFile{LibraryID: 1, ID: id})).To(BeNil())
|
|
Expect(mr.SetStar(true, id)).To(BeNil())
|
|
playDate := time.Now()
|
|
Expect(mr.IncPlayCount(id, playDate)).To(BeNil())
|
|
|
|
mf, err := mr.Get(id)
|
|
Expect(err).To(BeNil())
|
|
|
|
Expect(mf.PlayDate.Unix()).To(Equal(playDate.Unix()))
|
|
Expect(mf.PlayCount).To(Equal(int64(1)))
|
|
})
|
|
})
|
|
|
|
Context("RecentlyAddedByModTime", func() {
|
|
var originalValue bool
|
|
|
|
BeforeEach(func() {
|
|
originalValue = conf.Server.RecentlyAddedByModTime
|
|
})
|
|
|
|
AfterEach(func() {
|
|
conf.Server.RecentlyAddedByModTime = originalValue
|
|
})
|
|
|
|
It("sorts by updated_at when RecentlyAddedByModTime is true", func() {
|
|
conf.Server.RecentlyAddedByModTime = true
|
|
mediaFile1 := &model.MediaFile{ID: id.NewRandom(), LibraryID: 1, Title: "Track 1"}
|
|
mediaFile2 := &model.MediaFile{ID: id.NewRandom(), LibraryID: 1, Title: "Track 2"}
|
|
|
|
Expect(mr.Put(mediaFile1)).To(Succeed())
|
|
Expect(mr.Put(mediaFile2)).To(Succeed())
|
|
|
|
// Get all media files sorted by recently_added
|
|
options := model.QueryOptions{Sort: "recently_added"}
|
|
results, err := mr.GetAll(options)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// Verify that we're getting the right sort field
|
|
Expect(results).ToNot(BeEmpty())
|
|
})
|
|
|
|
It("sorts by created_at when RecentlyAddedByModTime is false", func() {
|
|
conf.Server.RecentlyAddedByModTime = false
|
|
mediaFile1 := &model.MediaFile{ID: id.NewRandom(), LibraryID: 1, Title: "Track 1"}
|
|
mediaFile2 := &model.MediaFile{ID: id.NewRandom(), LibraryID: 1, Title: "Track 2"}
|
|
|
|
Expect(mr.Put(mediaFile1)).To(Succeed())
|
|
Expect(mr.Put(mediaFile2)).To(Succeed())
|
|
|
|
// Get all media files sorted by recently_added
|
|
options := model.QueryOptions{Sort: "recently_added"}
|
|
results, err := mr.GetAll(options)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// Verify that we're getting the right sort field
|
|
Expect(results).ToNot(BeEmpty())
|
|
})
|
|
})
|
|
})
|