7ad2907719
* refactor: move playlist business logic from repositories to core.Playlists service Move authorization, permission checks, and orchestration logic from playlist repositories to the core.Playlists service, following the existing pattern used by core.Share and core.Library. Changes: - Expand core.Playlists interface with read, mutation, track management, and REST adapter methods - Add playlistRepositoryWrapper for REST Save/Update/Delete with permission checks (follows Share/Library pattern) - Simplify persistence/playlist_repository.go: remove isWritable(), auth checks from Delete()/Put()/updatePlaylist() - Simplify persistence/playlist_track_repository.go: remove isTracksEditable() and permission checks from Add/Delete/Reorder - Update Subsonic API handlers to route through service - Update Native API handlers to accept core.Playlists instead of model.DataStore * test: add coverage for playlist service methods and REST wrapper Add 30 new tests covering the service methods added during the playlist refactoring: - Delete: owner, admin, denied, not found - Create: new playlist, replace tracks, admin bypass, denied, not found - AddTracks: owner, admin, denied, smart playlist, not found - RemoveTracks: owner, smart playlist denied, non-owner denied - ReorderTrack: owner, smart playlist denied - NewRepository wrapper: Save (owner assignment, ID clearing), Update (owner, admin, denied, ownership change, not found), Delete (delegation with permission checks) Expand mockedPlaylistRepo with Get, Delete, Tracks, GetWithTracks, and rest.Persistable methods. Add mockedPlaylistTrackRepo for track operation verification. * fix: add authorization check to playlist Update method Added ownership verification to the Subsonic Update endpoint in the playlist service layer. The authorization check was present in the old repository code but was not carried over during the refactoring to the service layer, allowing any authenticated user to modify playlists they don't own via the Subsonic API. Also added corresponding tests for the Update method's permission logic. * refactor: improve playlist permission checks and error handling, add e2e tests Signed-off-by: Deluan <deluan@navidrome.org> * refactor: rename core.Playlists to playlists package and update references Signed-off-by: Deluan <deluan@navidrome.org> * refactor: rename playlists_internal_test.go to parse_m3u_test.go and update tests; add new parse_nsp.go and rest_adapter.go files Signed-off-by: Deluan <deluan@navidrome.org> * fix: block track mutations on smart playlists in Create and Update Create now rejects replacing tracks on smart playlists (pre-existing gap). Update now uses checkTracksEditable instead of checkWritable when track changes are requested, restoring the protection that was removed from the repository layer during the refactoring. Metadata-only updates on smart playlists remain allowed. * test: add smart playlist protection tests to ensure readonly behavior and mutation restrictions * refactor: optimize track removal and renumbering in playlists Signed-off-by: Deluan <deluan@navidrome.org> * refactor: implement track reordering in playlists with SQL updates Signed-off-by: Deluan <deluan@navidrome.org> * refactor: wrap track deletion and reordering in transactions for consistency Signed-off-by: Deluan <deluan@navidrome.org> * refactor: remove unused getTracks method from playlistTrackRepository Signed-off-by: Deluan <deluan@navidrome.org> * refactor: optimize playlist track renumbering with CTE-based UPDATE Replace the DELETE + re-INSERT renumbering strategy with a two-step UPDATE approach using a materialized CTE and ROW_NUMBER() window function. The previous approach (SELECT all IDs, DELETE all tracks, re-INSERT in chunks of 200) required 13 SQL operations for a 2000-track playlist. The new approach uses just 2 UPDATEs: first negating all IDs to clear the positive space, then assigning sequential positions via UPDATE...FROM with a CTE. This avoids the UNIQUE constraint violations that affected the original correlated subquery while reducing per-delete request time from ~110ms to ~12ms on a 2000-track playlist. Signed-off-by: Deluan <deluan@navidrome.org> * refactor: rename New function to NewPlaylists for clarity Signed-off-by: Deluan <deluan@navidrome.org> * refactor: update mock playlist repository and tests for consistency Signed-off-by: Deluan <deluan@navidrome.org> --------- Signed-off-by: Deluan <deluan@navidrome.org>
609 lines
22 KiB
Go
609 lines
22 KiB
Go
package persistence
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/conf/configtest"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/model/criteria"
|
|
"github.com/navidrome/navidrome/model/request"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
"github.com/pocketbase/dbx"
|
|
)
|
|
|
|
var _ = Describe("PlaylistRepository", func() {
|
|
var repo model.PlaylistRepository
|
|
|
|
BeforeEach(func() {
|
|
ctx := log.NewContext(GinkgoT().Context())
|
|
ctx = request.WithUser(ctx, model.User{ID: "userid", UserName: "userid", IsAdmin: true})
|
|
repo = NewPlaylistRepository(ctx, GetDBXBuilder())
|
|
})
|
|
|
|
Describe("Count", func() {
|
|
It("returns the number of playlists in the DB", func() {
|
|
Expect(repo.CountAll()).To(Equal(int64(2)))
|
|
})
|
|
})
|
|
|
|
Describe("Exists", func() {
|
|
It("returns true for an existing playlist", func() {
|
|
Expect(repo.Exists(plsCool.ID)).To(BeTrue())
|
|
})
|
|
It("returns false for a non-existing playlist", func() {
|
|
Expect(repo.Exists("666")).To(BeFalse())
|
|
})
|
|
})
|
|
|
|
Describe("Get", func() {
|
|
It("returns an existing playlist", func() {
|
|
p, err := repo.Get(plsBest.ID)
|
|
Expect(err).To(BeNil())
|
|
// Compare all but Tracks and timestamps
|
|
p2 := *p
|
|
p2.Tracks = plsBest.Tracks
|
|
p2.UpdatedAt = plsBest.UpdatedAt
|
|
p2.CreatedAt = plsBest.CreatedAt
|
|
Expect(p2).To(Equal(plsBest))
|
|
// Compare tracks
|
|
for i := range p.Tracks {
|
|
Expect(p.Tracks[i].ID).To(Equal(plsBest.Tracks[i].ID))
|
|
}
|
|
})
|
|
It("returns ErrNotFound for a non-existing playlist", func() {
|
|
_, err := repo.Get("666")
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
})
|
|
It("returns all tracks", func() {
|
|
pls, err := repo.GetWithTracks(plsBest.ID, true, false)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(pls.Name).To(Equal(plsBest.Name))
|
|
Expect(pls.Tracks).To(HaveLen(2))
|
|
Expect(pls.Tracks[0].ID).To(Equal("1"))
|
|
Expect(pls.Tracks[0].PlaylistID).To(Equal(plsBest.ID))
|
|
Expect(pls.Tracks[0].MediaFileID).To(Equal(songDayInALife.ID))
|
|
Expect(pls.Tracks[0].MediaFile.ID).To(Equal(songDayInALife.ID))
|
|
Expect(pls.Tracks[1].ID).To(Equal("2"))
|
|
Expect(pls.Tracks[1].PlaylistID).To(Equal(plsBest.ID))
|
|
Expect(pls.Tracks[1].MediaFileID).To(Equal(songRadioactivity.ID))
|
|
Expect(pls.Tracks[1].MediaFile.ID).To(Equal(songRadioactivity.ID))
|
|
mfs := pls.MediaFiles()
|
|
Expect(mfs).To(HaveLen(2))
|
|
Expect(mfs[0].ID).To(Equal(songDayInALife.ID))
|
|
Expect(mfs[1].ID).To(Equal(songRadioactivity.ID))
|
|
})
|
|
})
|
|
|
|
It("Put/Exists/Delete", func() {
|
|
By("saves the playlist to the DB")
|
|
newPls := model.Playlist{Name: "Great!", OwnerID: "userid"}
|
|
newPls.AddMediaFilesByID([]string{"1004", "1003"})
|
|
|
|
By("saves the playlist to the DB")
|
|
Expect(repo.Put(&newPls)).To(BeNil())
|
|
|
|
By("adds repeated songs to a playlist and keeps the order")
|
|
newPls.AddMediaFilesByID([]string{"1004"})
|
|
Expect(repo.Put(&newPls)).To(BeNil())
|
|
saved, _ := repo.GetWithTracks(newPls.ID, true, false)
|
|
Expect(saved.Tracks).To(HaveLen(3))
|
|
Expect(saved.Tracks[0].MediaFileID).To(Equal("1004"))
|
|
Expect(saved.Tracks[1].MediaFileID).To(Equal("1003"))
|
|
Expect(saved.Tracks[2].MediaFileID).To(Equal("1004"))
|
|
|
|
By("returns the newly created playlist")
|
|
Expect(repo.Exists(newPls.ID)).To(BeTrue())
|
|
|
|
By("returns deletes the playlist")
|
|
Expect(repo.Delete(newPls.ID)).To(BeNil())
|
|
|
|
By("returns error if tries to retrieve the deleted playlist")
|
|
Expect(repo.Exists(newPls.ID)).To(BeFalse())
|
|
})
|
|
|
|
Describe("GetAll", func() {
|
|
It("returns all playlists from DB", func() {
|
|
all, err := repo.GetAll()
|
|
Expect(err).To(BeNil())
|
|
Expect(all[0].ID).To(Equal(plsBest.ID))
|
|
Expect(all[1].ID).To(Equal(plsCool.ID))
|
|
})
|
|
})
|
|
|
|
Describe("GetPlaylists", func() {
|
|
It("returns playlists for a track", func() {
|
|
pls, err := repo.GetPlaylists(songRadioactivity.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(pls).To(HaveLen(1))
|
|
Expect(pls[0].ID).To(Equal(plsBest.ID))
|
|
})
|
|
|
|
It("returns empty when none", func() {
|
|
pls, err := repo.GetPlaylists("9999")
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(pls).To(HaveLen(0))
|
|
})
|
|
})
|
|
|
|
Context("Smart Playlists", func() {
|
|
var rules *criteria.Criteria
|
|
BeforeEach(func() {
|
|
rules = &criteria.Criteria{
|
|
Expression: criteria.All{
|
|
criteria.Contains{"title": "love"},
|
|
},
|
|
}
|
|
})
|
|
Context("valid rules", func() {
|
|
Specify("Put/Get", func() {
|
|
newPls := model.Playlist{Name: "Great!", OwnerID: "userid", Rules: rules}
|
|
Expect(repo.Put(&newPls)).To(Succeed())
|
|
|
|
savedPls, err := repo.Get(newPls.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(savedPls.Rules).To(Equal(rules))
|
|
})
|
|
})
|
|
|
|
Context("invalid rules", func() {
|
|
It("fails to Put it in the DB", func() {
|
|
rules = &criteria.Criteria{
|
|
// This is invalid because "contains" cannot have multiple fields
|
|
Expression: criteria.All{
|
|
criteria.Contains{"genre": "Hardcore", "filetype": "mp3"},
|
|
},
|
|
}
|
|
newPls := model.Playlist{Name: "Great!", OwnerID: "userid", Rules: rules}
|
|
Expect(repo.Put(&newPls)).To(MatchError(ContainSubstring("invalid criteria expression")))
|
|
})
|
|
})
|
|
|
|
Context("child smart playlists", func() {
|
|
BeforeEach(func() {
|
|
DeferCleanup(configtest.SetupConfig())
|
|
})
|
|
|
|
When("refresh delay has expired", func() {
|
|
It("should refresh tracks for smart playlist referenced in parent smart playlist criteria", func() {
|
|
conf.Server.SmartPlaylistRefreshDelay = -1 * time.Second
|
|
|
|
childRules := &criteria.Criteria{
|
|
Expression: criteria.All{
|
|
criteria.Contains{"title": "Day"},
|
|
},
|
|
}
|
|
nestedPls := model.Playlist{Name: "Nested", OwnerID: "userid", Public: true, Rules: childRules}
|
|
Expect(repo.Put(&nestedPls)).To(Succeed())
|
|
DeferCleanup(func() { _ = repo.Delete(nestedPls.ID) })
|
|
|
|
parentPls := model.Playlist{Name: "Parent", OwnerID: "userid", Rules: &criteria.Criteria{
|
|
Expression: criteria.All{
|
|
criteria.InPlaylist{"id": nestedPls.ID},
|
|
},
|
|
}}
|
|
Expect(repo.Put(&parentPls)).To(Succeed())
|
|
DeferCleanup(func() { _ = repo.Delete(parentPls.ID) })
|
|
|
|
// Nested playlist has not been evaluated yet
|
|
nestedPlsRead, err := repo.Get(nestedPls.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(nestedPlsRead.EvaluatedAt).To(BeNil())
|
|
|
|
// Getting parent with refresh should recursively refresh the nested playlist
|
|
pls, err := repo.GetWithTracks(parentPls.ID, true, false)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(pls.EvaluatedAt).ToNot(BeNil())
|
|
Expect(*pls.EvaluatedAt).To(BeTemporally("~", time.Now(), 2*time.Second))
|
|
|
|
// Parent should have tracks from the nested playlist
|
|
Expect(pls.Tracks).To(HaveLen(1))
|
|
Expect(pls.Tracks[0].MediaFileID).To(Equal(songDayInALife.ID))
|
|
|
|
// Nested playlist should now have been refreshed (EvaluatedAt set)
|
|
nestedPlsAfterParentGet, err := repo.Get(nestedPls.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(nestedPlsAfterParentGet.EvaluatedAt).ToNot(BeNil())
|
|
Expect(*nestedPlsAfterParentGet.EvaluatedAt).To(BeTemporally("~", time.Now(), 2*time.Second))
|
|
})
|
|
})
|
|
|
|
When("refresh delay has not expired", func() {
|
|
It("should NOT refresh tracks for smart playlist referenced in parent smart playlist criteria", func() {
|
|
conf.Server.SmartPlaylistRefreshDelay = 1 * time.Hour
|
|
childEvaluatedAt := time.Now().Add(-30 * time.Minute)
|
|
|
|
childRules := &criteria.Criteria{
|
|
Expression: criteria.All{
|
|
criteria.Contains{"title": "Day"},
|
|
},
|
|
}
|
|
nestedPls := model.Playlist{Name: "Nested", OwnerID: "userid", Public: true, Rules: childRules, EvaluatedAt: &childEvaluatedAt}
|
|
Expect(repo.Put(&nestedPls)).To(Succeed())
|
|
DeferCleanup(func() { _ = repo.Delete(nestedPls.ID) })
|
|
|
|
// Parent has no EvaluatedAt, so it WILL refresh, but the child should not
|
|
parentPls := model.Playlist{Name: "Parent", OwnerID: "userid", Rules: &criteria.Criteria{
|
|
Expression: criteria.All{
|
|
criteria.InPlaylist{"id": nestedPls.ID},
|
|
},
|
|
}}
|
|
Expect(repo.Put(&parentPls)).To(Succeed())
|
|
DeferCleanup(func() { _ = repo.Delete(parentPls.ID) })
|
|
|
|
nestedPlsRead, err := repo.Get(nestedPls.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// Getting parent with refresh should NOT recursively refresh the nested playlist
|
|
parent, err := repo.GetWithTracks(parentPls.ID, true, false)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// Parent should have been refreshed (its EvaluatedAt was nil)
|
|
Expect(parent.EvaluatedAt).ToNot(BeNil())
|
|
Expect(*parent.EvaluatedAt).To(BeTemporally("~", time.Now(), 2*time.Second))
|
|
|
|
// Nested playlist should NOT have been refreshed (still within delay window)
|
|
nestedPlsAfterParentGet, err := repo.Get(nestedPls.ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(*nestedPlsAfterParentGet.EvaluatedAt).To(BeTemporally("~", childEvaluatedAt, time.Second))
|
|
Expect(*nestedPlsAfterParentGet.EvaluatedAt).To(Equal(*nestedPlsRead.EvaluatedAt))
|
|
})
|
|
})
|
|
})
|
|
})
|
|
|
|
Describe("Playlist Track Sorting", func() {
|
|
var testPlaylistID string
|
|
|
|
AfterEach(func() {
|
|
if testPlaylistID != "" {
|
|
Expect(repo.Delete(testPlaylistID)).To(BeNil())
|
|
testPlaylistID = ""
|
|
}
|
|
})
|
|
|
|
It("sorts tracks correctly by album (disc and track number)", func() {
|
|
By("creating a playlist with multi-disc album tracks in arbitrary order")
|
|
newPls := model.Playlist{Name: "Multi-Disc Test", OwnerID: "userid"}
|
|
// Add tracks in intentionally scrambled order
|
|
newPls.AddMediaFilesByID([]string{"2001", "2002", "2003", "2004"})
|
|
Expect(repo.Put(&newPls)).To(Succeed())
|
|
testPlaylistID = newPls.ID
|
|
|
|
By("retrieving tracks sorted by album")
|
|
tracksRepo := repo.Tracks(newPls.ID, false)
|
|
tracks, err := tracksRepo.GetAll(model.QueryOptions{Sort: "album", Order: "asc"})
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
By("verifying tracks are sorted by disc number then track number")
|
|
Expect(tracks).To(HaveLen(4))
|
|
// Expected order: Disc 1 Track 1, Disc 1 Track 2, Disc 2 Track 1, Disc 2 Track 11
|
|
Expect(tracks[0].MediaFileID).To(Equal("2002")) // Disc 1, Track 1
|
|
Expect(tracks[1].MediaFileID).To(Equal("2004")) // Disc 1, Track 2
|
|
Expect(tracks[2].MediaFileID).To(Equal("2003")) // Disc 2, Track 1
|
|
Expect(tracks[3].MediaFileID).To(Equal("2001")) // Disc 2, Track 11
|
|
})
|
|
})
|
|
|
|
Describe("Smart Playlists with Tag Criteria", func() {
|
|
var mfRepo model.MediaFileRepository
|
|
var testPlaylistID string
|
|
var songWithGrouping, songWithoutGrouping model.MediaFile
|
|
|
|
BeforeEach(func() {
|
|
ctx := log.NewContext(GinkgoT().Context())
|
|
ctx = request.WithUser(ctx, model.User{ID: "userid", UserName: "userid", IsAdmin: true})
|
|
mfRepo = NewMediaFileRepository(ctx, GetDBXBuilder())
|
|
|
|
// Register 'grouping' as a valid tag for smart playlists
|
|
criteria.AddTagNames([]string{"grouping"})
|
|
|
|
// Create a song with the grouping tag
|
|
songWithGrouping = model.MediaFile{
|
|
ID: "test-grouping-1",
|
|
Title: "Song With Grouping",
|
|
Artist: "Test Artist",
|
|
ArtistID: "1",
|
|
Album: "Test Album",
|
|
AlbumID: "101",
|
|
Path: "/test/grouping/song1.mp3",
|
|
Tags: model.Tags{
|
|
"grouping": []string{"My Crate"},
|
|
},
|
|
Participants: model.Participants{},
|
|
LibraryID: 1,
|
|
Lyrics: "[]",
|
|
}
|
|
Expect(mfRepo.Put(&songWithGrouping)).To(Succeed())
|
|
|
|
// Create a song without the grouping tag
|
|
songWithoutGrouping = model.MediaFile{
|
|
ID: "test-grouping-2",
|
|
Title: "Song Without Grouping",
|
|
Artist: "Test Artist",
|
|
ArtistID: "1",
|
|
Album: "Test Album",
|
|
AlbumID: "101",
|
|
Path: "/test/grouping/song2.mp3",
|
|
Tags: model.Tags{},
|
|
Participants: model.Participants{},
|
|
LibraryID: 1,
|
|
Lyrics: "[]",
|
|
}
|
|
Expect(mfRepo.Put(&songWithoutGrouping)).To(Succeed())
|
|
})
|
|
|
|
AfterEach(func() {
|
|
if testPlaylistID != "" {
|
|
_ = repo.Delete(testPlaylistID)
|
|
testPlaylistID = ""
|
|
}
|
|
// Clean up test media files
|
|
_, _ = GetDBXBuilder().Delete("media_file", dbx.HashExp{"id": "test-grouping-1"}).Execute()
|
|
_, _ = GetDBXBuilder().Delete("media_file", dbx.HashExp{"id": "test-grouping-2"}).Execute()
|
|
})
|
|
|
|
It("matches tracks with a tag value using 'contains' with empty string (issue #4728 workaround)", func() {
|
|
By("creating a smart playlist that checks if grouping tag has any value")
|
|
// This is the workaround for issue #4728: using 'contains' with empty string
|
|
// generates SQL: value LIKE '%%' which matches any non-empty string
|
|
rules := &criteria.Criteria{
|
|
Expression: criteria.All{
|
|
criteria.Contains{"grouping": ""},
|
|
},
|
|
}
|
|
newPls := model.Playlist{Name: "Tracks with Grouping", OwnerID: "userid", Rules: rules}
|
|
Expect(repo.Put(&newPls)).To(Succeed())
|
|
testPlaylistID = newPls.ID
|
|
|
|
By("refreshing the smart playlist")
|
|
conf.Server.SmartPlaylistRefreshDelay = -1 * time.Second // Force refresh
|
|
pls, err := repo.GetWithTracks(newPls.ID, true, false)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
By("verifying only the track with grouping tag is matched")
|
|
Expect(pls.Tracks).To(HaveLen(1))
|
|
Expect(pls.Tracks[0].MediaFileID).To(Equal(songWithGrouping.ID))
|
|
})
|
|
|
|
It("excludes tracks with a tag value using 'notContains' with empty string", func() {
|
|
By("creating a smart playlist that checks if grouping tag is NOT set")
|
|
rules := &criteria.Criteria{
|
|
Expression: criteria.All{
|
|
criteria.NotContains{"grouping": ""},
|
|
},
|
|
}
|
|
newPls := model.Playlist{Name: "Tracks without Grouping", OwnerID: "userid", Rules: rules}
|
|
Expect(repo.Put(&newPls)).To(Succeed())
|
|
testPlaylistID = newPls.ID
|
|
|
|
By("refreshing the smart playlist")
|
|
conf.Server.SmartPlaylistRefreshDelay = -1 * time.Second // Force refresh
|
|
pls, err := repo.GetWithTracks(newPls.ID, true, false)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
By("verifying the track with grouping is NOT in the playlist")
|
|
for _, track := range pls.Tracks {
|
|
Expect(track.MediaFileID).ToNot(Equal(songWithGrouping.ID))
|
|
}
|
|
|
|
By("verifying the track without grouping IS in the playlist")
|
|
var foundWithoutGrouping bool
|
|
for _, track := range pls.Tracks {
|
|
if track.MediaFileID == songWithoutGrouping.ID {
|
|
foundWithoutGrouping = true
|
|
break
|
|
}
|
|
}
|
|
Expect(foundWithoutGrouping).To(BeTrue())
|
|
})
|
|
})
|
|
|
|
Describe("Track Deletion and Renumbering", func() {
|
|
var testPlaylistID string
|
|
|
|
AfterEach(func() {
|
|
if testPlaylistID != "" {
|
|
Expect(repo.Delete(testPlaylistID)).To(BeNil())
|
|
testPlaylistID = ""
|
|
}
|
|
})
|
|
|
|
// helper to get track positions and media file IDs
|
|
getTrackInfo := func(playlistID string) (ids []string, mediaFileIDs []string) {
|
|
pls, err := repo.GetWithTracks(playlistID, false, false)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
for _, t := range pls.Tracks {
|
|
ids = append(ids, t.ID)
|
|
mediaFileIDs = append(mediaFileIDs, t.MediaFileID)
|
|
}
|
|
return
|
|
}
|
|
|
|
It("renumbers correctly after deleting a track from the middle", func() {
|
|
By("creating a playlist with 4 tracks")
|
|
newPls := model.Playlist{Name: "Renumber Test Middle", OwnerID: "userid"}
|
|
newPls.AddMediaFilesByID([]string{"1001", "1002", "1003", "1004"})
|
|
Expect(repo.Put(&newPls)).To(Succeed())
|
|
testPlaylistID = newPls.ID
|
|
|
|
By("deleting the second track (position 2)")
|
|
tracksRepo := repo.Tracks(newPls.ID, false)
|
|
Expect(tracksRepo.Delete("2")).To(Succeed())
|
|
|
|
By("verifying remaining tracks are renumbered sequentially")
|
|
ids, mediaFileIDs := getTrackInfo(newPls.ID)
|
|
Expect(ids).To(Equal([]string{"1", "2", "3"}))
|
|
Expect(mediaFileIDs).To(Equal([]string{"1001", "1003", "1004"}))
|
|
})
|
|
|
|
It("renumbers correctly after deleting the first track", func() {
|
|
By("creating a playlist with 3 tracks")
|
|
newPls := model.Playlist{Name: "Renumber Test First", OwnerID: "userid"}
|
|
newPls.AddMediaFilesByID([]string{"1001", "1002", "1003"})
|
|
Expect(repo.Put(&newPls)).To(Succeed())
|
|
testPlaylistID = newPls.ID
|
|
|
|
By("deleting the first track (position 1)")
|
|
tracksRepo := repo.Tracks(newPls.ID, false)
|
|
Expect(tracksRepo.Delete("1")).To(Succeed())
|
|
|
|
By("verifying remaining tracks are renumbered sequentially")
|
|
ids, mediaFileIDs := getTrackInfo(newPls.ID)
|
|
Expect(ids).To(Equal([]string{"1", "2"}))
|
|
Expect(mediaFileIDs).To(Equal([]string{"1002", "1003"}))
|
|
})
|
|
|
|
It("renumbers correctly after deleting the last track", func() {
|
|
By("creating a playlist with 3 tracks")
|
|
newPls := model.Playlist{Name: "Renumber Test Last", OwnerID: "userid"}
|
|
newPls.AddMediaFilesByID([]string{"1001", "1002", "1003"})
|
|
Expect(repo.Put(&newPls)).To(Succeed())
|
|
testPlaylistID = newPls.ID
|
|
|
|
By("deleting the last track (position 3)")
|
|
tracksRepo := repo.Tracks(newPls.ID, false)
|
|
Expect(tracksRepo.Delete("3")).To(Succeed())
|
|
|
|
By("verifying remaining tracks are renumbered sequentially")
|
|
ids, mediaFileIDs := getTrackInfo(newPls.ID)
|
|
Expect(ids).To(Equal([]string{"1", "2"}))
|
|
Expect(mediaFileIDs).To(Equal([]string{"1001", "1002"}))
|
|
})
|
|
})
|
|
|
|
Describe("Smart Playlists Library Filtering", func() {
|
|
var mfRepo model.MediaFileRepository
|
|
var testPlaylistID string
|
|
var lib2ID int
|
|
var restrictedUserID string
|
|
var uniqueLibPath string
|
|
|
|
BeforeEach(func() {
|
|
db := GetDBXBuilder()
|
|
|
|
// Generate unique IDs for this test run
|
|
uniqueSuffix := time.Now().Format("20060102150405.000")
|
|
restrictedUserID = "restricted-user-" + uniqueSuffix
|
|
uniqueLibPath = "/music/lib2-" + uniqueSuffix
|
|
|
|
// Create a second library with unique name and path to avoid conflicts with other tests
|
|
_, err := db.DB().Exec("INSERT INTO library (name, path, created_at, updated_at) VALUES (?, ?, datetime('now'), datetime('now'))", "Library 2-"+uniqueSuffix, uniqueLibPath)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
err = db.DB().QueryRow("SELECT last_insert_rowid()").Scan(&lib2ID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// Create a restricted user with access only to library 1
|
|
_, err = db.DB().Exec("INSERT INTO user (id, user_name, name, is_admin, password, created_at, updated_at) VALUES (?, ?, 'Restricted User', false, 'pass', datetime('now'), datetime('now'))", restrictedUserID, restrictedUserID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
_, err = db.DB().Exec("INSERT INTO user_library (user_id, library_id) VALUES (?, 1)", restrictedUserID)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
// Create test media files in each library
|
|
ctx := log.NewContext(GinkgoT().Context())
|
|
ctx = request.WithUser(ctx, model.User{ID: "userid", UserName: "userid", IsAdmin: true})
|
|
mfRepo = NewMediaFileRepository(ctx, db)
|
|
|
|
// Song in library 1 (accessible by restricted user)
|
|
songLib1 := model.MediaFile{
|
|
ID: "lib1-song",
|
|
Title: "Song in Lib1",
|
|
Artist: "Test Artist",
|
|
ArtistID: "1",
|
|
Album: "Test Album",
|
|
AlbumID: "101",
|
|
Path: "/music/lib1/song.mp3",
|
|
LibraryID: 1,
|
|
Participants: model.Participants{},
|
|
Tags: model.Tags{},
|
|
Lyrics: "[]",
|
|
}
|
|
Expect(mfRepo.Put(&songLib1)).To(Succeed())
|
|
|
|
// Song in library 2 (NOT accessible by restricted user)
|
|
songLib2 := model.MediaFile{
|
|
ID: "lib2-song",
|
|
Title: "Song in Lib2",
|
|
Artist: "Test Artist",
|
|
ArtistID: "1",
|
|
Album: "Test Album",
|
|
AlbumID: "101",
|
|
Path: uniqueLibPath + "/song.mp3",
|
|
LibraryID: lib2ID,
|
|
Participants: model.Participants{},
|
|
Tags: model.Tags{},
|
|
Lyrics: "[]",
|
|
}
|
|
Expect(mfRepo.Put(&songLib2)).To(Succeed())
|
|
})
|
|
|
|
AfterEach(func() {
|
|
db := GetDBXBuilder()
|
|
if testPlaylistID != "" {
|
|
_ = repo.Delete(testPlaylistID)
|
|
testPlaylistID = ""
|
|
}
|
|
// Clean up test data
|
|
_, _ = db.Delete("media_file", dbx.HashExp{"id": "lib1-song"}).Execute()
|
|
_, _ = db.Delete("media_file", dbx.HashExp{"id": "lib2-song"}).Execute()
|
|
_, _ = db.Delete("user_library", dbx.HashExp{"user_id": restrictedUserID}).Execute()
|
|
_, _ = db.Delete("user", dbx.HashExp{"id": restrictedUserID}).Execute()
|
|
_, _ = db.DB().Exec("DELETE FROM library WHERE id = ?", lib2ID)
|
|
})
|
|
|
|
It("should only include tracks from libraries the user has access to (issue #4738)", func() {
|
|
db := GetDBXBuilder()
|
|
ctx := log.NewContext(GinkgoT().Context())
|
|
|
|
// Create the smart playlist as the restricted user
|
|
restrictedUser := model.User{ID: restrictedUserID, UserName: restrictedUserID, IsAdmin: false}
|
|
ctx = request.WithUser(ctx, restrictedUser)
|
|
restrictedRepo := NewPlaylistRepository(ctx, db)
|
|
|
|
// Create a smart playlist that matches all songs
|
|
rules := &criteria.Criteria{
|
|
Expression: criteria.All{
|
|
criteria.Gt{"playCount": -1}, // Matches everything
|
|
},
|
|
}
|
|
newPls := model.Playlist{Name: "All Songs", OwnerID: restrictedUserID, Rules: rules}
|
|
Expect(restrictedRepo.Put(&newPls)).To(Succeed())
|
|
testPlaylistID = newPls.ID
|
|
|
|
By("refreshing the smart playlist")
|
|
conf.Server.SmartPlaylistRefreshDelay = -1 * time.Second // Force refresh
|
|
pls, err := restrictedRepo.GetWithTracks(newPls.ID, true, false)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
By("verifying only the track from library 1 is in the playlist")
|
|
var foundLib1Song, foundLib2Song bool
|
|
for _, track := range pls.Tracks {
|
|
if track.MediaFileID == "lib1-song" {
|
|
foundLib1Song = true
|
|
}
|
|
if track.MediaFileID == "lib2-song" {
|
|
foundLib2Song = true
|
|
}
|
|
}
|
|
Expect(foundLib1Song).To(BeTrue(), "Song from library 1 should be in the playlist")
|
|
Expect(foundLib2Song).To(BeFalse(), "Song from library 2 should NOT be in the playlist")
|
|
|
|
By("verifying playlist_tracks table only contains the accessible track")
|
|
var playlistTracksCount int
|
|
err = db.DB().QueryRow("SELECT count(*) FROM playlist_tracks WHERE playlist_id = ?", newPls.ID).Scan(&playlistTracksCount)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
// Count should only include tracks visible to the user (lib1-song)
|
|
// The count may include other test songs from library 1, but NOT lib2-song
|
|
var lib2TrackCount int
|
|
err = db.DB().QueryRow("SELECT count(*) FROM playlist_tracks WHERE playlist_id = ? AND media_file_id = 'lib2-song'", newPls.ID).Scan(&lib2TrackCount)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(lib2TrackCount).To(Equal(0), "lib2-song should not be in playlist_tracks")
|
|
|
|
By("verifying SongCount matches visible tracks")
|
|
Expect(pls.SongCount).To(Equal(len(pls.Tracks)), "SongCount should match the number of visible tracks")
|
|
})
|
|
})
|
|
})
|