feat: send calendar event invitations by email or federation
Build & Push Docker Image / build (push) Successful in 4m18s

- New endpoint POST /api/calendar/events/:id/email sends event
  invitations with an .ics attachment to external email addresses
  (rate-limited like room email invites)
- Create-event dialog gets an optional invitations section to invite
  people by email or federation address right away
- The "Invite remote" dialog on existing events now accepts email
  addresses in addition to federation IDs

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-17 08:58:28 +02:00
co-authored by Claude Fable 5
parent 849f54db7d
commit 69e901f145
7 changed files with 310 additions and 19 deletions
+74 -1
View File
@@ -3,7 +3,7 @@ import crypto from 'crypto';
import { getDb } from '../config/database.js';
import { authenticateToken, getBaseUrl } from '../middleware/auth.js';
import { log } from '../config/logger.js';
import { sendCalendarInviteEmail } from '../config/mailer.js';
import { sendCalendarInviteEmail, sendCalendarEventEmail } from '../config/mailer.js';
import { getAppName } from '../config/appName.js';
import {
isFederationEnabled,
@@ -23,6 +23,16 @@ const SAFE_COLOR_RE = /^(?:#[0-9a-fA-F]{3,8}|hsl\(\d{1,3},\s*\d{1,3}%,\s*\d{1,3}
// Allowed reminder intervals in minutes
const VALID_REMINDERS = new Set([5, 15, 30, 60, 120, 1440]);
// Rate limit email invitations: each request may carry up to 50 addresses, so
// without a cap any registered account could be abused as an SMTP spam relay.
const calendarEmailInviteLimiter = rateLimit({
windowMs: 60 * 60 * 1000, // 1 hour
max: 10,
standardHeaders: true,
legacyHeaders: false,
message: { error: 'Too many email invitations. Please try again later.' },
});
// Rate limit for federation calendar receive
const calendarFederationLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
@@ -581,6 +591,69 @@ router.post('/events/:id/federation', authenticateToken, async (req, res) => {
}
});
// ── POST /api/calendar/events/:id/email — Send event invitation by email ────
router.post('/events/:id/email', authenticateToken, calendarEmailInviteLimiter, async (req, res) => {
try {
const { emails } = req.body;
if (!emails || !Array.isArray(emails) || !emails.length) {
return res.status(400).json({ error: 'At least one email address is required' });
}
if (emails.length > 50) {
return res.status(400).json({ error: 'Maximum 50 email addresses allowed' });
}
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
for (const email of emails) {
if (typeof email !== 'string' || !emailRegex.test(email) || email.length > 254) {
return res.status(400).json({ error: `Invalid email address: ${email}` });
}
}
const db = getDb();
const event = await db.get(`
SELECT ce.*, COALESCE(NULLIF(u.display_name,''), u.name) as organizer_name, u.email as organizer_email
FROM calendar_events ce
JOIN users u ON ce.user_id = u.id
WHERE ce.id = ? AND ce.user_id = ?
`, [req.params.id, req.user.id]);
if (!event) return res.status(404).json({ error: 'Event not found or no permission' });
const baseUrl = getBaseUrl(req);
let joinUrl = null;
if (event.room_uid) {
joinUrl = `${baseUrl}/join/${event.room_uid}`;
}
const ics = generateICS(event, joinUrl || '', baseUrl);
const appName = await getAppName();
const fromUser = req.user.display_name || req.user.name;
const lang = req.user.language || 'en';
// Send emails (in parallel but collect errors)
const results = await Promise.allSettled(
emails.map(email =>
sendCalendarEventEmail(
email, fromUser, event.title, event.start_time, event.end_time,
event.description, joinUrl, ics, appName, lang
)
)
);
const failed = results.filter(r => r.status === 'rejected');
if (failed.length === emails.length) {
return res.status(500).json({ error: 'Failed to send all email invitations' });
}
if (failed.length > 0) {
log.server.warn(`${failed.length}/${emails.length} calendar email invitations failed`);
}
res.json({ success: true, sent: emails.length - failed.length, failed: failed.length });
} catch (err) {
log.server.error(`Calendar email invite error: ${err.message}`);
res.status(500).json({ error: err.message || 'Failed to send email invitations' });
}
});
// ── POST /receive-event or /calendar-event — Receive calendar event from remote ──
// '/receive-event' when mounted at /api/calendar
// '/calendar-event' when mounted at /api/federation (for remote instance discovery)