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,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"
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user