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 (
|
||||
"errors"
|
||||
@@ -52,20 +52,21 @@ func (h *PostHandler) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
type CreateRequest struct {
|
||||
Title string `json:"title" binding:"required"`
|
||||
Content string `json:"content" binding:"required"`
|
||||
Images []string `json:"images"`
|
||||
ChannelID *string `json:"channel_id,omitempty"`
|
||||
}
|
||||
|
||||
var req CreateRequest
|
||||
var req dto.CreatePostWithSegmentsRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
post, err := h.postService.Create(c.Request.Context(), userID, req.Title, req.Content, req.Images, req.ChannelID)
|
||||
content := req.Content
|
||||
segments := req.Segments
|
||||
|
||||
if len(segments) == 0 && content == "" {
|
||||
response.BadRequest(c, "content or segments is required")
|
||||
return
|
||||
}
|
||||
|
||||
post, err := h.postService.Create(c.Request.Context(), userID, req.Title, content, segments, req.Images, req.ChannelID)
|
||||
if err != nil {
|
||||
var moderationErr *service.PostModerationRejectedError
|
||||
if errors.As(err, &moderationErr) {
|
||||
@@ -123,6 +124,7 @@ func (h *PostHandler) GetByID(c *gin.Context) {
|
||||
ChannelID: post.ChannelID,
|
||||
Title: post.Title,
|
||||
Content: post.Content,
|
||||
Segments: dto.SegmentsOrDefault(post.Segments, post.Content),
|
||||
Images: dto.ConvertPostImagesToResponse(post.Images),
|
||||
Status: string(post.Status),
|
||||
LikesCount: post.LikesCount,
|
||||
@@ -365,9 +367,10 @@ func (h *PostHandler) Update(c *gin.Context) {
|
||||
}
|
||||
|
||||
type UpdateRequest struct {
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
Images *[]string `json:"images"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
Segments model.MessageSegments `json:"segments"`
|
||||
Images *[]string `json:"images"`
|
||||
}
|
||||
|
||||
var req UpdateRequest
|
||||
@@ -382,6 +385,12 @@ func (h *PostHandler) Update(c *gin.Context) {
|
||||
if req.Content != "" {
|
||||
post.Content = req.Content
|
||||
}
|
||||
if len(req.Segments) > 0 {
|
||||
post.Segments = req.Segments
|
||||
if req.Content == "" {
|
||||
post.Content = dto.ExtractTextContent(req.Segments)
|
||||
}
|
||||
}
|
||||
|
||||
err = h.postService.UpdateWithImages(c.Request.Context(), post, req.Images)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user