restrict v1

This commit is contained in:
tamakyi
2026-10-18 21:14:59 +08:00
parent da7e76a7db
commit 6bd90779f8
23 changed files with 1754 additions and 1 deletions
+15
View File
@@ -626,6 +626,7 @@ func (db *users) GetShowInHome(ctx context.Context, page, pageSize int, sort str
}
query := db.WithContext(ctx).Where("show_in_home = ?", true)
query = query.Where("id NOT IN (?)", activeRestrictionsSubQuery(db.WithContext(ctx)))
switch sort {
case "oldest":
query = query.Order("created_at asc")
@@ -653,6 +654,7 @@ func (db *users) GetByTag(ctx context.Context, tag string, page, pageSize int, s
tagCond = "FIND_IN_SET(?, tags)"
}
baseQuery := db.WithContext(ctx).Model(&User{}).Where("show_in_home = ? AND tags IS NOT NULL AND "+tagCond, true, tag)
baseQuery = baseQuery.Where("id NOT IN (?)", activeRestrictionsSubQuery(db.WithContext(ctx)))
if err := baseQuery.Count(&count).Error; err != nil {
return nil, 0, errors.Wrap(err, "count users by tag")
}
@@ -746,6 +748,7 @@ func (db *users) getRandomUsers(ctx context.Context, limit int) ([]*User, error)
}
if err := db.WithContext(ctx).
Where("show_in_home = ?", true).
Where("id NOT IN (?)", activeRestrictionsSubQuery(db.WithContext(ctx))).
Order(randomFunc).
Limit(limit).
Find(&users).Error; err != nil {
@@ -1047,3 +1050,15 @@ func purgeUserContent(ctx context.Context, tx *gorm.DB, userID uint) error {
}
return nil
}
// activeRestrictionsSubQuery 返回「当前处于生效中的处置」的用户 ID 子查询。
//
// 用于把被处置的箱主从首页 / 探索 / 标签检索 / 随机位摘除,即 L1「停止分发」的曝光部分。
// 用子查询而不是先查一遍 ID 列表:这三处都在列表热路径上,多一次往返没有收益;
// 没有处置记录时子查询返回空集,正常路径不产生额外代价。
// 注意 GetRandom 有 30 秒缓存,处置后随机位最多滞后 30 秒生效(其余入口即时生效)。
func activeRestrictionsSubQuery(tx *gorm.DB) *gorm.DB {
return tx.Table("user_restrictions").Select("user_id").
Where("revoked_at IS NULL").
Where("expires_at > ?", time.Now())
}