Add CalDav routes and methods, with mock data
This commit is contained in:
250
lib/data/calendar.ts
Normal file
250
lib/data/calendar.ts
Normal file
@@ -0,0 +1,250 @@
|
||||
// import Database, { sql } from '/lib/interfaces/database.ts';
|
||||
import { Calendar, CalendarEvent } from '/lib/types.ts';
|
||||
|
||||
// const db = new Database();
|
||||
|
||||
// TODO: Build this
|
||||
export async function getCalendars(userId: string): Promise<Calendar[]> {
|
||||
// TODO: Remove this
|
||||
await new Promise((resolve) => setTimeout(() => resolve(true), 1));
|
||||
|
||||
return [
|
||||
{
|
||||
id: 'family-1',
|
||||
user_id: userId,
|
||||
name: 'Family',
|
||||
color: 'bg-purple-500',
|
||||
is_visible: true,
|
||||
revision: 'fake-rev',
|
||||
extra: {
|
||||
default_transparency: 'opaque',
|
||||
},
|
||||
updated_at: new Date(),
|
||||
created_at: new Date(),
|
||||
},
|
||||
{
|
||||
id: 'personal-1',
|
||||
user_id: userId,
|
||||
name: 'Personal',
|
||||
color: 'bg-sky-600',
|
||||
is_visible: true,
|
||||
revision: 'fake-rev',
|
||||
extra: {
|
||||
default_transparency: 'opaque',
|
||||
},
|
||||
updated_at: new Date(),
|
||||
created_at: new Date(),
|
||||
},
|
||||
{
|
||||
id: 'house-chores-1',
|
||||
user_id: userId,
|
||||
name: 'House Chores',
|
||||
color: 'bg-red-700',
|
||||
is_visible: true,
|
||||
revision: 'fake-rev',
|
||||
extra: {
|
||||
default_transparency: 'opaque',
|
||||
},
|
||||
updated_at: new Date(),
|
||||
created_at: new Date(),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
// TODO: Build this
|
||||
export async function getCalendarEvents(userId: string, calendarIds: string[]): Promise<CalendarEvent[]> {
|
||||
// TODO: Remove this
|
||||
await new Promise((resolve) => setTimeout(() => resolve(true), 1));
|
||||
|
||||
const now = new Date();
|
||||
const today = now.toISOString().substring(0, 10);
|
||||
const tomorrow = new Date(new Date(now).setDate(now.getDate() + 1)).toISOString().substring(0, 10);
|
||||
const twoDaysFromNow = new Date(new Date(now).setDate(now.getDate() + 2)).toISOString().substring(0, 10);
|
||||
|
||||
const calendarEvents = [
|
||||
{
|
||||
id: 'event-1',
|
||||
user_id: userId,
|
||||
calendar_id: 'family-1',
|
||||
revision: 'fake-rev',
|
||||
title: 'Dentist',
|
||||
start_date: new Date(`${today}T14:00:00.000Z`),
|
||||
end_date: new Date(`${today}T15:00:00.000Z`),
|
||||
is_all_day: false,
|
||||
status: 'scheduled',
|
||||
extra: {
|
||||
organizer_email: 'user@example.com',
|
||||
transparency: 'default',
|
||||
},
|
||||
updated_at: new Date(),
|
||||
created_at: new Date(),
|
||||
},
|
||||
{
|
||||
id: 'event-2',
|
||||
user_id: userId,
|
||||
calendar_id: 'family-1',
|
||||
revision: 'fake-rev',
|
||||
title: 'Dermatologist',
|
||||
start_date: new Date(`${today}T16:30:00.000Z`),
|
||||
end_date: new Date(`${today}T17:30:00.000Z`),
|
||||
is_all_day: false,
|
||||
status: 'scheduled',
|
||||
extra: {
|
||||
organizer_email: 'user@example.com',
|
||||
transparency: 'default',
|
||||
},
|
||||
updated_at: new Date(),
|
||||
created_at: new Date(),
|
||||
},
|
||||
{
|
||||
id: 'event-3',
|
||||
user_id: userId,
|
||||
calendar_id: 'house-chores-1',
|
||||
revision: 'fake-rev',
|
||||
title: 'Vacuum',
|
||||
start_date: new Date(`${tomorrow}T15:00:00.000Z`),
|
||||
end_date: new Date(`${tomorrow}T16:00:00.000Z`),
|
||||
is_all_day: false,
|
||||
status: 'scheduled',
|
||||
extra: {
|
||||
organizer_email: 'user@example.com',
|
||||
transparency: 'default',
|
||||
},
|
||||
updated_at: new Date(),
|
||||
created_at: new Date(),
|
||||
},
|
||||
{
|
||||
id: 'event-4',
|
||||
user_id: userId,
|
||||
calendar_id: 'personal-1',
|
||||
revision: 'fake-rev',
|
||||
title: 'Schedule server updates',
|
||||
start_date: new Date(`${twoDaysFromNow}T09:00:00.000Z`),
|
||||
end_date: new Date(`${twoDaysFromNow}T21:00:00.000Z`),
|
||||
is_all_day: true,
|
||||
status: 'scheduled',
|
||||
extra: {
|
||||
organizer_email: 'user@example.com',
|
||||
transparency: 'default',
|
||||
},
|
||||
updated_at: new Date(),
|
||||
created_at: new Date(),
|
||||
},
|
||||
] as const;
|
||||
|
||||
return calendarEvents.filter((calendarEvent) => calendarIds.includes(calendarEvent.calendar_id));
|
||||
}
|
||||
|
||||
// TODO: Build this
|
||||
export async function getCalendarEvent(id: string, calendarId: string, userId: string): Promise<CalendarEvent> {
|
||||
// TODO: Build this
|
||||
// const calendarEvents = await db.query<CalendarEvent>(
|
||||
// sql`SELECT * FROM "bewcloud_calendar_events" WHERE "id" = $1 AND "calendar_id" = $2 AND "user_id" = $3 LIMIT 1`,
|
||||
// [
|
||||
// id,
|
||||
// calendarId,
|
||||
// userId,
|
||||
// ],
|
||||
// );
|
||||
|
||||
// return calendarEvents[0];
|
||||
const calendarEvents = await getCalendarEvents(userId, [calendarId]);
|
||||
|
||||
return calendarEvents.find((calendarEvent) => calendarEvent.id === id)!;
|
||||
}
|
||||
|
||||
export async function getCalendar(id: string, userId: string) {
|
||||
// TODO: Build this
|
||||
// const calendars = await db.query<Calendar>(
|
||||
// sql`SELECT * FROM "bewcloud_calendars" WHERE "id" = $1 AND "user_id" = $2 LIMIT 1`,
|
||||
// [
|
||||
// id,
|
||||
// userId,
|
||||
// ],
|
||||
// );
|
||||
|
||||
// return calendars[0];
|
||||
|
||||
const calendars = await getCalendars(userId);
|
||||
|
||||
return calendars.find((calendar) => calendar.id === id)!;
|
||||
}
|
||||
|
||||
export async function createCalendar(userId: string, name: string, color?: string) {
|
||||
const extra: Calendar['extra'] = {
|
||||
default_transparency: 'opaque',
|
||||
};
|
||||
|
||||
const revision = crypto.randomUUID();
|
||||
|
||||
// TODO: Build this
|
||||
// const newCalendar = (await db.query<Calendar>(
|
||||
// sql`INSERT INTO "bewcloud_calendars" (
|
||||
// "user_id",
|
||||
// "revision",
|
||||
// "name",
|
||||
// "color",
|
||||
// "extra"
|
||||
// ) VALUES ($1, $2, $3, $4, $5)
|
||||
// RETURNING *`,
|
||||
// [
|
||||
// userId,
|
||||
// revision,
|
||||
// name,
|
||||
// color,
|
||||
// JSON.stringify(extra),
|
||||
// ],
|
||||
// ))[0];
|
||||
|
||||
// TODO: Generate new, non-existing color
|
||||
const newColor = color || 'bg-green-600';
|
||||
|
||||
const calendars = await getCalendars(userId);
|
||||
const newCalendar = { ...calendars[0], id: crypto.randomUUID(), revision, extra, name, color: newColor };
|
||||
|
||||
return newCalendar;
|
||||
}
|
||||
|
||||
export async function updateCalendar(calendar: Calendar) {
|
||||
const revision = crypto.randomUUID();
|
||||
|
||||
// TODO: Build this
|
||||
// await db.query(
|
||||
// sql`UPDATE "bewcloud_calendars" SET
|
||||
// "revision" = $3,
|
||||
// "name" = $4,
|
||||
// "color" = $5,
|
||||
// "extra" = $6,
|
||||
// "updated_at" = now()
|
||||
// WHERE "id" = $1 AND "revision" = $2`,
|
||||
// [
|
||||
// calendar.id,
|
||||
// calendar.revision,
|
||||
// revision,
|
||||
// calendar.name,
|
||||
// calendar.color,
|
||||
// JSON.stringify(calendar.extra),
|
||||
// ],
|
||||
// );
|
||||
|
||||
calendar.revision = revision;
|
||||
|
||||
// TODO: Remove this
|
||||
await new Promise((resolve) => setTimeout(() => resolve(true), 1));
|
||||
}
|
||||
|
||||
export async function deleteCalendar(_id: string, _userId: string) {
|
||||
// TODO: Build this
|
||||
// await db.query(
|
||||
// sql`DELETE FROM "bewcloud_calendars" WHERE "id" = $1 AND "user_id" = $2`,
|
||||
// [
|
||||
// id,
|
||||
// userId,
|
||||
// ],
|
||||
// );
|
||||
|
||||
// TODO: Remove this
|
||||
await new Promise((resolve) => setTimeout(() => resolve(true), 1));
|
||||
}
|
||||
|
||||
// TODO: When creating, updating, or deleting events, also update the calendar's revision
|
||||
23
lib/types.ts
23
lib/types.ts
@@ -141,7 +141,8 @@ export interface Calendar {
|
||||
extra: {
|
||||
shared_read_user_ids?: string[];
|
||||
shared_write_user_ids?: string[];
|
||||
default_visibility: 'private';
|
||||
default_transparency: 'opaque' | 'transparent';
|
||||
calendar_timezone?: string;
|
||||
};
|
||||
updated_at: Date;
|
||||
created_at: Date;
|
||||
@@ -159,12 +160,23 @@ export interface CalendarEvent {
|
||||
is_all_day: boolean;
|
||||
status: 'scheduled' | 'pending' | 'canceled';
|
||||
extra: {
|
||||
organizer_email: string;
|
||||
description?: string;
|
||||
location?: string;
|
||||
url?: string;
|
||||
attendees?: CalendarEventAttendee[];
|
||||
visibility: 'default' | 'public' | 'private';
|
||||
transparency: 'default' | Calendar['extra']['default_transparency'];
|
||||
is_recurring?: boolean;
|
||||
recurring_id?: string;
|
||||
recurring_sequence?: number;
|
||||
recurring_rrule?: string;
|
||||
recurring_rdate?: string;
|
||||
recurring_exdate?: string;
|
||||
is_task?: boolean;
|
||||
task_due_date?: string;
|
||||
task_completed_at?: string;
|
||||
uid?: string;
|
||||
reminders?: CalendarEventReminder[];
|
||||
};
|
||||
updated_at: Date;
|
||||
created_at: Date;
|
||||
@@ -175,3 +187,10 @@ export interface CalendarEventAttendee {
|
||||
status: 'accepted' | 'rejected' | 'invited';
|
||||
name?: string;
|
||||
}
|
||||
|
||||
export interface CalendarEventReminder {
|
||||
uid?: string;
|
||||
start_date: string;
|
||||
type: 'email' | 'sound';
|
||||
acknowledged_at?: string;
|
||||
}
|
||||
|
||||
113
lib/utils.ts
113
lib/utils.ts
@@ -1,4 +1,4 @@
|
||||
import { Contact, ContactAddress, ContactField } from './types.ts';
|
||||
import { Calendar, CalendarEvent, Contact, ContactAddress, ContactField } from './types.ts';
|
||||
|
||||
let BASE_URL = typeof window !== 'undefined' && window.location
|
||||
? `${window.location.protocol}//${window.location.host}`
|
||||
@@ -17,9 +17,11 @@ export const helpEmail = 'help@bewcloud.com';
|
||||
|
||||
export const CONTACTS_PER_PAGE_COUNT = 20;
|
||||
|
||||
export const DAV_RESPONSE_HEADER = '1, 3, 4, addressbook';
|
||||
// '1, 3, extended-mkcol, access-control, calendarserver-principal-property-search, oc-resource-sharing, addressbook, nextcloud-checksum-update, nc-calendar-search, nc-enable-birthday-calendar'
|
||||
// '1, 3, extended-mkcol, access-control, calendarserver-principal-property-search, calendar-access, calendar-proxy, calendar-auto-schedule, calendar-availability, nc-calendar-trashbin, nc-calendar-webcal-cache, calendarserver-subscribed, oc-resource-sharing, oc-calendar-publishing, calendarserver-sharing, addressbook, nextcloud-checksum-update, nc-calendar-search, nc-enable-birthday-calendar
|
||||
export const DAV_RESPONSE_HEADER = '1, 2, 3, 4, addressbook, calendar-access';
|
||||
// Response headers from Nextcloud:
|
||||
// 1, 3, extended-mkcol, access-control, calendarserver-principal-property-search, oc-resource-sharing, addressbook, nextcloud-checksum-update, nc-calendar-search, nc-enable-birthday-calendar
|
||||
// 1, 3, extended-mkcol, access-control, calendarserver-principal-property-search, calendar-access, calendar-proxy, calendar-auto-schedule, calendar-availability, nc-calendar-trashbin, nc-calendar-webcal-cache, calendarserver-subscribed, oc-resource-sharing, oc-calendar-publishing, calendarserver-sharing, addressbook, nextcloud-checksum-update, nc-calendar-search, nc-enable-birthday-calendar
|
||||
// 1, 3, extended-mkcol, access-control, calendarserver-principal-property-search, calendar-access, calendar-proxy, calendar-auto-schedule, calendar-availability, nc-calendar-trashbin, nc-calendar-webcal-cache, calendarserver-subscribed, oc-resource-sharing, oc-calendar-publishing, calendarserver-sharing, nextcloud-checksum-update, nc-calendar-search, nc-enable-birthday-calendar
|
||||
|
||||
export function isRunningLocally(request: Request) {
|
||||
return request.url.includes('localhost');
|
||||
@@ -388,8 +390,8 @@ export function parseVCardFromTextContents(text: string): Partial<Contact>[] {
|
||||
}
|
||||
|
||||
if (vCardVersion !== '2.1' && vCardVersion !== '3.0' && vCardVersion !== '4.0') {
|
||||
vCardVersion = '2.1';
|
||||
console.warn(`Invalid vCard version found: "${vCardVersion}". Defaulting to 2.1 parser.`);
|
||||
vCardVersion = '2.1';
|
||||
}
|
||||
|
||||
if (line.startsWith('UID:')) {
|
||||
@@ -625,6 +627,105 @@ export function parseVCardFromTextContents(text: string): Partial<Contact>[] {
|
||||
return partialContacts;
|
||||
}
|
||||
|
||||
// TODO: Build this
|
||||
export function formatCalendarEventsToVCalendar(calendarEvents: CalendarEvent[], _calendar: Calendar): string {
|
||||
const vCalendarText = calendarEvents.map((calendarEvent) =>
|
||||
`BEGIN:VEVENT
|
||||
DTSTAMP:${calendarEvent.start_date.toISOString().substring(0, 19).replaceAll('T', '').replaceAll(':', '')}
|
||||
DTSTART:${calendarEvent.start_date.toISOString().substring(0, 19).replaceAll('T', '').replaceAll(':', '')}
|
||||
DTEND:${calendarEvent.end_date.toISOString().substring(0, 19).replaceAll('T', '').replaceAll(':', '')}
|
||||
ORGANIZER;CN=:MAILTO:${calendarEvent.extra.organizer_email}
|
||||
SUMMARY:${calendarEvent.title}
|
||||
${calendarEvent.extra.uid ? `UID:${calendarEvent.extra.uid}` : ''}
|
||||
END:VEVENT`
|
||||
).join('\n');
|
||||
|
||||
return `BEGIN:VCALENDAR\nVERSION:2.0\nCALSCALE:GREGORIAN\n${vCalendarText}\nEND:VCALENDAR`.split('\n').map((line) =>
|
||||
line.trim()
|
||||
).filter(
|
||||
Boolean,
|
||||
).join('\n');
|
||||
}
|
||||
|
||||
type VCalendarVersion = '1.0' | '2.0';
|
||||
|
||||
export function parseVCalendarFromTextContents(text: string): Partial<CalendarEvent>[] {
|
||||
const lines = text.split('\n').map((line) => line.trim()).filter(Boolean);
|
||||
|
||||
const partialCalendarEvents: Partial<CalendarEvent>[] = [];
|
||||
|
||||
let partialCalendarEvent: Partial<CalendarEvent> = {};
|
||||
let vCalendarVersion: VCalendarVersion = '2.0';
|
||||
|
||||
// Loop through every line
|
||||
for (const line of lines) {
|
||||
// Start new vCard version
|
||||
if (line.startsWith('BEGIN:VCALENDAR')) {
|
||||
vCalendarVersion = '2.0';
|
||||
continue;
|
||||
}
|
||||
|
||||
// Start new event
|
||||
if (line.startsWith('BEGIN:VEVENT')) {
|
||||
partialCalendarEvent = {};
|
||||
continue;
|
||||
}
|
||||
|
||||
// Finish contact
|
||||
if (line.startsWith('END:VCARD')) {
|
||||
partialCalendarEvents.push(partialCalendarEvent);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Select proper vCalendar version
|
||||
if (line.startsWith('VERSION:')) {
|
||||
if (line.startsWith('VERSION:1.0')) {
|
||||
vCalendarVersion = '1.0';
|
||||
} else if (line.startsWith('VERSION:2.0')) {
|
||||
vCalendarVersion = '2.0';
|
||||
} else {
|
||||
// Default to 2.0, log warning
|
||||
vCalendarVersion = '2.0';
|
||||
console.warn(`Invalid vCalendar version found: "${line}". Defaulting to 2.0 parser.`);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (vCalendarVersion !== '1.0' && vCalendarVersion !== '2.0') {
|
||||
console.warn(`Invalid vCalendar version found: "${vCalendarVersion}". Defaulting to 2.0 parser.`);
|
||||
vCalendarVersion = '2.0';
|
||||
}
|
||||
|
||||
if (line.startsWith('UID:')) {
|
||||
const uid = line.replace('UID:', '');
|
||||
|
||||
if (!uid) {
|
||||
continue;
|
||||
}
|
||||
|
||||
partialCalendarEvent.extra = {
|
||||
...(partialCalendarEvent.extra! || {}),
|
||||
uid,
|
||||
};
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO: Build this ( https://en.wikipedia.org/wiki/ICalendar#List_of_components,_properties,_and_parameters )
|
||||
|
||||
if (line.startsWith('SUMMARY:')) {
|
||||
const title = line.split('SUMMARY:')[1] || '';
|
||||
|
||||
partialCalendarEvent.title = title;
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return partialCalendarEvents;
|
||||
}
|
||||
|
||||
export const capitalizeWord = (string: string) => {
|
||||
return `${string.charAt(0).toLocaleUpperCase()}${string.slice(1)}`;
|
||||
};
|
||||
@@ -684,7 +785,7 @@ export function getDaysForWeek(
|
||||
const dayDate = new Date(startingDate);
|
||||
dayDate.setDate(dayDate.getDate() + dayIndex);
|
||||
|
||||
const isSameDay = dayDate.toISOString() === shortIsoDate;
|
||||
const isSameDay = dayDate.toISOString().substring(0, 10) === shortIsoDate;
|
||||
|
||||
days[dayIndex] = {
|
||||
date: dayDate,
|
||||
|
||||
Reference in New Issue
Block a user