feat: add Google Calendar timetable sync via accounts OAuth

OAuth flows through accounts.betterseqta.org with token exchange server-side. Sync runs in the content script to avoid MV3 service worker timeouts. Adds Svelte calendar sync UI on the timetable page, extension asset URL helper, and excludes calendar tokens from cloud sync.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-06-27 11:51:48 +09:30
parent bd13042fe4
commit e6b8be6821
29 changed files with 1984 additions and 17 deletions
@@ -0,0 +1,44 @@
import { GOOGLE_CALENDAR_API } from "@/config/googleCalendar";
export async function upsertGoogleCalendarEvent(
accessToken: string,
calendarId: string,
existingEventId: string | undefined,
body: Record<string, unknown>,
refreshAccessToken?: () => Promise<string>,
): Promise<string> {
const headers = {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
};
if (existingEventId) {
const res = await fetch(
`${GOOGLE_CALENDAR_API}/calendars/${encodeURIComponent(calendarId)}/events/${encodeURIComponent(existingEventId)}`,
{ method: "PATCH", headers, body: JSON.stringify(body) },
);
if (res.status === 401 && refreshAccessToken) {
const nextToken = await refreshAccessToken();
return upsertGoogleCalendarEvent(nextToken, calendarId, existingEventId, body);
}
if (res.ok) return existingEventId;
if (res.status !== 404) {
const err = (await res.json().catch(() => ({}))) as { error?: { message?: string } };
throw new Error(err?.error?.message ?? `Google Calendar update failed (${res.status})`);
}
}
const res = await fetch(
`${GOOGLE_CALENDAR_API}/calendars/${encodeURIComponent(calendarId)}/events`,
{ method: "POST", headers, body: JSON.stringify(body) },
);
if (res.status === 401 && refreshAccessToken) {
const nextToken = await refreshAccessToken();
return upsertGoogleCalendarEvent(nextToken, calendarId, undefined, body);
}
const json = (await res.json().catch(() => ({}))) as { id?: string; error?: { message?: string } };
if (!res.ok || !json.id) {
throw new Error(json?.error?.message ?? `Google Calendar create failed (${res.status})`);
}
return json.id;
}