✨ Interactive feed provider
This commit is contained in:
26
pkg/internal/services/feed.go
Normal file
26
pkg/internal/services/feed.go
Normal file
@ -0,0 +1,26 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hypernet/reader/pkg/internal/database"
|
||||
"git.solsynth.dev/hypernet/reader/pkg/internal/models"
|
||||
)
|
||||
|
||||
func GetTodayNewsRandomly(limit int, isAdvanced bool) ([]models.NewsArticle, error) {
|
||||
var sources []string
|
||||
for _, srv := range GetNewsSources() {
|
||||
if !isAdvanced && srv.Advanced {
|
||||
continue
|
||||
}
|
||||
sources = append(sources, srv.ID)
|
||||
}
|
||||
|
||||
var articles []models.NewsArticle
|
||||
if err := database.C.Limit(limit).
|
||||
Where("source IN ?", sources).
|
||||
Where("DATE(created_at) = CURRENT_DATE"). // Created in today
|
||||
Order("RANDOM()").
|
||||
Find(&articles).Error; err != nil {
|
||||
return articles, err
|
||||
}
|
||||
return articles, nil
|
||||
}
|
@ -17,13 +17,17 @@ import (
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
var NewsSources []models.NewsSource
|
||||
var newsSources []models.NewsSource
|
||||
|
||||
func GetNewsSources() []models.NewsSource {
|
||||
return newsSources
|
||||
}
|
||||
|
||||
func LoadNewsSources() error {
|
||||
if err := viper.UnmarshalKey("sources", &NewsSources); err != nil {
|
||||
if err := viper.UnmarshalKey("sources", &newsSources); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info().Int("count", len(NewsSources)).Msg("Loaded news sources configuration.")
|
||||
log.Info().Int("count", len(newsSources)).Msg("Loaded news sources configuration.")
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -33,7 +37,7 @@ func ScanNewsSourcesNoEager() {
|
||||
|
||||
func ScanNewsSources(eager ...bool) {
|
||||
count := 0
|
||||
for _, src := range NewsSources {
|
||||
for _, src := range newsSources {
|
||||
if !src.Enabled {
|
||||
continue
|
||||
}
|
Reference in New Issue
Block a user