mirror of
https://github.com/truewhile/MeBox.git
synced 2026-09-28 11:16:37 +08:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 205c71b0ad | |||
| 5474784107 | |||
| fb92373f0d | |||
| 2b99f5f108 | |||
| 9ab32c10ca | |||
| e872223b4e | |||
| c7441e06d4 | |||
| 936be24328 | |||
| b7f2760728 | |||
| ea732e5bd7 |
@@ -94,18 +94,26 @@ func listLibrariesHandler(svc *service.Container) gin.HandlerFunc {
|
||||
out = append(out, webLibraryPayload{Library: l})
|
||||
}
|
||||
}
|
||||
// 远程 Emby 挂载库追加在本地库之后。
|
||||
// 远程 Emby 挂载库追加在本地库之后(非管理员视图仍受 allowed_library_ids 约束)。
|
||||
if svc.EmbyRemote != nil {
|
||||
if views, err := svc.EmbyRemote.RemoteLibraries(ctx); err == nil {
|
||||
remotePayloads := make([]webLibraryPayload, len(views))
|
||||
for i, v := range views {
|
||||
visibility := mediaVisibilityForRequest(c, svc)
|
||||
allowedViews := make([]service.RemoteLibraryView, 0, len(views))
|
||||
for _, v := range views {
|
||||
if !includeHidden && !service.LibraryVisibleForUser(ctx, svc.Repo, v.Library, visibility) {
|
||||
continue
|
||||
}
|
||||
allowedViews = append(allowedViews, v)
|
||||
}
|
||||
remotePayloads := make([]webLibraryPayload, len(allowedViews))
|
||||
for i, v := range allowedViews {
|
||||
remotePayloads[i] = webLibraryPayload{Library: v.Library, IsRemoteEmby: true, RemoteSource: v.AccountName}
|
||||
}
|
||||
if withPreview && len(views) > 0 {
|
||||
if withPreview && len(allowedViews) > 0 {
|
||||
const maxRemotePreviewWorkers = 6
|
||||
sem := make(chan struct{}, maxRemotePreviewWorkers)
|
||||
var wg sync.WaitGroup
|
||||
for i, v := range views {
|
||||
for i, v := range allowedViews {
|
||||
i, v := i, v
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
@@ -151,6 +159,12 @@ func getLibraryHandler(svc *service.Container) gin.HandlerFunc {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
role, _ := c.Get(middleware.CtxUserRole)
|
||||
includeHidden := role == "admin" && (c.Query("include_hidden") == "1" || c.Query("include_hidden") == "true" || c.Query("all") == "1")
|
||||
if !includeHidden && !service.LibraryVisibleForUser(ctx, svc.Repo, view.Library, mediaVisibilityForRequest(c, svc)) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, webLibraryPayload{Library: view.Library, IsRemoteEmby: true, RemoteSource: view.AccountName})
|
||||
return
|
||||
}
|
||||
@@ -330,6 +344,10 @@ func listMediaHandler(svc *service.Container) gin.HandlerFunc {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
if !service.EmbyMountLibraryAllowed(mediaVisibilityForRequest(c, svc), mount) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
itemTypes := ""
|
||||
if view, err := svc.EmbyRemote.RemoteLibraryByID(ctx, mountID, remoteID); err == nil && view != nil {
|
||||
itemTypes = remoteLibraryItemTypes(view.CollectionType)
|
||||
@@ -397,6 +415,10 @@ func getMediaHandler(svc *service.Container) gin.HandlerFunc {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
if !service.EmbyMountLibraryAllowed(mediaVisibilityForRequest(c, svc), mount) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
m, err := svc.EmbyRemote.RemoteMediaDetail(ctx, mount, acct, remoteID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
@@ -579,6 +601,10 @@ func streamHandler(svc *service.Container) gin.HandlerFunc {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
if !service.EmbyMountLibraryAllowed(mediaVisibilityForRequest(c, svc), mount) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
if mount.ProxyPlay {
|
||||
if err := svc.Emby.ProxyRemoteVideoStream(ctx, c.Writer, c.Request, mountID, remoteID); err != nil {
|
||||
if !c.Writer.Written() {
|
||||
|
||||
@@ -74,6 +74,10 @@ func listLibrarySeriesHandler(svc *service.Container) gin.HandlerFunc {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
if !service.EmbyMountLibraryAllowed(mediaVisibilityForRequest(c, svc), mount) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
cards, err := svc.EmbyRemote.RemoteSeriesCards(ctx, mount, acct, remoteID)
|
||||
if err != nil {
|
||||
writeInternalOrCanceled(c, err)
|
||||
@@ -165,6 +169,10 @@ func listLibrarySeriesEpisodesHandler(svc *service.Container) gin.HandlerFunc {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
if !service.EmbyMountLibraryAllowed(mediaVisibilityForRequest(c, svc), mount) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
items, err := svc.EmbyRemote.RemoteEpisodes(ctx, mount, acct, remoteSeriesID)
|
||||
if err != nil {
|
||||
writeInternalOrCanceled(c, err)
|
||||
@@ -207,6 +215,10 @@ func listMediaEpisodesHandler(svc *service.Container) gin.HandlerFunc {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
if !service.EmbyMountLibraryAllowed(mediaVisibilityForRequest(c, svc), mount) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
}
|
||||
items, err := svc.EmbyRemote.RemoteEpisodes(ctx, mount, acct, remoteID)
|
||||
if err != nil {
|
||||
writeInternalOrCanceled(c, err)
|
||||
|
||||
@@ -156,6 +156,9 @@ func (e *EmbyService) Items(ctx context.Context, p ItemsParams) (map[string]any,
|
||||
if mount == nil || acct == nil {
|
||||
return emptyItemsEnvelope(p.StartIndex), nil
|
||||
}
|
||||
if !EmbyMountLibraryAllowed(e.mediaVisibility(ctx, p.UserID), mount) {
|
||||
return emptyItemsEnvelope(p.StartIndex), nil
|
||||
}
|
||||
out, err := e.remote.RemoteItems(ctx, mount, acct, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -275,6 +278,9 @@ func (e *EmbyService) aggregatedSearch(ctx context.Context, p ItemsParams) (map[
|
||||
if !m.Enabled {
|
||||
continue
|
||||
}
|
||||
if !EmbyMountLibraryAllowed(e.mediaVisibility(ctx, p.UserID), &m) {
|
||||
continue
|
||||
}
|
||||
acct := e.remote.AccountByID(ctx, m.AccountID)
|
||||
if acct == nil {
|
||||
continue
|
||||
|
||||
@@ -21,6 +21,9 @@ func (e *EmbyService) Item(ctx context.Context, mediaID, userID string) (map[str
|
||||
if mount == nil || acct == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if !EmbyMountLibraryAllowed(e.mediaVisibility(ctx, userID), mount) {
|
||||
return nil, nil
|
||||
}
|
||||
out, err := e.remote.RemoteItem(ctx, mount, acct, remoteID)
|
||||
if err != nil || out == nil {
|
||||
return out, err
|
||||
@@ -109,6 +112,9 @@ func (e *EmbyService) LatestItems(ctx context.Context, userID, parentID string,
|
||||
if mount == nil || acct == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if !EmbyMountLibraryAllowed(e.mediaVisibility(ctx, userID), mount) {
|
||||
return nil, nil
|
||||
}
|
||||
out, err := e.remote.RemoteLatest(ctx, mount, acct, remoteParent, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/truewhile/MeBox/internal/model"
|
||||
)
|
||||
|
||||
func TestViewsHidesDisallowedMountedEmbyLibraries(t *testing.T) {
|
||||
svc := newTestEmbyService(t)
|
||||
if err := svc.repo.DB.AutoMigrate(&model.EmbyMount{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
local := model.Library{Name: "Local", Path: "/media/local", Type: "movie", Enabled: true}
|
||||
if err := svc.repo.Library.Create(t.Context(), &local); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mount := &model.EmbyMount{
|
||||
AccountID: "acct-1",
|
||||
RemoteViewID: "view-1",
|
||||
RemoteViewName: "Remote Movies",
|
||||
Enabled: true,
|
||||
}
|
||||
if err := svc.repo.EmbyMount.Create(t.Context(), mount); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
remoteID := EncodeEmbyRemoteID(mount.ID, mount.RemoteViewID)
|
||||
|
||||
user := &model.User{Username: "viewer", PasswordHash: "hash", Role: "user"}
|
||||
allowed, err := json.Marshal([]string{local.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
user.AllowedLibraryIDs = string(allowed)
|
||||
if err := svc.repo.User.Create(t.Context(), user); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Without a live remote service, remoteViews is empty; assert helper ACL instead
|
||||
// and that local Views still honor the allow-list.
|
||||
views, err := svc.Views(t.Context(), user.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Views: %v", err)
|
||||
}
|
||||
items := views["Items"].([]map[string]any)
|
||||
for _, item := range items {
|
||||
if id, _ := item["Id"].(string); id == remoteID {
|
||||
t.Fatalf("disallowed remote library should not appear in Views: %#v", item)
|
||||
}
|
||||
}
|
||||
if !EmbyMountLibraryAllowed(MediaVisibility{AllowedLibraryIDs: []string{local.ID, remoteID}}, mount) {
|
||||
t.Fatal("expected remote library allowed when listed")
|
||||
}
|
||||
if EmbyMountLibraryAllowed(MediaVisibility{AllowedLibraryIDs: []string{local.ID}}, mount) {
|
||||
t.Fatal("expected remote library denied when not listed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLibraryIDAllowed(t *testing.T) {
|
||||
if !LibraryIDAllowed(MediaVisibility{}, "any") {
|
||||
t.Fatal("empty allow-list should allow all")
|
||||
}
|
||||
if LibraryIDAllowed(MediaVisibility{AllowedLibraryIDs: []string{"a"}}, "b") {
|
||||
t.Fatal("missing id should be denied")
|
||||
}
|
||||
if !LibraryIDAllowed(MediaVisibility{AllowedLibraryIDs: []string{"a", "b"}}, "b") {
|
||||
t.Fatal("listed id should be allowed")
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,9 @@ func (e *EmbyService) PlaybackInfo(ctx context.Context, mediaID, userID string)
|
||||
if err != nil {
|
||||
return nil, ErrEmbyRemoteNotFound
|
||||
}
|
||||
if !EmbyMountLibraryAllowed(e.mediaVisibility(ctx, userID), mount) {
|
||||
return nil, ErrEmbyRemoteNotFound
|
||||
}
|
||||
out, err := e.remote.RemotePlaybackInfo(ctx, mount, acct, remoteID, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -125,7 +126,8 @@ func (e *EmbyService) userPayload(u *model.User) map[string]any {
|
||||
}
|
||||
|
||||
// Views 返回 Emby 中"虚拟根目录"——每个 library 一个条目,外加所有启用的
|
||||
// 远程 Emby 挂载的媒体库(联邦聚合)。
|
||||
// 远程 Emby 挂载的媒体库(联邦聚合)。顺序遵循用户置顶偏好:置顶库靠前,
|
||||
// 未置顶保持原有 sort_order / 远程挂载顺序。
|
||||
func (e *EmbyService) Views(ctx context.Context, userID string) (map[string]any, error) {
|
||||
libs, err := e.repo.Library.List(ctx)
|
||||
if err != nil {
|
||||
@@ -141,11 +143,60 @@ func (e *EmbyService) Views(ctx context.Context, userID string) (map[string]any,
|
||||
items = append(items, e.libraryAsView(ctx, &l))
|
||||
}
|
||||
for _, remote := range e.remoteViews(ctx) {
|
||||
id, _ := remote["Id"].(string)
|
||||
if !LibraryIDAllowed(visibility, id) {
|
||||
continue
|
||||
}
|
||||
items = append(items, remote)
|
||||
}
|
||||
items = sortViewItemsByPinnedIDs(items, e.pinnedLibraryIDsForUser(ctx, userID))
|
||||
return map[string]any{"Items": items, "TotalRecordCount": len(items), "StartIndex": 0}, nil
|
||||
}
|
||||
|
||||
func (e *EmbyService) pinnedLibraryIDsForUser(ctx context.Context, userID string) []string {
|
||||
if e == nil || e.repo == nil || e.repo.User == nil || strings.TrimSpace(userID) == "" {
|
||||
return nil
|
||||
}
|
||||
user, err := e.repo.User.FindByID(ctx, userID)
|
||||
if err != nil || user == nil {
|
||||
return nil
|
||||
}
|
||||
return user.DecodePinnedLibraryIDs()
|
||||
}
|
||||
|
||||
func sortViewItemsByPinnedIDs(items []map[string]any, pinnedIDs []string) []map[string]any {
|
||||
if len(items) < 2 || len(pinnedIDs) == 0 {
|
||||
return items
|
||||
}
|
||||
rank := make(map[string]int, len(pinnedIDs))
|
||||
for i, id := range pinnedIDs {
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
if _, exists := rank[id]; !exists {
|
||||
rank[id] = i
|
||||
}
|
||||
}
|
||||
if len(rank) == 0 {
|
||||
return items
|
||||
}
|
||||
sorted := append([]map[string]any(nil), items...)
|
||||
sort.SliceStable(sorted, func(i, j int) bool {
|
||||
iID, _ := sorted[i]["Id"].(string)
|
||||
jID, _ := sorted[j]["Id"].(string)
|
||||
iRank, iPinned := rank[iID]
|
||||
jRank, jPinned := rank[jID]
|
||||
if iPinned != jPinned {
|
||||
return iPinned
|
||||
}
|
||||
if iPinned && jPinned {
|
||||
return iRank < jRank
|
||||
}
|
||||
return false
|
||||
})
|
||||
return sorted
|
||||
}
|
||||
|
||||
// remoteViews 返回全部启用挂载的远程媒体库视图(只有显式挂载的库才出现)。
|
||||
func (e *EmbyService) remoteViews(ctx context.Context) []map[string]any {
|
||||
if e == nil || e.remote == nil {
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/truewhile/MeBox/internal/model"
|
||||
)
|
||||
|
||||
func TestViewsOrdersPinnedLibrariesFirst(t *testing.T) {
|
||||
svc := newTestEmbyService(t)
|
||||
first := model.Library{Name: "AAA", Path: "/media/a", Type: "movie", Enabled: true, SortOrder: 0}
|
||||
second := model.Library{Name: "BBB", Path: "/media/b", Type: "movie", Enabled: true, SortOrder: 1}
|
||||
third := model.Library{Name: "CCC", Path: "/media/c", Type: "movie", Enabled: true, SortOrder: 2}
|
||||
for _, lib := range []*model.Library{&first, &second, &third} {
|
||||
if err := svc.repo.Library.Create(t.Context(), lib); err != nil {
|
||||
t.Fatalf("create library: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
user := &model.User{Username: "viewer", PasswordHash: "hash", Role: "user"}
|
||||
pinned, err := json.Marshal([]string{third.ID, first.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
user.PinnedLibraryIDs = string(pinned)
|
||||
if err := svc.repo.User.Create(t.Context(), user); err != nil {
|
||||
t.Fatalf("create user: %v", err)
|
||||
}
|
||||
|
||||
views, err := svc.Views(t.Context(), user.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Views: %v", err)
|
||||
}
|
||||
items := views["Items"].([]map[string]any)
|
||||
if len(items) != 3 {
|
||||
t.Fatalf("expected 3 views, got %d", len(items))
|
||||
}
|
||||
got := []string{items[0]["Id"].(string), items[1]["Id"].(string), items[2]["Id"].(string)}
|
||||
want := []string{third.ID, first.ID, second.ID}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("Views order = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSortViewItemsByPinnedIDsKeepsUnpinnedOrder(t *testing.T) {
|
||||
items := []map[string]any{
|
||||
{"Id": "a", "Name": "A"},
|
||||
{"Id": "b", "Name": "B"},
|
||||
{"Id": "c", "Name": "C"},
|
||||
{"Id": "d", "Name": "D"},
|
||||
}
|
||||
sorted := sortViewItemsByPinnedIDs(items, []string{"c", "a"})
|
||||
got := make([]string, len(sorted))
|
||||
for i, item := range sorted {
|
||||
got[i] = item["Id"].(string)
|
||||
}
|
||||
want := []string{"c", "a", "b", "d"}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("order = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -119,6 +119,9 @@ func (p *ProfileService) SetPinnedLibraryIDs(ctx context.Context, userID string,
|
||||
return nil, err
|
||||
}
|
||||
normalized := filterPinnedLibraryIDs(normalizePinnedLibraryIDs(ids), accessible)
|
||||
if normalized == nil {
|
||||
normalized = []string{}
|
||||
}
|
||||
raw, err := json.Marshal(normalized)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -146,6 +149,22 @@ func (p *ProfileService) accessibleLibraryIDSet(ctx context.Context, visibility
|
||||
}
|
||||
out[lib.ID] = struct{}{}
|
||||
}
|
||||
// Mounted Emby libraries are not rows in the local libraries table; their
|
||||
// web IDs are embyremote~{mountID}~{remoteViewID}. Include enabled mounts
|
||||
// from the mount table so pinning them does not get stripped (and so a
|
||||
// pin-save that includes remotes cannot accidentally wipe local pins).
|
||||
if p.repo.EmbyMount != nil {
|
||||
mounts, err := p.repo.EmbyMount.List(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, mount := range mounts {
|
||||
if !mount.Enabled || strings.TrimSpace(mount.RemoteViewID) == "" {
|
||||
continue
|
||||
}
|
||||
out[EncodeEmbyRemoteID(mount.ID, mount.RemoteViewID)] = struct{}{}
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ func TestProfilePinnedLibrariesFiltersInaccessibleAndPreservesOrder(t *testing.T
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.User{}, &model.Library{}); err != nil {
|
||||
if err := db.AutoMigrate(&model.User{}, &model.Library{}, &model.EmbyMount{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repos := repository.New(db)
|
||||
@@ -59,3 +59,65 @@ func TestProfilePinnedLibrariesFiltersInaccessibleAndPreservesOrder(t *testing.T
|
||||
t.Fatalf("GetPinnedLibraryIDs = %v, want %v", loaded, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProfilePinnedLibrariesKeepsMountedEmbyAndLocalPins(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.User{}, &model.Library{}, &model.EmbyMount{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repos := repository.New(db)
|
||||
svc := NewProfileService(zap.NewNop(), repos)
|
||||
|
||||
user := &model.User{Username: "viewer", PasswordHash: "hash", Role: "user"}
|
||||
if err := repos.User.Create(t.Context(), user); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
local := &model.Library{Name: "Movies", Path: "/media/movies", Type: "movie", Enabled: true}
|
||||
if err := repos.Library.Create(t.Context(), local); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mount := &model.EmbyMount{
|
||||
AccountID: "acct-1",
|
||||
RemoteViewID: "view-42",
|
||||
RemoteViewName: "Remote Movies",
|
||||
Enabled: true,
|
||||
}
|
||||
if err := repos.EmbyMount.Create(t.Context(), mount); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
remoteID := EncodeEmbyRemoteID(mount.ID, mount.RemoteViewID)
|
||||
disabled := &model.EmbyMount{
|
||||
AccountID: "acct-1",
|
||||
RemoteViewID: "view-99",
|
||||
Enabled: true,
|
||||
}
|
||||
if err := repos.EmbyMount.Create(t.Context(), disabled); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Model(disabled).Update("enabled", false).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
disabledID := EncodeEmbyRemoteID(disabled.ID, disabled.RemoteViewID)
|
||||
|
||||
got, err := svc.SetPinnedLibraryIDs(t.Context(), user.ID, []string{
|
||||
local.ID, remoteID, disabledID, "embyremote~missing~view",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("SetPinnedLibraryIDs: %v", err)
|
||||
}
|
||||
want := []string{local.ID, remoteID}
|
||||
if len(got) != len(want) || got[0] != want[0] || got[1] != want[1] {
|
||||
t.Fatalf("SetPinnedLibraryIDs = %v, want %v", got, want)
|
||||
}
|
||||
|
||||
loaded, err := svc.GetPinnedLibraryIDs(t.Context(), user.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetPinnedLibraryIDs: %v", err)
|
||||
}
|
||||
if len(loaded) != len(want) || loaded[0] != want[0] || loaded[1] != want[1] {
|
||||
t.Fatalf("GetPinnedLibraryIDs = %v, want %v", loaded, want)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,20 +131,39 @@ func DecodeAllowedLibraryIDs(raw string) []string {
|
||||
return out
|
||||
}
|
||||
|
||||
// LibraryIDAllowed reports whether libraryID is permitted by the allow-list.
|
||||
// An empty AllowedLibraryIDs means unrestricted access.
|
||||
func LibraryIDAllowed(visibility MediaVisibility, libraryID string) bool {
|
||||
if len(visibility.AllowedLibraryIDs) == 0 {
|
||||
return true
|
||||
}
|
||||
for _, id := range visibility.AllowedLibraryIDs {
|
||||
if id == libraryID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// EmbyMountLibraryID is the web/Emby library id for a mounted remote view.
|
||||
func EmbyMountLibraryID(mount *model.EmbyMount) string {
|
||||
if mount == nil {
|
||||
return ""
|
||||
}
|
||||
return EncodeEmbyRemoteID(mount.ID, mount.RemoteViewID)
|
||||
}
|
||||
|
||||
// EmbyMountLibraryAllowed reports whether a mounted Emby library is allowed for
|
||||
// the given visibility policy.
|
||||
func EmbyMountLibraryAllowed(visibility MediaVisibility, mount *model.EmbyMount) bool {
|
||||
return LibraryIDAllowed(visibility, EmbyMountLibraryID(mount))
|
||||
}
|
||||
|
||||
// LibraryVisibleForUser applies profile library limits and adult-directory
|
||||
// hiding to a library card/folder.
|
||||
func LibraryVisibleForUser(ctx context.Context, repo *repository.Container, lib model.Library, visibility MediaVisibility) bool {
|
||||
if len(visibility.AllowedLibraryIDs) > 0 {
|
||||
found := false
|
||||
for _, id := range visibility.AllowedLibraryIDs {
|
||||
if id == lib.ID {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return false
|
||||
}
|
||||
if !LibraryIDAllowed(visibility, lib.ID) {
|
||||
return false
|
||||
}
|
||||
if visibility.IncludeNSFW {
|
||||
return true
|
||||
|
||||
@@ -278,12 +278,19 @@ export function AdminUserLibrariesDialog({
|
||||
<div className="min-w-0 flex-1">
|
||||
<p className="truncate text-xs font-semibold text-ink-600">
|
||||
{lib.name}
|
||||
{lib.is_remote_emby ? (
|
||||
<span className="ml-1.5 rounded bg-sky-50 px-1.5 py-0.5 text-[10px] font-bold text-sky-700">
|
||||
Emby 挂载
|
||||
</span>
|
||||
) : null}
|
||||
</p>
|
||||
<p
|
||||
className="truncate text-[10px] text-sand-500"
|
||||
title={lib.path}
|
||||
title={lib.is_remote_emby ? lib.remote_source || lib.name : lib.path}
|
||||
>
|
||||
{lib.type} · {libraryDisplayPath(lib.path)}
|
||||
{lib.is_remote_emby
|
||||
? `远程 · ${lib.remote_source || 'Emby'}`
|
||||
: `${lib.type} · ${libraryDisplayPath(lib.path)}`}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
|
||||
import {
|
||||
loadPinnedLibraryIds,
|
||||
@@ -10,16 +10,32 @@ export function usePinnedLibraries() {
|
||||
const [pinnedIds, setPinnedIds] = useState<string[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [syncing, setSyncing] = useState(false)
|
||||
const [loadError, setLoadError] = useState(false)
|
||||
const loadedRef = useRef(false)
|
||||
const pinnedIdsRef = useRef<string[]>([])
|
||||
const syncingRef = useRef(false)
|
||||
|
||||
useEffect(() => {
|
||||
pinnedIdsRef.current = pinnedIds
|
||||
}, [pinnedIds])
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false
|
||||
setLoading(true)
|
||||
setLoadError(false)
|
||||
loadedRef.current = false
|
||||
loadPinnedLibraryIds()
|
||||
.then((ids) => {
|
||||
if (!cancelled) setPinnedIds(ids)
|
||||
if (cancelled) return
|
||||
loadedRef.current = true
|
||||
pinnedIdsRef.current = ids
|
||||
setPinnedIds(ids)
|
||||
setLoadError(false)
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled) setPinnedIds([])
|
||||
if (cancelled) return
|
||||
loadedRef.current = false
|
||||
setLoadError(true)
|
||||
})
|
||||
.finally(() => {
|
||||
if (!cancelled) setLoading(false)
|
||||
@@ -30,23 +46,30 @@ export function usePinnedLibraries() {
|
||||
}, [])
|
||||
|
||||
const togglePin = useCallback(async (libraryId: string) => {
|
||||
let previous: string[] = []
|
||||
let optimistic: string[] = []
|
||||
setPinnedIds((current) => {
|
||||
previous = current
|
||||
optimistic = togglePinnedLibraryId(current, libraryId)
|
||||
return optimistic
|
||||
})
|
||||
if (!loadedRef.current || loading || loadError || syncingRef.current) return
|
||||
|
||||
// Compute the next list synchronously from a ref. Do NOT capture the next
|
||||
// value inside setState updater callbacks — React may defer those, leaving
|
||||
// optimistic as [] and wiping the server-side pin list.
|
||||
const previous = pinnedIdsRef.current
|
||||
const optimistic = togglePinnedLibraryId(previous, libraryId)
|
||||
|
||||
pinnedIdsRef.current = optimistic
|
||||
setPinnedIds(optimistic)
|
||||
syncingRef.current = true
|
||||
setSyncing(true)
|
||||
try {
|
||||
const saved = await savePinnedLibraryIds(optimistic)
|
||||
pinnedIdsRef.current = saved
|
||||
setPinnedIds(saved)
|
||||
} catch {
|
||||
pinnedIdsRef.current = previous
|
||||
setPinnedIds(previous)
|
||||
} finally {
|
||||
syncingRef.current = false
|
||||
setSyncing(false)
|
||||
}
|
||||
}, [])
|
||||
}, [loading, loadError])
|
||||
|
||||
return { pinnedIds, loading, syncing, togglePin }
|
||||
return { pinnedIds, loading, syncing, loadError, togglePin }
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import toast from 'react-hot-toast'
|
||||
import { playbackAPI } from '../api/playback'
|
||||
import { MediaCard } from '../components/MediaCard'
|
||||
import type { Media } from '../types'
|
||||
import { favouriteMediaLink } from '../utils/mediaNavigation'
|
||||
|
||||
export function FavouritesPage() {
|
||||
const [items, setItems] = useState<Media[]>([])
|
||||
@@ -75,7 +74,7 @@ export function FavouritesPage() {
|
||||
{items.length > 0 && (
|
||||
<div className="grid grid-cols-2 gap-4 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6">
|
||||
{items.map((m) => (
|
||||
<MediaCard key={m.id} media={m} linkTo={favouriteMediaLink(m)} />
|
||||
<MediaCard key={m.id} media={m} />
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -155,6 +155,7 @@ export function ProfileLibraryAccessField({
|
||||
}
|
||||
>
|
||||
{library.name}
|
||||
{library.is_remote_emby ? ' · Emby' : ''}
|
||||
</button>
|
||||
))}
|
||||
{libraries.length === 0 && (
|
||||
|
||||
Reference in New Issue
Block a user