Support exporting calendar events
Also update Deno and libraries
This commit is contained in:
38
routes/api/calendar/get-events.tsx
Normal file
38
routes/api/calendar/get-events.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { CalendarEvent, FreshContextState } from '/lib/types.ts';
|
||||
import { getCalendarEvents } from '/lib/data/calendar.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
calendarIds: string[];
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
calendarEvents: CalendarEvent[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (!requestBody.calendarIds) {
|
||||
return new Response('Bad Request', { status: 400 });
|
||||
}
|
||||
|
||||
const calendarEvents = await getCalendarEvents(
|
||||
context.state.user.id,
|
||||
requestBody.calendarIds,
|
||||
);
|
||||
|
||||
const responseBody: ResponseBody = { success: true, calendarEvents };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
@@ -118,7 +118,7 @@ export const handler: Handler<Data, FreshContextState> = async (request, context
|
||||
const calendarEvents = await getCalendarEvents(context.state.user.id, [calendar.id]);
|
||||
|
||||
if (request.method === 'GET') {
|
||||
const response = new Response(formatCalendarEventsToVCalendar(calendarEvents, calendar), {
|
||||
const response = new Response(formatCalendarEventsToVCalendar(calendarEvents, [calendar]), {
|
||||
status: 200,
|
||||
});
|
||||
|
||||
@@ -172,7 +172,7 @@ export const handler: Handler<Data, FreshContextState> = async (request, context
|
||||
|
||||
if (includeVCalendar) {
|
||||
parsedCalendarEvent['d:propstat'][0]['d:prop']['cal:calendar-data'] = escapeXml(
|
||||
formatCalendarEventsToVCalendar([calendarEvent], calendar!),
|
||||
formatCalendarEventsToVCalendar([calendarEvent], [calendar!]),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
formatCalendarEventsToVCalendar,
|
||||
parseVCalendarFromTextContents,
|
||||
} from '/lib/utils.ts';
|
||||
import { getCalendar, getCalendarEvent, getCalendarEvents } from '/lib/data/calendar.ts';
|
||||
import { getCalendar, getCalendarEvent, getCalendarEvents, updateCalendarEvent } from '/lib/data/calendar.ts';
|
||||
import { createSessionCookie } from '/lib/auth.ts';
|
||||
|
||||
interface Data {}
|
||||
@@ -157,7 +157,15 @@ export const handler: Handler<Data, FreshContextState> = async (request, context
|
||||
// }
|
||||
|
||||
if (request.method === 'GET') {
|
||||
const response = new Response(formatCalendarEventsToVCalendar([calendarEvent], calendar), {
|
||||
// Set a UID if there isn't one
|
||||
if (!calendarEvent.extra.uid) {
|
||||
calendarEvent.extra.uid = crypto.randomUUID();
|
||||
await updateCalendarEvent(calendarEvent);
|
||||
|
||||
calendarEvent = await getCalendarEvent(calendarEventId, context.state.user.id);
|
||||
}
|
||||
|
||||
const response = new Response(formatCalendarEventsToVCalendar([calendarEvent], [calendar]), {
|
||||
status: 200,
|
||||
});
|
||||
|
||||
@@ -195,7 +203,7 @@ export const handler: Handler<Data, FreshContextState> = async (request, context
|
||||
|
||||
if (includeVCalendar) {
|
||||
parsedCalendarEvent['d:propstat'][0]['d:prop']['cal:calendar-data'] = escapeXml(
|
||||
formatCalendarEventsToVCalendar([calendarEvent], calendar!),
|
||||
formatCalendarEventsToVCalendar([calendarEvent], [calendar!]),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user