-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathemail.ts
More file actions
366 lines (328 loc) · 12 KB
/
Copy pathemail.ts
File metadata and controls
366 lines (328 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* Resend Email Service
*
* Send transactional + alert email via Resend. Falls back to server-level
* env vars if no per-user override is supplied.
*
* @module lib/alerts/email
*/
import { Resend } from 'resend';
// =============================================================================
// Types
// =============================================================================
export interface EmailConfig {
apiKey: string;
from: string;
replyTo?: string;
}
export interface SendEmailOptions {
/** Override server-level Resend config (e.g. user supplied their own key). */
override?: Partial<EmailConfig>;
to: string | string[];
subject: string;
/** HTML body. Plain-text fallback is auto-generated if `text` is omitted. */
html: string;
text?: string;
/** Override the From address for this send (e.g. per-user sender). */
fromOverride?: string;
/** Severity tag for tracing (`info|low|medium|high|critical`). */
severity?: string;
/** Idempotency key — Resend deduplicates within 24h. */
idempotencyKey?: string;
}
export interface SendEmailResult {
id: string;
to: string | string[];
from: string;
}
// =============================================================================
// Server-level config (fallback)
// =============================================================================
function getServerConfig(): EmailConfig | null {
const apiKey = process.env['RESEND_API_KEY'];
const from = process.env['EMAIL_FROM'];
const replyTo = process.env['EMAIL_REPLY_TO'];
if (!apiKey || !from) return null;
return { apiKey, from, replyTo };
}
/**
* Whether the server has a Resend key configured (for /api/notifications/status).
*/
export function isResendConfigured(): boolean {
return getServerConfig() !== null;
}
// =============================================================================
// Client cache (one client per API key)
// =============================================================================
const clientCache = new Map<string, Resend>();
function getClient(apiKey: string): Resend {
let client = clientCache.get(apiKey);
if (!client) {
client = new Resend(apiKey);
clientCache.set(apiKey, client);
}
return client;
}
// =============================================================================
// Send
// =============================================================================
/**
* Send an email via Resend.
*
* Resolution order for credentials:
* 1. `options.override` (per-user / per-call values)
* 2. server env (RESEND_API_KEY, EMAIL_FROM, EMAIL_REPLY_TO)
*
* Throws `EmailNotConfiguredError` when neither path yields a complete config.
*/
export async function sendEmail(options: SendEmailOptions): Promise<SendEmailResult> {
const server = getServerConfig();
const config: EmailConfig = {
apiKey: options.override?.apiKey ?? server?.apiKey ?? '',
from: options.fromOverride ?? options.override?.from ?? server?.from ?? '',
replyTo: options.override?.replyTo ?? server?.replyTo,
};
if (!config.apiKey || !config.from) {
throw new EmailNotConfiguredError(
'Resend is not configured. Set RESEND_API_KEY + EMAIL_FROM (server) or provide an override.',
);
}
const client = getClient(config.apiKey);
const text = options.text ?? htmlToText(options.html);
const tags = options.severity
? [{ name: 'severity', value: options.severity }]
: undefined;
const response = await client.emails.send({
from: config.from,
to: options.to,
subject: options.subject,
html: options.html,
text,
replyTo: config.replyTo,
headers: options.idempotencyKey
? { 'Idempotency-Key': options.idempotencyKey }
: undefined,
tags,
});
if (response.error) {
throw new Error(`Resend send failed: ${response.error.message}`);
}
const id = response.data?.id;
if (!id) {
throw new Error('Resend returned no message id');
}
return { id, to: options.to, from: config.from };
}
// =============================================================================
// Templates
// =============================================================================
/**
* Send a severity-coloured alert email. Used by NotificationManager.
*/
export async function sendAlertEmail(
to: string,
severity: 'info' | 'low' | 'medium' | 'high' | 'critical',
title: string,
message: string,
options: {
override?: Partial<EmailConfig>;
fromOverride?: string;
streamId?: string;
alertId?: string;
thumbnailUrl?: string;
timestamp?: string;
siteUrl?: string;
} = {},
): Promise<SendEmailResult> {
const html = renderAlertEmail({
severity,
title,
message,
streamId: options.streamId,
alertId: options.alertId,
thumbnailUrl: options.thumbnailUrl,
timestamp: options.timestamp ?? new Date().toISOString(),
siteUrl: options.siteUrl ?? process.env['NEXT_PUBLIC_SITE_URL'] ?? 'https://safeos.sh',
});
return sendEmail({
to,
subject: `[SafeOS ${severity.toUpperCase()}] ${title}`,
html,
severity,
override: options.override,
fromOverride: options.fromOverride,
idempotencyKey: options.alertId,
});
}
/**
* Send an account verification email. Always uses server-level config —
* per-user keys are not allowed for transactional auth mail.
*/
export async function sendVerificationEmail(
to: string,
verificationUrl: string,
displayName?: string,
): Promise<SendEmailResult> {
const greeting = displayName ? `Hi ${escapeHtml(displayName)},` : 'Hi,';
const html = `
<div style="font-family:system-ui,-apple-system,sans-serif;max-width:560px;margin:0 auto;color:#1a1f24;">
<h1 style="font-size:20px;margin:0 0 16px;">Verify your SafeOS Guardian account</h1>
<p>${greeting}</p>
<p>Click the link below to confirm your email address. This link expires in 24 hours.</p>
<p style="margin:24px 0;">
<a href="${verificationUrl}" style="background:#10b981;color:#fff;padding:12px 20px;border-radius:8px;text-decoration:none;font-weight:600;">
Verify email
</a>
</p>
<p style="color:#5a6270;font-size:14px;">
If the button doesn't work, paste this URL into your browser:<br>
<span style="word-break:break-all;">${verificationUrl}</span>
</p>
<hr style="border:none;border-top:1px solid #e5e7eb;margin:24px 0;">
<p style="color:#5a6270;font-size:12px;">
You're receiving this because someone (hopefully you) signed up for SafeOS Guardian. If it wasn't you, ignore this email.
</p>
</div>
`;
return sendEmail({
to,
subject: 'Verify your SafeOS Guardian account',
html,
idempotencyKey: `verify:${to}:${verificationUrl}`,
});
}
/**
* Send a password reset email. Always uses server-level config.
*/
export async function sendPasswordResetEmail(
to: string,
resetUrl: string,
displayName?: string,
): Promise<SendEmailResult> {
const greeting = displayName ? `Hi ${escapeHtml(displayName)},` : 'Hi,';
const html = `
<div style="font-family:system-ui,-apple-system,sans-serif;max-width:560px;margin:0 auto;color:#1a1f24;">
<h1 style="font-size:20px;margin:0 0 16px;">Reset your SafeOS Guardian password</h1>
<p>${greeting}</p>
<p>We received a request to reset the password on your SafeOS Guardian account. Click below to choose a new one. This link expires in 1 hour.</p>
<p style="margin:24px 0;">
<a href="${resetUrl}" style="background:#10b981;color:#fff;padding:12px 20px;border-radius:8px;text-decoration:none;font-weight:600;">
Reset password
</a>
</p>
<p style="color:#5a6270;font-size:14px;">
If you didn't request this, you can safely ignore this email — your password will stay the same.
</p>
</div>
`;
return sendEmail({
to,
subject: 'Reset your SafeOS Guardian password',
html,
idempotencyKey: `reset:${to}:${resetUrl}`,
});
}
/**
* Send a one-off test email so users can verify their Resend config works.
*/
export async function sendTestEmail(
to: string,
options: { override?: Partial<EmailConfig>; fromOverride?: string } = {},
): Promise<SendEmailResult> {
const html = `
<div style="font-family:system-ui,-apple-system,sans-serif;max-width:560px;margin:0 auto;color:#1a1f24;">
<h1 style="font-size:20px;margin:0 0 16px;">SafeOS Guardian email is working ✅</h1>
<p>This is a test email sent from your Settings → Notifications page.</p>
<p>If you're seeing this, alerts will reach you when SafeOS Guardian detects something important.</p>
<hr style="border:none;border-top:1px solid #e5e7eb;margin:24px 0;">
<p style="color:#5a6270;font-size:12px;">Sent at ${new Date().toISOString()}</p>
</div>
`;
return sendEmail({
to,
subject: 'SafeOS Guardian — email is working',
html,
override: options.override,
fromOverride: options.fromOverride,
});
}
// =============================================================================
// Internal helpers
// =============================================================================
const SEVERITY_COLORS: Record<string, string> = {
info: '#3b82f6',
low: '#10b981',
medium: '#f59e0b',
high: '#f97316',
critical: '#ef4444',
};
function renderAlertEmail(params: {
severity: keyof typeof SEVERITY_COLORS;
title: string;
message: string;
streamId?: string;
alertId?: string;
thumbnailUrl?: string;
timestamp: string;
siteUrl: string;
}): string {
const color = SEVERITY_COLORS[params.severity] ?? SEVERITY_COLORS.info;
const safeTitle = escapeHtml(params.title);
const safeMessage = escapeHtml(params.message);
const historyUrl = `${params.siteUrl.replace(/\/+$/, '')}/history${params.alertId ? `?alert=${encodeURIComponent(params.alertId)}` : ''}`;
return `
<div style="font-family:system-ui,-apple-system,sans-serif;max-width:560px;margin:0 auto;color:#1a1f24;">
<div style="background:${color};color:#fff;padding:12px 16px;border-radius:8px 8px 0 0;font-size:12px;font-weight:700;text-transform:uppercase;letter-spacing:0.1em;">
${params.severity}
</div>
<div style="border:1px solid #e5e7eb;border-top:0;border-radius:0 0 8px 8px;padding:20px;">
<h1 style="font-size:20px;margin:0 0 8px;">${safeTitle}</h1>
<p style="margin:0 0 16px;color:#374151;">${safeMessage}</p>
${params.thumbnailUrl ? `<img src="${params.thumbnailUrl}" alt="Detection thumbnail" style="max-width:100%;border-radius:6px;margin:8px 0;">` : ''}
<p style="color:#5a6270;font-size:13px;margin:16px 0 8px;">
${params.streamId ? `<strong>Stream:</strong> ${escapeHtml(params.streamId)}<br>` : ''}
<strong>Time:</strong> ${escapeHtml(params.timestamp)}
</p>
<p style="margin:20px 0 0;">
<a href="${historyUrl}" style="background:#10b981;color:#fff;padding:10px 16px;border-radius:6px;text-decoration:none;font-weight:600;font-size:14px;">
Open SafeOS Guardian
</a>
</p>
</div>
<p style="color:#5a6270;font-size:11px;text-align:center;margin:16px 0 0;">
SafeOS Guardian is a supplemental monitoring tool. It does not replace human supervision.
</p>
</div>
`;
}
function escapeHtml(input: string): string {
return input
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function htmlToText(html: string): string {
return html
.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '')
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')
.replace(/<br\s*\/?>/gi, '\n')
.replace(/<\/p>/gi, '\n\n')
.replace(/<[^>]+>/g, '')
.replace(/ /g, ' ')
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\n{3,}/g, '\n\n')
.trim();
}
// =============================================================================
// Errors
// =============================================================================
export class EmailNotConfiguredError extends Error {
constructor(message: string) {
super(message);
this.name = 'EmailNotConfiguredError';
}
}