refactor trending functions and introduce Post type for better structure
This commit is contained in:
parent
793bffed8c
commit
7c2b5a4955
@ -8,22 +8,12 @@ import (
|
|||||||
"git.highperfocused.tech/highperfocused/lumina-relay/relay/cache"
|
"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 (
|
var (
|
||||||
trendingCache = cache.New()
|
trendingCache = cache.New()
|
||||||
cacheDuration = 5 * time.Minute
|
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) {
|
func GetTrendingBasicKind20(db *sql.DB) ([]Post, error) {
|
||||||
if cached, ok := trendingCache.Get("trending_kind_20"); ok {
|
if cached, ok := trendingCache.Get("trending_kind_20"); ok {
|
||||||
return cached.([]Post), nil
|
return cached.([]Post), nil
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"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) {
|
func GetTrendingScoreKind20(db *sql.DB) ([]Post, error) {
|
||||||
if cached, ok := trendingCache.Get("trending_kind_20"); ok {
|
if cached, ok := trendingCache.Get("trending_kind_20"); ok {
|
||||||
return cached.([]Post), nil
|
return cached.([]Post), nil
|
||||||
@ -45,7 +45,7 @@ func GetTrendingScoreKind20(db *sql.DB) ([]Post, error) {
|
|||||||
e.kind,
|
e.kind,
|
||||||
e.content,
|
e.content,
|
||||||
e.tags,
|
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
|
FROM event e
|
||||||
LEFT JOIN scores s ON e.id = s.original_event_id
|
LEFT JOIN scores s ON e.id = s.original_event_id
|
||||||
WHERE e.kind::text = '20'
|
WHERE e.kind::text = '20'
|
||||||
|
13
relay/trending/types.go
Normal file
13
relay/trending/types.go
Normal 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"`
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user