✨ Reaction info
This commit is contained in:
@ -12,8 +12,8 @@ import (
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func contextArticle() *services.PostTypeContext[models.Article] {
|
||||
return &services.PostTypeContext[models.Article]{
|
||||
func contextArticle() *services.PostTypeContext[*models.Article] {
|
||||
return &services.PostTypeContext[*models.Article]{
|
||||
Tx: database.C,
|
||||
TypeName: "Article",
|
||||
CanReply: false,
|
||||
@ -31,6 +31,11 @@ func getArticle(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
item.ReactionList, err = mx.CountReactions(item.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(item)
|
||||
}
|
||||
|
||||
@ -102,7 +107,7 @@ func createArticle(c *fiber.Ctx) error {
|
||||
|
||||
mx := contextArticle()
|
||||
|
||||
item := models.Article{
|
||||
item := &models.Article{
|
||||
PostBase: models.PostBase{
|
||||
Alias: data.Alias,
|
||||
Attachments: data.Attachments,
|
||||
@ -192,7 +197,7 @@ func reactArticle(c *fiber.Ctx) error {
|
||||
|
||||
mx := contextArticle()
|
||||
|
||||
item, err := mx.Get(uint(id))
|
||||
item, err := mx.Get(uint(id), true)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
@ -218,7 +223,7 @@ func deleteArticle(c *fiber.Ctx) error {
|
||||
|
||||
mx := contextArticle().FilterAuthor(user.ID)
|
||||
|
||||
item, err := mx.Get(uint(id))
|
||||
item, err := mx.Get(uint(id), true)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
@ -13,8 +13,8 @@ import (
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func contextComment() *services.PostTypeContext[models.Comment] {
|
||||
return &services.PostTypeContext[models.Comment]{
|
||||
func contextComment() *services.PostTypeContext[*models.Comment] {
|
||||
return &services.PostTypeContext[*models.Comment]{
|
||||
Tx: database.C,
|
||||
TypeName: "Comment",
|
||||
CanReply: false,
|
||||
@ -32,6 +32,11 @@ func getComment(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
item.ReactionList, err = mx.CountReactions(item.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(item)
|
||||
}
|
||||
|
||||
@ -103,7 +108,7 @@ func createComment(c *fiber.Ctx) error {
|
||||
|
||||
mx := contextComment()
|
||||
|
||||
item := models.Comment{
|
||||
item := &models.Comment{
|
||||
PostBase: models.PostBase{
|
||||
Alias: data.Alias,
|
||||
Attachments: data.Attachments,
|
||||
@ -199,7 +204,7 @@ func reactComment(c *fiber.Ctx) error {
|
||||
|
||||
mx := contextComment()
|
||||
|
||||
item, err := mx.Get(uint(id))
|
||||
item, err := mx.Get(uint(id), true)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
@ -225,7 +230,7 @@ func deleteComment(c *fiber.Ctx) error {
|
||||
|
||||
mx := contextComment().FilterAuthor(user.ID)
|
||||
|
||||
item, err := mx.Get(uint(id))
|
||||
item, err := mx.Get(uint(id), true)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"code.smartsheep.studio/hydrogen/interactive/pkg/models"
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/samber/lo"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
@ -22,7 +23,8 @@ type FeedItem struct {
|
||||
AuthorID uint `json:"author_id"`
|
||||
RealmID *uint `json:"realm_id"`
|
||||
|
||||
Author models.Account `json:"author" gorm:"embedded"`
|
||||
Author models.Account `json:"author" gorm:"embedded"`
|
||||
ReactionList map[string]int64 `json:"reaction_list"`
|
||||
}
|
||||
|
||||
const (
|
||||
@ -56,7 +58,7 @@ func listFeed(c *fiber.Ctx) error {
|
||||
}
|
||||
}
|
||||
|
||||
var result []FeedItem
|
||||
var result []*FeedItem
|
||||
|
||||
userTable := viper.GetString("database.prefix") + "accounts"
|
||||
commentTable := viper.GetString("database.prefix") + "comments"
|
||||
@ -84,6 +86,59 @@ func listFeed(c *fiber.Ctx) error {
|
||||
offset,
|
||||
).Scan(&result)
|
||||
|
||||
if !c.QueryBool("noReact", false) {
|
||||
var reactions []struct {
|
||||
PostID uint
|
||||
Symbol string
|
||||
Count int64
|
||||
}
|
||||
|
||||
revertReaction := func(dataset string) error {
|
||||
itemMap := lo.SliceToMap(lo.FilterMap(result, func(item *FeedItem, index int) (*FeedItem, bool) {
|
||||
return item, item.ModelType == dataset
|
||||
}), func(item *FeedItem) (uint, *FeedItem) {
|
||||
return item.ID, item
|
||||
})
|
||||
|
||||
idx := lo.Map(lo.Filter(result, func(item *FeedItem, index int) bool {
|
||||
return item.ModelType == dataset
|
||||
}), func(item *FeedItem, index int) uint {
|
||||
return item.ID
|
||||
})
|
||||
|
||||
if err := database.C.Model(&models.Reaction{}).
|
||||
Select(dataset+"_id as post_id, symbol, COUNT(id) as count").
|
||||
Where(dataset+"_id IN (?)", idx).
|
||||
Group("post_id, symbol").
|
||||
Scan(&reactions).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
list := map[uint]map[string]int64{}
|
||||
for _, info := range reactions {
|
||||
if _, ok := list[info.PostID]; !ok {
|
||||
list[info.PostID] = make(map[string]int64)
|
||||
}
|
||||
list[info.PostID][info.Symbol] = info.Count
|
||||
}
|
||||
|
||||
for k, v := range list {
|
||||
if post, ok := itemMap[k]; ok {
|
||||
post.ReactionList = v
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := revertReaction("article"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := revertReaction("moment"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var count int64
|
||||
database.C.Raw(`SELECT COUNT(*) FROM (? UNION ALL ?) as feed`,
|
||||
database.C.Select(queryArticle).Model(&models.Article{}),
|
||||
|
@ -12,8 +12,8 @@ import (
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func contextMoment() *services.PostTypeContext[models.Moment] {
|
||||
return &services.PostTypeContext[models.Moment]{
|
||||
func contextMoment() *services.PostTypeContext[*models.Moment] {
|
||||
return &services.PostTypeContext[*models.Moment]{
|
||||
Tx: database.C,
|
||||
TypeName: "Moment",
|
||||
CanReply: false,
|
||||
@ -31,6 +31,11 @@ func getMoment(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
item.ReactionList, err = mx.CountReactions(item.ID)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(item)
|
||||
}
|
||||
|
||||
@ -101,7 +106,7 @@ func createMoment(c *fiber.Ctx) error {
|
||||
|
||||
mx := contextMoment()
|
||||
|
||||
item := models.Moment{
|
||||
item := &models.Moment{
|
||||
PostBase: models.PostBase{
|
||||
Alias: data.Alias,
|
||||
Attachments: data.Attachments,
|
||||
@ -197,7 +202,7 @@ func reactMoment(c *fiber.Ctx) error {
|
||||
|
||||
mx := contextMoment()
|
||||
|
||||
item, err := mx.Get(uint(id))
|
||||
item, err := mx.Get(uint(id), true)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
@ -223,7 +228,7 @@ func deleteMoment(c *fiber.Ctx) error {
|
||||
|
||||
mx := contextMoment().FilterAuthor(user.ID)
|
||||
|
||||
item, err := mx.Get(uint(id))
|
||||
item, err := mx.Get(uint(id), true)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
Reference in New Issue
Block a user