From dc264a64db5fa95275cff42016a8981ee0862168 Mon Sep 17 00:00:00 2001 From: Deluan Date: Sat, 28 Jun 2025 13:12:51 -0400 Subject: [PATCH] feat: enhance library management with refresh event broadcasting Signed-off-by: Deluan --- core/library.go | 14 +++++++++++++- model/user.go | 2 ++ ui/src/common/LibrarySelector.jsx | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/core/library.go b/core/library.go index 406d985a6..7abd35c8f 100644 --- a/core/library.go +++ b/core/library.go @@ -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 { diff --git a/model/user.go b/model/user.go index b250c85e3..da4813278 100644 --- a/model/user.go +++ b/model/user.go @@ -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 } diff --git a/ui/src/common/LibrarySelector.jsx b/ui/src/common/LibrarySelector.jsx index 341552015..dd96b148b 100644 --- a/ui/src/common/LibrarySelector.jsx +++ b/ui/src/common/LibrarySelector.jsx @@ -105,7 +105,7 @@ const LibrarySelector = () => { // Reload user libraries when library changes occur useRefreshOnEvents({ - events: ['library'], + events: ['library', 'user'], onRefresh: loadUserLibraries, })