Compare commits
2 Commits
f5544e73f3
...
db85cebe06
Author | SHA1 | Date | |
---|---|---|---|
db85cebe06 | |||
f86a7527a9 |
@ -49,7 +49,7 @@ func getPost(c *fiber.Ctx) error {
|
||||
ReplyCount: services.CountPostReply(item.ID),
|
||||
ReactionCount: services.CountPostReactions(item.ID),
|
||||
}
|
||||
item.Metric.ReactionList, err = services.ListResourceReactions(database.C.Where("post_id = ?", item.ID))
|
||||
item.Metric.ReactionList, err = services.ListPostReactions(database.C.Where("post_id = ?", item.ID))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
39
pkg/internal/services/conversations.go
Normal file
39
pkg/internal/services/conversations.go
Normal file
@ -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
|
||||
}
|
@ -194,7 +194,7 @@ func ListPost(tx *gorm.DB, take int, offset int, order any, noReact ...bool) ([]
|
||||
|
||||
// Load reactions
|
||||
if len(noReact) <= 0 || !noReact[0] {
|
||||
if mapping, err := BatchListResourceReactions(database.C.Where("post_id IN ?", idx), "post_id"); err != nil {
|
||||
if mapping, err := BatchListPostReactions(database.C.Where("post_id IN ?", idx), "post_id"); err != nil {
|
||||
return items, err
|
||||
} else {
|
||||
itemMap := lo.SliceToMap(items, func(item *models.Post) (uint, *models.Post) {
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func ListResourceReactions(tx *gorm.DB) (map[string]int64, error) {
|
||||
func ListPostReactions(tx *gorm.DB) (map[string]int64, error) {
|
||||
var reactions []struct {
|
||||
Symbol string
|
||||
Count int64
|
||||
@ -30,7 +30,7 @@ func ListResourceReactions(tx *gorm.DB) (map[string]int64, error) {
|
||||
}), nil
|
||||
}
|
||||
|
||||
func BatchListResourceReactions(tx *gorm.DB, indexField string) (map[uint]map[string]int64, error) {
|
||||
func BatchListPostReactions(tx *gorm.DB, indexField string) (map[uint]map[string]int64, error) {
|
||||
var reactions []struct {
|
||||
ID uint
|
||||
Symbol string
|
||||
@ -39,7 +39,7 @@ func BatchListResourceReactions(tx *gorm.DB, indexField string) (map[uint]map[st
|
||||
|
||||
reactInfo := map[uint]map[string]int64{}
|
||||
if err := tx.Model(&models.Reaction{}).
|
||||
Select(fmt.Sprintf("%s as id, symbol, COUNT(id) as count", indexField)).
|
||||
Select(fmt.Sprintf("%s as id, symbol, COUNT(*) as count", indexField)).
|
||||
Group("id, symbol").
|
||||
Scan(&reactions).Error; err != nil {
|
||||
return reactInfo, err
|
||||
|
Loading…
x
Reference in New Issue
Block a user