feat: add CalDAV synchronization with automatic sync
- Add CaldavService with tsdav/ical.js for CalDAV server communication - Add CaldavController, CaldavRepository, and caldav routes - Add client-side CaldavConfigService with sync(), config CRUD - Add CalDAV settings UI with config load/save in settings screen - Sync on login, auto-login (AuthGuard), periodic timer (calendar), and sync button - Push single events to CalDAV on server-side create/update/delete - Push all events to CalDAV after chat event confirmation - Refactor ChatService to use EventService instead of direct EventRepository - Rename CalDav/calDav to Caldav/caldav for consistent naming - Add Radicale Docker setup for local CalDAV testing - Update PlantUML diagrams and CLAUDE.md with CalDAV architecture
This commit is contained in:
@@ -1,17 +1,43 @@
|
||||
import { Response } from "express";
|
||||
import { RecurringDeleteMode } from "@calchat/shared";
|
||||
import { CalendarEvent, RecurringDeleteMode } from "@calchat/shared";
|
||||
import { EventService } from "../services";
|
||||
import { createLogger } from "../logging";
|
||||
import { AuthenticatedRequest } from "./AuthMiddleware";
|
||||
import { CaldavService } from "../services/CaldavService";
|
||||
|
||||
const log = createLogger("EventController");
|
||||
|
||||
export class EventController {
|
||||
constructor(private eventService: EventService) {}
|
||||
constructor(
|
||||
private eventService: EventService,
|
||||
private caldavService: CaldavService,
|
||||
) {}
|
||||
|
||||
private async pushToCaldav(userId: string, event: CalendarEvent) {
|
||||
if (await this.caldavService.getConfig(userId)) {
|
||||
try {
|
||||
await this.caldavService.pushEvent(userId, event);
|
||||
} catch (error) {
|
||||
log.error({ error, userId }, "Error pushing event to CalDAV");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async deleteFromCaldav(userId: string, event: CalendarEvent) {
|
||||
if (event.caldavUUID && (await this.caldavService.getConfig(userId))) {
|
||||
try {
|
||||
await this.caldavService.deleteEvent(userId, event.caldavUUID);
|
||||
} catch (error) {
|
||||
log.error({ error, userId }, "Error deleting event from CalDAV");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async create(req: AuthenticatedRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const event = await this.eventService.create(req.user!.userId, req.body);
|
||||
const userId = req.user!.userId;
|
||||
const event = await this.eventService.create(userId, req.body);
|
||||
await this.pushToCaldav(userId, event);
|
||||
res.status(201).json(event);
|
||||
} catch (error) {
|
||||
log.error({ error, userId: req.user?.userId }, "Error creating event");
|
||||
@@ -83,15 +109,19 @@ export class EventController {
|
||||
|
||||
async update(req: AuthenticatedRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const userId = req.user!.userId;
|
||||
const event = await this.eventService.update(
|
||||
req.params.id,
|
||||
req.user!.userId,
|
||||
userId,
|
||||
req.body,
|
||||
);
|
||||
if (!event) {
|
||||
res.status(404).json({ error: "Event not found" });
|
||||
return;
|
||||
}
|
||||
|
||||
await this.pushToCaldav(userId, event);
|
||||
|
||||
res.json(event);
|
||||
} catch (error) {
|
||||
log.error({ error, eventId: req.params.id }, "Error updating event");
|
||||
@@ -101,46 +131,44 @@ export class EventController {
|
||||
|
||||
async delete(req: AuthenticatedRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const userId = req.user!.userId;
|
||||
const { mode, occurrenceDate } = req.query as {
|
||||
mode?: RecurringDeleteMode;
|
||||
occurrenceDate?: string;
|
||||
};
|
||||
|
||||
// Fetch event before deletion to get caldavUUID for sync
|
||||
const event = await this.eventService.getById(req.params.id, userId);
|
||||
if (!event) {
|
||||
res.status(404).json({ error: "Event not found" });
|
||||
return;
|
||||
}
|
||||
|
||||
// If mode is specified, use deleteRecurring
|
||||
if (mode) {
|
||||
const result = await this.eventService.deleteRecurring(
|
||||
req.params.id,
|
||||
req.user!.userId,
|
||||
userId,
|
||||
mode,
|
||||
occurrenceDate,
|
||||
);
|
||||
|
||||
// For 'all' mode or when event was completely deleted, return 204
|
||||
if (result === null && mode === "all") {
|
||||
res.status(204).send();
|
||||
return;
|
||||
}
|
||||
|
||||
// For 'single' or 'future' modes, return updated event
|
||||
// Event was updated (single/future mode) - push update to CalDAV
|
||||
if (result) {
|
||||
await this.pushToCaldav(userId, result);
|
||||
res.json(result);
|
||||
return;
|
||||
}
|
||||
|
||||
// result is null but mode wasn't 'all' - event not found or was deleted
|
||||
// Event was fully deleted (all mode, or future from first occurrence)
|
||||
await this.deleteFromCaldav(userId, event);
|
||||
res.status(204).send();
|
||||
return;
|
||||
}
|
||||
|
||||
// Default behavior: delete completely
|
||||
const deleted = await this.eventService.delete(
|
||||
req.params.id,
|
||||
req.user!.userId,
|
||||
);
|
||||
if (!deleted) {
|
||||
res.status(404).json({ error: "Event not found" });
|
||||
return;
|
||||
}
|
||||
await this.eventService.delete(req.params.id, userId);
|
||||
await this.deleteFromCaldav(userId, event);
|
||||
res.status(204).send();
|
||||
} catch (error) {
|
||||
log.error({ error, eventId: req.params.id }, "Error deleting event");
|
||||
|
||||
Reference in New Issue
Block a user