feat(api): add message segments support for posts and comments
Add rich message segment support enabling structured content (text, images, votes, mentions) in posts and comments. Includes segments field in models, DTOs, handlers, and services. Also adds @mention notification processing and migrates vote data to embedded segments.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package handler
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"with_you/internal/dto"
|
||||
"with_you/internal/model"
|
||||
"with_you/internal/pkg/cursor"
|
||||
"with_you/internal/pkg/response"
|
||||
"with_you/internal/service"
|
||||
@@ -33,10 +34,11 @@ func (h *CommentHandler) Create(c *gin.Context) {
|
||||
}
|
||||
|
||||
type CreateRequest struct {
|
||||
PostID string `json:"post_id" binding:"required"`
|
||||
Content string `json:"content"` // 内容可选,允许只发图片
|
||||
ParentID *string `json:"parent_id"`
|
||||
Images []string `json:"images"` // 图片URL列表
|
||||
PostID string `json:"post_id" binding:"required"`
|
||||
Content string `json:"content"`
|
||||
Segments model.MessageSegments `json:"segments"`
|
||||
ParentID *string `json:"parent_id"`
|
||||
Images []string `json:"images"`
|
||||
}
|
||||
|
||||
var req CreateRequest
|
||||
@@ -45,12 +47,18 @@ func (h *CommentHandler) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 验证:评论必须有内容或图片
|
||||
if req.Content == "" && len(req.Images) == 0 {
|
||||
// 验证:评论必须有内容或图片或segments
|
||||
if req.Content == "" && len(req.Images) == 0 && len(req.Segments) == 0 {
|
||||
response.BadRequest(c, "评论内容或图片不能同时为空")
|
||||
return
|
||||
}
|
||||
|
||||
// 如果没有content但有segments,从segments提取文本
|
||||
content := req.Content
|
||||
if content == "" && len(req.Segments) > 0 {
|
||||
content = dto.ExtractTextContent(req.Segments)
|
||||
}
|
||||
|
||||
// 将图片列表转换为JSON字符串
|
||||
var imagesJSON string
|
||||
if len(req.Images) > 0 {
|
||||
@@ -58,7 +66,7 @@ func (h *CommentHandler) Create(c *gin.Context) {
|
||||
imagesJSON = string(imagesBytes)
|
||||
}
|
||||
|
||||
comment, err := h.commentService.Create(c.Request.Context(), req.PostID, userID, req.Content, req.ParentID, imagesJSON, req.Images)
|
||||
comment, err := h.commentService.Create(c.Request.Context(), req.PostID, userID, content, req.Segments, req.ParentID, imagesJSON, req.Images)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, "failed to create comment")
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user