🐛 Trying to fix batch list reactions of post miscounted

This commit is contained in:
LittleSheep 2024-10-13 13:37:11 +08:00
parent f86a7527a9
commit db85cebe06
3 changed files with 5 additions and 5 deletions

View File

@ -49,7 +49,7 @@ func getPost(c *fiber.Ctx) error {
ReplyCount: services.CountPostReply(item.ID), ReplyCount: services.CountPostReply(item.ID),
ReactionCount: services.CountPostReactions(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 { if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} }

View File

@ -194,7 +194,7 @@ func ListPost(tx *gorm.DB, take int, offset int, order any, noReact ...bool) ([]
// Load reactions // Load reactions
if len(noReact) <= 0 || !noReact[0] { 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 return items, err
} else { } else {
itemMap := lo.SliceToMap(items, func(item *models.Post) (uint, *models.Post) { itemMap := lo.SliceToMap(items, func(item *models.Post) (uint, *models.Post) {

View File

@ -8,7 +8,7 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
func ListResourceReactions(tx *gorm.DB) (map[string]int64, error) { func ListPostReactions(tx *gorm.DB) (map[string]int64, error) {
var reactions []struct { var reactions []struct {
Symbol string Symbol string
Count int64 Count int64
@ -30,7 +30,7 @@ func ListResourceReactions(tx *gorm.DB) (map[string]int64, error) {
}), nil }), 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 { var reactions []struct {
ID uint ID uint
Symbol string Symbol string
@ -39,7 +39,7 @@ func BatchListResourceReactions(tx *gorm.DB, indexField string) (map[uint]map[st
reactInfo := map[uint]map[string]int64{} reactInfo := map[uint]map[string]int64{}
if err := tx.Model(&models.Reaction{}). 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"). Group("id, symbol").
Scan(&reactions).Error; err != nil { Scan(&reactions).Error; err != nil {
return reactInfo, err return reactInfo, err