💄 Optimized the feed
This commit is contained in:
parent
e34f248cfa
commit
6cfa6e8285
@ -2,6 +2,7 @@ package grpc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
iproto "git.solsynth.dev/hypernet/interactive/pkg/proto"
|
iproto "git.solsynth.dev/hypernet/interactive/pkg/proto"
|
||||||
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
||||||
@ -14,7 +15,11 @@ import (
|
|||||||
|
|
||||||
func (v *Server) GetFeed(_ context.Context, in *iproto.GetFeedRequest) (*iproto.GetFeedResponse, error) {
|
func (v *Server) GetFeed(_ context.Context, in *iproto.GetFeedRequest) (*iproto.GetFeedResponse, error) {
|
||||||
limit := int(in.GetLimit())
|
limit := int(in.GetLimit())
|
||||||
articles, err := services.GetTodayFeedRandomly(limit)
|
var cursor *time.Time
|
||||||
|
if in.Cursor != nil {
|
||||||
|
cursor = lo.ToPtr(time.UnixMilli(int64(in.GetCursor())))
|
||||||
|
}
|
||||||
|
articles, err := services.GetTodayFeedRandomly(limit, cursor)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Error(codes.Internal, err.Error())
|
return nil, status.Error(codes.Internal, err.Error())
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,18 @@ package services
|
|||||||
import (
|
import (
|
||||||
"git.solsynth.dev/hypernet/reader/pkg/internal/database"
|
"git.solsynth.dev/hypernet/reader/pkg/internal/database"
|
||||||
"git.solsynth.dev/hypernet/reader/pkg/internal/models"
|
"git.solsynth.dev/hypernet/reader/pkg/internal/models"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetTodayFeedRandomly(limit int) ([]models.SubscriptionItem, error) {
|
func GetTodayFeedRandomly(limit int, cursor *time.Time) ([]models.SubscriptionItem, error) {
|
||||||
|
tx := database.C
|
||||||
|
if cursor != nil {
|
||||||
|
tx = tx.Where("published_at < ?", *cursor)
|
||||||
|
}
|
||||||
|
|
||||||
var articles []models.SubscriptionItem
|
var articles []models.SubscriptionItem
|
||||||
if err := database.C.Limit(limit).
|
if err := tx.Limit(limit).
|
||||||
Where("DATE(created_at) = CURRENT_DATE"). // Created in today
|
Order("published_at DESC").
|
||||||
Order("RANDOM()").
|
|
||||||
Omit("Content").
|
Omit("Content").
|
||||||
Preload("Feed").
|
Preload("Feed").
|
||||||
Find(&articles).Error; err != nil {
|
Find(&articles).Error; err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user