Skip to content

feat(notify): add user-configurable notification cooldown (+ tests & e2e) - #7

Merged
yang-yang9 merged 8 commits into
devfrom
investigate/quota-warning-email-frequency
Jul 19, 2026
Merged

yang-yang9 merged 8 commits into
devfrom
investigate/quota-warning-email-frequency

Conversation

@yang-yang9

Copy link
Copy Markdown

⚠️ 提交说明 / PR Notice

Important

  • 本 PR 在 AI 辅助下完成,描述已人工整理。

🤖 AI-assisted disclosure: 当前 git 提交者(秋朗 / qiulang.yy@alibaba-inc.com)非本仓库历史核心维护者(CaIon / Calcium-Ion / JustSong 等)。本 PR 的功能实现、单测与 e2e 脚本由 AI 辅助生成(Claude,各 commit 含 Co-Authored-By trailer),提交者已审阅并本地验证通过。

📝 变更描述 / Description

为每个用户增加可配置的通知冷却时间 notify_cooldown_minutes,避免活跃用户在额度低于预警阈值时被频繁邮件轰炸。

生效机制(已核对代码):

  • service/notify-limit.go:CheckNotificationLimit 新增 cooldownMinutes 参数;cooldownMinutes > 0 时走新增的 checkRedisCooldown / checkMemoryCooldown 分支——key 为 {userId}:{category}:cooldown,窗口内仅放行 1 条。
  • service/user_notify.go:NotifyUser 传入 userSetting.NotifyCooldownMinutes;冷却检查发生在渠道分派(switch notifyType)之前,且 key 不含渠道字段——因此对所有通知渠道(email / webhook / bark / gotify / feishu / qqbot)一致生效,换渠道不会绕过冷却;按"通知类别"(quota_exceed / channel_update 等)独立分桶,类别之间互不抑制。
  • cooldownMinutes = 0:回退原服务端默认(NOTIFY_LIMIT_COUNT=2 / NOTIFICATION_LIMIT_DURATION_MINUTE=10,同样渠道无关)。
  • Controller UpdateUserSetting 校验 0 ≤ 值 ≤ 43200。
  • 前端:default(notification-tab.tsx 的 #cooldown 数字输入)+ classic(NotificationSettings.jsx 的 Semi AutoComplete notifyCooldownMinutes)。

测试覆盖:

  • 后端单测 service/notify-limit_test.go(5 项:窗口内抑制 / per-user / per-notifyType / 窗口过期再放行 / 0 回退服务端默认)。
  • Playwright 真实浏览器 e2e:web/e2e-cooldown.mjs(default)+ web/e2e-classic-cooldown.mjs(classic),覆盖 setup → 登录 → 设置页 cooldown 字段渲染 → 填值 → 保存 → 重载持久化 → API 校验。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix)
  • ✨ 新功能 (New feature)
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

  • 设计决策见 docs/decisions/2026-07-14-notify-cooldown-user-configurable.md

✅ 提交前检查项 / Checklist

  • 人工确认: 本 PR 描述由 AI 辅助起草并经提交者整理,未直接粘贴未处理的 AI 输出。
  • 非重复提交: 已确认无既有 open PR for investigate/quota-warning-email-frequency。
  • 变更理解: 冷却按 (用户 × 通知类别) 分桶、渠道无关、检查在渠道分派前。
  • 范围聚焦: 仅含 cooldown 功能 + 其测试,无无关改动(CI workflow 差异已在 dev 上对齐)。
  • 本地验证: 见下方「运行证明」。
  • 安全合规: 无敏感凭据;e2e 脚本中的账号仅本地一次性。

📸 运行证明 / Proof of Work

后端

  • go build ./... ✅
  • go test ./service/... ./controller/... ✅(全 package pass)
  • go test ./service -run TestCheckNotificationLimit → 5/5 PASS
  • go vet ./service/ clean

前端(bun install from web/,vitest run)

  • web/default → 130 files / 2660 tests ✅
  • web/classic → 28 files / 592 tests ✅

Playwright 真实浏览器 e2e(chromium headless,against sqlite 后端 + 真实 dist)

  • default:9/9 PASS — cooldown 输入渲染 → 填 30 → Save → toast 成功 → reload 仍 30;API setting = {"notify_type":"email",...,"notify_cooldown_minutes":30}
  • classic:8/8 PASS — /console/personal 的 Semi AutoComplete → 填 60 → 保存 → reload 仍 60;API = {"...","notify_cooldown_minutes":60}

Diff vs dev: 19 files, +577 / -10。

yyy and others added 8 commits July 14, 2026 15:20
Allow users to set a custom cooldown period (in minutes) between
quota warning notifications, reducing email spam for active users.
When set to 0, the server default (2 per 10 minutes) applies.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add the notify_cooldown_minutes field to the classic theme's
notification settings, matching the default theme implementation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The lockfile lives at web/bun.lock (workspace root), but CI was
running bun install from web/default/ and web/classic/ subdirectories,
causing --frozen-lockfile to hang looking for a non-existent lockfile.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The bun.lock was generated with the internal Alibaba npm mirror
(registry.anpm.alibaba-inc.com), which is unreachable from GitHub
Actions runners. Switch to the public npm registry and drop
--frozen-lockfile since the registry URLs differ from the lockfile.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Cover CheckNotificationLimit's user-configurable cooldown branches:
within-window suppression, per-user and per-notifyType bucket scoping,
re-allow after the window elapses, and the cooldownMinutes=0 fallback
to the server-default cap. The default-path test initializes
constant.NotifyLimitCount / NotificationLimitDurationMinute in-fixture
since those are only populated at server startup.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add real-browser e2e coverage for the notify_cooldown_minutes field in
both themes: default (/sign-in -> /profile -> Settings & Preferences
tab -> #cooldown) and classic (/login -> /console/personal -> Semi
AutoComplete). Both verify render + set/save/persist round-trip against
PUT/GET /api/user/self.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Resolve .github/workflows/frontend-tests.yml by taking dev's npm-based
CI (setup-node + resolve-catalogs + npm install + npx vitest run),
consistent with the setup-node step git auto-merged in. Trigger paths
become the union (workflow file + web/bun.lock + web/package.json +
theme dirs), which equals dev's version. Cooldown feature + tests + e2e
unaffected.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@yang-yang9
yang-yang9 merged commit cb93c89 into dev Jul 19, 2026
6 checks passed
@yang-yang9
yang-yang9 deleted the investigate/quota-warning-email-frequency branch November 1, 2026 02:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant