feat(api): add message segments support for posts and comments
All checks were successful
Build Backend / build (push) Successful in 19m52s
Build Backend / build-docker (push) Successful in 1m29s

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:
lafay
2026-04-23 22:29:34 +08:00
parent 15e7c99353
commit 05c7f8a5eb
14 changed files with 364 additions and 154 deletions

View File

@@ -1,9 +1,9 @@
package repository
package repository
import (
"with_you/internal/model"
"strings"
"time"
"with_you/internal/model"
"gorm.io/gorm"
"gorm.io/gorm/clause"

View File

@@ -1,8 +1,8 @@
package repository
package repository
import (
"with_you/internal/model"
"errors"
"with_you/internal/model"
"gorm.io/gorm"
)
@@ -16,6 +16,7 @@ type VoteRepository interface {
GetUserVote(postID, userID string) (*model.UserVote, error)
UpdateOption(optionID, content string) error
DeleteOptionsByPostID(postID string) error
CountVotesByOption(postID, optionID string) (int, error)
}
// voteRepository 投票仓储实现
@@ -72,9 +73,9 @@ func (r *voteRepository) Vote(postID, userID, optionID string) error {
// 验证选项是否属于该帖子
var option model.VoteOption
if err := tx.Where("id = ? AND post_id = ?", optionID, postID).First(&option).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return errors.New("invalid option")
}
if errors.Is(err, gorm.ErrRecordNotFound) {
return errors.New("invalid option")
}
return err
}
@@ -100,9 +101,9 @@ func (r *voteRepository) Unvote(postID, userID string) error {
var userVote model.UserVote
err := tx.Where("post_id = ? AND user_id = ?", postID, userID).First(&userVote).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil // 没有投票记录,直接返回
}
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil // 没有投票记录,直接返回
}
return err
}
@@ -144,12 +145,16 @@ func (r *voteRepository) UpdateOption(optionID, content string) error {
// DeleteOptionsByPostID 删除帖子的所有投票选项
func (r *voteRepository) DeleteOptionsByPostID(postID string) error {
return r.db.Transaction(func(tx *gorm.DB) error {
// 删除关联的用户投票记录
if err := tx.Where("post_id = ?", postID).Delete(&model.UserVote{}).Error; err != nil {
return err
}
// 删除投票选项
return tx.Where("post_id = ?", postID).Delete(&model.VoteOption{}).Error
})
}
// CountVotesByOption 统计指定选项的投票数
func (r *voteRepository) CountVotesByOption(postID, optionID string) (int, error) {
var count int64
err := r.db.Model(&model.UserVote{}).Where("post_id = ? AND option_id = ?", postID, optionID).Count(&count).Error
return int(count), err
}