Replace websocket flow with SSE support in backend.

Update handlers, services, router, and data conversion logic to support server-sent events and related message pipeline changes.

Made-with: Cursor
This commit is contained in:
2026-03-10 12:58:23 +08:00
parent 4c0177149a
commit 86ef150fec
19 changed files with 689 additions and 1719 deletions

View File

@@ -105,6 +105,7 @@ func (h *PostHandler) GetByID(c *gin.Context) {
Title: post.Title,
Content: post.Content,
Images: dto.ConvertPostImagesToResponse(post.Images),
Status: string(post.Status),
LikesCount: post.LikesCount,
CommentsCount: post.CommentsCount,
FavoritesCount: post.FavoritesCount,
@@ -114,6 +115,7 @@ func (h *PostHandler) GetByID(c *gin.Context) {
IsLocked: post.IsLocked,
IsVote: post.IsVote,
CreatedAt: dto.FormatTime(post.CreatedAt),
UpdatedAt: dto.FormatTime(post.UpdatedAt),
Author: authorWithFollowStatus,
IsLiked: isLiked,
IsFavorited: isFavorited,
@@ -175,10 +177,18 @@ func (h *PostHandler) List(c *gin.Context) {
posts, total, err = h.postService.GetRecommendedPosts(c.Request.Context(), currentUserID, page, pageSize)
case "latest":
// 最新帖子
posts, total, err = h.postService.GetLatestPosts(c.Request.Context(), page, pageSize, userID)
if userID != "" && userID == currentUserID {
posts, total, err = h.postService.GetLatestPostsForOwner(c.Request.Context(), page, pageSize, userID)
} else {
posts, total, err = h.postService.GetLatestPosts(c.Request.Context(), page, pageSize, userID)
}
default:
// 默认获取最新帖子
posts, total, err = h.postService.GetLatestPosts(c.Request.Context(), page, pageSize, userID)
if userID != "" && userID == currentUserID {
posts, total, err = h.postService.GetLatestPostsForOwner(c.Request.Context(), page, pageSize, userID)
} else {
posts, total, err = h.postService.GetLatestPosts(c.Request.Context(), page, pageSize, userID)
}
}
if err != nil {
@@ -225,8 +235,9 @@ func (h *PostHandler) Update(c *gin.Context) {
}
type UpdateRequest struct {
Title string `json:"title"`
Content string `json:"content"`
Title string `json:"title"`
Content string `json:"content"`
Images *[]string `json:"images"`
}
var req UpdateRequest
@@ -242,12 +253,18 @@ func (h *PostHandler) Update(c *gin.Context) {
post.Content = req.Content
}
err = h.postService.Update(c.Request.Context(), post)
err = h.postService.UpdateWithImages(c.Request.Context(), post, req.Images)
if err != nil {
response.InternalServerError(c, "failed to update post")
return
}
post, err = h.postService.GetByID(c.Request.Context(), post.ID)
if err != nil {
response.InternalServerError(c, "failed to get updated post")
return
}
currentUserID := c.GetString("user_id")
var isLiked, isFavorited bool
if currentUserID != "" {
@@ -410,14 +427,15 @@ func (h *PostHandler) GetUserPosts(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
posts, total, err := h.postService.GetUserPosts(c.Request.Context(), userID, page, pageSize)
currentUserID := c.GetString("user_id")
includePending := currentUserID != "" && currentUserID == userID
posts, total, err := h.postService.GetUserPosts(c.Request.Context(), userID, page, pageSize, includePending)
if err != nil {
response.InternalServerError(c, "failed to get user posts")
return
}
// 获取当前用户ID用于判断点赞和收藏状态
currentUserID := c.GetString("user_id")
isLikedMap := make(map[string]bool)
isFavoritedMap := make(map[string]bool)
if currentUserID != "" {