refactor trending functions and introduce Post type for better structure

This commit is contained in:
highperfocused 2025-02-23 23:22:28 +01:00
parent 793bffed8c
commit 7c2b5a4955
3 changed files with 16 additions and 13 deletions

View File

@ -8,22 +8,12 @@ import (
"git.highperfocused.tech/highperfocused/lumina-relay/relay/cache"
)
type Post struct {
ID string `json:"id"`
PubKey string `json:"pubkey"`
CreatedAt time.Time `json:"created_at"`
Kind string `json:"kind"`
Content string `json:"content"`
Tags [][]string `json:"tags"`
ReactionCount int `json:"reaction_count"`
}
var (
trendingCache = cache.New()
cacheDuration = 5 * time.Minute
)
// GetTrendingKind20 returns the top 20 trending posts of kind 20 from the last 24 hours
// GetTrendingBasicKind20 returns the top 20 trending posts of kind 20 from the last 24 hours
func GetTrendingBasicKind20(db *sql.DB) ([]Post, error) {
if cached, ok := trendingCache.Get("trending_kind_20"); ok {
return cached.([]Post), nil

View File

@ -5,7 +5,7 @@ import (
"encoding/json"
)
// GetTrendingKind20 returns the top 20 trending posts of kind 20 from the last 24 hours
// GetTrendingScoreKind20 returns the top 20 trending posts of kind 20 from the last 24 hours
func GetTrendingScoreKind20(db *sql.DB) ([]Post, error) {
if cached, ok := trendingCache.Get("trending_kind_20"); ok {
return cached.([]Post), nil
@ -45,7 +45,7 @@ func GetTrendingScoreKind20(db *sql.DB) ([]Post, error) {
e.kind,
e.content,
e.tags,
COALESCE(s.trending_score, 0) as reaction_count
COALESCE(s.trending_score, 0.0) as reaction_count -- Cast to float
FROM event e
LEFT JOIN scores s ON e.id = s.original_event_id
WHERE e.kind::text = '20'

13
relay/trending/types.go Normal file
View File

@ -0,0 +1,13 @@
package trending
import "time"
type Post struct {
ID string `json:"id"`
PubKey string `json:"pubkey"`
CreatedAt time.Time `json:"created_at"`
Kind int `json:"kind"`
Content string `json:"content"`
Tags [][]string `json:"tags"`
ReactionCount float64 `json:"reaction_count"`
}