fix: improve cleanup rule display

This commit is contained in:
ShukeBta
2026-06-08 09:52:17 +08:00
parent dea45c5423
commit cf6c58d26e
3 changed files with 206 additions and 22 deletions
+82
View File
@@ -3,12 +3,20 @@ package service
import (
"context"
"encoding/json"
"regexp"
"strconv"
"strings"
"github.com/ShukeBta/MediaStationGo/internal/repository"
)
var (
cleanupRuleWindowDaysRE = regexp.MustCompile(`(?i)(?:^|[_-])(\d+)[_-](\d+)\s*d(?:$|[_-])`)
cleanupRuleSingleDayRE = regexp.MustCompile(`(?i)(?:^|[_-])(\d+)\s*d(?:$|[_-])`)
cleanupRuleHoursRE = regexp.MustCompile(`(?i)(?:^|[_-])(\d+(?:\.\d+)?)\s*h(?:$|[_-])`)
cleanupRuleNumberRE = regexp.MustCompile(`(?i)(?:^|[_-])(\d+)(?:$|[_-])`)
)
// Bot / 设备管控相关的设置键。全部存储在 settings 表,由管理员通过
// Telegram Bot 命令调整。带安全默认值:所有"自动删号"策略默认关闭。
const (
@@ -187,6 +195,7 @@ func normalizeCleanupRules(rules []accountCleanupRule) []accountCleanupRule {
if r.Name == "" {
r.Name = r.ID
}
inferCleanupRuleValuesFromID(&r)
if r.WindowDaysMin < 1 {
r.WindowDaysMin = 1
}
@@ -206,3 +215,76 @@ func normalizeCleanupRules(rules []accountCleanupRule) []accountCleanupRule {
}
return out
}
func inferCleanupRuleValuesFromID(rule *accountCleanupRule) {
if rule == nil {
return
}
key := strings.ToLower(strings.TrimSpace(rule.ID + "_" + rule.Name))
switch rule.Type {
case "watch_hours":
if minDays, maxDays := cleanupRuleWindowDays(key); minDays > 0 && maxDays > 0 {
rule.WindowDaysMin = minDays
rule.WindowDaysMax = maxDays
} else if days := cleanupRuleSingleDay(key); days > 0 {
rule.WindowDaysMin = days
rule.WindowDaysMax = days
}
if hours := cleanupRuleHours(key); hours > 0 {
rule.MinHours = hours
}
case "recent_login":
if days := cleanupRuleSingleDay(key); days > 0 {
rule.WindowDaysMax = days
}
case "account_age_grace":
if days := cleanupRuleSingleDay(key); days > 0 {
rule.MinCount = days
}
case "signin_streak":
if n := cleanupRuleTrailingNumber(key); n > 0 {
rule.MinCount = n
}
}
}
func cleanupRuleWindowDays(value string) (int, int) {
if m := cleanupRuleWindowDaysRE.FindStringSubmatch(value); len(m) >= 3 {
minDays, _ := strconv.Atoi(m[1])
maxDays, _ := strconv.Atoi(m[2])
if maxDays < minDays {
maxDays = minDays
}
return minDays, maxDays
}
return 0, 0
}
func cleanupRuleSingleDay(value string) int {
if m := cleanupRuleSingleDayRE.FindStringSubmatch(value); len(m) >= 2 {
days, _ := strconv.Atoi(m[1])
return days
}
return 0
}
func cleanupRuleHours(value string) float64 {
if m := cleanupRuleHoursRE.FindStringSubmatch(value); len(m) >= 2 {
hours, _ := strconv.ParseFloat(m[1], 64)
return hours
}
return 0
}
func cleanupRuleTrailingNumber(value string) int {
matches := cleanupRuleNumberRE.FindAllStringSubmatch(value, -1)
if len(matches) == 0 {
return 0
}
last := matches[len(matches)-1]
if len(last) < 2 {
return 0
}
n, _ := strconv.Atoi(last[1])
return n
}