feat(api): add tab-based post feed endpoints with cursor pagination
Add support for different post feed types via tab parameter: - follow: posts from followed users (requires authentication) - hot: trending posts using Gorse most_liked_weekly or database fallback - recommend: personalized recommendations with random offset for diversity - latest: default chronological feed The cursor pagination detection is also improved to check for parameter existence rather than non-empty string value.
This commit is contained in:
@@ -147,8 +147,9 @@ func (h *PostHandler) RecordView(c *gin.Context) {
|
||||
// List 获取帖子列表(支持游标分页和偏移分页)
|
||||
func (h *PostHandler) List(c *gin.Context) {
|
||||
// 检查是否使用游标分页
|
||||
cursorStr := c.Query("cursor")
|
||||
if cursorStr != "" {
|
||||
// 当 cursor 参数存在时(包括空字符串),使用游标分页
|
||||
_, cursorExists := c.GetQuery("cursor")
|
||||
if cursorExists {
|
||||
h.ListByCursor(c)
|
||||
return
|
||||
}
|
||||
@@ -220,6 +221,7 @@ func (h *PostHandler) List(c *gin.Context) {
|
||||
func (h *PostHandler) ListByCursor(c *gin.Context) {
|
||||
userID := c.Query("user_id")
|
||||
currentUserID := c.GetString("user_id")
|
||||
tab := c.Query("tab")
|
||||
|
||||
// 解析游标分页请求
|
||||
req := h.parseCursorRequest(c)
|
||||
@@ -227,8 +229,32 @@ func (h *PostHandler) ListByCursor(c *gin.Context) {
|
||||
// 判断是否包含待审核帖子
|
||||
includePending := userID != "" && userID == currentUserID
|
||||
|
||||
// 调用游标分页服务
|
||||
result, err := h.postService.ListByCursor(c.Request.Context(), userID, includePending, req)
|
||||
var result *cursor.CursorPageResult[*model.Post]
|
||||
var err error
|
||||
|
||||
// 根据 tab 参数选择不同的获取方式
|
||||
switch tab {
|
||||
case "follow":
|
||||
// 获取关注用户的帖子,需要登录
|
||||
if currentUserID == "" {
|
||||
response.Unauthorized(c, "请先登录")
|
||||
return
|
||||
}
|
||||
result, err = h.postService.GetFollowingPostsByCursor(c.Request.Context(), currentUserID, req)
|
||||
case "hot":
|
||||
// 获取热门帖子
|
||||
result, err = h.postService.GetHotPostsByCursor(c.Request.Context(), req)
|
||||
case "recommend":
|
||||
// 推荐帖子(从Gorse获取个性化推荐)
|
||||
result, err = h.postService.GetRecommendedPostsByCursor(c.Request.Context(), currentUserID, req)
|
||||
case "latest":
|
||||
// 最新帖子
|
||||
result, err = h.postService.ListByCursor(c.Request.Context(), userID, includePending, req)
|
||||
default:
|
||||
// 默认获取最新帖子
|
||||
result, err = h.postService.ListByCursor(c.Request.Context(), userID, includePending, req)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
response.InternalServerError(c, "failed to get posts")
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user