feat: enhance library management with refresh event broadcasting

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan 2025-06-28 13:12:51 -04:00
commit dc264a64db
3 changed files with 16 additions and 2 deletions

View file

@ -18,6 +18,7 @@ import (
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/model/request"
"github.com/navidrome/navidrome/server/events"
"github.com/navidrome/navidrome/utils/slice"
)
// Scanner interface for triggering scans
@ -92,7 +93,18 @@ func (s *libraryService) SetUserLibraries(ctx context.Context, userID string, li
}
}
return s.ds.User(ctx).SetUserLibraries(userID, libraryIDs)
// Set user libraries
err = s.ds.User(ctx).SetUserLibraries(userID, libraryIDs)
if err != nil {
return fmt.Errorf("error setting user libraries: %w", err)
}
// Send refresh event to all clients
event := &events.RefreshResource{}
libIDs := slice.Map(libraryIDs, func(id int) string { return strconv.Itoa(id) })
event = event.With("user", userID).With("library", libIDs...)
s.broker.SendBroadcastMessage(ctx, event)
return nil
}
func (s *libraryService) ValidateLibraryAccess(ctx context.Context, userID string, libraryID int) error {

View file

@ -75,6 +75,8 @@ type UserRepository interface {
// Library association methods
GetUserLibraries(userID string) (Libraries, error)
SetUserLibraries(userID string, libraryIDs []int) error
// TODO Remove these methods in favor of SetUserLibraries
AddUserLibrary(userID string, libraryID int) error
RemoveUserLibrary(userID string, libraryID int) error
}

View file

@ -105,7 +105,7 @@ const LibrarySelector = () => {
// Reload user libraries when library changes occur
useRefreshOnEvents({
events: ['library'],
events: ['library', 'user'],
onRefresh: loadUserLibraries,
})