From f86a7527a9469556b6dd411fb1475c4474ecf954 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 13 Oct 2024 13:33:22 +0800 Subject: [PATCH] :sparkles: Get the replying chain (conversation) (wip) (skip ci) --- pkg/internal/services/conversations.go | 39 ++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 pkg/internal/services/conversations.go diff --git a/pkg/internal/services/conversations.go b/pkg/internal/services/conversations.go new file mode 100644 index 0000000..157cafe --- /dev/null +++ b/pkg/internal/services/conversations.go @@ -0,0 +1,39 @@ +package services + +import ( + "fmt" + "git.solsynth.dev/hydrogen/interactive/pkg/internal/database" + "git.solsynth.dev/hydrogen/interactive/pkg/internal/models" + "github.com/spf13/viper" +) + +func GetConversation(start uint, offset, take int, order string, participants []uint) ([]models.Post, error) { + var posts []models.Post + + tablePrefix := viper.GetString("database.prefix") + table := tablePrefix + "posts" + + result := database.C.Raw(fmt.Sprintf( + ` + WITH RECURSIVE conversation AS ( + SELECT * + FROM %s + WHERE id = ? + + UNION ALL + + SELECT p.* + FROM %s p + INNER JOIN conversation c ON p.reply_id = c.id AND p.author_id IN (?) + ) + SELECT * FROM conversation ORDER BY %s DESC OFFSET %d LIMIT %d`, + table, table, order, offset, take, + ), start, participants).Scan(&posts) + + // Check for errors + if result.Error != nil { + return nil, result.Error + } + + return posts, nil +}