Skip to content

新增 POST /api/notify:用 enk_ API Key 或登录态直接发推送 - #109

Merged
todosdev[bot] merged 1 commit into
mainfrom
tds/conv-01a148e2-580d-7cd5-b52f-e3c8b7a438a7
Oct 17, 2026
Merged

todosdev[bot] merged 1 commit into
mainfrom
tds/conv-01a148e2-580d-7cd5-b52f-e3c8b7a438a7

Conversation

@todosdev

@todosdev todosdev Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add POST /api/notify (exact path, no :token) so enk_ API-key holders — or a logged-in cookie session — can trigger push notifications directly, reusing their existing key instead of a separate notify token.
  • Layer the existing notify_trigger rate limit (30/min) on top of the per-key 120/min limit as a second anti-spam gate.
  • Extract the shared per-user dispatch into one helper reused by both routes; the legacy /:token route is unchanged.

Changes

  • worker/src/routes/notify.ts
    • Register notifyRoutes.use("/", apiKeyOrCookieAuth) and notifyRoutes.use("/", rateLimit({ max: 30, window: 60, keyPrefix: "notify_trigger" })) on the exact root path.
    • Add POST / handler: reads userId from context (middleware already guarantees an active user), looks up the notification config (400 when absent), parses { title?, body?, url? } (title defaults to Notification), sends, and returns { success: true, results }.
    • Extract notifyByUserId(c, userId) reused by the new and legacy routes; the legacy /:token auth and 401 branches are left untouched. Switch to the shared HonoEnv type from ../types (type-only).
  • README.md / README.en.md
    • Document POST /api/notify under the API Key section, noting the default Notification title, cookie-session support, and the 30/min per-IP limit shared with the legacy route.

Testing

  • pnpm lint, pnpm format:check, pnpm typecheck, pnpm build all pass.
  • Full local wrangler dev chain verified with a seeded local D1/KV and a public webhook.site sink (real outbound delivery confirmed):
    • enk_ key → 200 {success:true, results:[{channel:"webhook", success:true}]}; webhook.site received the exact payload.
    • Invalid/expired key → 401; no auth → 401.
    • Valid key but no notification config → 400 {"error":"No notification channels configured"}.
    • Cookie session → 200.
    • Legacy POST /api/notify/:token → 200; invalid token → 401.
    • Malformed JSON → 400 {"error":"Invalid request body"} via the global onError handler.

  - Add POST /api/notify (exact path, no :token) behind
    apiKeyOrCookieAuth so enk_ key holders can trigger pushes directly
    with their session's permissions; the handler takes userId from
    c.get("userId") without re-validating the already-active user.
  - Layer the same 30/min notify_trigger rate limit on the new route,
    on top of the per-key 120/min limit inside apiKeyOrCookieAuth.
    Both routes share the per-IP quota via the same keyPrefix.
  - Extract notifyByUserId (config lookup -> 400 when missing, body
    parse, NotifyMessage assembly, sendNotifications) reused by both
    routes; the legacy /:token route's behaviour is unchanged.
  - Document POST /api/notify in README.md and README.en.md.

Checkpoint: Wc-n6Gb0ko9Esa9rikO37
@todosdev
todosdev Bot merged commit 9062f73 into main Oct 17, 2026
1 check passed
@mapxn
mapxn deleted the tds/conv-01a148e2-580d-7cd5-b52f-e3c8b7a438a7 branch October 18, 2026 08:59
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.

0 participants