✨ Count post views
This commit is contained in:
49
pkg/internal/services/post_views.go
Normal file
49
pkg/internal/services/post_views.go
Normal file
@ -0,0 +1,49 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
|
||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var postViewQueue []models.PostView
|
||||
|
||||
func AddPostView(post models.Post, account uint) {
|
||||
postViewQueue = append(postViewQueue, models.PostView{
|
||||
AccountID: account,
|
||||
PostID: post.ID,
|
||||
})
|
||||
}
|
||||
|
||||
func AddPostViews(posts []models.Post, account uint) {
|
||||
for _, post := range posts {
|
||||
postViewQueue = append(postViewQueue, models.PostView{
|
||||
AccountID: account,
|
||||
PostID: post.ID,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func FlushPostViews() {
|
||||
if len(postViewQueue) == 0 {
|
||||
return
|
||||
}
|
||||
workingQueue := make([]models.PostView, len(postViewQueue))
|
||||
copy(workingQueue, postViewQueue)
|
||||
clear(postViewQueue)
|
||||
updateRequiredPost := make(map[uint]bool)
|
||||
for _, item := range workingQueue {
|
||||
updateRequiredPost[item.PostID] = true
|
||||
}
|
||||
_ = database.C.CreateInBatches(workingQueue, 1000).Error
|
||||
for k := range updateRequiredPost {
|
||||
var count int64
|
||||
if err := database.C.Model(&models.PostView{}).Where("post_id = ?", k).Count(&count).Error; err != nil {
|
||||
continue
|
||||
}
|
||||
database.C.Model(&models.Post{}).Where("id = ?", k).Updates(map[string]any{
|
||||
"total_views": count,
|
||||
"total_aggressive_views": gorm.Expr("total_aggressive_views + ?", count),
|
||||
})
|
||||
}
|
||||
}
|
@ -262,7 +262,7 @@ func CountPostReactions(id uint) int64 {
|
||||
return count
|
||||
}
|
||||
|
||||
func ListPost(tx *gorm.DB, take int, offset int, order any, noReact ...bool) ([]*models.Post, error) {
|
||||
func ListPost(tx *gorm.DB, take int, offset int, order any, user *uint, noReact ...bool) ([]*models.Post, error) {
|
||||
if take > 100 {
|
||||
take = 100
|
||||
}
|
||||
@ -332,6 +332,12 @@ func ListPost(tx *gorm.DB, take int, offset int, order any, noReact ...bool) ([]
|
||||
}
|
||||
}
|
||||
|
||||
if user != nil {
|
||||
AddPostViews(lo.Map(items, func(item *models.Post, index int) models.Post {
|
||||
return *item
|
||||
}), *user)
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user