mirror of
https://github.com/koala73/worldmonitor.git
synced 2026-04-25 17:14:57 +02:00
* feat(notifications): Phase 6 — web-push channel for PWA notifications
Adds a web_push notification channel so PWA users receive native
notifications when this tab is closed. Deep-links click to the
brief magazine URL for brief_ready events, to the event link for
everything else.
Schema / API:
- channelTypeValidator gains 'web_push' literal
- notificationChannels union adds { endpoint, p256dh, auth,
userAgent? } variant (standard PushSubscription identity triple +
cosmetic UA for the settings UI)
- new setWebPushChannelForUser internal mutation upserts the row
- /relay/deactivate allow-list extended to accept 'web_push'
- api/notification-channels: 'set-web-push' action validates the
triple, rejects non-https, truncates UA to 200 chars
Client (src/services/push-notifications.ts + src/config/push.ts):
- isWebPushSupported guards Tauri webview + iOS Safari
- subscribeToPush: permission + pushManager.subscribe + POST triple
- unsubscribeFromPush: pushManager.unsubscribe + DELETE row
- VAPID_PUBLIC_KEY constant (with VITE_VAPID_PUBLIC_KEY env override)
- base64 <-> Uint8Array helpers (VAPID key encoding)
Service worker (public/push-handler.js):
- Imported into VitePWA's generated sw.js via workbox.importScripts
- push event: renders notification; requireInteraction=true for
brief_ready so a lock-screen swipe does not dismiss the CTA
- notificationclick: focuses+navigates existing same-origin client
when present, otherwise opens a new window
- Malformed JSON falls back to raw text body, missing data falls
back to a minimal WorldMonitor default
Relay (scripts/notification-relay.cjs):
- sendWebPush() with lazy-loaded web-push dep. 404/410 triggers
deactivateChannel('web_push'). Missing VAPID env vars logs once
and skips — other channels keep delivering.
- processEvent dispatch loop + drainHeldForUser both gain web_push
branches
Settings UI (src/services/notifications-settings.ts):
- New 'Browser Push' tile with bell icon
- Enable button lazy-imports push-notifications, calls subscribe,
renders 'Not supported' on Tauri/in-app webviews
- Remove button routes web_push specifically through
unsubscribeFromPush so the browser side is cleaned up too
Env vars required on Railway services:
VAPID_PUBLIC_KEY public key
VAPID_PRIVATE_KEY private key
VAPID_SUBJECT mailto:support@worldmonitor.app (optional)
Public key is also committed as the default in src/config/push.ts
so the client bundle works without a build-time override.
Tests: 11 new cases in tests/brief-web-push.test.mjs
- base64 <-> Uint8Array round-trip + null guards
- VAPID default fallback when env absent
- SW push event rendering, requireInteraction gating, malformed JSON
+ no-data fallbacks
- SW notificationclick: openWindow vs focus+navigate, default url
154/154 tests pass. Both tsconfigs typecheck clean.
* fix(brief): address PR #3173 review findings + drop hardcoded VAPID
P1 (security): VAPID private key leaked in PR description.
Rotated the keypair. Old pair permanently invalidated. Structural fix:
Removed DEFAULT_VAPID_PUBLIC_KEY entirely. Hardcoding the public
key in src/config/push.ts gave rotations two sources of truth
(code vs env) — exactly the friction that caused me to paste the
private key in a PR description in the first place. VAPID_PUBLIC_KEY
now comes SOLELY from VITE_VAPID_PUBLIC_KEY at build time.
isWebPushConfigured() gates the subscribe flow so builds without
the env var surface as 'Not supported' rather than crashing
pushManager.subscribe.
Operator setup (one-time):
Vercel build: VITE_VAPID_PUBLIC_KEY=<public>
Railway services: VAPID_PUBLIC_KEY=<public>
VAPID_PRIVATE_KEY=<private>
VAPID_SUBJECT=mailto:support@worldmonitor.app
Rotation: update env on both sides, redeploy. No code change, no
PR body — no chance of leaking a key in a commit.
P2: single-device fan-out — setWebPushChannelForUser replaces the
previous subscription silently. Per-device fan-out is a schema change
deferred to follow-up. Fix for now: surface the replacement in
settings UI copy ('Enabling here replaces any previously registered
browser.') so users who expect multi-device see the warning.
P2: 24h push TTL floods offline devices on reconnect. Event-type-aware:
brief_ready: 12h (daily editorial — still interesting)
quiet_hours_batch: 6h (by definition queued-on-wake)
everything else: 30m (transient alerts: noise after 30min)
REGRESSION test: VAPID_PUBLIC_KEY must be '' when env var is unset.
If a committed default is reintroduced, the test fails loudly.
11/11 web-push tests pass. Both tsconfigs typecheck clean.
* fix(notifications): deliver channel_welcome push for web_push connects (#3173 P2)
The settings UI queues a channel_welcome event on first web_push
subscribe (api/notification-channels.ts:240 via publishWelcome), but
processWelcome() in the relay only branched on slack/discord/email —
no web_push arm. The welcome event was consumed off the queue and
then silently dropped, leaving first-time subscribers with no
'connection confirmed' signal.
Fix: add a web_push branch to processWelcome. Calls sendWebPush with
eventType='channel_welcome' which maps to the 30-minute TTL tier in
the push-delivery switch — a welcome that arrives >30 min after
subscribe is noise, not confirmation.
Short body (under 80 chars) so Chrome/Firefox/Safari notification
shelves don't clip past ellipsis.
11/11 web-push tests pass.
* fix(notifications): address two P1 review findings on #3173
P1-A: SSRF via user-supplied web_push endpoint.
The set-web-push edge handler accepted any https:// URL and wrote
it to Convex. The relay's sendWebPush() later POSTs to whatever
endpoint sits in that row, giving any Pro user a server-side-request
primitive bounded only by the relay's network egress.
Fix: isAllowedPushEndpointHost() allow-list in api/notification-
channels.ts. Only the four known browser push-service hosts pass:
fcm.googleapis.com (Chrome / Edge / Brave)
updates.push.services.mozilla.com (Firefox)
web.push.apple.com (Safari, macOS 13+)
*.notify.windows.com (Windows Notification Service)
Fail-closed: unknown hosts rejected with 400 before the row ever
reaches Convex. If a future browser ships a new push service we'll
need to widen this list (guarded by the SSRF regression tests).
P1-B: cross-account endpoint reuse on shared devices.
The browser's PushSubscription is bound to the origin, NOT to the
Clerk session. User A subscribes on device X, signs out, user B
signs in on X and subscribes — the browser hands out the SAME
endpoint/p256dh/auth triple. The previous setWebPushChannelForUser
upsert keyed only by (userId, channelType), so BOTH rows now carry
the same endpoint. Every push the relay fans out for user A also
lands on device X which is now showing user B's session.
Fix: setWebPushChannelForUser scans all web_push rows and deletes
any that match the new endpoint BEFORE upserting. Effectively
transfers ownership of the subscription to the current caller.
The previous user will need to re-subscribe on that device if they
sign in again.
No endpoint-based index on notificationChannels — the scan happens
at <10k rows and is well-bounded to the one write-path per user
per connect. If volume grows, add an + migration.
Regression tests (tests/brief-web-push.test.mjs, 3 new cases):
- allow-list defines all four browser hosts + fail-closed return
- allow-list is invoked BEFORE convexRelay() in the handler
- setWebPushChannelForUser compares + deletes rows by endpoint
14/14 web-push tests pass. Both tsconfigs typecheck clean.
492 lines
17 KiB
TypeScript
492 lines
17 KiB
TypeScript
import { ConvexError, v } from "convex/values";
|
|
import { internalMutation, internalQuery, mutation, query } from "./_generated/server";
|
|
import { channelTypeValidator } from "./constants";
|
|
|
|
export const getChannelsByUserId = internalQuery({
|
|
args: { userId: v.string() },
|
|
handler: async (ctx, args) => {
|
|
return await ctx.db
|
|
.query("notificationChannels")
|
|
.withIndex("by_user", (q) => q.eq("userId", args.userId))
|
|
.collect();
|
|
},
|
|
});
|
|
|
|
export const setChannelForUser = internalMutation({
|
|
args: {
|
|
userId: v.string(),
|
|
channelType: channelTypeValidator,
|
|
chatId: v.optional(v.string()),
|
|
webhookEnvelope: v.optional(v.string()),
|
|
email: v.optional(v.string()),
|
|
webhookLabel: v.optional(v.string()),
|
|
},
|
|
handler: async (ctx, args) => {
|
|
const { userId, channelType, chatId, webhookEnvelope, email, webhookLabel } = args;
|
|
const existing = await ctx.db
|
|
.query("notificationChannels")
|
|
.withIndex("by_user_channel", (q) =>
|
|
q.eq("userId", userId).eq("channelType", channelType),
|
|
)
|
|
.unique();
|
|
const isNew = !existing;
|
|
const now = Date.now();
|
|
if (channelType === "telegram") {
|
|
if (!chatId) throw new ConvexError("chatId required for telegram channel");
|
|
const doc = { userId, channelType: "telegram" as const, chatId, verified: true, linkedAt: now };
|
|
if (existing) { await ctx.db.replace(existing._id, doc); } else { await ctx.db.insert("notificationChannels", doc); }
|
|
} else if (channelType === "slack") {
|
|
if (!webhookEnvelope) throw new ConvexError("webhookEnvelope required for slack channel");
|
|
const doc = { userId, channelType: "slack" as const, webhookEnvelope, verified: true, linkedAt: now };
|
|
if (existing) { await ctx.db.replace(existing._id, doc); } else { await ctx.db.insert("notificationChannels", doc); }
|
|
} else if (channelType === "email") {
|
|
if (!email) throw new ConvexError("email required for email channel");
|
|
const doc = { userId, channelType: "email" as const, email, verified: true, linkedAt: now };
|
|
if (existing) { await ctx.db.replace(existing._id, doc); } else { await ctx.db.insert("notificationChannels", doc); }
|
|
} else if (channelType === "webhook") {
|
|
if (!webhookEnvelope) throw new ConvexError("webhookEnvelope required for webhook channel");
|
|
const doc = { userId, channelType: "webhook" as const, webhookEnvelope, verified: true, linkedAt: now, webhookLabel };
|
|
if (existing) { await ctx.db.replace(existing._id, doc); } else { await ctx.db.insert("notificationChannels", doc); }
|
|
} else {
|
|
throw new ConvexError("discord channel must be set via set-discord-oauth");
|
|
}
|
|
return { isNew };
|
|
},
|
|
});
|
|
|
|
// Web Push (Phase 6). Stored as its own internal mutation because the
|
|
// payload shape is incompatible with setChannelForUser (three required
|
|
// identity fields, no chatId/webhookEnvelope/email). Replaces any
|
|
// prior subscription for this user — one subscription per user until
|
|
// per-device fan-out is needed.
|
|
//
|
|
// Cross-account dedupe: the browser's PushSubscription is bound to
|
|
// the origin, NOT to the Clerk session. If user A subscribes on
|
|
// device X, signs out, then user B signs in on the same device X
|
|
// and subscribes, the browser hands out the SAME endpoint. Without
|
|
// this dedupe, both users' rows carry the same endpoint — meaning
|
|
// every alert the relay fans out to user A would also deliver to
|
|
// user B on that shared device, and vice versa. That's a cross-
|
|
// account privacy leak.
|
|
//
|
|
// Fix: before writing the new row, delete any existing rows
|
|
// anywhere in the table that match this endpoint. Effectively
|
|
// transfers ownership of the subscription to the current caller.
|
|
// The previous user will need to re-subscribe on that device if
|
|
// they sign in again.
|
|
export const setWebPushChannelForUser = internalMutation({
|
|
args: {
|
|
userId: v.string(),
|
|
endpoint: v.string(),
|
|
p256dh: v.string(),
|
|
auth: v.string(),
|
|
userAgent: v.optional(v.string()),
|
|
},
|
|
handler: async (ctx, args) => {
|
|
// Step 1: scan for any existing rows with this endpoint across
|
|
// ALL users and delete them. notificationChannels has no
|
|
// endpoint-based index, so we filter at read time — acceptable
|
|
// at current scale (<10k rows) and well-bounded to a single
|
|
// write-path per user per connect.
|
|
const allWebPush = await ctx.db
|
|
.query("notificationChannels")
|
|
.collect();
|
|
for (const row of allWebPush) {
|
|
if (
|
|
row.channelType === "web_push" &&
|
|
// Narrow through the channel-type literal so TS knows
|
|
// `endpoint` exists on this row.
|
|
row.endpoint === args.endpoint
|
|
) {
|
|
await ctx.db.delete(row._id);
|
|
}
|
|
}
|
|
|
|
// Step 2: upsert the current-user row by (userId, channelType).
|
|
// After the delete above there is at most one row matching the
|
|
// unique index, so .unique() is safe.
|
|
const existing = await ctx.db
|
|
.query("notificationChannels")
|
|
.withIndex("by_user_channel", (q) =>
|
|
q.eq("userId", args.userId).eq("channelType", "web_push"),
|
|
)
|
|
.unique();
|
|
const isNew = !existing;
|
|
const doc = {
|
|
userId: args.userId,
|
|
channelType: "web_push" as const,
|
|
endpoint: args.endpoint,
|
|
p256dh: args.p256dh,
|
|
auth: args.auth,
|
|
verified: true,
|
|
linkedAt: Date.now(),
|
|
userAgent: args.userAgent,
|
|
};
|
|
if (existing) {
|
|
await ctx.db.replace(existing._id, doc);
|
|
} else {
|
|
await ctx.db.insert("notificationChannels", doc);
|
|
}
|
|
return { isNew };
|
|
},
|
|
});
|
|
|
|
export const setSlackOAuthChannelForUser = internalMutation({
|
|
args: {
|
|
userId: v.string(),
|
|
webhookEnvelope: v.string(),
|
|
slackChannelName: v.optional(v.string()),
|
|
slackTeamName: v.optional(v.string()),
|
|
slackConfigurationUrl: v.optional(v.string()),
|
|
},
|
|
handler: async (ctx, args) => {
|
|
const existing = await ctx.db
|
|
.query("notificationChannels")
|
|
.withIndex("by_user_channel", (q) =>
|
|
q.eq("userId", args.userId).eq("channelType", "slack"),
|
|
)
|
|
.unique();
|
|
const isNew = !existing;
|
|
const doc = {
|
|
userId: args.userId,
|
|
channelType: "slack" as const,
|
|
webhookEnvelope: args.webhookEnvelope,
|
|
verified: true,
|
|
linkedAt: Date.now(),
|
|
slackChannelName: args.slackChannelName,
|
|
slackTeamName: args.slackTeamName,
|
|
slackConfigurationUrl: args.slackConfigurationUrl,
|
|
};
|
|
if (existing) {
|
|
await ctx.db.replace(existing._id, doc);
|
|
} else {
|
|
await ctx.db.insert("notificationChannels", doc);
|
|
}
|
|
return { isNew };
|
|
},
|
|
});
|
|
|
|
export const setDiscordOAuthChannelForUser = internalMutation({
|
|
args: {
|
|
userId: v.string(),
|
|
webhookEnvelope: v.string(),
|
|
discordGuildId: v.optional(v.string()),
|
|
discordChannelId: v.optional(v.string()),
|
|
},
|
|
handler: async (ctx, args) => {
|
|
const existing = await ctx.db
|
|
.query("notificationChannels")
|
|
.withIndex("by_user_channel", (q) =>
|
|
q.eq("userId", args.userId).eq("channelType", "discord"),
|
|
)
|
|
.unique();
|
|
const isNew = !existing;
|
|
const doc = {
|
|
userId: args.userId,
|
|
channelType: "discord" as const,
|
|
webhookEnvelope: args.webhookEnvelope,
|
|
verified: true,
|
|
linkedAt: Date.now(),
|
|
discordGuildId: args.discordGuildId,
|
|
discordChannelId: args.discordChannelId,
|
|
};
|
|
if (existing) {
|
|
await ctx.db.replace(existing._id, doc);
|
|
} else {
|
|
await ctx.db.insert("notificationChannels", doc);
|
|
}
|
|
return { isNew };
|
|
},
|
|
});
|
|
|
|
export const deleteChannelForUser = internalMutation({
|
|
args: { userId: v.string(), channelType: channelTypeValidator },
|
|
handler: async (ctx, args) => {
|
|
const existing = await ctx.db
|
|
.query("notificationChannels")
|
|
.withIndex("by_user_channel", (q) =>
|
|
q.eq("userId", args.userId).eq("channelType", args.channelType),
|
|
)
|
|
.unique();
|
|
if (!existing) return;
|
|
await ctx.db.delete(existing._id);
|
|
const rules = await ctx.db
|
|
.query("alertRules")
|
|
.withIndex("by_user", (q) => q.eq("userId", args.userId))
|
|
.collect();
|
|
for (const rule of rules) {
|
|
const filtered = rule.channels.filter((c) => c !== args.channelType);
|
|
if (filtered.length !== rule.channels.length) {
|
|
await ctx.db.patch(rule._id, { channels: filtered });
|
|
}
|
|
}
|
|
},
|
|
});
|
|
|
|
export const createPairingTokenForUser = internalMutation({
|
|
args: { userId: v.string(), variant: v.optional(v.string()) },
|
|
handler: async (ctx, args) => {
|
|
const { userId, variant } = args;
|
|
const existing = await ctx.db
|
|
.query("telegramPairingTokens")
|
|
.withIndex("by_user", (q) => q.eq("userId", userId))
|
|
.collect();
|
|
for (const t of existing) {
|
|
if (!t.used) await ctx.db.patch(t._id, { used: true });
|
|
}
|
|
const bytes = new Uint8Array(32);
|
|
crypto.getRandomValues(bytes);
|
|
const token = btoa(String.fromCharCode(...bytes))
|
|
.replace(/\+/g, "-")
|
|
.replace(/\//g, "_")
|
|
.replace(/=+$/, "");
|
|
const expiresAt = Date.now() + 15 * 60 * 1000;
|
|
await ctx.db.insert("telegramPairingTokens", { userId, token, expiresAt, used: false, variant });
|
|
return { token, expiresAt };
|
|
},
|
|
});
|
|
|
|
export const getChannels = query({
|
|
args: {},
|
|
handler: async (ctx) => {
|
|
const identity = await ctx.auth.getUserIdentity();
|
|
if (!identity) return [];
|
|
return await ctx.db
|
|
.query("notificationChannels")
|
|
.withIndex("by_user", (q) => q.eq("userId", identity.subject))
|
|
.collect();
|
|
},
|
|
});
|
|
|
|
export const setChannel = mutation({
|
|
args: {
|
|
channelType: channelTypeValidator,
|
|
chatId: v.optional(v.string()),
|
|
webhookEnvelope: v.optional(v.string()),
|
|
email: v.optional(v.string()),
|
|
webhookLabel: v.optional(v.string()),
|
|
},
|
|
handler: async (ctx, args) => {
|
|
const identity = await ctx.auth.getUserIdentity();
|
|
if (!identity) throw new ConvexError("UNAUTHENTICATED");
|
|
const userId = identity.subject;
|
|
|
|
const existing = await ctx.db
|
|
.query("notificationChannels")
|
|
.withIndex("by_user_channel", (q) =>
|
|
q.eq("userId", userId).eq("channelType", args.channelType),
|
|
)
|
|
.unique();
|
|
|
|
const now = Date.now();
|
|
|
|
if (args.channelType === "telegram") {
|
|
if (!args.chatId) throw new ConvexError("chatId required for telegram channel");
|
|
const doc = { userId, channelType: "telegram" as const, chatId: args.chatId, verified: true, linkedAt: now };
|
|
if (existing) {
|
|
await ctx.db.replace(existing._id, doc);
|
|
} else {
|
|
await ctx.db.insert("notificationChannels", doc);
|
|
}
|
|
} else if (args.channelType === "slack") {
|
|
if (!args.webhookEnvelope) throw new ConvexError("webhookEnvelope required for slack channel");
|
|
const doc = { userId, channelType: "slack" as const, webhookEnvelope: args.webhookEnvelope, verified: true, linkedAt: now };
|
|
if (existing) {
|
|
await ctx.db.replace(existing._id, doc);
|
|
} else {
|
|
await ctx.db.insert("notificationChannels", doc);
|
|
}
|
|
} else if (args.channelType === "email") {
|
|
if (!args.email) throw new ConvexError("email required for email channel");
|
|
const doc = { userId, channelType: "email" as const, email: args.email, verified: true, linkedAt: now };
|
|
if (existing) {
|
|
await ctx.db.replace(existing._id, doc);
|
|
} else {
|
|
await ctx.db.insert("notificationChannels", doc);
|
|
}
|
|
} else if (args.channelType === "webhook") {
|
|
if (!args.webhookEnvelope) throw new ConvexError("webhookEnvelope required for webhook channel");
|
|
const doc = { userId, channelType: "webhook" as const, webhookEnvelope: args.webhookEnvelope, verified: true, linkedAt: now, webhookLabel: args.webhookLabel };
|
|
if (existing) {
|
|
await ctx.db.replace(existing._id, doc);
|
|
} else {
|
|
await ctx.db.insert("notificationChannels", doc);
|
|
}
|
|
} else {
|
|
throw new ConvexError("discord channel must be set via set-discord-oauth");
|
|
}
|
|
},
|
|
});
|
|
|
|
export const deleteChannel = mutation({
|
|
args: { channelType: channelTypeValidator },
|
|
handler: async (ctx, args) => {
|
|
const identity = await ctx.auth.getUserIdentity();
|
|
if (!identity) throw new ConvexError("UNAUTHENTICATED");
|
|
const userId = identity.subject;
|
|
|
|
const existing = await ctx.db
|
|
.query("notificationChannels")
|
|
.withIndex("by_user_channel", (q) =>
|
|
q.eq("userId", userId).eq("channelType", args.channelType),
|
|
)
|
|
.unique();
|
|
|
|
if (!existing) return;
|
|
await ctx.db.delete(existing._id);
|
|
|
|
// Remove this channel from all alert rules for this user
|
|
const rules = await ctx.db
|
|
.query("alertRules")
|
|
.withIndex("by_user", (q) => q.eq("userId", userId))
|
|
.collect();
|
|
for (const rule of rules) {
|
|
const filtered = rule.channels.filter((c) => c !== args.channelType);
|
|
if (filtered.length !== rule.channels.length) {
|
|
await ctx.db.patch(rule._id, { channels: filtered });
|
|
}
|
|
}
|
|
},
|
|
});
|
|
|
|
// Called by the notification relay via /relay/deactivate HTTP action
|
|
// when Telegram returns 403 or Slack returns 404/410.
|
|
export const deactivateChannelForUser = internalMutation({
|
|
args: { userId: v.string(), channelType: channelTypeValidator },
|
|
handler: async (ctx, args) => {
|
|
const existing = await ctx.db
|
|
.query("notificationChannels")
|
|
.withIndex("by_user_channel", (q) =>
|
|
q.eq("userId", args.userId).eq("channelType", args.channelType),
|
|
)
|
|
.unique();
|
|
if (existing) {
|
|
await ctx.db.patch(existing._id, { verified: false });
|
|
}
|
|
},
|
|
});
|
|
|
|
export const deactivateChannel = mutation({
|
|
args: { channelType: channelTypeValidator },
|
|
handler: async (ctx, args) => {
|
|
const identity = await ctx.auth.getUserIdentity();
|
|
if (!identity) throw new ConvexError("UNAUTHENTICATED");
|
|
const userId = identity.subject;
|
|
|
|
const existing = await ctx.db
|
|
.query("notificationChannels")
|
|
.withIndex("by_user_channel", (q) =>
|
|
q.eq("userId", userId).eq("channelType", args.channelType),
|
|
)
|
|
.unique();
|
|
|
|
if (existing) {
|
|
await ctx.db.patch(existing._id, { verified: false });
|
|
}
|
|
},
|
|
});
|
|
|
|
export const createPairingToken = mutation({
|
|
args: { variant: v.optional(v.string()) },
|
|
handler: async (ctx, args) => {
|
|
const identity = await ctx.auth.getUserIdentity();
|
|
if (!identity) throw new ConvexError("UNAUTHENTICATED");
|
|
const userId = identity.subject;
|
|
|
|
// Invalidate any existing unused tokens for this user
|
|
const existing = await ctx.db
|
|
.query("telegramPairingTokens")
|
|
.withIndex("by_user", (q) => q.eq("userId", userId))
|
|
.collect();
|
|
for (const t of existing) {
|
|
if (!t.used) await ctx.db.patch(t._id, { used: true });
|
|
}
|
|
|
|
// Generate a base64url token (43 chars from 32 random bytes)
|
|
const bytes = new Uint8Array(32);
|
|
crypto.getRandomValues(bytes);
|
|
const token = btoa(String.fromCharCode(...bytes))
|
|
.replace(/\+/g, "-")
|
|
.replace(/\//g, "_")
|
|
.replace(/=+$/, "");
|
|
|
|
const expiresAt = Date.now() + 15 * 60 * 1000;
|
|
|
|
await ctx.db.insert("telegramPairingTokens", {
|
|
userId,
|
|
token,
|
|
expiresAt,
|
|
used: false,
|
|
variant: args.variant,
|
|
});
|
|
|
|
return { token, expiresAt };
|
|
},
|
|
});
|
|
|
|
export const claimPairingToken = mutation({
|
|
args: { token: v.string(), chatId: v.string() },
|
|
handler: async (ctx, args) => {
|
|
const record = await ctx.db
|
|
.query("telegramPairingTokens")
|
|
.withIndex("by_token", (q) => q.eq("token", args.token))
|
|
.unique();
|
|
|
|
if (!record) return { ok: false, reason: "NOT_FOUND" as const };
|
|
if (record.used) return { ok: false, reason: "ALREADY_USED" as const };
|
|
if (record.expiresAt < Date.now()) return { ok: false, reason: "EXPIRED" as const };
|
|
|
|
// Mark token used
|
|
await ctx.db.patch(record._id, { used: true });
|
|
|
|
// Upsert telegram channel for this user
|
|
const existing = await ctx.db
|
|
.query("notificationChannels")
|
|
.withIndex("by_user_channel", (q) =>
|
|
q.eq("userId", record.userId).eq("channelType", "telegram"),
|
|
)
|
|
.unique();
|
|
|
|
const doc = {
|
|
userId: record.userId,
|
|
channelType: "telegram" as const,
|
|
chatId: args.chatId,
|
|
verified: true,
|
|
linkedAt: Date.now(),
|
|
};
|
|
|
|
if (existing) {
|
|
await ctx.db.replace(existing._id, doc);
|
|
} else {
|
|
await ctx.db.insert("notificationChannels", doc);
|
|
}
|
|
|
|
// On first-time pairing only, add 'telegram' to the alert rule so alerts
|
|
// are delivered immediately without requiring a manual rule edit.
|
|
// Skip on re-pair (existing channel) to preserve any intentional per-rule
|
|
// customization the user may have made (e.g. removed Telegram from a variant).
|
|
// If the token carries a variant, scope the update to that variant's rule only.
|
|
// Fall back to all rules when variant is absent (backward compat for old tokens).
|
|
if (!existing) {
|
|
const rules = await (record.variant
|
|
? ctx.db
|
|
.query("alertRules")
|
|
.withIndex("by_user_variant", (q) =>
|
|
q.eq("userId", record.userId).eq("variant", record.variant as string),
|
|
)
|
|
.collect()
|
|
: ctx.db
|
|
.query("alertRules")
|
|
.withIndex("by_user", (q) => q.eq("userId", record.userId))
|
|
.collect());
|
|
for (const rule of rules) {
|
|
if (!rule.channels.includes("telegram")) {
|
|
await ctx.db.patch(rule._id, { channels: [...rule.channels, "telegram"] });
|
|
}
|
|
}
|
|
}
|
|
|
|
return { ok: true, reason: null };
|
|
},
|
|
});
|