mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-07 22:06:38 +08:00
fix full library display and cloud search visibility
This commit is contained in:
@@ -145,6 +145,17 @@ func TestListMediaVisibleIncludesMergedCloudLibraryItems(t *testing.T) {
|
||||
if total != 2 || !slices.Equal(mediaTitles(items), []string{"云盘剧", "本地剧"}) {
|
||||
t.Fatalf("profile-limited merged list total=%d items=%#v", total, mediaTitles(items))
|
||||
}
|
||||
|
||||
searchItems, err := svc.SearchMediaVisible(t.Context(), "剧", 20, MediaVisibility{
|
||||
IncludeNSFW: true,
|
||||
AllowedLibraryIDs: []string{local.ID},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := mediaTitles(searchItems); !slices.Equal(got, []string{"云盘剧", "本地剧"}) {
|
||||
t.Fatalf("profile-limited merged search items=%#v, want local+hidden cloud", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartAllCloudLibraryScansIncludesMergedCloudMounts(t *testing.T) {
|
||||
|
||||
@@ -29,6 +29,9 @@ type MediaVisibility struct {
|
||||
HiddenLibraryIDs []string
|
||||
}
|
||||
|
||||
const maxMediaSearchLimit = 50000
|
||||
const maxMediaSearchPageSize = 2000
|
||||
|
||||
func (v MediaVisibility) Allows(media *model.Media) bool {
|
||||
if media == nil {
|
||||
return false
|
||||
@@ -281,8 +284,8 @@ func (s *MediaService) SearchMedia(ctx context.Context, query string, limit int)
|
||||
func (s *MediaService) SearchMediaVisible(ctx context.Context, query string, limit int, visibility MediaVisibility) ([]model.Media, error) {
|
||||
if limit <= 0 {
|
||||
limit = 50
|
||||
} else if limit > 2000 {
|
||||
limit = 2000
|
||||
} else if limit > maxMediaSearchLimit {
|
||||
limit = maxMediaSearchLimit
|
||||
}
|
||||
visibility = ExpandMediaVisibilityForMergedCloudLibraries(ctx, s.repo, visibility)
|
||||
return s.repo.Media.SearchFiltered(ctx, query, limit, repository.MediaQueryFilter{
|
||||
@@ -292,6 +295,24 @@ func (s *MediaService) SearchMediaVisible(ctx context.Context, query string, lim
|
||||
})
|
||||
}
|
||||
|
||||
func (s *MediaService) SearchMediaVisiblePage(ctx context.Context, query string, page, pageSize int, visibility MediaVisibility) ([]model.Media, int64, error) {
|
||||
if pageSize <= 0 {
|
||||
pageSize = 50
|
||||
}
|
||||
if pageSize > maxMediaSearchPageSize {
|
||||
pageSize = maxMediaSearchPageSize
|
||||
}
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
visibility = ExpandMediaVisibilityForMergedCloudLibraries(ctx, s.repo, visibility)
|
||||
return s.repo.Media.SearchFilteredPage(ctx, query, (page-1)*pageSize, pageSize, repository.MediaQueryFilter{
|
||||
IncludeNSFW: visibility.IncludeNSFW,
|
||||
AllowedLibraryIDs: visibility.AllowedLibraryIDs,
|
||||
HiddenLibraryIDs: visibility.HiddenLibraryIDs,
|
||||
})
|
||||
}
|
||||
|
||||
// GetMedia returns a single media row.
|
||||
func (s *MediaService) GetMedia(ctx context.Context, id string) (*model.Media, error) {
|
||||
return s.repo.Media.FindByID(ctx, id)
|
||||
|
||||
@@ -176,6 +176,61 @@ func TestSearchMediaVisibleHonorsLargePosterWallLimit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchMediaVisibleCanReturnHugeLibraryResultsWhenRequested(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.Library{}, &model.Media{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repos := repository.New(db)
|
||||
lib := model.Library{Name: "海量剧集", Path: "/media/huge", Type: "tv", Enabled: true}
|
||||
if err := repos.Library.Create(t.Context(), &lib); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const total = 2505
|
||||
rows := make([]model.Media, total)
|
||||
for i := range rows {
|
||||
rows[i] = model.Media{
|
||||
LibraryID: lib.ID,
|
||||
Title: fmt.Sprintf("海量剧集 %04d", i),
|
||||
Path: fmt.Sprintf("/media/huge/show-%04d.mkv", i),
|
||||
SeasonNum: 1,
|
||||
EpisodeNum: i + 1,
|
||||
}
|
||||
}
|
||||
if err := db.CreateInBatches(&rows, 500).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
items, err := NewMediaService(&config.Config{}, zap.NewNop(), repos).
|
||||
SearchMediaVisible(t.Context(), "海量剧集", total, MediaVisibility{IncludeNSFW: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(items) != total {
|
||||
t.Fatalf("huge search returned %d rows, want %d", len(items), total)
|
||||
}
|
||||
|
||||
firstPage, totalRows, err := NewMediaService(&config.Config{}, zap.NewNop(), repos).
|
||||
SearchMediaVisiblePage(t.Context(), "海量剧集", 1, 2000, MediaVisibility{IncludeNSFW: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if totalRows != total || len(firstPage) != 2000 {
|
||||
t.Fatalf("huge search page 1 len=%d total=%d, want len=2000 total=%d", len(firstPage), totalRows, total)
|
||||
}
|
||||
secondPage, totalRows, err := NewMediaService(&config.Config{}, zap.NewNop(), repos).
|
||||
SearchMediaVisiblePage(t.Context(), "海量剧集", 2, 2000, MediaVisibility{IncludeNSFW: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if totalRows != total || len(secondPage) != total-2000 {
|
||||
t.Fatalf("huge search page 2 len=%d total=%d, want len=%d total=%d", len(secondPage), totalRows, total-2000, total)
|
||||
}
|
||||
}
|
||||
|
||||
func sortedMediaTitles(rows []model.Media) []string {
|
||||
out := make([]string, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
|
||||
Reference in New Issue
Block a user