增加 @提及、搜索筛选与找回密码,并将右栏热门改为正在聊。
补齐评论提及补全与通知、按作者/板块/仅标题搜索,以及邮箱验证码重置密码;同时修复 Go 提及正则并优化侧栏悬停样式。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
65
service/mention.go
Normal file
65
service/mention.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.iioio.com/freefire/jiang13-forum/model"
|
||||
)
|
||||
|
||||
const maxMentionsPerContent = 10
|
||||
|
||||
// 与前端 highlightMentions 字符集对齐(字母数字下划线中文,兼容历史 -)
|
||||
// Go RE2 不支持 JS 的 \uXXXX,需用 \x{HHHH}
|
||||
var mentionPattern = regexp.MustCompile(`@([0-9A-Za-z_\x{4e00}-\x{9fa5}-]+)`)
|
||||
|
||||
// ExtractMentionNames 从纯文本提取 @提及名(去重、保序)
|
||||
func ExtractMentionNames(text string) []string {
|
||||
matches := mentionPattern.FindAllStringSubmatch(text, -1)
|
||||
if len(matches) == 0 {
|
||||
return nil
|
||||
}
|
||||
seen := make(map[string]struct{}, len(matches))
|
||||
out := make([]string, 0, len(matches))
|
||||
for _, m := range matches {
|
||||
name := strings.TrimSpace(m[1])
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
key := strings.ToLower(name)
|
||||
if _, ok := seen[key]; ok {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
out = append(out, name)
|
||||
if len(out) >= maxMentionsPerContent {
|
||||
break
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// ResolveMentionUserIDs 将提及名解析为用户 ID(优先 username,其次 nickname;排除 excludeUserID)
|
||||
func ResolveMentionUserIDs(names []string, excludeUserID uint) []uint {
|
||||
if len(names) == 0 {
|
||||
return nil
|
||||
}
|
||||
ids := make([]uint, 0, len(names))
|
||||
seen := make(map[uint]struct{}, len(names))
|
||||
for _, name := range names {
|
||||
var u model.User
|
||||
err := model.DB.Select("id").Where("username = ?", name).First(&u).Error
|
||||
if err != nil {
|
||||
err = model.DB.Select("id").Where("nickname = ?", name).First(&u).Error
|
||||
}
|
||||
if err != nil || u.ID == 0 || u.ID == excludeUserID {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[u.ID]; ok {
|
||||
continue
|
||||
}
|
||||
seen[u.ID] = struct{}{}
|
||||
ids = append(ids, u.ID)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
Reference in New Issue
Block a user