mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-03 20:26:36 +08:00
fix: improve discover and series special handling
This commit is contained in:
@@ -40,10 +40,7 @@ var discoverSectionCatalog = []discoverSectionDef{
|
||||
func discoverSectionsHandler(svc *service.Container) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
sections := make([]gin.H, 0, len(discoverSectionCatalog))
|
||||
for _, section := range discoverSectionCatalog {
|
||||
if !discoverProviderEnabled(c.Request.Context(), svc, section.Provider) {
|
||||
continue
|
||||
}
|
||||
for _, section := range enabledDiscoverSections(c.Request.Context(), svc) {
|
||||
sections = append(sections, gin.H{"key": section.Key, "label": section.Label, "provider": section.Provider})
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"sections": sections})
|
||||
@@ -56,11 +53,18 @@ func discoverSectionsHandler(svc *service.Container) gin.HandlerFunc {
|
||||
// the page.
|
||||
func discoverFeedHandler(svc *service.Container) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
keys := strings.Split(c.DefaultQuery("sections", "tmdb_trending_day,tmdb_popular_movie,douban_hot_movie,bangumi_calendar"), ",")
|
||||
rawSections := c.Query("sections")
|
||||
if strings.TrimSpace(rawSections) == "" {
|
||||
rawSections = strings.Join(defaultDiscoverSectionKeys(c.Request.Context(), svc), ",")
|
||||
}
|
||||
keys := strings.Split(rawSections, ",")
|
||||
out := gin.H{}
|
||||
artworkItems := []service.ExternalMediaResult{}
|
||||
for _, raw := range keys {
|
||||
k := strings.TrimSpace(raw)
|
||||
if k == "" {
|
||||
continue
|
||||
}
|
||||
if provider := discoverSectionProvider(k); provider != "" && !discoverProviderEnabled(c.Request.Context(), svc, provider) {
|
||||
out[k] = []service.ExternalMediaResult{}
|
||||
continue
|
||||
@@ -78,6 +82,41 @@ func discoverFeedHandler(svc *service.Container) gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func enabledDiscoverSections(ctx context.Context, svc *service.Container) []discoverSectionDef {
|
||||
sections := make([]discoverSectionDef, 0, len(discoverSectionCatalog))
|
||||
for _, section := range discoverSectionCatalog {
|
||||
if !discoverProviderEnabled(ctx, svc, section.Provider) {
|
||||
continue
|
||||
}
|
||||
sections = append(sections, section)
|
||||
}
|
||||
return sections
|
||||
}
|
||||
|
||||
func defaultDiscoverSectionKeys(ctx context.Context, svc *service.Container) []string {
|
||||
preferred := []string{"tmdb_trending_day", "douban_hot_movie", "douban_hot_tv", "bangumi_calendar"}
|
||||
enabled := map[string]struct{}{}
|
||||
for _, section := range enabledDiscoverSections(ctx, svc) {
|
||||
enabled[section.Key] = struct{}{}
|
||||
}
|
||||
out := make([]string, 0, len(preferred))
|
||||
for _, key := range preferred {
|
||||
if _, ok := enabled[key]; ok {
|
||||
out = append(out, key)
|
||||
}
|
||||
}
|
||||
if len(out) > 0 {
|
||||
return out
|
||||
}
|
||||
for _, section := range enabledDiscoverSections(ctx, svc) {
|
||||
out = append(out, section.Key)
|
||||
if len(out) >= 4 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func discoverSectionProvider(key string) string {
|
||||
for _, section := range discoverSectionCatalog {
|
||||
if section.Key == key {
|
||||
|
||||
@@ -35,3 +35,33 @@ func TestDiscoverProviderEnabledHonorsAPIConfigToggle(t *testing.T) {
|
||||
t.Fatal("missing API config should keep discover provider available")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDefaultDiscoverSectionKeysSkipDisabledProviders(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.APIConfig{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repos := repository.New(db)
|
||||
apiConfig := service.NewAPIConfigService(zap.NewNop(), repos, service.NewCryptoService("", zap.NewNop()))
|
||||
disabled := false
|
||||
for _, provider := range []string{"douban", "bangumi"} {
|
||||
if _, err := apiConfig.Update(t.Context(), provider, service.APIConfigPatch{Enabled: &disabled}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
svc := &service.Container{APIConfig: apiConfig}
|
||||
|
||||
keys := defaultDiscoverSectionKeys(t.Context(), svc)
|
||||
for _, key := range keys {
|
||||
switch discoverSectionProvider(key) {
|
||||
case "douban", "bangumi":
|
||||
t.Fatalf("disabled provider key %q should not be selected by default; keys=%v", key, keys)
|
||||
}
|
||||
}
|
||||
if len(keys) == 0 {
|
||||
t.Fatal("default keys should keep enabled providers")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user