mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-20 09:08:50 +01:00
feat(artist): add Share and Download actions to the Artist detail page (#5944)
* feat(ui): add Share button to artist detail page * feat(ui): add Download button to artist detail page * fix(ui): scope artist share/download to album-artist content Gate the artist Share/Download actions on album-artist stats and show the album-artist size, since ZipArtist and the share query only cover album_artist_id songs. Previously the total (role-inclusive) size was shown and guest-only artists could produce an empty archive. Applies to the artist toolbar, the shared context menu, and the download dialog title. * fix: match artist download/share to album-artist participation ZipArtist and the artist share query filtered the deprecated album_artist_id column, which only stores the first album artist of a track. Secondary album-artists (co-credited but not first) got an empty download/share even though the UI offered it. Filter by the album-artist role participation instead, matching the artist's album-artist stats used to gate the actions. Also cover the artist-specific size branch of the download dialog. * fix(share): scope artist shares to the owner's libraries The artist share query broadened to album-artist participation, which could pull a secondary album artist's tracks from libraries the (non-admin) share owner cannot access into the public share. Load the artist share as the owner so their library access is applied, mirroring how playlist shares already work. Adds a repository test covering co-album-artist inclusion and library scoping. * test(share): assert album participation branch of artist shares Link the co-album-artist fixtures to albums and assert share.Albums (used by Subsonic getShares) includes the accessible album and excludes the one in a library the owner cannot access, so the album participation + scoping branch is covered too. * fix: exclude missing files from artist download/share actions An artist's stats still count files that went missing, so the toolbar/context menu could offer Download/Share for an artist whose files are all gone, while the share query (missing=false) returns nothing and downloads open dead paths. Hide the actions when the artist is missing and exclude missing files from ZipArtist, matching the share semantics. * refactor: dedupe artist download-size and share-owner lookups Extract the 'album-artist download size (or none when missing)' rule into a single artistDownloadSize() helper shared by the toolbar, context menu, and download dialog, and factor the duplicated share-owner context lookup into a shareRepository.ownerContext() method used by both the artist and playlist share cases. * refactor(ui): move artistDownloadSize helper to common utils is for domain-agnostic, potentially portable code; this helper is Navidrome-specific (artist stats shape), so it belongs in common. Consumers import it directly from common/artist to avoid pulling in the common barrel.
This commit is contained in:
parent
8978c7b9fa
commit
752b38609c
13 changed files with 411 additions and 19 deletions
|
|
@ -82,13 +82,20 @@ func (r *shareRepository) loadMedia(share *model.Share) error {
|
|||
}
|
||||
switch share.ResourceType {
|
||||
case "artist":
|
||||
albumRepo := NewAlbumRepository(r.ctx, r.db)
|
||||
share.Albums, err = albumRepo.GetAll(model.QueryOptions{Filters: noMissing(Eq{"album_artist_id": ids}), Sort: "artist"})
|
||||
// Match by album-artist participation, not the deprecated album_artist_id
|
||||
// column (first album artist only), so co-album-artists are included too.
|
||||
// Load as the share owner so their library access is applied.
|
||||
ctx, err := r.ownerContext(share)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mfRepo := NewMediaFileRepository(r.ctx, r.db)
|
||||
share.Tracks, err = mfRepo.GetAll(model.QueryOptions{Filters: noMissing(Eq{"album_artist_id": ids}), Sort: "artist"})
|
||||
albumRepo := NewAlbumRepository(ctx, r.db)
|
||||
share.Albums, err = albumRepo.GetAll(model.QueryOptions{Filters: noMissing(ParticipantIDFilter("album", ids, model.RoleAlbumArtist)), Sort: "artist"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
mfRepo := NewMediaFileRepository(ctx, r.db)
|
||||
share.Tracks, err = mfRepo.GetAll(model.QueryOptions{Filters: noMissing(ParticipantIDFilter("media_file", ids, model.RoleAlbumArtist)), Sort: "artist"})
|
||||
return err
|
||||
case "album":
|
||||
albumRepo := NewAlbumRepository(r.ctx, r.db)
|
||||
|
|
@ -101,14 +108,10 @@ func (r *shareRepository) loadMedia(share *model.Share) error {
|
|||
return err
|
||||
case "playlist":
|
||||
// Load tracks as the share owner so their library access is applied.
|
||||
owner, err := NewUserRepository(r.ctx, r.db).Get(share.UserID)
|
||||
ctx, err := r.ownerContext(share)
|
||||
if err != nil {
|
||||
return fmt.Errorf("loading share owner %q: %w", share.UserID, err)
|
||||
return err
|
||||
}
|
||||
if owner == nil {
|
||||
return fmt.Errorf("share owner %q not found", share.UserID)
|
||||
}
|
||||
ctx := request.WithUser(r.ctx, *owner)
|
||||
plsRepo := NewPlaylistRepository(ctx, r.db)
|
||||
// Tracks returns nil when the playlist is no longer visible to the owner
|
||||
// (e.g. it was made private after the share was created); leave the share
|
||||
|
|
@ -133,6 +136,19 @@ func (r *shareRepository) loadMedia(share *model.Share) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// ownerContext returns a context scoped to the share owner, so repository
|
||||
// queries apply the owner's library access when a public share is rendered.
|
||||
func (r *shareRepository) ownerContext(share *model.Share) (context.Context, error) {
|
||||
owner, err := NewUserRepository(r.ctx, r.db).Get(share.UserID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loading share owner %q: %w", share.UserID, err)
|
||||
}
|
||||
if owner == nil {
|
||||
return nil, fmt.Errorf("share owner %q not found", share.UserID)
|
||||
}
|
||||
return request.WithUser(r.ctx, *owner), nil
|
||||
}
|
||||
|
||||
func sortByIdPosition(mfs model.MediaFiles, ids []string) model.MediaFiles {
|
||||
m := map[string]int{}
|
||||
for i, mf := range mfs {
|
||||
|
|
|
|||
|
|
@ -228,6 +228,89 @@ var _ = Describe("ShareRepository", func() {
|
|||
})
|
||||
})
|
||||
|
||||
Describe("Artist share library scoping", func() {
|
||||
var otherLib model.Library
|
||||
var owner model.User
|
||||
const primaryID = "share-aa-primary"
|
||||
const secondaryID = "share-aa-secondary"
|
||||
|
||||
BeforeEach(func() {
|
||||
adminCtx := request.WithUser(log.NewContext(GinkgoT().Context()), adminUser)
|
||||
b := GetDBXBuilder()
|
||||
|
||||
// A second library the owner has no access to
|
||||
lr := NewLibraryRepository(adminCtx, b)
|
||||
otherLib = model.Library{ID: 0, Name: "Artist Share Other Library", Path: "/share/artist/other"}
|
||||
Expect(lr.Put(&otherLib)).To(Succeed())
|
||||
|
||||
ar := NewArtistRepository(adminCtx, b)
|
||||
Expect(createArtistWithLibrary(ar, &model.Artist{ID: primaryID, Name: "AA Primary", OrderArtistName: "aa primary"}, 1)).To(Succeed())
|
||||
Expect(createArtistWithLibrary(ar, &model.Artist{ID: secondaryID, Name: "AA Secondary", OrderArtistName: "aa secondary"}, 1)).To(Succeed())
|
||||
|
||||
// Secondary is a co-album-artist (not the first): album_artist_id points at
|
||||
// primary, so the legacy-column filter would miss both tracks.
|
||||
aaParticipants := model.Participants{model.RoleAlbumArtist: {
|
||||
{Artist: model.Artist{ID: primaryID, Name: "AA Primary"}},
|
||||
{Artist: model.Artist{ID: secondaryID, Name: "AA Secondary"}},
|
||||
}}
|
||||
alr := NewAlbumRepository(adminCtx, b)
|
||||
Expect(alr.Put(&model.Album{ID: "art-album-ok", LibraryID: 1, Name: "Art Album OK", AlbumArtistID: primaryID, AlbumArtist: "AA Primary", Participants: aaParticipants})).To(Succeed())
|
||||
Expect(alr.Put(&model.Album{ID: "art-album-other", LibraryID: otherLib.ID, Name: "Art Album Other", AlbumArtistID: primaryID, AlbumArtist: "AA Primary", Participants: aaParticipants})).To(Succeed())
|
||||
|
||||
mr := NewMediaFileRepository(adminCtx, b)
|
||||
Expect(mr.Put(&model.MediaFile{ID: "art-ok", LibraryID: 1, AlbumID: "art-album-ok", Path: "a/ok.mp3", Title: "ArtOK", AlbumArtistID: primaryID, Participants: aaParticipants})).To(Succeed())
|
||||
Expect(mr.Put(&model.MediaFile{ID: "art-other", LibraryID: otherLib.ID, AlbumID: "art-album-other", Path: "a/other.mp3", Title: "ArtOther", AlbumArtistID: primaryID, Participants: aaParticipants})).To(Succeed())
|
||||
|
||||
// Non-admin owner with access to library 1 only
|
||||
owner = createUserWithLibraries("artist-share-owner", []int{1})
|
||||
ur := NewUserRepository(adminCtx, b)
|
||||
Expect(ur.Put(&owner)).To(Succeed())
|
||||
Expect(ur.SetUserLibraries(owner.ID, []int{1})).To(Succeed())
|
||||
|
||||
_, err := b.NewQuery(`
|
||||
INSERT INTO share (id, user_id, description, resource_type, resource_ids, created_at, updated_at)
|
||||
VALUES ({:id}, {:user}, {:desc}, {:type}, {:ids}, {:created}, {:updated})
|
||||
`).Bind(map[string]any{
|
||||
"id": "art-share", "user": owner.ID, "desc": "Artist scope share",
|
||||
"type": "artist", "ids": secondaryID, "created": time.Now(), "updated": time.Now(),
|
||||
}).Execute()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
adminCtx := request.WithUser(log.NewContext(GinkgoT().Context()), adminUser)
|
||||
b := GetDBXBuilder()
|
||||
_, _ = b.NewQuery(`DELETE FROM share WHERE id = 'art-share'`).Execute()
|
||||
mr := NewMediaFileRepository(adminCtx, b).(*mediaFileRepository)
|
||||
_, _ = mr.executeSQL(squirrel.Delete("media_file").Where(squirrel.Eq{"id": []string{"art-ok", "art-other"}}))
|
||||
alr := NewAlbumRepository(adminCtx, b).(*albumRepository)
|
||||
_, _ = alr.executeSQL(squirrel.Delete("album").Where(squirrel.Eq{"id": []string{"art-album-ok", "art-album-other"}}))
|
||||
ar := NewArtistRepository(adminCtx, b).(*artistRepository)
|
||||
_, _ = ar.executeSQL(squirrel.Delete("artist").Where(squirrel.Eq{"id": []string{primaryID, secondaryID}}))
|
||||
lr := NewLibraryRepository(adminCtx, b).(*libraryRepository)
|
||||
_ = lr.delete(squirrel.Eq{"id": otherLib.ID})
|
||||
_ = NewUserRepository(adminCtx, b).Delete(owner.ID)
|
||||
})
|
||||
|
||||
It("includes co-album-artist tracks the owner can access and excludes those they cannot", func() {
|
||||
// Read as admin (mimics the public-share render path); loadMedia must still
|
||||
// scope to the owner's libraries.
|
||||
adminRepo := NewShareRepository(request.WithUser(log.NewContext(GinkgoT().Context()), adminUser), GetDBXBuilder())
|
||||
share, err := adminRepo.Get("art-share")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
Expect(share.Tracks).To(ContainElement(HaveField("ID", "art-ok")),
|
||||
"a co-album-artist track (not matched by album_artist_id) must be included")
|
||||
Expect(share.Tracks).ToNot(ContainElement(HaveField("ID", "art-other")),
|
||||
"a track outside the owner's libraries must not appear in the share")
|
||||
|
||||
Expect(share.Albums).To(ContainElement(HaveField("ID", "art-album-ok")),
|
||||
"a co-album-artist album must be included")
|
||||
Expect(share.Albums).ToNot(ContainElement(HaveField("ID", "art-album-other")),
|
||||
"an album outside the owner's libraries must not appear in the share")
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Ownership Checks", func() {
|
||||
var ownerUser = model.User{ID: "2222", UserName: "regular-user"}
|
||||
var otherUser = model.User{ID: "3333", UserName: "third-user"}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue