feat: @提及支持改名后链接跟随UID + 显示名自动更新
- comment_mentions新增original_username列,创建时记录原始@用户名
- Mentions map改为{original_username: {uid, name}}结构,name为当前显示名
- 用户改名后:旧@文本仍能匹配original_username,链接跟随uid不变,显示名自动更新
- 前端renderMentions适配新结构
This commit is contained in:
@ -17,8 +17,9 @@ func (r *CommentRepo) FindMentionsByComment(commentID uint) ([]model.CommentMent
|
||||
return list, err
|
||||
}
|
||||
|
||||
// LoadMentionsForComments 批量加载多条评论的有效@提及映射(username -> uid)
|
||||
// 传入的 comments 会被原地修改,填充 Mentions 字段
|
||||
// LoadMentionsForComments 批量加载多条评论的有效@提及映射
|
||||
// key=创建时的原始@用户名, value={uid, 用户当前显示名}
|
||||
// 用户改名后:原始名匹配body中的旧@文本,显示名随users表更新
|
||||
func (r *CommentRepo) LoadMentionsForComments(comments []model.Comment) error {
|
||||
if len(comments) == 0 {
|
||||
return nil
|
||||
@ -29,15 +30,15 @@ func (r *CommentRepo) LoadMentionsForComments(comments []model.Comment) error {
|
||||
commentIDs[i] = c.ID
|
||||
}
|
||||
|
||||
// 查询 comment_mentions JOIN users,拿到 username 和 uid
|
||||
type mentionRow struct {
|
||||
CommentID uint
|
||||
UID uint
|
||||
Username string
|
||||
CommentID uint
|
||||
UID uint
|
||||
OriginalUsername string
|
||||
CurrentName string
|
||||
}
|
||||
var rows []mentionRow
|
||||
err := r.db.Table("comment_mentions").
|
||||
Select("comment_mentions.comment_id, comment_mentions.uid, users.username").
|
||||
Select("comment_mentions.comment_id, comment_mentions.uid, comment_mentions.original_username, users.username AS current_name").
|
||||
Joins("LEFT JOIN users ON users.id = comment_mentions.uid").
|
||||
Where("comment_mentions.comment_id IN ?", commentIDs).
|
||||
Find(&rows).Error
|
||||
@ -45,13 +46,16 @@ func (r *CommentRepo) LoadMentionsForComments(comments []model.Comment) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// 按 comment_id 分组
|
||||
mentionMap := make(map[uint]map[string]uint)
|
||||
// 按 comment_id 分组,key = original_username, value = MentionInfo
|
||||
mentionMap := make(map[uint]map[string]model.MentionInfo)
|
||||
for _, row := range rows {
|
||||
if mentionMap[row.CommentID] == nil {
|
||||
mentionMap[row.CommentID] = make(map[string]uint)
|
||||
mentionMap[row.CommentID] = make(map[string]model.MentionInfo)
|
||||
}
|
||||
mentionMap[row.CommentID][row.OriginalUsername] = model.MentionInfo{
|
||||
UID: row.UID,
|
||||
Name: row.CurrentName,
|
||||
}
|
||||
mentionMap[row.CommentID][row.Username] = row.UID
|
||||
}
|
||||
|
||||
for i := range comments {
|
||||
|
||||
Reference in New Issue
Block a user