This commit is contained in:
tamakyi
2026-10-19 01:58:00 +08:00
parent bd613a6f5a
commit 4592b6ef24
14 changed files with 608 additions and 244 deletions
+18 -18
View File
@@ -202,29 +202,29 @@ func (s *restrictions) RevokeActive(ctx context.Context, userID uint) (int64, er
}
func (s *restrictions) ActiveLevel(ctx context.Context, userID uint) (RestrictionLevel, *UserRestriction, error) {
levels, err := s.ActiveLevels(ctx, []uint{userID})
if err != nil {
return "", nil, err
}
level, ok := levels[userID]
if !ok {
return "", nil, nil
}
var restriction UserRestriction
// 只查一次:这个方法在每次内容提交时都会被调用(发布拦截 + 审核严格度),
// 早先「先 ActiveLevels 再 First」是两次往返,1C1G 上没必要。
var rows []*UserRestriction
if err := s.db.WithContext(ctx).
Where("user_id = ?", userID).
Where("level = ?", string(level)).
Where("revoked_at IS NULL").
Where("expires_at > ?", time.Now()).
Order("created_at DESC").
First(&restriction).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return level, nil, nil
}
return "", nil, errors.Wrap(err, "find active restriction")
Find(&rows).Error; err != nil {
return "", nil, errors.Wrap(err, "list active restrictions")
}
return level, &restriction, nil
// 正常情况最多一条(Create 是替换语义),这里取最高级别以防历史脏数据叠加。
var best *UserRestriction
bestRank := 0
for _, row := range rows {
if rank, ok := restrictionLevelRank[RestrictionLevel(row.Level)]; ok && rank > bestRank {
best, bestRank = row, rank
}
}
if best == nil {
return "", nil, nil
}
return RestrictionLevel(best.Level), best, nil
}
func (s *restrictions) ActiveLevels(ctx context.Context, userIDs []uint) (map[uint]RestrictionLevel, error) {