mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-16 23:28:44 +01:00
feat(subsonic): update GetIndexes and GetArtists method to support library ID filtering
Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
parent
0b61df1b90
commit
a3281ca027
8 changed files with 280 additions and 23 deletions
|
|
@ -7,6 +7,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/navidrome/navidrome/conf"
|
||||
"github.com/navidrome/navidrome/consts"
|
||||
"github.com/navidrome/navidrome/log"
|
||||
"github.com/navidrome/navidrome/model"
|
||||
"github.com/navidrome/navidrome/server/public"
|
||||
|
|
@ -34,22 +35,31 @@ func (api *Router) GetMusicFolders(r *http.Request) (*responses.Subsonic, error)
|
|||
|
||||
func (api *Router) getArtist(r *http.Request, libId int, ifModifiedSince time.Time) (model.ArtistIndexes, int64, error) {
|
||||
ctx := r.Context()
|
||||
lib, err := api.ds.Library(ctx).Get(libId)
|
||||
|
||||
lastScanStr, err := api.ds.Property(ctx).DefaultGet(consts.LastScanStartTimeKey, "")
|
||||
if err != nil {
|
||||
log.Error(ctx, "Error retrieving Library", "id", libId, err)
|
||||
log.Error(ctx, "Error retrieving last scan start time", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
lastScan := time.Now()
|
||||
if lastScanStr != "" {
|
||||
lastScan, err = time.Parse(time.RFC3339, lastScanStr)
|
||||
}
|
||||
|
||||
var indexes model.ArtistIndexes
|
||||
if lib.LastScanAt.After(ifModifiedSince) {
|
||||
indexes, err = api.ds.Artist(ctx).GetIndex(false, model.RoleAlbumArtist)
|
||||
if lastScan.After(ifModifiedSince) {
|
||||
indexes, err = api.ds.Artist(ctx).GetIndex(false, libId, model.RoleAlbumArtist)
|
||||
if err != nil {
|
||||
log.Error(ctx, "Error retrieving Indexes", err)
|
||||
return nil, 0, err
|
||||
}
|
||||
if len(indexes) == 0 {
|
||||
log.Debug(ctx, "No artists found in library", "libId", libId)
|
||||
return nil, 0, newError(responses.ErrorDataNotFound, "Library not found or empty")
|
||||
}
|
||||
}
|
||||
|
||||
return indexes, lib.LastScanAt.UnixMilli(), err
|
||||
return indexes, lastScan.UnixMilli(), err
|
||||
}
|
||||
|
||||
func (api *Router) getArtistIndex(r *http.Request, libId int, ifModifiedSince time.Time) (*responses.Indexes, error) {
|
||||
|
|
@ -92,7 +102,7 @@ func (api *Router) getArtistIndexID3(r *http.Request, libId int, ifModifiedSince
|
|||
|
||||
func (api *Router) GetIndexes(r *http.Request) (*responses.Subsonic, error) {
|
||||
p := req.Params(r)
|
||||
musicFolderId := p.IntOr("musicFolderId", 1)
|
||||
musicFolderId := p.IntOr("musicFolderId", 0) // 0 means all accessible libraries
|
||||
ifModifiedSince := p.TimeOr("ifModifiedSince", time.Time{})
|
||||
|
||||
res, err := api.getArtistIndex(r, musicFolderId, ifModifiedSince)
|
||||
|
|
@ -107,7 +117,8 @@ func (api *Router) GetIndexes(r *http.Request) (*responses.Subsonic, error) {
|
|||
|
||||
func (api *Router) GetArtists(r *http.Request) (*responses.Subsonic, error) {
|
||||
p := req.Params(r)
|
||||
musicFolderId := p.IntOr("musicFolderId", 1)
|
||||
musicFolderId := p.IntOr("musicFolderId", 0) // 0 means all accessible libraries
|
||||
|
||||
res, err := api.getArtistIndexID3(r, musicFolderId, time.Time{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"net/http/httptest"
|
||||
|
||||
"github.com/navidrome/navidrome/core/auth"
|
||||
"github.com/navidrome/navidrome/model"
|
||||
"github.com/navidrome/navidrome/model/request"
|
||||
"github.com/navidrome/navidrome/tests"
|
||||
|
|
@ -18,6 +19,7 @@ var _ = Describe("Browsing", func() {
|
|||
|
||||
BeforeEach(func() {
|
||||
ds = &tests.MockDataStore{}
|
||||
auth.Init(ds)
|
||||
api = &Router{ds: ds}
|
||||
ctx = context.Background()
|
||||
})
|
||||
|
|
@ -52,4 +54,134 @@ var _ = Describe("Browsing", func() {
|
|||
Expect(response.MusicFolders.Folders[2].Name).To(Equal("Test Library 3"))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("GetIndexes", func() {
|
||||
It("should validate user access to the specified musicFolderId", func() {
|
||||
// Create mock user
|
||||
user := model.User{
|
||||
ID: "user-id",
|
||||
}
|
||||
ctx = request.WithUser(ctx, user)
|
||||
|
||||
// Setup mock expectations - user only has access to library 1
|
||||
mockUserRepo := ds.User(ctx).(*tests.MockedUserRepo)
|
||||
err := mockUserRepo.SetUserLibraries("user-id", []int{1})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
// Create request with musicFolderId=2 (not accessible)
|
||||
r := httptest.NewRequest("GET", "/rest/getIndexes?musicFolderId=2", nil)
|
||||
r = r.WithContext(ctx)
|
||||
|
||||
// Call endpoint
|
||||
response, err := api.GetIndexes(r)
|
||||
|
||||
// Should return error due to lack of access
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(response).To(BeNil())
|
||||
})
|
||||
|
||||
It("should default to first accessible library when no musicFolderId specified", func() {
|
||||
// Create mock user
|
||||
user := model.User{
|
||||
ID: "user-id",
|
||||
}
|
||||
ctx = request.WithUser(ctx, user)
|
||||
|
||||
// Setup minimal mock library data for working tests
|
||||
mockLibRepo := ds.Library(ctx).(*tests.MockLibraryRepo)
|
||||
mockLibRepo.SetData(model.Libraries{
|
||||
{ID: 2, Name: "Test Library 2", Path: "/music/library2"},
|
||||
{ID: 3, Name: "Test Library 3", Path: "/music/library3"},
|
||||
})
|
||||
|
||||
// Setup mock expectations
|
||||
mockUserRepo := ds.User(ctx).(*tests.MockedUserRepo)
|
||||
err := mockUserRepo.SetUserLibraries("user-id", []int{2, 3})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
// Setup mock artist data
|
||||
mockArtistRepo := ds.Artist(ctx).(*tests.MockArtistRepo)
|
||||
mockArtistRepo.SetData(model.Artists{
|
||||
{ID: "1", Name: "Test Artist 1"},
|
||||
{ID: "2", Name: "Test Artist 2"},
|
||||
})
|
||||
|
||||
// Create request without musicFolderId
|
||||
r := httptest.NewRequest("GET", "/rest/getIndexes", nil)
|
||||
r = r.WithContext(ctx)
|
||||
|
||||
// Call endpoint
|
||||
response, err := api.GetIndexes(r)
|
||||
|
||||
// Should succeed and use first accessible library (2)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(response).ToNot(BeNil())
|
||||
Expect(response.Indexes).ToNot(BeNil())
|
||||
})
|
||||
})
|
||||
|
||||
Describe("GetArtists", func() {
|
||||
It("should validate user access to the specified musicFolderId", func() {
|
||||
// Create mock user
|
||||
user := model.User{
|
||||
ID: "user-id",
|
||||
}
|
||||
ctx = request.WithUser(ctx, user)
|
||||
|
||||
// Setup mock expectations - user only has access to library 1
|
||||
mockUserRepo := ds.User(ctx).(*tests.MockedUserRepo)
|
||||
err := mockUserRepo.SetUserLibraries("user-id", []int{1})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
// Create request with musicFolderId=3 (not accessible)
|
||||
r := httptest.NewRequest("GET", "/rest/getArtists?musicFolderId=3", nil)
|
||||
r = r.WithContext(ctx)
|
||||
|
||||
// Call endpoint
|
||||
response, err := api.GetArtists(r)
|
||||
|
||||
// Should return error due to lack of access
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(response).To(BeNil())
|
||||
})
|
||||
|
||||
It("should default to first accessible library when no musicFolderId specified", func() {
|
||||
// Create mock user
|
||||
user := model.User{
|
||||
ID: "user-id",
|
||||
}
|
||||
ctx = request.WithUser(ctx, user)
|
||||
|
||||
// Setup minimal mock library data for working tests
|
||||
mockLibRepo := ds.Library(ctx).(*tests.MockLibraryRepo)
|
||||
mockLibRepo.SetData(model.Libraries{
|
||||
{ID: 1, Name: "Test Library 1", Path: "/music/library1"},
|
||||
{ID: 2, Name: "Test Library 2", Path: "/music/library2"},
|
||||
})
|
||||
|
||||
// Setup mock expectations
|
||||
mockUserRepo := ds.User(ctx).(*tests.MockedUserRepo)
|
||||
err := mockUserRepo.SetUserLibraries("user-id", []int{1, 2})
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
// Setup mock artist data
|
||||
mockArtistRepo := ds.Artist(ctx).(*tests.MockArtistRepo)
|
||||
mockArtistRepo.SetData(model.Artists{
|
||||
{ID: "1", Name: "Test Artist 1"},
|
||||
{ID: "2", Name: "Test Artist 2"},
|
||||
})
|
||||
|
||||
// Create request without musicFolderId
|
||||
r := httptest.NewRequest("GET", "/rest/getArtists", nil)
|
||||
r = r.WithContext(ctx)
|
||||
|
||||
// Call endpoint
|
||||
response, err := api.GetArtists(r)
|
||||
|
||||
// Should succeed and use first accessible library (1)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(response).ToNot(BeNil())
|
||||
Expect(response.Artist).ToNot(BeNil())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -479,8 +479,5 @@ func buildLyricsList(mf *model.MediaFile, lyricsList model.LyricList) *responses
|
|||
// Admin users get all libraries (auto-assigned), regular users get their assigned libraries
|
||||
func getUserAccessibleLibraries(ctx context.Context, ds model.DataStore) ([]model.Library, error) {
|
||||
user := getUser(ctx)
|
||||
|
||||
// All users (admin and regular) use the same method since admin users
|
||||
// are automatically assigned to all libraries via user_library table
|
||||
return ds.User(ctx).GetUserLibraries(user.ID)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue