From 8861eebe21c74de386fb05f9c6eb63b8bbeea6fd Mon Sep 17 00:00:00 2001 From: Deluan Date: Fri, 16 Jan 2026 10:27:51 -0500 Subject: [PATCH] feat: add global smart playlist functionality Signed-off-by: Deluan --- core/playlists.go | 16 ++++- core/playlists_test.go | 9 +++ ...260116135347_add_global_smart_playlist.sql | 9 +++ model/playlist.go | 1 + persistence/playlist_repository.go | 6 +- persistence/playlist_repository_test.go | 62 +++++++++++++++++++ .../playlists/global_smart_playlist.nsp | 10 +++ 7 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 db/migrations/20260116135347_add_global_smart_playlist.sql create mode 100644 tests/fixtures/playlists/global_smart_playlist.nsp diff --git a/core/playlists.go b/core/playlists.go index ed90cc23b..58626a510 100644 --- a/core/playlists.go +++ b/core/playlists.go @@ -168,6 +168,10 @@ func (s *playlists) parseNSP(_ context.Context, pls *model.Playlist, reader io.R if nsp.Comment != "" { pls.Comment = nsp.Comment } + pls.Global = nsp.Global + if nsp.Global { + pls.Public = true + } return nil } @@ -404,12 +408,18 @@ func (s *playlists) updatePlaylist(ctx context.Context, newPls *model.Playlist) newPls.Name = pls.Name newPls.Comment = pls.Comment newPls.OwnerID = pls.OwnerID - newPls.Public = pls.Public + // Preserve Public from existing playlist, unless the new playlist is Global + if !newPls.Global { + newPls.Public = pls.Public + } newPls.EvaluatedAt = &time.Time{} } else { log.Info(ctx, "Adding synced playlist", "playlist", newPls.Name, "path", newPls.Path, "owner", owner.UserName) newPls.OwnerID = owner.ID - newPls.Public = conf.Server.DefaultPlaylistPublicVisibility + // Only apply default visibility if not a global playlist (which is always public) + if !newPls.Global { + newPls.Public = conf.Server.DefaultPlaylistPublicVisibility + } } return s.ds.Playlist(ctx).Put(newPls) } @@ -473,6 +483,7 @@ type nspFile struct { criteria.Criteria Name string `json:"name"` Comment string `json:"comment"` + Global bool `json:"global"` } func (i *nspFile) UnmarshalJSON(data []byte) error { @@ -483,5 +494,6 @@ func (i *nspFile) UnmarshalJSON(data []byte) error { } i.Name, _ = m["name"].(string) i.Comment, _ = m["comment"].(string) + i.Global, _ = m["global"].(bool) return json.Unmarshal(data, &i.Criteria) } diff --git a/core/playlists_test.go b/core/playlists_test.go index 6aa8aac9a..11e752b16 100644 --- a/core/playlists_test.go +++ b/core/playlists_test.go @@ -107,11 +107,20 @@ var _ = Describe("Playlists", func() { Expect(pls.Rules.Order).To(Equal("desc")) Expect(pls.Rules.Limit).To(Equal(100)) Expect(pls.Rules.Expression).To(BeAssignableToTypeOf(criteria.All{})) + Expect(pls.Global).To(BeFalse()) + Expect(pls.Public).To(BeFalse()) }) It("returns an error if the playlist is not well-formed", func() { _, err := ps.ImportFile(ctx, folder, "invalid_json.nsp") Expect(err.Error()).To(ContainSubstring("line 19, column 1: invalid character '\\n'")) }) + It("parses global attribute and sets playlist to public", func() { + pls, err := ps.ImportFile(ctx, folder, "global_smart_playlist.nsp") + Expect(err).ToNot(HaveOccurred()) + Expect(pls.Name).To(Equal("Global Smart Playlist")) + Expect(pls.Global).To(BeTrue()) + Expect(pls.Public).To(BeTrue()) + }) }) Describe("Cross-library relative paths", func() { diff --git a/db/migrations/20260116135347_add_global_smart_playlist.sql b/db/migrations/20260116135347_add_global_smart_playlist.sql new file mode 100644 index 000000000..25fa1393c --- /dev/null +++ b/db/migrations/20260116135347_add_global_smart_playlist.sql @@ -0,0 +1,9 @@ +-- +goose Up +-- +goose StatementBegin +ALTER TABLE playlist ADD COLUMN global BOOL DEFAULT FALSE NOT NULL; +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +ALTER TABLE playlist DROP COLUMN global; +-- +goose StatementEnd diff --git a/model/playlist.go b/model/playlist.go index a87019ed5..ae67cc258 100644 --- a/model/playlist.go +++ b/model/playlist.go @@ -27,6 +27,7 @@ type Playlist struct { // SmartPlaylist attributes Rules *criteria.Criteria `structs:"rules" json:"rules"` EvaluatedAt *time.Time `structs:"evaluated_at" json:"evaluatedAt"` + Global bool `structs:"global" json:"global"` } func (pls Playlist) IsSmartPlaylist() bool { diff --git a/persistence/playlist_repository.go b/persistence/playlist_repository.go index 3fdd19af2..27d026578 100644 --- a/persistence/playlist_repository.go +++ b/persistence/playlist_repository.go @@ -227,10 +227,10 @@ func (r *playlistRepository) refreshSmartPlaylist(pls *model.Playlist) bool { return false } - // Never refresh other users' playlists + // Only refresh for owners, unless the playlist is marked as global usr := loggedUser(r.ctx) - if pls.OwnerID != usr.ID { - log.Trace(r.ctx, "Not refreshing smart playlist from other user", "playlist", pls.Name, "id", pls.ID) + if pls.OwnerID != usr.ID && !pls.Global { + log.Debug(r.ctx, "Not refreshing smart playlist from other user", "playlist", pls.Name, "id", pls.ID) return false } diff --git a/persistence/playlist_repository_test.go b/persistence/playlist_repository_test.go index 05a36352f..a493d73c2 100644 --- a/persistence/playlist_repository_test.go +++ b/persistence/playlist_repository_test.go @@ -4,6 +4,7 @@ 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" @@ -147,6 +148,67 @@ var _ = Describe("PlaylistRepository", func() { }) }) + Context("Global smart playlists", func() { + var globalPls model.Playlist + var otherUserRepo model.PlaylistRepository + + BeforeEach(func() { + // Force smart playlist refresh + DeferCleanup(configtest.SetupConfig()) + conf.Server.SmartPlaylistRefreshDelay = -1 * time.Second + + // Create a global smart playlist owned by the admin user + globalPls = model.Playlist{Name: "Global Smart", OwnerID: "userid", Rules: rules, Global: true, Public: true} + Expect(repo.Put(&globalPls)).To(Succeed()) + + // Create a different user context (using regularUser who has library access) + otherCtx := log.NewContext(GinkgoT().Context()) + otherCtx = request.WithUser(otherCtx, regularUser) + otherUserRepo = NewPlaylistRepository(otherCtx, GetDBXBuilder()) + }) + + AfterEach(func() { + _ = repo.Delete(globalPls.ID) + }) + + It("stores and retrieves the Global attribute", func() { + savedPls, err := repo.Get(globalPls.ID) + Expect(err).ToNot(HaveOccurred()) + Expect(savedPls.Global).To(BeTrue()) + }) + + It("allows non-owner to refresh a global smart playlist", func() { + // Verify the playlist can be retrieved by non-owner and has Global=true + plsCheck, err := otherUserRepo.Get(globalPls.ID) + Expect(err).ToNot(HaveOccurred()) + Expect(plsCheck.Global).To(BeTrue(), "Global should be true when retrieved by non-owner") + Expect(plsCheck.IsSmartPlaylist()).To(BeTrue(), "Should be smart playlist") + Expect(plsCheck.EvaluatedAt).To(BeNil(), "Should not be evaluated yet") + + // Non-owner requests the playlist with refresh + _, err = otherUserRepo.GetWithTracks(globalPls.ID, true, false) + Expect(err).ToNot(HaveOccurred()) + + // Re-fetch to verify EvaluatedAt was updated in DB + pls, err := otherUserRepo.Get(globalPls.ID) + Expect(err).ToNot(HaveOccurred()) + Expect(pls.EvaluatedAt).ToNot(BeNil(), "Global smart playlist should be refreshed for non-owner") + }) + + It("does not allow non-owner to refresh a non-global smart playlist", func() { + // Create a non-global smart playlist + nonGlobalPls := model.Playlist{Name: "Non-Global Smart", OwnerID: "userid", Rules: rules, Global: false, Public: true} + Expect(repo.Put(&nonGlobalPls)).To(Succeed()) + DeferCleanup(func() { _ = repo.Delete(nonGlobalPls.ID) }) + + // Non-owner requests the playlist with refresh + pls, err := otherUserRepo.GetWithTracks(nonGlobalPls.ID, true, false) + Expect(err).ToNot(HaveOccurred()) + // EvaluatedAt should be nil because the playlist was not refreshed + Expect(pls.EvaluatedAt).To(BeNil()) + }) + }) + Context("invalid rules", func() { It("fails to Put it in the DB", func() { rules = &criteria.Criteria{ diff --git a/tests/fixtures/playlists/global_smart_playlist.nsp b/tests/fixtures/playlists/global_smart_playlist.nsp new file mode 100644 index 000000000..bbc26bb6a --- /dev/null +++ b/tests/fixtures/playlists/global_smart_playlist.nsp @@ -0,0 +1,10 @@ +{ + "name": "Global Smart Playlist", + "comment": "Available for evaluation by any user", + "global": true, + "all": [ + {"is": {"loved": true}} + ], + "sort": "title", + "order": "asc" +}