♻️ Refactored feed module

This commit is contained in:
2025-04-05 00:37:00 +08:00
parent 3a8d85684c
commit 2fb98edb83
15 changed files with 295 additions and 286 deletions

View File

@@ -2,14 +2,9 @@ package api
import (
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"git.solsynth.dev/hypernet/reader/pkg/internal/database"
"git.solsynth.dev/hypernet/reader/pkg/internal/models"
"git.solsynth.dev/hypernet/reader/pkg/internal/server/exts"
"git.solsynth.dev/hypernet/reader/pkg/internal/services"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
"gorm.io/gorm/clause"
)
func adminTriggerScanTask(c *fiber.Ctx) error {
@@ -26,36 +21,7 @@ func adminTriggerScanTask(c *fiber.Ctx) error {
return err
}
go func() {
count := 0
for _, src := range services.GetNewsSources() {
if !src.Enabled {
continue
}
if len(data.Sources) > 0 && !lo.Contains(data.Sources, src.ID) {
continue
}
log.Debug().Str("source", src.ID).Msg("Scanning news source...")
result, err := services.NewsSourceRead(src, data.Eager)
if err != nil {
log.Warn().Err(err).Str("source", src.ID).Msg("Failed to scan a news source.")
}
result = lo.UniqBy(result, func(item models.NewsArticle) string {
return item.Hash
})
database.C.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "hash"}},
DoUpdates: clause.AssignmentColumns([]string{"thumbnail", "title", "content", "description", "published_at"}),
}).Create(&result)
log.Info().Str("source", src.ID).Int("count", len(result)).Msg("Scanned a news sources.")
count += len(result)
}
log.Info().Int("count", count).Msg("Scanned all news sources.")
}()
go services.FetchFeedTimed()
return c.SendStatus(fiber.StatusOK)
}