并发分组(新功能): - 新表 concurrency_groups(名称/上限/默认),用户加 concurrency_group_id - 启动自动建「默认并发」组(上限10、默认),老用户回填、新注册自动绑定 - 并发计数改用 Redis(自愈 sorted-set + TTL + fail-open): · 用户并发(画图台 + API key 合计)受其分组上限限制,0=不限制 → 超返回 429 · 账号级并发也从内存 gate 换成同一套 Redis(6 处调用点) · 移除旧的「已有正在生成的任务」单任务锁 - 后台「并发分组」新菜单:增删改、设默认、用户数;默认组不可删(删别的组成员转默认) - 用户管理:并发列 + 新建/编辑可选分组 - 个人设置页:账户信息卡(用户名/邮箱/角色/余额/并发);/me 暴露 concurrency_group/limit 品牌 / Logo(上传到 RustFS): - Logo 改成拖拽/点击上传,点保存才上传;替换自动删旧;branding/ 设为公开且被清理任务 pin 住(永不删) - 有自定义就用:前台左侧 nav + 后台侧栏 + favicon(浏览器标签);没有则默认 V 图标 - 前台页头还原成文字;首页 Hero 子标题用 site.subtitle(默认那句宣传语,设置页预填) - 邮件验证码标题用站点名;新增 POST/DELETE /settings/logo + POST /settings/asset(首页底图上传) 兑换码开关: - 系统设置→积分 新增「开启兑换码」(默认开);关闭后后端拒绝兑换、前台隐藏兑换入口(/site 暴露 cdk_redeem_enabled) 文档 / 分辨率: - 去掉 quality 参数:size(宽x高)同时决定比例 + 分辨率档(长边映射 1K/2K/4K) - 文档加「分辨率对照表」(14 个比例 × 1K/2K/4K → size 该传的值);guessRatio 与自定义模型 RATIO_OPTS 对齐到 14 个 其它: - 删模型时同步清掉各上游账号「支持模型」里的该 id - 首页设置/兑换码弹窗去固定高度滚动条;展示位弹窗浅色主题适配 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
85 lines
3.6 KiB
Go
85 lines
3.6 KiB
Go
package bootstrap
|
|
|
|
import (
|
|
"context"
|
|
|
|
"backend/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func seedDefaults(ctx context.Context, db *gorm.DB) error {
|
|
defaults := []model.SiteSetting{
|
|
{Key: "site.title", Value: "Vivid"},
|
|
{Key: "site.logo", Value: ""},
|
|
{Key: "site.subtitle", Value: ""},
|
|
{Key: "contact.qq", Value: "1114639355"},
|
|
{Key: "contact.qq_link", Value: "https://qm.qq.com/q/ItgCcNA7ac"},
|
|
{Key: "contact.qq_group", Value: "1106849765"},
|
|
{Key: "contact.qq_group_link", Value: "https://qm.qq.com/q/976LeMFoHu"},
|
|
{Key: "contact.email", Value: "vividairun@gmail.com"},
|
|
{Key: "contact.shop", Value: "https://pay.ldxp.cn/shop/chiyi"},
|
|
{Key: "auth.open", Value: "true"},
|
|
{Key: "auth.email_code", Value: "false"},
|
|
{Key: "auth.allow_password_reset", Value: "false"},
|
|
{Key: "auth.allowed_email_domains", Value: ""},
|
|
{Key: "auth.code_ttl_seconds", Value: "600"},
|
|
{Key: "smtp.host", Value: ""},
|
|
{Key: "smtp.port", Value: "587"},
|
|
{Key: "smtp.username", Value: ""},
|
|
{Key: "smtp.password", Value: ""},
|
|
{Key: "smtp.from_addr", Value: ""},
|
|
{Key: "smtp.use_tls", Value: "true"},
|
|
{Key: "proxy.url", Value: ""},
|
|
{Key: "credits.checkin_enabled", Value: "true"},
|
|
{Key: "credits.checkin_reward", Value: "3"},
|
|
{Key: "credits.invite_enabled", Value: "true"},
|
|
{Key: "credits.invite_reward", Value: "3"},
|
|
{Key: "credits.cdk_redeem_enabled", Value: "true"},
|
|
{Key: "logs.retention_days", Value: "30"},
|
|
{Key: "media.retention_days", Value: "30"},
|
|
}
|
|
for _, item := range defaults {
|
|
var count int64
|
|
if err := db.WithContext(ctx).Model(&model.SiteSetting{}).Where("key = ?", item.Key).Count(&count).Error; err != nil {
|
|
return err
|
|
}
|
|
if count > 0 {
|
|
continue
|
|
}
|
|
if err := db.WithContext(ctx).Create(&item).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
// One-time backfill of the persistent per-model generation counter from
|
|
// historical success logs, so the admin "次数" keeps its running total when we
|
|
// switch it off the (retention-pruned) event_log. Only touches models still at
|
|
// 0, so it never double-counts after the first run; increments take over next.
|
|
if err := db.WithContext(ctx).Exec(
|
|
`UPDATE model_configs m SET generation_count = COALESCE(
|
|
(SELECT COUNT(*) FROM event_logs e WHERE e.model = m.id AND e.status = 'success'), 0)
|
|
WHERE m.generation_count = 0`).Error; err != nil {
|
|
return err
|
|
}
|
|
// Same one-time backfill for the per-user generation counter.
|
|
if err := db.WithContext(ctx).Exec(
|
|
`UPDATE users u SET generation_count = COALESCE(
|
|
(SELECT COUNT(*) FROM event_logs e WHERE e.user_id = u.id AND e.status = 'success'), 0)
|
|
WHERE u.generation_count = 0`).Error; err != nil {
|
|
return err
|
|
}
|
|
// Seed the dashboard lifetime counters from logs ONCE (only when empty), so the
|
|
// all-time cards start from real history then track forward via the hooks.
|
|
var counterRows int64
|
|
if err := db.WithContext(ctx).Model(&model.StatCounter{}).Count(&counterRows).Error; err == nil && counterRows == 0 {
|
|
_ = db.WithContext(ctx).Exec(`INSERT INTO stat_counters (key, value, updated_at)
|
|
SELECT 'total', COUNT(*), now() FROM event_logs
|
|
UNION ALL SELECT 'success', COUNT(*) FILTER (WHERE status='success'), now() FROM event_logs
|
|
UNION ALL SELECT 'failed', COUNT(*) FILTER (WHERE status='failed'), now() FROM event_logs
|
|
UNION ALL SELECT 'image', COUNT(*) FILTER (WHERE kind='image'), now() FROM event_logs
|
|
UNION ALL SELECT 'video', COUNT(*) FILTER (WHERE kind='video'), now() FROM event_logs
|
|
UNION ALL SELECT 'api', COUNT(*) FILTER (WHERE source='v1'), now() FROM event_logs
|
|
ON CONFLICT (key) DO NOTHING`).Error
|
|
}
|
|
return nil
|
|
}
|