From 61a48c1cf4edea184c15682f2c3a1f0a4aa94f2d Mon Sep 17 00:00:00 2001 From: Mohammad Mahdi Date: Tue, 13 May 2025 22:58:46 +0330 Subject: [PATCH] Add random post selection --- internal/services/postService.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/services/postService.go b/internal/services/postService.go index a0fcd1d..ac79a86 100644 --- a/internal/services/postService.go +++ b/internal/services/postService.go @@ -22,7 +22,7 @@ func NewPostService(db *gorm.DB) *PostService { return &PostService{db: db} } -func (ps *PostService) GetPostsFromApi(ctx context.Context, tag string, instance string) (error, []models.Post) { +func (*PostService) GetPostsFromApi(ctx context.Context, tag string, instance string) (error, []models.Post) { var requestUrl = instance + "/api/v1/timelines/tag/" + tag + "?limit=40" req, err := http.NewRequestWithContext(ctx, "GET", requestUrl, nil) if err != nil { @@ -93,6 +93,12 @@ func (ps *PostService) InsertNewAccounts(newAccounts []models.Account) int { return int(ps.db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&newAccounts).RowsAffected) } +func (ps *PostService) GetRandomPost() models.Post { + var post models.Post + ps.db.Order("RANDOM()").First(&post) // TODO: only approved posts + return post +} + func arrayContains(arr []string, str string) bool { for _, a := range arr { if a == str {