Count post views

This commit is contained in:
2025-02-17 17:21:15 +08:00
parent 14c17eded8
commit 269e79ca58
11 changed files with 141 additions and 20 deletions

View File

@ -2,13 +2,14 @@ package api
import (
"fmt"
"time"
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
"git.solsynth.dev/hypernet/interactive/pkg/internal/services"
vocab "github.com/go-ap/activitypub"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
"time"
)
func apGetPublisher(c *fiber.Ctx) error {
@ -42,7 +43,7 @@ func apGetPost(c *fiber.Ctx) error {
return err
}
items, err := services.ListPost(tx, take, offset, "published_at DESC")
items, err := services.ListPost(tx, take, offset, "published_at DESC", nil)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}

View File

@ -2,13 +2,14 @@ package api
import (
"fmt"
"strconv"
"strings"
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"git.solsynth.dev/hypernet/passport/pkg/authkit"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"gorm.io/gorm"
"strconv"
"strings"
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
"git.solsynth.dev/hypernet/interactive/pkg/internal/gap"
@ -114,13 +115,18 @@ func searchPost(c *fiber.Ctx) error {
return err
}
var userId *uint
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
userId = &user.ID
}
countTx := tx
count, err := services.CountPost(countTx)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
items, err := services.ListPost(tx, take, offset, "published_at DESC")
items, err := services.ListPost(tx, take, offset, "published_at DESC", userId)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
@ -150,13 +156,18 @@ func listPost(c *fiber.Ctx) error {
return err
}
var userId *uint
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
userId = &user.ID
}
countTx := tx
count, err := services.CountPost(countTx)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
items, err := services.ListPost(tx, take, offset, "published_at DESC")
items, err := services.ListPost(tx, take, offset, "published_at DESC", userId)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
@ -222,12 +233,17 @@ func listDraftPost(c *fiber.Ctx) error {
tx := services.FilterPostWithAuthorDraft(database.C, user.ID)
var userId *uint
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
userId = &user.ID
}
count, err := services.CountPost(tx)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
items, err := services.ListPost(tx, take, offset, "created_at DESC", true)
items, err := services.ListPost(tx, take, offset, "created_at DESC", userId, true)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}

View File

@ -28,7 +28,12 @@ func listPinnedPost(c *fiber.Ctx) error {
tx = tx.Where("publisher_id = ?", user.ID)
tx = tx.Where("pinned_at IS NOT NULL")
items, err := services.ListPost(tx, 100, 0, "published_at DESC")
var userId *uint
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
userId = &user.ID
}
items, err := services.ListPost(tx, 100, 0, "published_at DESC", userId)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}

View File

@ -25,8 +25,13 @@ func listRecommendation(c *fiber.Ctx) error {
return item.ID
})
var userId *uint
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
userId = &user.ID
}
tx := database.C.Where("id IN ?", postIdx)
newPosts, err := services.ListPost(tx, featuredMax, 0, "id ASC")
newPosts, err := services.ListPost(tx, featuredMax, 0, "id ASC", userId)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
@ -65,6 +70,11 @@ func listRecommendationFriends(c *fiber.Ctx) error {
tx = tx.Where("publisher_id IN ?", friendList)
var userId *uint
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
userId = &user.ID
}
countTx := tx
count, err := services.CountPost(countTx)
if err != nil {
@ -76,7 +86,7 @@ func listRecommendationFriends(c *fiber.Ctx) error {
order = "published_at DESC, (COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC"
}
items, err := services.ListPost(tx, take, offset, order)
items, err := services.ListPost(tx, take, offset, order, userId)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
@ -106,13 +116,18 @@ func listRecommendationShuffle(c *fiber.Ctx) error {
return err
}
var userId *uint
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
userId = &user.ID
}
countTx := tx
count, err := services.CountPost(countTx)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
items, err := services.ListPost(tx, take, offset, "RANDOM()")
items, err := services.ListPost(tx, take, offset, "RANDOM()", userId)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}

View File

@ -2,9 +2,11 @@ package api
import (
"fmt"
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
"git.solsynth.dev/hypernet/interactive/pkg/internal/services"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"github.com/gofiber/fiber/v2"
)
@ -35,12 +37,17 @@ func listPostReplies(c *fiber.Ctx) error {
tx = services.FilterPostWithTag(tx, c.Query("tag"))
}
var userId *uint
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
userId = &user.ID
}
count, err := services.CountPost(tx)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
items, err := services.ListPost(tx, take, offset, "published_at DESC")
items, err := services.ListPost(tx, take, offset, "published_at DESC", userId)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
@ -55,6 +62,11 @@ func listPostFeaturedReply(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
take = max(1, min(take, 3))
var userId *uint
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
userId = &user.ID
}
tx := database.C
var post models.Post
if err := database.C.Where("id = ?", c.Params("postId")).First(&post).Error; err != nil {
@ -78,7 +90,7 @@ func listPostFeaturedReply(c *fiber.Ctx) error {
tx = services.FilterPostWithTag(tx, c.Query("tag"))
}
items, err := services.ListPost(tx, take, 0, "(COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC, published_at DESC")
items, err := services.ListPost(tx, take, 0, "(COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC, published_at DESC", userId)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}

View File

@ -24,6 +24,11 @@ func getWhatsNew(c *fiber.Ctx) error {
tx = tx.Where("id > ?", pivot)
var userId *uint
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
userId = &user.ID
}
countTx := tx
count, err := services.CountPost(countTx)
if err != nil {
@ -35,7 +40,7 @@ func getWhatsNew(c *fiber.Ctx) error {
order = "published_at DESC, (COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC"
}
items, err := services.ListPost(tx, 10, 0, order)
items, err := services.ListPost(tx, 10, 0, order, userId)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}