mirror of
https://github.com/BetterSEQTA/BetterSEQTA-Plus.git
synced 2026-08-28 09:11:06 +00:00
refactor: consolidate and debloat calendar sync implementation
Merge duplicated Google/Outlook engines, background handlers, and API layers into shared calendarSync modules to cut complexity without changing user-facing sync behavior.
This commit is contained in:
+1
-6
@@ -1,8 +1,3 @@
|
||||
# Optional override for Google OAuth client ID at build/dev time.
|
||||
# Client secret is NOT used in the extension — token exchange is on accounts.betterseqta.org.
|
||||
# See docs/GOOGLE_CALENDAR_ACCOUNTS_CALLBACK.md
|
||||
|
||||
# Optional OAuth client ID overrides (secrets live on accounts.betterseqta.org)
|
||||
# GOOGLE_OAUTH_CLIENT_ID=your-id.apps.googleusercontent.com
|
||||
|
||||
# Outlook / Microsoft Graph (see docs/OUTLOOK_CALENDAR_ACCOUNTS_CALLBACK.md)
|
||||
# OUTLOOK_OAUTH_CLIENT_ID=your-azure-application-client-id
|
||||
|
||||
@@ -11,7 +11,6 @@ export default {
|
||||
},
|
||||
moduleNameMapper: {
|
||||
'^@/(.*)$': '<rootDir>/src/$1',
|
||||
'^color$': '<rootDir>/src/test/mocks/color.ts',
|
||||
'^webextension-polyfill$': '<rootDir>/src/test/mocks/webextension-polyfill.ts',
|
||||
},
|
||||
moduleFileExtensions: ['ts', 'js', 'json'],
|
||||
|
||||
+6
-3
@@ -13,8 +13,11 @@ import {
|
||||
withSuppressedCloudAutoUpload,
|
||||
} from "./background/cloudSettingsAutoSync";
|
||||
import { isAllowedFetchUrl } from "@/seqta/utils/allowedFetchUrl";
|
||||
import { registerGoogleCalendarMessageHandlers, initGoogleCalendarBackground } from "./background/googleCalendar";
|
||||
import { registerOutlookCalendarMessageHandlers } from "./background/outlookCalendar";
|
||||
import { initCalendarBackground } from "./background/calendarBackground";
|
||||
import {
|
||||
registerGoogleCalendarMessageHandlers,
|
||||
registerOutlookCalendarMessageHandlers,
|
||||
} from "./background/calendarBackground";
|
||||
|
||||
/**
|
||||
* Session-only dev-mode override of the content API base.
|
||||
@@ -561,7 +564,7 @@ const MESSAGE_HANDLERS: Record<string, MessageHandler> = {
|
||||
|
||||
registerGoogleCalendarMessageHandlers(MESSAGE_HANDLERS, isTrustedSender);
|
||||
registerOutlookCalendarMessageHandlers(MESSAGE_HANDLERS, isTrustedSender);
|
||||
initGoogleCalendarBackground();
|
||||
initCalendarBackground();
|
||||
|
||||
browser.runtime.onMessage.addListener(
|
||||
// @ts-ignore - OnMessageListener expects literal true for async, we return boolean
|
||||
|
||||
@@ -0,0 +1,531 @@
|
||||
import {
|
||||
GOOGLE_AUTH_URL,
|
||||
GOOGLE_CALENDAR_ACCOUNTS_NOT_READY_HINT,
|
||||
GOOGLE_CALENDAR_OAUTH_CALLBACK,
|
||||
GOOGLE_CALENDAR_REFRESH_URL,
|
||||
GOOGLE_CALENDAR_SCOPE,
|
||||
GOOGLE_CALENDAR_TOKEN_URL,
|
||||
GOOGLE_OAUTH_CLIENT_ID,
|
||||
googleOAuthRedirectUriHint,
|
||||
isGoogleCalendarConfigured,
|
||||
} from "@/config/googleCalendar";
|
||||
import {
|
||||
OUTLOOK_AUTH_URL,
|
||||
OUTLOOK_CALENDAR_ACCOUNTS_NOT_READY_HINT,
|
||||
OUTLOOK_CALENDAR_OAUTH_CALLBACK,
|
||||
OUTLOOK_CALENDAR_REFRESH_URL,
|
||||
OUTLOOK_CALENDAR_SCOPE,
|
||||
OUTLOOK_CALENDAR_TOKEN_URL,
|
||||
OUTLOOK_OAUTH_CLIENT_ID,
|
||||
isOutlookCalendarConfigured,
|
||||
outlookOAuthRedirectUriHint,
|
||||
} from "@/config/outlookCalendar";
|
||||
import {
|
||||
exchangeAccountsCode,
|
||||
refreshAccountsToken,
|
||||
type AccountsTokenPayload,
|
||||
} from "@/seqta/utils/calendarSync/accountsToken";
|
||||
import {
|
||||
CALENDAR_WEEKLY_ALARM,
|
||||
clampSyncWeeks,
|
||||
getAutoSyncWeekly,
|
||||
getSyncWeeksAhead,
|
||||
isAnyCalendarConnected,
|
||||
markWeeklySyncPending,
|
||||
readSharedCalendarSyncSettings,
|
||||
writeSharedCalendarSyncSettings,
|
||||
} from "@/seqta/utils/calendarSync/settings";
|
||||
import {
|
||||
clearGoogleCalendarState,
|
||||
readGoogleCalendarState,
|
||||
writeGoogleCalendarState,
|
||||
} from "@/seqta/utils/googleCalendar/storage";
|
||||
import type {
|
||||
GoogleCalendarStatus,
|
||||
GoogleCalendarSyncResult,
|
||||
} from "@/seqta/utils/googleCalendar/types";
|
||||
import {
|
||||
clearOutlookCalendarState,
|
||||
readOutlookCalendarState,
|
||||
writeOutlookCalendarState,
|
||||
} from "@/seqta/utils/outlookCalendar/storage";
|
||||
import type { OutlookCalendarStatus } from "@/seqta/utils/outlookCalendar/storage";
|
||||
import browser from "webextension-polyfill";
|
||||
|
||||
const WEEKLY_PERIOD_MINUTES = 7 * 24 * 60;
|
||||
|
||||
export type CalendarMessageHandler = (
|
||||
request: unknown,
|
||||
sendResponse: (response?: unknown) => void,
|
||||
sender?: browser.Runtime.MessageSender,
|
||||
) => boolean | void;
|
||||
|
||||
export type CalendarMessageHandlerMap = Record<string, CalendarMessageHandler>;
|
||||
|
||||
type StoredTokens = {
|
||||
accessToken?: string;
|
||||
refreshToken?: string;
|
||||
expiresAt?: number;
|
||||
lastSyncAt?: number;
|
||||
lastSyncOrigin?: string;
|
||||
};
|
||||
|
||||
type OAuthTabFlowOptions = {
|
||||
callbackPrefix: string;
|
||||
timeoutMessage: string;
|
||||
cancelledMessage: string;
|
||||
tabOpenErrorMessage: string;
|
||||
timeoutMs?: number;
|
||||
};
|
||||
|
||||
type CalendarProviderBackend = {
|
||||
label: string;
|
||||
isConfigured: () => boolean;
|
||||
redirectHint: () => string;
|
||||
mismatchRe: RegExp;
|
||||
authUrl: string;
|
||||
clientId: string;
|
||||
scope: string;
|
||||
callback: string;
|
||||
tabOpts: Omit<OAuthTabFlowOptions, "callbackPrefix">;
|
||||
notConfiguredError: string;
|
||||
notConnectedError: string;
|
||||
signInFailed: string;
|
||||
read: () => Promise<StoredTokens>;
|
||||
write: (patch: Partial<StoredTokens> & { connectedAt?: number }) => Promise<unknown>;
|
||||
clear: () => Promise<unknown>;
|
||||
exchange: (code: string, redirectUri: string, verifier: string) => Promise<AccountsTokenPayload>;
|
||||
refresh: (refreshToken: string) => Promise<AccountsTokenPayload>;
|
||||
applyAuthParams: (authUrl: URL) => void;
|
||||
messagePrefix: string;
|
||||
};
|
||||
|
||||
function createProviderBackend(
|
||||
config: Omit<CalendarProviderBackend, "exchange" | "refresh"> & {
|
||||
tokenUrl: string;
|
||||
refreshUrl: string;
|
||||
notReadyHint: string;
|
||||
includeErrorDescription?: boolean;
|
||||
},
|
||||
): CalendarProviderBackend {
|
||||
const { tokenUrl, refreshUrl, notReadyHint, includeErrorDescription, ...backend } = config;
|
||||
return {
|
||||
...backend,
|
||||
exchange: (code, redirectUri, verifier) =>
|
||||
exchangeAccountsCode(tokenUrl, code, redirectUri, verifier, notReadyHint, includeErrorDescription),
|
||||
refresh: (refreshToken) =>
|
||||
refreshAccountsToken(refreshUrl, refreshToken, notReadyHint, includeErrorDescription),
|
||||
};
|
||||
}
|
||||
|
||||
const GOOGLE_BACKEND = createProviderBackend({
|
||||
label: "Google",
|
||||
isConfigured: isGoogleCalendarConfigured,
|
||||
redirectHint: googleOAuthRedirectUriHint,
|
||||
mismatchRe: /redirect_uri_mismatch|invalid_request/i,
|
||||
authUrl: GOOGLE_AUTH_URL,
|
||||
clientId: GOOGLE_OAUTH_CLIENT_ID,
|
||||
scope: GOOGLE_CALENDAR_SCOPE,
|
||||
callback: GOOGLE_CALENDAR_OAUTH_CALLBACK,
|
||||
tabOpts: {
|
||||
timeoutMessage: "Google sign-in timed out. Close the tab and try again.",
|
||||
cancelledMessage: "Google sign-in was cancelled.",
|
||||
tabOpenErrorMessage: "Could not open Google sign-in tab.",
|
||||
},
|
||||
notConfiguredError: "Google Calendar is not configured in this extension build.",
|
||||
notConnectedError: "Not connected to Google Calendar.",
|
||||
signInFailed: "Google sign-in failed",
|
||||
read: readGoogleCalendarState,
|
||||
write: writeGoogleCalendarState,
|
||||
clear: clearGoogleCalendarState,
|
||||
applyAuthParams(authUrl) {
|
||||
authUrl.searchParams.set("access_type", "offline");
|
||||
},
|
||||
messagePrefix: "googleCalendar",
|
||||
tokenUrl: GOOGLE_CALENDAR_TOKEN_URL,
|
||||
refreshUrl: GOOGLE_CALENDAR_REFRESH_URL,
|
||||
notReadyHint: GOOGLE_CALENDAR_ACCOUNTS_NOT_READY_HINT,
|
||||
});
|
||||
|
||||
const OUTLOOK_BACKEND = createProviderBackend({
|
||||
label: "Microsoft",
|
||||
isConfigured: isOutlookCalendarConfigured,
|
||||
redirectHint: outlookOAuthRedirectUriHint,
|
||||
mismatchRe: /redirect_uri|invalid_request|AADSTS50011/i,
|
||||
authUrl: OUTLOOK_AUTH_URL,
|
||||
clientId: OUTLOOK_OAUTH_CLIENT_ID,
|
||||
scope: OUTLOOK_CALENDAR_SCOPE,
|
||||
callback: OUTLOOK_CALENDAR_OAUTH_CALLBACK,
|
||||
tabOpts: {
|
||||
timeoutMessage: "Microsoft sign-in timed out. Close the tab and try again.",
|
||||
cancelledMessage: "Microsoft sign-in was cancelled.",
|
||||
tabOpenErrorMessage: "Could not open Microsoft sign-in tab.",
|
||||
},
|
||||
notConfiguredError: "Outlook Calendar is not configured in this extension build.",
|
||||
notConnectedError: "Not connected to Outlook Calendar.",
|
||||
signInFailed: "Microsoft sign-in failed",
|
||||
read: readOutlookCalendarState,
|
||||
write: writeOutlookCalendarState,
|
||||
clear: clearOutlookCalendarState,
|
||||
applyAuthParams(authUrl) {
|
||||
authUrl.searchParams.set("response_mode", "query");
|
||||
},
|
||||
messagePrefix: "outlookCalendar",
|
||||
tokenUrl: OUTLOOK_CALENDAR_TOKEN_URL,
|
||||
refreshUrl: OUTLOOK_CALENDAR_REFRESH_URL,
|
||||
notReadyHint: OUTLOOK_CALENDAR_ACCOUNTS_NOT_READY_HINT,
|
||||
includeErrorDescription: true,
|
||||
});
|
||||
|
||||
function base64UrlEncode(buffer: ArrayBuffer): string {
|
||||
const bytes = new Uint8Array(buffer);
|
||||
let binary = "";
|
||||
for (const b of bytes) binary += String.fromCharCode(b);
|
||||
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
|
||||
}
|
||||
|
||||
function randomPkceVerifier(): string {
|
||||
const bytes = new Uint8Array(32);
|
||||
crypto.getRandomValues(bytes);
|
||||
return base64UrlEncode(bytes.buffer);
|
||||
}
|
||||
|
||||
async function pkceChallenge(verifier: string): Promise<string> {
|
||||
const data = new TextEncoder().encode(verifier);
|
||||
const digest = await crypto.subtle.digest("SHA-256", data);
|
||||
return base64UrlEncode(digest);
|
||||
}
|
||||
|
||||
function parseOAuthRedirectCode(responseUrl: string, providerLabel: string): string {
|
||||
const url = new URL(responseUrl);
|
||||
const err = url.searchParams.get("error");
|
||||
if (err) throw new Error(url.searchParams.get("error_description") ?? err);
|
||||
const code = url.searchParams.get("code");
|
||||
if (!code) throw new Error(`${providerLabel} sign-in did not return an authorization code.`);
|
||||
return code;
|
||||
}
|
||||
|
||||
function isOAuthCallbackUrl(url: string, callbackPrefix: string): boolean {
|
||||
if (!url.startsWith(callbackPrefix)) return false;
|
||||
const parsed = new URL(url);
|
||||
return parsed.searchParams.has("code") || parsed.searchParams.has("error");
|
||||
}
|
||||
|
||||
function waitForOAuthCallback(authTabId: number, opts: OAuthTabFlowOptions): Promise<string> {
|
||||
const timeoutMs = opts.timeoutMs ?? 10 * 60 * 1000;
|
||||
return new Promise((resolve, reject) => {
|
||||
const timeoutId = setTimeout(() => {
|
||||
cleanup();
|
||||
reject(new Error(opts.timeoutMessage));
|
||||
}, timeoutMs);
|
||||
|
||||
const cleanup = () => {
|
||||
clearTimeout(timeoutId);
|
||||
browser.tabs.onUpdated.removeListener(onUpdated);
|
||||
browser.tabs.onRemoved.removeListener(onRemoved);
|
||||
};
|
||||
|
||||
const finishFromUrl = (url: string, tabId: number) => {
|
||||
if (!isOAuthCallbackUrl(url, opts.callbackPrefix)) return false;
|
||||
cleanup();
|
||||
void browser.tabs.remove(tabId).catch(() => {});
|
||||
resolve(url);
|
||||
return true;
|
||||
};
|
||||
|
||||
const onRemoved = (tabId: number) => {
|
||||
if (tabId !== authTabId) return;
|
||||
cleanup();
|
||||
reject(new Error(opts.cancelledMessage));
|
||||
};
|
||||
|
||||
const onUpdated = (
|
||||
tabId: number,
|
||||
changeInfo: browser.Tabs.OnUpdatedChangeInfoType,
|
||||
tab: browser.Tabs.Tab,
|
||||
) => {
|
||||
if (tabId !== authTabId) return;
|
||||
const url =
|
||||
changeInfo.url ?? (changeInfo.status === "complete" ? tab.url : undefined);
|
||||
if (url) finishFromUrl(url, tabId);
|
||||
};
|
||||
|
||||
browser.tabs.onUpdated.addListener(onUpdated);
|
||||
browser.tabs.onRemoved.addListener(onRemoved);
|
||||
|
||||
void browser.tabs
|
||||
.get(authTabId)
|
||||
.then((tab) => {
|
||||
if (tab.url) finishFromUrl(tab.url, authTabId);
|
||||
})
|
||||
.catch(() => {});
|
||||
});
|
||||
}
|
||||
|
||||
async function openOAuthTab(authUrl: string, opts: OAuthTabFlowOptions): Promise<string> {
|
||||
const tab = await browser.tabs.create({ url: authUrl, active: true });
|
||||
if (tab.id === undefined) throw new Error(opts.tabOpenErrorMessage);
|
||||
return waitForOAuthCallback(tab.id, opts);
|
||||
}
|
||||
|
||||
function tokenExpiresAt(expiresIn?: number): number {
|
||||
return expiresIn ? Date.now() + expiresIn * 1000 : Date.now() + 3_600_000;
|
||||
}
|
||||
|
||||
async function getValidStoredAccessToken(
|
||||
provider: CalendarProviderBackend,
|
||||
): Promise<string> {
|
||||
const state = await provider.read();
|
||||
const now = Date.now();
|
||||
if (state.accessToken && state.expiresAt && state.expiresAt > now + 60_000) {
|
||||
return state.accessToken;
|
||||
}
|
||||
if (!state.refreshToken) throw new Error(provider.notConnectedError);
|
||||
const refreshed = await provider.refresh(state.refreshToken);
|
||||
await provider.write({
|
||||
accessToken: refreshed.access_token,
|
||||
refreshToken: refreshed.refresh_token ?? state.refreshToken,
|
||||
expiresAt: tokenExpiresAt(refreshed.expires_in),
|
||||
});
|
||||
return refreshed.access_token;
|
||||
}
|
||||
|
||||
export function registerTrustedAsyncHandler(
|
||||
handlers: CalendarMessageHandlerMap,
|
||||
isTrustedSender: (sender?: browser.Runtime.MessageSender) => boolean,
|
||||
key: string,
|
||||
fn: (request: unknown) => Promise<unknown>,
|
||||
onError?: (err: unknown) => unknown,
|
||||
): void {
|
||||
handlers[key] = (request, sendResponse, sender) => {
|
||||
if (!isTrustedSender(sender)) {
|
||||
sendResponse({ success: false, error: "Unauthorized sender" });
|
||||
return false;
|
||||
}
|
||||
void fn(request)
|
||||
.then(sendResponse)
|
||||
.catch((err) => {
|
||||
sendResponse(
|
||||
onError?.(err) ?? {
|
||||
success: false,
|
||||
error: err instanceof Error ? err.message : "Request failed",
|
||||
},
|
||||
);
|
||||
});
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
async function getGoogleCalendarStatus(): Promise<GoogleCalendarStatus> {
|
||||
const [state, shared, syncWeeksAhead, autoSyncWeekly] = await Promise.all([
|
||||
readGoogleCalendarState(),
|
||||
readSharedCalendarSyncSettings(),
|
||||
getSyncWeeksAhead(),
|
||||
getAutoSyncWeekly(),
|
||||
]);
|
||||
return {
|
||||
configured: isGoogleCalendarConfigured(),
|
||||
connected: !!(state.refreshToken || state.accessToken),
|
||||
lastSyncAt: state.lastSyncAt,
|
||||
lastWeeklySyncAt: shared.lastWeeklySyncAt,
|
||||
lastSyncOrigin: state.lastSyncOrigin,
|
||||
syncWeeksAhead,
|
||||
autoSyncWeekly,
|
||||
};
|
||||
}
|
||||
|
||||
async function getOutlookCalendarStatus(): Promise<OutlookCalendarStatus> {
|
||||
const state = await readOutlookCalendarState();
|
||||
return {
|
||||
configured: isOutlookCalendarConfigured(),
|
||||
connected: !!(state.refreshToken || state.accessToken),
|
||||
lastSyncAt: state.lastSyncAt,
|
||||
lastSyncOrigin: state.lastSyncOrigin,
|
||||
};
|
||||
}
|
||||
|
||||
async function connectCalendar(provider: CalendarProviderBackend): Promise<GoogleCalendarSyncResult> {
|
||||
if (!provider.isConfigured()) {
|
||||
return { success: false, configured: false, error: provider.notConfiguredError };
|
||||
}
|
||||
|
||||
const redirectUri = provider.callback;
|
||||
const verifier = randomPkceVerifier();
|
||||
const challenge = await pkceChallenge(verifier);
|
||||
const authUrl = new URL(provider.authUrl);
|
||||
authUrl.searchParams.set("client_id", provider.clientId);
|
||||
authUrl.searchParams.set("response_type", "code");
|
||||
authUrl.searchParams.set("redirect_uri", redirectUri);
|
||||
authUrl.searchParams.set("scope", provider.scope);
|
||||
authUrl.searchParams.set("prompt", "consent");
|
||||
authUrl.searchParams.set("code_challenge", challenge);
|
||||
authUrl.searchParams.set("code_challenge_method", "S256");
|
||||
provider.applyAuthParams(authUrl);
|
||||
|
||||
let responseUrl: string;
|
||||
try {
|
||||
responseUrl = await openOAuthTab(authUrl.toString(), {
|
||||
callbackPrefix: provider.callback,
|
||||
...provider.tabOpts,
|
||||
});
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : provider.signInFailed;
|
||||
const mismatch =
|
||||
provider.mismatchRe.test(message) || provider.mismatchRe.test(String(err));
|
||||
return {
|
||||
success: false,
|
||||
configured: true,
|
||||
error: mismatch
|
||||
? `${provider.label} redirect URI mismatch. ${provider.redirectHint()}`
|
||||
: message.includes("cancel")
|
||||
? provider.tabOpts.cancelledMessage
|
||||
: message,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const code = parseOAuthRedirectCode(responseUrl, provider.label);
|
||||
const tokens = await provider.exchange(code, redirectUri, verifier);
|
||||
const existing = await provider.read();
|
||||
await provider.write({
|
||||
accessToken: tokens.access_token,
|
||||
refreshToken: tokens.refresh_token ?? existing.refreshToken,
|
||||
expiresAt: tokenExpiresAt(tokens.expires_in),
|
||||
connectedAt: Date.now(),
|
||||
});
|
||||
await ensureWeeklySyncAlarm();
|
||||
return { success: true, configured: true, connected: true };
|
||||
} catch (err) {
|
||||
return {
|
||||
success: false,
|
||||
configured: true,
|
||||
error: err instanceof Error ? err.message : provider.signInFailed,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function registerProviderHandlers(
|
||||
handlers: CalendarMessageHandlerMap,
|
||||
isTrustedSender: (sender?: browser.Runtime.MessageSender) => boolean,
|
||||
provider: CalendarProviderBackend,
|
||||
getStatus: () => Promise<unknown>,
|
||||
): void {
|
||||
const { messagePrefix, signInFailed } = provider;
|
||||
const errMsg = (err: unknown, fallback: string) =>
|
||||
err instanceof Error ? err.message : fallback;
|
||||
|
||||
registerTrustedAsyncHandler(
|
||||
handlers,
|
||||
isTrustedSender,
|
||||
`${messagePrefix}Connect`,
|
||||
() => connectCalendar(provider),
|
||||
(err) => ({ success: false, error: errMsg(err, signInFailed) }),
|
||||
);
|
||||
|
||||
registerTrustedAsyncHandler(
|
||||
handlers,
|
||||
isTrustedSender,
|
||||
`${messagePrefix}Disconnect`,
|
||||
async () => {
|
||||
await provider.clear();
|
||||
await ensureWeeklySyncAlarm();
|
||||
return { success: true };
|
||||
},
|
||||
(err) => ({ success: false, error: errMsg(err, "Disconnect failed") }),
|
||||
);
|
||||
|
||||
registerTrustedAsyncHandler(
|
||||
handlers,
|
||||
isTrustedSender,
|
||||
`${messagePrefix}Status`,
|
||||
getStatus,
|
||||
() => ({ configured: provider.isConfigured(), connected: false }),
|
||||
);
|
||||
|
||||
registerTrustedAsyncHandler(
|
||||
handlers,
|
||||
isTrustedSender,
|
||||
`${messagePrefix}GetAccessToken`,
|
||||
() => getValidStoredAccessToken(provider).then((accessToken) => ({ success: true, accessToken })),
|
||||
(err) => ({ success: false, error: errMsg(err, "Token refresh failed") }),
|
||||
);
|
||||
}
|
||||
|
||||
export function registerGoogleCalendarMessageHandlers(
|
||||
handlers: CalendarMessageHandlerMap,
|
||||
isTrustedSender: (sender?: browser.Runtime.MessageSender) => boolean,
|
||||
): void {
|
||||
registerProviderHandlers(handlers, isTrustedSender, GOOGLE_BACKEND, getGoogleCalendarStatus);
|
||||
|
||||
registerTrustedAsyncHandler(handlers, isTrustedSender, "googleCalendarEnsureWeeklyAlarm", async () => {
|
||||
await ensureWeeklySyncAlarm();
|
||||
return { success: true };
|
||||
}, (err) => ({
|
||||
success: false,
|
||||
error: err instanceof Error ? err.message : "Could not schedule weekly sync",
|
||||
}));
|
||||
|
||||
registerTrustedAsyncHandler(handlers, isTrustedSender, "googleCalendarUpdateSyncSettings", async (request) => {
|
||||
const body = request as { syncWeeksAhead?: number; autoSyncWeekly?: boolean };
|
||||
const patch: Record<string, unknown> = {};
|
||||
if (body.syncWeeksAhead != null) patch.syncWeeksAhead = clampSyncWeeks(body.syncWeeksAhead);
|
||||
if (body.autoSyncWeekly != null) patch.autoSyncWeekly = !!body.autoSyncWeekly;
|
||||
if (Object.keys(patch).length > 0) await writeSharedCalendarSyncSettings(patch);
|
||||
await ensureWeeklySyncAlarm();
|
||||
return { success: true, ...(await getGoogleCalendarStatus()) };
|
||||
}, (err) => ({
|
||||
success: false,
|
||||
error: err instanceof Error ? err.message : "Could not update sync settings",
|
||||
}));
|
||||
}
|
||||
|
||||
export function registerOutlookCalendarMessageHandlers(
|
||||
handlers: CalendarMessageHandlerMap,
|
||||
isTrustedSender: (sender?: browser.Runtime.MessageSender) => boolean,
|
||||
): void {
|
||||
registerProviderHandlers(handlers, isTrustedSender, OUTLOOK_BACKEND, getOutlookCalendarStatus);
|
||||
}
|
||||
|
||||
function isSeqtaTab(tab: browser.Tabs.Tab): boolean {
|
||||
const title = tab.title ?? "";
|
||||
return title.includes("SEQTA Learn") || title.includes("SEQTA Engage");
|
||||
}
|
||||
|
||||
export async function ensureWeeklySyncAlarm(): Promise<void> {
|
||||
const [connected, enabled] = await Promise.all([isAnyCalendarConnected(), getAutoSyncWeekly()]);
|
||||
if (!connected || !enabled) {
|
||||
await browser.alarms.clear(CALENDAR_WEEKLY_ALARM);
|
||||
return;
|
||||
}
|
||||
const existing = await browser.alarms.get(CALENDAR_WEEKLY_ALARM);
|
||||
if (!existing) {
|
||||
await browser.alarms.create(CALENDAR_WEEKLY_ALARM, { periodInMinutes: WEEKLY_PERIOD_MINUTES });
|
||||
}
|
||||
}
|
||||
|
||||
async function triggerWeeklySyncOnSeqtaTabs(): Promise<boolean> {
|
||||
const tabs = await browser.tabs.query({});
|
||||
let delivered = false;
|
||||
for (const tab of tabs) {
|
||||
if (tab.id == null || !isSeqtaTab(tab)) continue;
|
||||
try {
|
||||
await browser.tabs.sendMessage(tab.id, { type: "calendarRunWeeklySync" });
|
||||
delivered = true;
|
||||
} catch {
|
||||
// Tab may not have content script yet.
|
||||
}
|
||||
}
|
||||
return delivered;
|
||||
}
|
||||
|
||||
async function handleWeeklySyncAlarm(): Promise<void> {
|
||||
if (!(await isAnyCalendarConnected()) || !(await getAutoSyncWeekly())) return;
|
||||
if (!(await triggerWeeklySyncOnSeqtaTabs())) await markWeeklySyncPending();
|
||||
}
|
||||
|
||||
export function initCalendarBackground(): void {
|
||||
browser.alarms.onAlarm.addListener((alarm) => {
|
||||
if (alarm.name === CALENDAR_WEEKLY_ALARM) void handleWeeklySyncAlarm();
|
||||
});
|
||||
void ensureWeeklySyncAlarm();
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
import browser from "webextension-polyfill";
|
||||
import { verboseLog } from "@/utils/verboseLog";
|
||||
import {
|
||||
CALENDAR_WEEKLY_ALARM,
|
||||
getAutoSyncWeekly,
|
||||
markWeeklySyncPending,
|
||||
} from "@/seqta/utils/calendarSync/settings";
|
||||
import { readGoogleCalendarState } from "@/seqta/utils/googleCalendar/storage";
|
||||
import { readOutlookCalendarState } from "@/seqta/utils/outlookCalendar/storage";
|
||||
|
||||
const WEEKLY_PERIOD_MINUTES = 7 * 24 * 60;
|
||||
|
||||
function isSeqtaTab(tab: browser.Tabs.Tab): boolean {
|
||||
const title = tab.title ?? "";
|
||||
return title.includes("SEQTA Learn") || title.includes("SEQTA Engage");
|
||||
}
|
||||
|
||||
async function isAnyCalendarConnected(): Promise<boolean> {
|
||||
const [google, outlook] = await Promise.all([
|
||||
readGoogleCalendarState(),
|
||||
readOutlookCalendarState(),
|
||||
]);
|
||||
return !!(
|
||||
google.refreshToken ||
|
||||
google.accessToken ||
|
||||
outlook.refreshToken ||
|
||||
outlook.accessToken
|
||||
);
|
||||
}
|
||||
|
||||
export async function ensureWeeklySyncAlarm(): Promise<void> {
|
||||
const connected = await isAnyCalendarConnected();
|
||||
const enabled = await getAutoSyncWeekly();
|
||||
if (!connected || !enabled) {
|
||||
await browser.alarms.clear(CALENDAR_WEEKLY_ALARM);
|
||||
return;
|
||||
}
|
||||
|
||||
const existing = await browser.alarms.get(CALENDAR_WEEKLY_ALARM);
|
||||
if (!existing) {
|
||||
await browser.alarms.create(CALENDAR_WEEKLY_ALARM, {
|
||||
periodInMinutes: WEEKLY_PERIOD_MINUTES,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export async function clearWeeklySyncAlarm(): Promise<void> {
|
||||
await browser.alarms.clear(CALENDAR_WEEKLY_ALARM);
|
||||
}
|
||||
|
||||
export async function triggerWeeklySyncOnSeqtaTabs(): Promise<boolean> {
|
||||
const tabs = await browser.tabs.query({});
|
||||
const seqtaTabs = tabs.filter((tab) => tab.id != null && isSeqtaTab(tab));
|
||||
if (seqtaTabs.length === 0) return false;
|
||||
|
||||
let delivered = false;
|
||||
for (const tab of seqtaTabs) {
|
||||
if (tab.id == null) continue;
|
||||
try {
|
||||
await browser.tabs.sendMessage(tab.id, { type: "calendarRunWeeklySync" });
|
||||
delivered = true;
|
||||
} catch (err) {
|
||||
verboseLog("[BetterSEQTA+] Weekly calendar sync message failed for tab:", tab.id, err);
|
||||
}
|
||||
}
|
||||
return delivered;
|
||||
}
|
||||
|
||||
export async function handleWeeklySyncAlarm(): Promise<void> {
|
||||
if (!(await isAnyCalendarConnected()) || !(await getAutoSyncWeekly())) return;
|
||||
|
||||
const delivered = await triggerWeeklySyncOnSeqtaTabs();
|
||||
if (!delivered) {
|
||||
await markWeeklySyncPending();
|
||||
}
|
||||
}
|
||||
|
||||
export function registerWeeklySyncAlarmListener(): void {
|
||||
browser.alarms.onAlarm.addListener((alarm) => {
|
||||
if (alarm.name !== CALENDAR_WEEKLY_ALARM) return;
|
||||
void handleWeeklySyncAlarm();
|
||||
});
|
||||
}
|
||||
|
||||
export function initCalendarBackground(): void {
|
||||
registerWeeklySyncAlarmListener();
|
||||
void ensureWeeklySyncAlarm();
|
||||
}
|
||||
@@ -1,363 +0,0 @@
|
||||
import browser from "webextension-polyfill";
|
||||
import {
|
||||
GOOGLE_AUTH_URL,
|
||||
GOOGLE_CALENDAR_OAUTH_CALLBACK,
|
||||
GOOGLE_CALENDAR_SCOPE,
|
||||
GOOGLE_OAUTH_CLIENT_ID,
|
||||
googleOAuthRedirectUriHint,
|
||||
isGoogleCalendarConfigured,
|
||||
} from "@/config/googleCalendar";
|
||||
import {
|
||||
exchangeGoogleCodeViaAccounts,
|
||||
refreshGoogleTokenViaAccounts,
|
||||
} from "@/seqta/utils/googleCalendar/accountsToken";
|
||||
import {
|
||||
clearGoogleCalendarState,
|
||||
readGoogleCalendarState,
|
||||
writeGoogleCalendarState,
|
||||
} from "@/seqta/utils/googleCalendar/storage";
|
||||
import {
|
||||
clampSyncWeeks,
|
||||
getAutoSyncWeekly,
|
||||
getSyncWeeksAhead,
|
||||
} from "@/seqta/utils/calendarSync/settings";
|
||||
import {
|
||||
readSharedCalendarSyncSettings,
|
||||
writeSharedCalendarSyncSettings,
|
||||
} from "@/seqta/utils/calendarSync/sharedSettings";
|
||||
import type {
|
||||
GoogleCalendarStatus,
|
||||
GoogleCalendarSyncResult,
|
||||
} from "@/seqta/utils/googleCalendar/types";
|
||||
import { ensureWeeklySyncAlarm, initCalendarBackground } from "./calendarWeekly";
|
||||
|
||||
function base64UrlEncode(buffer: ArrayBuffer): string {
|
||||
const bytes = new Uint8Array(buffer);
|
||||
let binary = "";
|
||||
for (const b of bytes) binary += String.fromCharCode(b);
|
||||
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
|
||||
}
|
||||
|
||||
async function sha256(input: string): Promise<ArrayBuffer> {
|
||||
const data = new TextEncoder().encode(input);
|
||||
return crypto.subtle.digest("SHA-256", data);
|
||||
}
|
||||
|
||||
function randomVerifier(): string {
|
||||
const bytes = new Uint8Array(32);
|
||||
crypto.getRandomValues(bytes);
|
||||
return base64UrlEncode(bytes.buffer);
|
||||
}
|
||||
|
||||
async function pkceChallenge(verifier: string): Promise<string> {
|
||||
return base64UrlEncode(await sha256(verifier));
|
||||
}
|
||||
|
||||
function parseRedirectCode(responseUrl: string): string {
|
||||
const url = new URL(responseUrl);
|
||||
const err = url.searchParams.get("error");
|
||||
if (err) throw new Error(url.searchParams.get("error_description") ?? err);
|
||||
const code = url.searchParams.get("code");
|
||||
if (!code) throw new Error("Google sign-in did not return an authorization code.");
|
||||
return code;
|
||||
}
|
||||
|
||||
const GOOGLE_OAUTH_CALLBACK_PREFIX = GOOGLE_CALENDAR_OAUTH_CALLBACK;
|
||||
const GOOGLE_OAUTH_TIMEOUT_MS = 10 * 60 * 1000;
|
||||
|
||||
function isGoogleOAuthCallbackUrl(url: string): boolean {
|
||||
if (!url.startsWith(GOOGLE_OAUTH_CALLBACK_PREFIX)) return false;
|
||||
const parsed = new URL(url);
|
||||
return parsed.searchParams.has("code") || parsed.searchParams.has("error");
|
||||
}
|
||||
|
||||
/** Tab-based OAuth (same pattern as cloud login) — launchWebAuthFlow does not reliably return external redirect URLs. */
|
||||
function waitForGoogleOAuthCallback(authTabId: number): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const timeoutId = setTimeout(() => {
|
||||
cleanup();
|
||||
reject(new Error("Google sign-in timed out. Close the tab and try again."));
|
||||
}, GOOGLE_OAUTH_TIMEOUT_MS);
|
||||
|
||||
const cleanup = () => {
|
||||
clearTimeout(timeoutId);
|
||||
browser.tabs.onUpdated.removeListener(onUpdated);
|
||||
browser.tabs.onRemoved.removeListener(onRemoved);
|
||||
};
|
||||
|
||||
const finishFromUrl = (url: string, tabId: number) => {
|
||||
if (!isGoogleOAuthCallbackUrl(url)) return false;
|
||||
cleanup();
|
||||
void browser.tabs.remove(tabId).catch(() => {});
|
||||
resolve(url);
|
||||
return true;
|
||||
};
|
||||
|
||||
const onRemoved = (tabId: number) => {
|
||||
if (tabId !== authTabId) return;
|
||||
cleanup();
|
||||
reject(new Error("Google sign-in was cancelled."));
|
||||
};
|
||||
|
||||
const onUpdated = (
|
||||
tabId: number,
|
||||
changeInfo: browser.Tabs.OnUpdatedChangeInfoType,
|
||||
tab: browser.Tabs.Tab,
|
||||
) => {
|
||||
if (tabId !== authTabId) return;
|
||||
const url =
|
||||
changeInfo.url ?? (changeInfo.status === "complete" ? tab.url : undefined);
|
||||
if (url) finishFromUrl(url, tabId);
|
||||
};
|
||||
|
||||
browser.tabs.onUpdated.addListener(onUpdated);
|
||||
browser.tabs.onRemoved.addListener(onRemoved);
|
||||
|
||||
void browser.tabs
|
||||
.get(authTabId)
|
||||
.then((tab) => {
|
||||
if (tab.url) finishFromUrl(tab.url, authTabId);
|
||||
})
|
||||
.catch(() => {});
|
||||
});
|
||||
}
|
||||
|
||||
async function openGoogleOAuthTab(authUrl: string): Promise<string> {
|
||||
const tab = await browser.tabs.create({ url: authUrl, active: true });
|
||||
if (tab.id === undefined) {
|
||||
throw new Error("Could not open Google sign-in tab.");
|
||||
}
|
||||
return waitForGoogleOAuthCallback(tab.id);
|
||||
}
|
||||
|
||||
async function getValidAccessToken(): Promise<string> {
|
||||
const state = await readGoogleCalendarState();
|
||||
const now = Date.now();
|
||||
if (state.accessToken && state.expiresAt && state.expiresAt > now + 60_000) {
|
||||
return state.accessToken;
|
||||
}
|
||||
if (!state.refreshToken) {
|
||||
throw new Error("Not connected to Google Calendar.");
|
||||
}
|
||||
const refreshed = await refreshGoogleTokenViaAccounts(state.refreshToken);
|
||||
const expiresAt = refreshed.expires_in
|
||||
? Date.now() + refreshed.expires_in * 1000
|
||||
: Date.now() + 3_600_000;
|
||||
await writeGoogleCalendarState({
|
||||
accessToken: refreshed.access_token,
|
||||
refreshToken: refreshed.refresh_token ?? state.refreshToken,
|
||||
expiresAt,
|
||||
});
|
||||
return refreshed.access_token;
|
||||
}
|
||||
|
||||
async function connectGoogleCalendar(): Promise<GoogleCalendarSyncResult> {
|
||||
if (!isGoogleCalendarConfigured()) {
|
||||
return {
|
||||
success: false,
|
||||
configured: false,
|
||||
error: "Google Calendar is not configured in this extension build.",
|
||||
};
|
||||
}
|
||||
|
||||
const redirectUri = GOOGLE_CALENDAR_OAUTH_CALLBACK;
|
||||
const verifier = randomVerifier();
|
||||
const challenge = await pkceChallenge(verifier);
|
||||
const authUrl = new URL(GOOGLE_AUTH_URL);
|
||||
authUrl.searchParams.set("client_id", GOOGLE_OAUTH_CLIENT_ID);
|
||||
authUrl.searchParams.set("response_type", "code");
|
||||
authUrl.searchParams.set("redirect_uri", redirectUri);
|
||||
authUrl.searchParams.set("scope", GOOGLE_CALENDAR_SCOPE);
|
||||
authUrl.searchParams.set("access_type", "offline");
|
||||
authUrl.searchParams.set("prompt", "consent");
|
||||
authUrl.searchParams.set("code_challenge", challenge);
|
||||
authUrl.searchParams.set("code_challenge_method", "S256");
|
||||
|
||||
let responseUrl: string;
|
||||
try {
|
||||
responseUrl = await openGoogleOAuthTab(authUrl.toString());
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : "Google sign-in failed";
|
||||
const mismatch =
|
||||
/redirect_uri_mismatch|invalid_request/i.test(message) ||
|
||||
/redirect_uri_mismatch|invalid_request/i.test(String(err));
|
||||
return {
|
||||
success: false,
|
||||
configured: true,
|
||||
error: mismatch
|
||||
? `Google redirect URI mismatch. ${googleOAuthRedirectUriHint()}`
|
||||
: message.includes("cancel")
|
||||
? "Google sign-in was cancelled."
|
||||
: message,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const code = parseRedirectCode(responseUrl);
|
||||
const tokens = await exchangeGoogleCodeViaAccounts(code, redirectUri, verifier);
|
||||
const expiresAt = tokens.expires_in
|
||||
? Date.now() + tokens.expires_in * 1000
|
||||
: Date.now() + 3_600_000;
|
||||
|
||||
const existing = await readGoogleCalendarState();
|
||||
await writeGoogleCalendarState({
|
||||
accessToken: tokens.access_token,
|
||||
refreshToken: tokens.refresh_token ?? existing.refreshToken,
|
||||
expiresAt,
|
||||
connectedAt: Date.now(),
|
||||
});
|
||||
|
||||
await ensureWeeklySyncAlarm();
|
||||
|
||||
return { success: true, configured: true, connected: true };
|
||||
} catch (err) {
|
||||
return {
|
||||
success: false,
|
||||
configured: true,
|
||||
error: err instanceof Error ? err.message : "Google sign-in failed",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function getGoogleCalendarStatus(): Promise<GoogleCalendarStatus> {
|
||||
const state = await readGoogleCalendarState();
|
||||
const shared = await readSharedCalendarSyncSettings();
|
||||
const syncWeeksAhead = await getSyncWeeksAhead();
|
||||
const autoSyncWeekly = await getAutoSyncWeekly();
|
||||
return {
|
||||
configured: isGoogleCalendarConfigured(),
|
||||
connected: !!(state.refreshToken || state.accessToken),
|
||||
lastSyncAt: state.lastSyncAt,
|
||||
lastWeeklySyncAt: shared.lastWeeklySyncAt,
|
||||
lastSyncOrigin: state.lastSyncOrigin,
|
||||
syncWeeksAhead,
|
||||
autoSyncWeekly,
|
||||
};
|
||||
}
|
||||
|
||||
export async function handleGoogleCalendarConnect(): Promise<GoogleCalendarSyncResult> {
|
||||
return connectGoogleCalendar();
|
||||
}
|
||||
|
||||
export async function handleGoogleCalendarDisconnect(): Promise<{ success: boolean }> {
|
||||
await clearGoogleCalendarState();
|
||||
await ensureWeeklySyncAlarm();
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export async function handleGoogleCalendarStatus(): Promise<GoogleCalendarStatus> {
|
||||
return getGoogleCalendarStatus();
|
||||
}
|
||||
|
||||
export function registerGoogleCalendarMessageHandlers(
|
||||
handlers: Record<
|
||||
string,
|
||||
(
|
||||
request: unknown,
|
||||
sendResponse: (response?: unknown) => void,
|
||||
sender?: browser.Runtime.MessageSender,
|
||||
) => boolean | void
|
||||
>,
|
||||
isTrustedSender: (sender?: browser.Runtime.MessageSender) => boolean,
|
||||
): void {
|
||||
const rejectUntrusted = (
|
||||
sendResponse: (response?: unknown) => void,
|
||||
sender?: browser.Runtime.MessageSender,
|
||||
): boolean => {
|
||||
if (isTrustedSender(sender)) return false;
|
||||
sendResponse({ success: false, error: "Unauthorized sender" });
|
||||
return true;
|
||||
};
|
||||
|
||||
handlers.googleCalendarConnect = (_req, sendResponse, sender) => {
|
||||
if (rejectUntrusted(sendResponse, sender)) return false;
|
||||
void handleGoogleCalendarConnect()
|
||||
.then(sendResponse)
|
||||
.catch((err) => {
|
||||
sendResponse({
|
||||
success: false,
|
||||
error: err instanceof Error ? err.message : "Google sign-in failed",
|
||||
});
|
||||
});
|
||||
return true;
|
||||
};
|
||||
|
||||
handlers.googleCalendarDisconnect = (_req, sendResponse, sender) => {
|
||||
if (rejectUntrusted(sendResponse, sender)) return false;
|
||||
void handleGoogleCalendarDisconnect()
|
||||
.then(sendResponse)
|
||||
.catch((err) => {
|
||||
sendResponse({
|
||||
success: false,
|
||||
error: err instanceof Error ? err.message : "Disconnect failed",
|
||||
});
|
||||
});
|
||||
return true;
|
||||
};
|
||||
|
||||
handlers.googleCalendarStatus = (_req, sendResponse, sender) => {
|
||||
if (rejectUntrusted(sendResponse, sender)) return false;
|
||||
void handleGoogleCalendarStatus()
|
||||
.then(sendResponse)
|
||||
.catch(() => {
|
||||
sendResponse({ configured: isGoogleCalendarConfigured(), connected: false });
|
||||
});
|
||||
return true;
|
||||
};
|
||||
|
||||
handlers.googleCalendarGetAccessToken = (_req, sendResponse, sender) => {
|
||||
if (rejectUntrusted(sendResponse, sender)) return false;
|
||||
void getValidAccessToken()
|
||||
.then((accessToken) => sendResponse({ success: true, accessToken }))
|
||||
.catch((err) => {
|
||||
sendResponse({
|
||||
success: false,
|
||||
error: err instanceof Error ? err.message : "Token refresh failed",
|
||||
});
|
||||
});
|
||||
return true;
|
||||
};
|
||||
|
||||
handlers.googleCalendarEnsureWeeklyAlarm = (_req, sendResponse, sender) => {
|
||||
if (rejectUntrusted(sendResponse, sender)) return false;
|
||||
void ensureWeeklySyncAlarm()
|
||||
.then(() => sendResponse({ success: true }))
|
||||
.catch((err) => {
|
||||
sendResponse({
|
||||
success: false,
|
||||
error: err instanceof Error ? err.message : "Could not schedule weekly sync",
|
||||
});
|
||||
});
|
||||
return true;
|
||||
};
|
||||
|
||||
handlers.googleCalendarUpdateSyncSettings = (request, sendResponse, sender) => {
|
||||
if (rejectUntrusted(sendResponse, sender)) return false;
|
||||
void (async () => {
|
||||
const body = request as {
|
||||
syncWeeksAhead?: number;
|
||||
autoSyncWeekly?: boolean;
|
||||
};
|
||||
const patch: Record<string, unknown> = {};
|
||||
if (body.syncWeeksAhead != null) {
|
||||
patch.syncWeeksAhead = clampSyncWeeks(body.syncWeeksAhead);
|
||||
}
|
||||
if (body.autoSyncWeekly != null) {
|
||||
patch.autoSyncWeekly = !!body.autoSyncWeekly;
|
||||
}
|
||||
if (Object.keys(patch).length > 0) {
|
||||
await writeSharedCalendarSyncSettings(patch);
|
||||
}
|
||||
await ensureWeeklySyncAlarm();
|
||||
sendResponse({ success: true, ...(await getGoogleCalendarStatus()) });
|
||||
})().catch((err) => {
|
||||
sendResponse({
|
||||
success: false,
|
||||
error: err instanceof Error ? err.message : "Could not update sync settings",
|
||||
});
|
||||
});
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
export { initCalendarBackground as initGoogleCalendarBackground } from "./calendarWeekly";
|
||||
@@ -1,302 +0,0 @@
|
||||
import browser from "webextension-polyfill";
|
||||
import {
|
||||
OUTLOOK_AUTH_URL,
|
||||
OUTLOOK_CALENDAR_OAUTH_CALLBACK,
|
||||
OUTLOOK_CALENDAR_SCOPE,
|
||||
OUTLOOK_OAUTH_CLIENT_ID,
|
||||
isOutlookCalendarConfigured,
|
||||
outlookOAuthRedirectUriHint,
|
||||
} from "@/config/outlookCalendar";
|
||||
import {
|
||||
exchangeOutlookCodeViaAccounts,
|
||||
refreshOutlookTokenViaAccounts,
|
||||
} from "@/seqta/utils/outlookCalendar/accountsToken";
|
||||
import {
|
||||
clearOutlookCalendarState,
|
||||
readOutlookCalendarState,
|
||||
writeOutlookCalendarState,
|
||||
} from "@/seqta/utils/outlookCalendar/storage";
|
||||
import type { OutlookCalendarStatus } from "@/seqta/utils/outlookCalendar/types";
|
||||
import type { GoogleCalendarSyncResult } from "@/seqta/utils/googleCalendar/types";
|
||||
import { ensureWeeklySyncAlarm } from "./calendarWeekly";
|
||||
|
||||
function base64UrlEncode(buffer: ArrayBuffer): string {
|
||||
const bytes = new Uint8Array(buffer);
|
||||
let binary = "";
|
||||
for (const b of bytes) binary += String.fromCharCode(b);
|
||||
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
|
||||
}
|
||||
|
||||
async function sha256(input: string): Promise<ArrayBuffer> {
|
||||
const data = new TextEncoder().encode(input);
|
||||
return crypto.subtle.digest("SHA-256", data);
|
||||
}
|
||||
|
||||
function randomVerifier(): string {
|
||||
const bytes = new Uint8Array(32);
|
||||
crypto.getRandomValues(bytes);
|
||||
return base64UrlEncode(bytes.buffer);
|
||||
}
|
||||
|
||||
async function pkceChallenge(verifier: string): Promise<string> {
|
||||
return base64UrlEncode(await sha256(verifier));
|
||||
}
|
||||
|
||||
function parseRedirectCode(responseUrl: string): string {
|
||||
const url = new URL(responseUrl);
|
||||
const err = url.searchParams.get("error");
|
||||
if (err) throw new Error(url.searchParams.get("error_description") ?? err);
|
||||
const code = url.searchParams.get("code");
|
||||
if (!code) throw new Error("Microsoft sign-in did not return an authorization code.");
|
||||
return code;
|
||||
}
|
||||
|
||||
const OAUTH_CALLBACK_PREFIX = OUTLOOK_CALENDAR_OAUTH_CALLBACK;
|
||||
const OAUTH_TIMEOUT_MS = 10 * 60 * 1000;
|
||||
|
||||
function isOAuthCallbackUrl(url: string): boolean {
|
||||
if (!url.startsWith(OAUTH_CALLBACK_PREFIX)) return false;
|
||||
const parsed = new URL(url);
|
||||
return parsed.searchParams.has("code") || parsed.searchParams.has("error");
|
||||
}
|
||||
|
||||
function waitForOAuthCallback(authTabId: number): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const timeoutId = setTimeout(() => {
|
||||
cleanup();
|
||||
reject(new Error("Microsoft sign-in timed out. Close the tab and try again."));
|
||||
}, OAUTH_TIMEOUT_MS);
|
||||
|
||||
const cleanup = () => {
|
||||
clearTimeout(timeoutId);
|
||||
browser.tabs.onUpdated.removeListener(onUpdated);
|
||||
browser.tabs.onRemoved.removeListener(onRemoved);
|
||||
};
|
||||
|
||||
const finishFromUrl = (url: string, tabId: number) => {
|
||||
if (!isOAuthCallbackUrl(url)) return false;
|
||||
cleanup();
|
||||
void browser.tabs.remove(tabId).catch(() => {});
|
||||
resolve(url);
|
||||
return true;
|
||||
};
|
||||
|
||||
const onRemoved = (tabId: number) => {
|
||||
if (tabId !== authTabId) return;
|
||||
cleanup();
|
||||
reject(new Error("Microsoft sign-in was cancelled."));
|
||||
};
|
||||
|
||||
const onUpdated = (
|
||||
tabId: number,
|
||||
changeInfo: browser.Tabs.OnUpdatedChangeInfoType,
|
||||
tab: browser.Tabs.Tab,
|
||||
) => {
|
||||
if (tabId !== authTabId) return;
|
||||
const url =
|
||||
changeInfo.url ?? (changeInfo.status === "complete" ? tab.url : undefined);
|
||||
if (url) finishFromUrl(url, tabId);
|
||||
};
|
||||
|
||||
browser.tabs.onUpdated.addListener(onUpdated);
|
||||
browser.tabs.onRemoved.addListener(onRemoved);
|
||||
|
||||
void browser.tabs
|
||||
.get(authTabId)
|
||||
.then((tab) => {
|
||||
if (tab.url) finishFromUrl(tab.url, authTabId);
|
||||
})
|
||||
.catch(() => {});
|
||||
});
|
||||
}
|
||||
|
||||
async function openOAuthTab(authUrl: string): Promise<string> {
|
||||
const tab = await browser.tabs.create({ url: authUrl, active: true });
|
||||
if (tab.id === undefined) {
|
||||
throw new Error("Could not open Microsoft sign-in tab.");
|
||||
}
|
||||
return waitForOAuthCallback(tab.id);
|
||||
}
|
||||
|
||||
async function getValidAccessToken(): Promise<string> {
|
||||
const state = await readOutlookCalendarState();
|
||||
const now = Date.now();
|
||||
if (state.accessToken && state.expiresAt && state.expiresAt > now + 60_000) {
|
||||
return state.accessToken;
|
||||
}
|
||||
if (!state.refreshToken) {
|
||||
throw new Error("Not connected to Outlook Calendar.");
|
||||
}
|
||||
const refreshed = await refreshOutlookTokenViaAccounts(state.refreshToken);
|
||||
const expiresAt = refreshed.expires_in
|
||||
? Date.now() + refreshed.expires_in * 1000
|
||||
: Date.now() + 3_600_000;
|
||||
await writeOutlookCalendarState({
|
||||
accessToken: refreshed.access_token,
|
||||
refreshToken: refreshed.refresh_token ?? state.refreshToken,
|
||||
expiresAt,
|
||||
});
|
||||
return refreshed.access_token;
|
||||
}
|
||||
|
||||
async function connectOutlookCalendar(): Promise<GoogleCalendarSyncResult> {
|
||||
if (!isOutlookCalendarConfigured()) {
|
||||
return {
|
||||
success: false,
|
||||
configured: false,
|
||||
error: "Outlook Calendar is not configured in this extension build.",
|
||||
};
|
||||
}
|
||||
|
||||
const redirectUri = OUTLOOK_CALENDAR_OAUTH_CALLBACK;
|
||||
const verifier = randomVerifier();
|
||||
const challenge = await pkceChallenge(verifier);
|
||||
const authUrl = new URL(OUTLOOK_AUTH_URL);
|
||||
authUrl.searchParams.set("client_id", OUTLOOK_OAUTH_CLIENT_ID);
|
||||
authUrl.searchParams.set("response_type", "code");
|
||||
authUrl.searchParams.set("redirect_uri", redirectUri);
|
||||
authUrl.searchParams.set("scope", OUTLOOK_CALENDAR_SCOPE);
|
||||
authUrl.searchParams.set("response_mode", "query");
|
||||
authUrl.searchParams.set("prompt", "consent");
|
||||
authUrl.searchParams.set("code_challenge", challenge);
|
||||
authUrl.searchParams.set("code_challenge_method", "S256");
|
||||
|
||||
let responseUrl: string;
|
||||
try {
|
||||
responseUrl = await openOAuthTab(authUrl.toString());
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : "Microsoft sign-in failed";
|
||||
const mismatch =
|
||||
/redirect_uri|invalid_request|AADSTS50011/i.test(message) ||
|
||||
/redirect_uri|invalid_request|AADSTS50011/i.test(String(err));
|
||||
return {
|
||||
success: false,
|
||||
configured: true,
|
||||
error: mismatch
|
||||
? `Microsoft redirect URI mismatch. ${outlookOAuthRedirectUriHint()}`
|
||||
: message.includes("cancel")
|
||||
? "Microsoft sign-in was cancelled."
|
||||
: message,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const code = parseRedirectCode(responseUrl);
|
||||
const tokens = await exchangeOutlookCodeViaAccounts(code, redirectUri, verifier);
|
||||
const expiresAt = tokens.expires_in
|
||||
? Date.now() + tokens.expires_in * 1000
|
||||
: Date.now() + 3_600_000;
|
||||
|
||||
const existing = await readOutlookCalendarState();
|
||||
await writeOutlookCalendarState({
|
||||
accessToken: tokens.access_token,
|
||||
refreshToken: tokens.refresh_token ?? existing.refreshToken,
|
||||
expiresAt,
|
||||
connectedAt: Date.now(),
|
||||
});
|
||||
|
||||
await ensureWeeklySyncAlarm();
|
||||
|
||||
return { success: true, configured: true, connected: true };
|
||||
} catch (err) {
|
||||
return {
|
||||
success: false,
|
||||
configured: true,
|
||||
error: err instanceof Error ? err.message : "Microsoft sign-in failed",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function getOutlookCalendarStatus(): Promise<OutlookCalendarStatus> {
|
||||
const state = await readOutlookCalendarState();
|
||||
return {
|
||||
configured: isOutlookCalendarConfigured(),
|
||||
connected: !!(state.refreshToken || state.accessToken),
|
||||
lastSyncAt: state.lastSyncAt,
|
||||
lastSyncOrigin: state.lastSyncOrigin,
|
||||
};
|
||||
}
|
||||
|
||||
export async function handleOutlookCalendarConnect(): Promise<GoogleCalendarSyncResult> {
|
||||
return connectOutlookCalendar();
|
||||
}
|
||||
|
||||
export async function handleOutlookCalendarDisconnect(): Promise<{ success: boolean }> {
|
||||
await clearOutlookCalendarState();
|
||||
await ensureWeeklySyncAlarm();
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
export async function handleOutlookCalendarStatus(): Promise<OutlookCalendarStatus> {
|
||||
return getOutlookCalendarStatus();
|
||||
}
|
||||
|
||||
export function registerOutlookCalendarMessageHandlers(
|
||||
handlers: Record<
|
||||
string,
|
||||
(
|
||||
request: unknown,
|
||||
sendResponse: (response?: unknown) => void,
|
||||
sender?: browser.Runtime.MessageSender,
|
||||
) => boolean | void
|
||||
>,
|
||||
isTrustedSender: (sender?: browser.Runtime.MessageSender) => boolean,
|
||||
): void {
|
||||
const rejectUntrusted = (
|
||||
sendResponse: (response?: unknown) => void,
|
||||
sender?: browser.Runtime.MessageSender,
|
||||
): boolean => {
|
||||
if (isTrustedSender(sender)) return false;
|
||||
sendResponse({ success: false, error: "Unauthorized sender" });
|
||||
return true;
|
||||
};
|
||||
|
||||
handlers.outlookCalendarConnect = (_req, sendResponse, sender) => {
|
||||
if (rejectUntrusted(sendResponse, sender)) return false;
|
||||
void handleOutlookCalendarConnect()
|
||||
.then(sendResponse)
|
||||
.catch((err) => {
|
||||
sendResponse({
|
||||
success: false,
|
||||
error: err instanceof Error ? err.message : "Microsoft sign-in failed",
|
||||
});
|
||||
});
|
||||
return true;
|
||||
};
|
||||
|
||||
handlers.outlookCalendarDisconnect = (_req, sendResponse, sender) => {
|
||||
if (rejectUntrusted(sendResponse, sender)) return false;
|
||||
void handleOutlookCalendarDisconnect()
|
||||
.then(sendResponse)
|
||||
.catch((err) => {
|
||||
sendResponse({
|
||||
success: false,
|
||||
error: err instanceof Error ? err.message : "Disconnect failed",
|
||||
});
|
||||
});
|
||||
return true;
|
||||
};
|
||||
|
||||
handlers.outlookCalendarStatus = (_req, sendResponse, sender) => {
|
||||
if (rejectUntrusted(sendResponse, sender)) return false;
|
||||
void handleOutlookCalendarStatus()
|
||||
.then(sendResponse)
|
||||
.catch(() => {
|
||||
sendResponse({ configured: isOutlookCalendarConfigured(), connected: false });
|
||||
});
|
||||
return true;
|
||||
};
|
||||
|
||||
handlers.outlookCalendarGetAccessToken = (_req, sendResponse, sender) => {
|
||||
if (rejectUntrusted(sendResponse, sender)) return false;
|
||||
void getValidAccessToken()
|
||||
.then((accessToken) => sendResponse({ success: true, accessToken }))
|
||||
.catch((err) => {
|
||||
sendResponse({
|
||||
success: false,
|
||||
error: err instanceof Error ? err.message : "Token refresh failed",
|
||||
});
|
||||
});
|
||||
return true;
|
||||
};
|
||||
}
|
||||
@@ -1,8 +1,4 @@
|
||||
/**
|
||||
* Google Calendar OAuth — public client config (extension).
|
||||
* Client secret and token exchange live on accounts.betterseqta.org.
|
||||
* See docs/GOOGLE_CALENDAR_ACCOUNTS_CALLBACK.md
|
||||
*/
|
||||
/** Google Calendar OAuth — public client config. Token exchange is on accounts.betterseqta.org. */
|
||||
|
||||
const HARDCODED_GOOGLE_OAUTH_CLIENT_ID =
|
||||
"270834969641-f6t7jtpu6j0cemse8updj3rkos7nl0hf.apps.googleusercontent.com";
|
||||
@@ -15,9 +11,7 @@ export const GOOGLE_OAUTH_CLIENT_ID: string =
|
||||
|
||||
export const ACCOUNTS_BASE = "https://accounts.betterseqta.org";
|
||||
|
||||
/** Must match Google Console + accounts callback route exactly. */
|
||||
export const GOOGLE_CALENDAR_OAUTH_CALLBACK = `${ACCOUNTS_BASE}/auth/google/calendar/callback`;
|
||||
|
||||
export const GOOGLE_CALENDAR_TOKEN_URL = `${ACCOUNTS_BASE}/api/bsplus/google/calendar/token`;
|
||||
export const GOOGLE_CALENDAR_REFRESH_URL = `${ACCOUNTS_BASE}/api/bsplus/google/calendar/refresh`;
|
||||
|
||||
@@ -28,9 +22,7 @@ export const GOOGLE_CALENDAR_API = "https://www.googleapis.com/calendar/v3";
|
||||
|
||||
export const BSPLUS_GOOGLE_CALENDAR_EVENT_PROP = "bsplusSeqtaKey";
|
||||
|
||||
/** Default weeks of timetable to sync forward (from start of current week). */
|
||||
export const GOOGLE_CALENDAR_SYNC_WEEKS = 12;
|
||||
export const GOOGLE_CALENDAR_SYNC_WEEKS_DEFAULT = GOOGLE_CALENDAR_SYNC_WEEKS;
|
||||
export const GOOGLE_CALENDAR_SYNC_WEEKS_MIN = 1;
|
||||
export const GOOGLE_CALENDAR_SYNC_WEEKS_MAX = 52;
|
||||
|
||||
|
||||
@@ -1,13 +1,8 @@
|
||||
/**
|
||||
* Outlook Calendar OAuth — public client config (extension).
|
||||
* Client secret and token exchange live on accounts.betterseqta.org.
|
||||
* See docs/OUTLOOK_CALENDAR_ACCOUNTS_CALLBACK.md
|
||||
*/
|
||||
/** Outlook Calendar OAuth — public client config. Token exchange is on accounts.betterseqta.org. */
|
||||
|
||||
import { ACCOUNTS_BASE } from "@/config/googleCalendar";
|
||||
|
||||
const HARDCODED_OUTLOOK_OAUTH_CLIENT_ID =
|
||||
"0b55168c-916c-4323-8f67-b3dd30af3c9e";
|
||||
const HARDCODED_OUTLOOK_OAUTH_CLIENT_ID = "0b55168c-916c-4323-8f67-b3dd30af3c9e";
|
||||
|
||||
const envClientId =
|
||||
typeof __OUTLOOK_OAUTH_CLIENT_ID__ !== "undefined" ? __OUTLOOK_OAUTH_CLIENT_ID__ : "";
|
||||
@@ -15,13 +10,10 @@ const envClientId =
|
||||
export const OUTLOOK_OAUTH_CLIENT_ID: string =
|
||||
envClientId.trim() || HARDCODED_OUTLOOK_OAUTH_CLIENT_ID.trim();
|
||||
|
||||
/** Must match Azure app registration + accounts callback route exactly. */
|
||||
export const OUTLOOK_CALENDAR_OAUTH_CALLBACK = `${ACCOUNTS_BASE}/auth/microsoft/calendar/callback`;
|
||||
|
||||
export const OUTLOOK_CALENDAR_TOKEN_URL = `${ACCOUNTS_BASE}/api/bsplus/microsoft/calendar/token`;
|
||||
export const OUTLOOK_CALENDAR_REFRESH_URL = `${ACCOUNTS_BASE}/api/bsplus/microsoft/calendar/refresh`;
|
||||
|
||||
/** Delegated Graph scopes for create/update/delete calendar events. */
|
||||
export const OUTLOOK_CALENDAR_SCOPE = "offline_access Calendars.ReadWrite User.Read";
|
||||
|
||||
export const OUTLOOK_AUTH_URL =
|
||||
|
||||
@@ -1,18 +1,9 @@
|
||||
import browser from "webextension-polyfill";
|
||||
|
||||
/** Vite asset imports are often already absolute extension URLs in production bundles. */
|
||||
export function resolveExtensionAssetUrl(importedUrl: string): string {
|
||||
if (!importedUrl) return importedUrl;
|
||||
|
||||
const doublePrefix = importedUrl.match(
|
||||
/^((?:chrome|moz)-extension:\/\/[^/]+)\/\1\/(.+)$/,
|
||||
);
|
||||
if (doublePrefix) {
|
||||
return `${doublePrefix[1]}/${doublePrefix[2]}`;
|
||||
}
|
||||
|
||||
if (/^(chrome-extension|moz-extension|https?|data):/.test(importedUrl)) {
|
||||
return importedUrl;
|
||||
}
|
||||
return browser.runtime.getURL(importedUrl.replace(/^\/+/, ""));
|
||||
/** Resolve Vite asset imports to a usable extension URL. */
|
||||
export function resolveExtensionAssetUrl(url: string): string {
|
||||
const doubled = url.match(/^((?:chrome|moz)-extension:\/\/[^/]+)\/\1\/(.+)$/);
|
||||
if (doubled) return `${doubled[1]}/${doubled[2]}`;
|
||||
if (/^(?:chrome|moz)-extension:\/\/|https?:|data:/.test(url)) return url;
|
||||
return browser.runtime.getURL(url.replace(/^\/+/, ""));
|
||||
}
|
||||
|
||||
@@ -1,144 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { fade } from "svelte/transition";
|
||||
import { portalToBody } from "./calendarSyncPortal";
|
||||
|
||||
let {
|
||||
open = false,
|
||||
busy = false,
|
||||
providerLabel = "Google",
|
||||
onConfirm,
|
||||
onCancel,
|
||||
} = $props<{
|
||||
open?: boolean;
|
||||
busy?: boolean;
|
||||
providerLabel?: string;
|
||||
onConfirm: () => void | Promise<void>;
|
||||
onCancel: () => void;
|
||||
}>();
|
||||
</script>
|
||||
|
||||
{#if open}
|
||||
<div
|
||||
class="bsplus-cal-modal-backdrop"
|
||||
use:portalToBody
|
||||
onclick={(e) => {
|
||||
if (e.target === e.currentTarget && !busy) onCancel();
|
||||
}}
|
||||
onkeydown={(e) => {
|
||||
if (e.key === "Escape" && !busy) onCancel();
|
||||
}}
|
||||
role="presentation"
|
||||
transition:fade={{ duration: 150 }}
|
||||
>
|
||||
<div
|
||||
class="bsplus-cal-modal"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-labelledby="bsplus-cal-delete-title"
|
||||
transition:fade={{ duration: 180 }}
|
||||
>
|
||||
<h2 id="bsplus-cal-delete-title" class="bsplus-cal-modal-title">
|
||||
Delete synced classes?
|
||||
</h2>
|
||||
<p class="bsplus-cal-modal-body">
|
||||
Removes every BetterSEQTA+ timetable event from your {providerLabel} Calendar for this school.
|
||||
Your account stays connected — use Update calendar to sync again.
|
||||
</p>
|
||||
<div class="bsplus-cal-modal-actions">
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-btn bsplus-cal-btn--ghost"
|
||||
disabled={busy}
|
||||
onclick={onCancel}
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-btn bsplus-cal-btn--danger"
|
||||
disabled={busy}
|
||||
onclick={() => void onConfirm()}
|
||||
>
|
||||
{busy ? "Deleting…" : "Delete synced classes"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<style>
|
||||
.bsplus-cal-modal-backdrop {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: var(--bsplus-cal-z-modal, 2147483647);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 16px;
|
||||
background: rgba(0, 0, 0, 0.5);
|
||||
backdrop-filter: blur(4px);
|
||||
}
|
||||
|
||||
.bsplus-cal-modal {
|
||||
width: min(100%, 400px);
|
||||
padding: 20px;
|
||||
border-radius: 16px;
|
||||
background: var(--bsplus-cal-surface, #fff);
|
||||
color: var(--bsplus-cal-text, #111);
|
||||
box-shadow: 0 24px 48px rgba(0, 0, 0, 0.22);
|
||||
border: 1px solid color-mix(in srgb, var(--bsplus-cal-text, #111) 12%, transparent);
|
||||
}
|
||||
|
||||
.bsplus-cal-modal-title {
|
||||
margin: 0 0 8px;
|
||||
font-size: 18px;
|
||||
font-weight: 700;
|
||||
line-height: 1.3;
|
||||
}
|
||||
|
||||
.bsplus-cal-modal-body {
|
||||
margin: 0 0 20px;
|
||||
font-size: 14px;
|
||||
line-height: 1.5;
|
||||
color: color-mix(in srgb, var(--bsplus-cal-text, #111) 72%, transparent);
|
||||
}
|
||||
|
||||
.bsplus-cal-modal-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.bsplus-cal-btn {
|
||||
padding: 8px 14px;
|
||||
border: none;
|
||||
border-radius: 10px;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
}
|
||||
|
||||
.bsplus-cal-btn:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.bsplus-cal-btn--ghost {
|
||||
background: color-mix(in srgb, var(--bsplus-cal-text, #111) 8%, transparent);
|
||||
color: var(--bsplus-cal-text, #111);
|
||||
}
|
||||
|
||||
.bsplus-cal-btn--ghost:hover:not(:disabled) {
|
||||
background: color-mix(in srgb, var(--bsplus-cal-text, #111) 14%, transparent);
|
||||
}
|
||||
|
||||
.bsplus-cal-btn--danger {
|
||||
background: #dc2626;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.bsplus-cal-btn--danger:hover:not(:disabled) {
|
||||
background: #b91c1c;
|
||||
}
|
||||
</style>
|
||||
@@ -1,144 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { fade } from "svelte/transition";
|
||||
import { portalToBody } from "./calendarSyncPortal";
|
||||
|
||||
let {
|
||||
open = false,
|
||||
busy = false,
|
||||
providerLabel = "Google",
|
||||
onConfirm,
|
||||
onCancel,
|
||||
} = $props<{
|
||||
open?: boolean;
|
||||
busy?: boolean;
|
||||
providerLabel?: string;
|
||||
onConfirm: () => void | Promise<void>;
|
||||
onCancel: () => void;
|
||||
}>();
|
||||
</script>
|
||||
|
||||
{#if open}
|
||||
<div
|
||||
class="bsplus-cal-modal-backdrop"
|
||||
use:portalToBody
|
||||
onclick={(e) => {
|
||||
if (e.target === e.currentTarget && !busy) onCancel();
|
||||
}}
|
||||
onkeydown={(e) => {
|
||||
if (e.key === "Escape" && !busy) onCancel();
|
||||
}}
|
||||
role="presentation"
|
||||
transition:fade={{ duration: 150 }}
|
||||
>
|
||||
<div
|
||||
class="bsplus-cal-modal"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-labelledby="bsplus-cal-disconnect-title"
|
||||
transition:fade={{ duration: 180 }}
|
||||
>
|
||||
<h2 id="bsplus-cal-disconnect-title" class="bsplus-cal-modal-title">
|
||||
Disconnect {providerLabel} Calendar?
|
||||
</h2>
|
||||
<p class="bsplus-cal-modal-body">
|
||||
Stops BetterSEQTA+ from updating your calendar. Synced classes stay in {providerLabel} Calendar
|
||||
until you delete them or connect again.
|
||||
</p>
|
||||
<div class="bsplus-cal-modal-actions">
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-btn bsplus-cal-btn--ghost"
|
||||
disabled={busy}
|
||||
onclick={onCancel}
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-btn bsplus-cal-btn--danger"
|
||||
disabled={busy}
|
||||
onclick={() => void onConfirm()}
|
||||
>
|
||||
{busy ? "Disconnecting…" : "Disconnect account"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<style>
|
||||
.bsplus-cal-modal-backdrop {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: var(--bsplus-cal-z-modal, 2147483647);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 16px;
|
||||
background: rgba(0, 0, 0, 0.5);
|
||||
backdrop-filter: blur(4px);
|
||||
}
|
||||
|
||||
.bsplus-cal-modal {
|
||||
width: min(100%, 400px);
|
||||
padding: 20px;
|
||||
border-radius: 16px;
|
||||
background: var(--bsplus-cal-surface, #fff);
|
||||
color: var(--bsplus-cal-text, #111);
|
||||
box-shadow: 0 24px 48px rgba(0, 0, 0, 0.22);
|
||||
border: 1px solid color-mix(in srgb, var(--bsplus-cal-text, #111) 12%, transparent);
|
||||
}
|
||||
|
||||
.bsplus-cal-modal-title {
|
||||
margin: 0 0 8px;
|
||||
font-size: 18px;
|
||||
font-weight: 700;
|
||||
line-height: 1.3;
|
||||
}
|
||||
|
||||
.bsplus-cal-modal-body {
|
||||
margin: 0 0 20px;
|
||||
font-size: 14px;
|
||||
line-height: 1.5;
|
||||
color: color-mix(in srgb, var(--bsplus-cal-text, #111) 72%, transparent);
|
||||
}
|
||||
|
||||
.bsplus-cal-modal-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.bsplus-cal-btn {
|
||||
padding: 8px 14px;
|
||||
border: none;
|
||||
border-radius: 10px;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
}
|
||||
|
||||
.bsplus-cal-btn:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.bsplus-cal-btn--ghost {
|
||||
background: color-mix(in srgb, var(--bsplus-cal-text, #111) 8%, transparent);
|
||||
color: var(--bsplus-cal-text, #111);
|
||||
}
|
||||
|
||||
.bsplus-cal-btn--ghost:hover:not(:disabled) {
|
||||
background: color-mix(in srgb, var(--bsplus-cal-text, #111) 14%, transparent);
|
||||
}
|
||||
|
||||
.bsplus-cal-btn--danger {
|
||||
background: #dc2626;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.bsplus-cal-btn--danger:hover:not(:disabled) {
|
||||
background: #b91c1c;
|
||||
}
|
||||
</style>
|
||||
@@ -1,5 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from "svelte";
|
||||
import type { Snippet } from "svelte";
|
||||
import { fade, fly } from "svelte/transition";
|
||||
import browser from "webextension-polyfill";
|
||||
import {
|
||||
@@ -7,40 +8,41 @@
|
||||
GOOGLE_CALENDAR_SYNC_WEEKS_MIN,
|
||||
} from "@/config/googleCalendar";
|
||||
import { maybeRunDueWeeklySync } from "@/seqta/utils/googleCalendar/calendarSyncListener";
|
||||
import { deleteSyncedEventsFromGoogleCalendar } from "@/seqta/utils/googleCalendar/syncEngine";
|
||||
import {
|
||||
formatOutlookSyncResultMessage,
|
||||
runOutlookCalendarSync,
|
||||
} from "@/seqta/utils/outlookCalendar/syncRunner";
|
||||
import {
|
||||
formatSyncResultMessage,
|
||||
runGoogleCalendarSync,
|
||||
} from "@/seqta/utils/googleCalendar/syncRunner";
|
||||
deleteSyncedEventsFromGoogleCalendar,
|
||||
deleteSyncedEventsFromOutlookCalendar,
|
||||
} from "@/seqta/utils/calendarSync/syncEngine";
|
||||
import { formatLessonSyncResultMessage } from "@/seqta/utils/calendarSync/lessonSyncShared";
|
||||
import { runGoogleCalendarSync, runOutlookCalendarSync } from "@/seqta/utils/calendarSync/syncRunner";
|
||||
import type {
|
||||
GoogleCalendarStatus,
|
||||
GoogleCalendarSyncProgress,
|
||||
GoogleCalendarSyncResult,
|
||||
} from "@/seqta/utils/googleCalendar/types";
|
||||
import type { OutlookCalendarStatus } from "@/seqta/utils/outlookCalendar/types";
|
||||
import { deleteSyncedEventsFromOutlookCalendar } from "@/seqta/utils/outlookCalendar/syncEngine";
|
||||
import CalendarDeleteEventsModal from "./CalendarDeleteEventsModal.svelte";
|
||||
import CalendarDisconnectModal from "./CalendarDisconnectModal.svelte";
|
||||
import type { OutlookCalendarStatus } from "@/seqta/utils/outlookCalendar/storage";
|
||||
import OutlookCalendarIcon from "./OutlookCalendarIcon.svelte";
|
||||
import { settingsState } from "@/seqta/utils/listeners/SettingsState";
|
||||
import { syncCalendarSyncTheme } from "./calendarSyncTheme";
|
||||
import { isCalendarSyncModalTarget, portalToBody } from "./calendarSyncPortal";
|
||||
|
||||
function syncProgressPercent(progress: GoogleCalendarSyncProgress | null): number {
|
||||
if (!progress || progress.phase === "done") return 0;
|
||||
if (progress.total > 0) {
|
||||
return Math.min(100, Math.round((progress.current / progress.total) * 100));
|
||||
}
|
||||
return progress.phase === "preparing" ? 8 : 0;
|
||||
}
|
||||
|
||||
type CalendarProvider = "google" | "outlook";
|
||||
import {
|
||||
formatLastSync,
|
||||
isCalendarSyncModalTarget,
|
||||
portalToBody,
|
||||
providerLabel as calendarProviderLabel,
|
||||
syncCalendarSyncTheme,
|
||||
syncProgressPercent,
|
||||
type CalendarProvider,
|
||||
} from "./calendarSyncUi";
|
||||
type BusyPhase = "connect" | "sync" | "delete" | "disconnect" | null;
|
||||
type BusyState = { provider: CalendarProvider; phase: BusyPhase } | null;
|
||||
type ProviderStatus = { configured: boolean; connected: boolean; lastSyncAt?: number };
|
||||
|
||||
function setProviderStatus(provider: CalendarProvider, patch: Partial<ProviderStatus>) {
|
||||
if (provider === "google") googleStatus = { ...googleStatus, ...patch };
|
||||
else outlookStatus = { ...outlookStatus, ...patch };
|
||||
}
|
||||
|
||||
function providerStatus(provider: CalendarProvider): ProviderStatus {
|
||||
return provider === "google" ? googleStatus : outlookStatus;
|
||||
}
|
||||
|
||||
let googleStatus = $state<GoogleCalendarStatus>({ configured: true, connected: false });
|
||||
let outlookStatus = $state<OutlookCalendarStatus>({ configured: true, connected: false });
|
||||
@@ -62,6 +64,11 @@
|
||||
|
||||
const isBusy = $derived(busy !== null);
|
||||
const anyConnected = $derived(googleStatus.connected || outlookStatus.connected);
|
||||
const modalOpen = $derived(showDisconnect || showDeleteEvents);
|
||||
const modalBusy = $derived(
|
||||
showDisconnect ? busy?.phase === "disconnect" : busy?.phase === "delete",
|
||||
);
|
||||
const providerLabel = $derived(calendarProviderLabel(modalProvider ?? "google"));
|
||||
const showTriggerProgress = $derived(
|
||||
isBusy &&
|
||||
(busy?.phase === "sync" ||
|
||||
@@ -96,14 +103,22 @@
|
||||
});
|
||||
const accent = "var(--bsplus-cal-accent, var(--better-main, #3b82f6))";
|
||||
|
||||
function isProviderBusy(provider: CalendarProvider): boolean {
|
||||
return busy?.provider === provider;
|
||||
}
|
||||
|
||||
function providerPhase(provider: CalendarProvider): BusyPhase {
|
||||
return busy?.provider === provider ? busy.phase : null;
|
||||
}
|
||||
|
||||
function providerStatusText(status: ProviderStatus, notConfigured: string): string {
|
||||
if (!status.configured) return notConfigured;
|
||||
if (!status.connected) return "Not connected";
|
||||
const lastSync = formatLastSync(status.lastSyncAt);
|
||||
return lastSync ? `Connected · ${lastSync}` : "Connected";
|
||||
}
|
||||
|
||||
function closeModal() {
|
||||
if (showDisconnect && busy?.phase !== "disconnect") showDisconnect = false;
|
||||
if (showDeleteEvents && busy?.phase !== "delete") showDeleteEvents = false;
|
||||
}
|
||||
|
||||
function showToastMessage(message: string, isError = false) {
|
||||
toast = { message, error: isError };
|
||||
if (toastTimer) clearTimeout(toastTimer);
|
||||
@@ -159,7 +174,11 @@
|
||||
mode: "full" | "incremental" = "full",
|
||||
): Promise<boolean> {
|
||||
const run = provider === "google" ? runGoogleCalendarSync : runOutlookCalendarSync;
|
||||
const format = provider === "google" ? formatSyncResultMessage : formatOutlookSyncResultMessage;
|
||||
const format = (result: GoogleCalendarSyncResult) =>
|
||||
formatLessonSyncResultMessage(
|
||||
result,
|
||||
`${calendarProviderLabel(provider)} Calendar`,
|
||||
);
|
||||
|
||||
const result = await run({ mode, onProgress: handleSyncProgress });
|
||||
syncProgress = null;
|
||||
@@ -169,26 +188,17 @@
|
||||
return false;
|
||||
}
|
||||
|
||||
if (provider === "google") {
|
||||
googleStatus = {
|
||||
...googleStatus,
|
||||
setProviderStatus(provider, {
|
||||
connected: true,
|
||||
lastSyncAt: result.lastSyncAt ?? googleStatus.lastSyncAt,
|
||||
};
|
||||
} else {
|
||||
outlookStatus = {
|
||||
...outlookStatus,
|
||||
connected: true,
|
||||
lastSyncAt: result.lastSyncAt ?? outlookStatus.lastSyncAt,
|
||||
};
|
||||
}
|
||||
lastSyncAt: result.lastSyncAt ?? providerStatus(provider).lastSyncAt,
|
||||
});
|
||||
|
||||
showToastMessage(format(result));
|
||||
return true;
|
||||
}
|
||||
|
||||
async function connectProvider(provider: CalendarProvider) {
|
||||
const status = provider === "google" ? googleStatus : outlookStatus;
|
||||
const status = providerStatus(provider);
|
||||
if (!status.configured || isBusy) return;
|
||||
menuOpen = false;
|
||||
busy = { provider, phase: "connect" };
|
||||
@@ -199,15 +209,13 @@
|
||||
type: connectType,
|
||||
})) as GoogleCalendarSyncResult;
|
||||
if (!result.success) {
|
||||
const label = provider === "google" ? "Google" : "Outlook";
|
||||
showToastMessage(result.error ?? `Could not connect to ${label} Calendar.`, true);
|
||||
showToastMessage(
|
||||
result.error ?? `Could not connect to ${calendarProviderLabel(provider)} Calendar.`,
|
||||
true,
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (provider === "google") {
|
||||
googleStatus = { ...googleStatus, connected: true };
|
||||
} else {
|
||||
outlookStatus = { ...outlookStatus, connected: true };
|
||||
}
|
||||
setProviderStatus(provider, { connected: true });
|
||||
busy = { provider, phase: "sync" };
|
||||
await performSync(provider);
|
||||
} catch (err) {
|
||||
@@ -219,7 +227,7 @@
|
||||
}
|
||||
|
||||
async function syncProvider(provider: CalendarProvider) {
|
||||
const status = provider === "google" ? googleStatus : outlookStatus;
|
||||
const status = providerStatus(provider);
|
||||
if (!status.configured || isBusy) return;
|
||||
if (!status.connected) {
|
||||
await connectProvider(provider);
|
||||
@@ -266,7 +274,7 @@
|
||||
|
||||
const removed = result.deleted ?? 0;
|
||||
modalProvider = null;
|
||||
const label = provider === "google" ? "Google" : "Outlook";
|
||||
const label = calendarProviderLabel(provider);
|
||||
if (removed === 0) {
|
||||
showToastMessage("No synced events to remove.");
|
||||
} else {
|
||||
@@ -298,7 +306,7 @@
|
||||
busy = { provider, phase: "disconnect" };
|
||||
const disconnectType =
|
||||
provider === "google" ? "googleCalendarDisconnect" : "outlookCalendarDisconnect";
|
||||
const label = provider === "google" ? "Google" : "Outlook";
|
||||
const label = calendarProviderLabel(provider);
|
||||
try {
|
||||
const result = (await browser.runtime.sendMessage({
|
||||
type: disconnectType,
|
||||
@@ -307,11 +315,7 @@
|
||||
showToastMessage(`Could not disconnect ${label} Calendar.`, true);
|
||||
return;
|
||||
}
|
||||
if (provider === "google") {
|
||||
googleStatus = { ...googleStatus, connected: false, lastSyncAt: undefined };
|
||||
} else {
|
||||
outlookStatus = { ...outlookStatus, connected: false, lastSyncAt: undefined };
|
||||
}
|
||||
setProviderStatus(provider, { connected: false, lastSyncAt: undefined });
|
||||
showDisconnect = false;
|
||||
menuOpen = false;
|
||||
modalProvider = null;
|
||||
@@ -323,56 +327,30 @@
|
||||
}
|
||||
}
|
||||
|
||||
function openDeleteModal(provider: CalendarProvider) {
|
||||
function openModal(provider: CalendarProvider, kind: "delete" | "disconnect") {
|
||||
if (isBusy) return;
|
||||
modalProvider = provider;
|
||||
menuOpen = false;
|
||||
showDeleteEvents = true;
|
||||
}
|
||||
|
||||
function openDisconnectModal(provider: CalendarProvider) {
|
||||
if (isBusy) return;
|
||||
modalProvider = provider;
|
||||
menuOpen = false;
|
||||
showDisconnect = true;
|
||||
showDisconnect = kind === "disconnect";
|
||||
showDeleteEvents = kind === "delete";
|
||||
}
|
||||
|
||||
function toggleMenu() {
|
||||
menuOpen = !menuOpen;
|
||||
if (menuOpen) {
|
||||
queueMicrotask(() => syncMenuTheme());
|
||||
}
|
||||
}
|
||||
|
||||
function formatLastSync(ts?: number): string | null {
|
||||
if (!ts) return null;
|
||||
const diff = Date.now() - ts;
|
||||
if (diff < 60_000) return "Synced just now";
|
||||
if (diff < 3_600_000) return `Synced ${Math.floor(diff / 60_000)}m ago`;
|
||||
if (diff < 86_400_000) return `Synced ${Math.floor(diff / 3_600_000)}h ago`;
|
||||
return `Synced ${new Date(ts).toLocaleDateString()}`;
|
||||
if (menuOpen) queueMicrotask(syncHostTheme);
|
||||
}
|
||||
|
||||
function updateMenuPosition() {
|
||||
if (!triggerEl) return;
|
||||
const rect = triggerEl.getBoundingClientRect();
|
||||
menuStyle = `top:${rect.bottom + 8}px;right:${window.innerWidth - rect.right}px;`;
|
||||
syncMenuTheme();
|
||||
syncHostTheme();
|
||||
}
|
||||
|
||||
function syncMenuTheme() {
|
||||
if (!menuEl) return;
|
||||
syncCalendarSyncTheme(menuEl);
|
||||
}
|
||||
|
||||
function syncMountedTheme() {
|
||||
const themeHost = rootEl?.closest(".bsplus-calendar-sync-mount") as HTMLElement | null;
|
||||
if (themeHost) syncCalendarSyncTheme(themeHost);
|
||||
if (menuOpen) syncMenuTheme();
|
||||
}
|
||||
|
||||
function portalMenu(node: HTMLElement) {
|
||||
return portalToBody(node);
|
||||
function syncHostTheme() {
|
||||
const themeHost = rootEl?.closest(".bsplus-calendar-sync-mount");
|
||||
if (themeHost instanceof HTMLElement) syncCalendarSyncTheme(themeHost);
|
||||
if (menuEl) syncCalendarSyncTheme(menuEl);
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
@@ -382,7 +360,7 @@
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
if (menuOpen && menuEl) syncMenuTheme();
|
||||
if (menuOpen && menuEl) syncHostTheme();
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
@@ -413,7 +391,7 @@
|
||||
"adaptiveThemeGradient",
|
||||
"selectedTheme",
|
||||
] as const;
|
||||
const onThemeChange = () => syncMountedTheme();
|
||||
const onThemeChange = () => syncHostTheme();
|
||||
for (const key of themeKeys) {
|
||||
settingsState.register(key, onThemeChange);
|
||||
}
|
||||
@@ -445,6 +423,72 @@
|
||||
});
|
||||
</script>
|
||||
|
||||
{#snippet providerPanel(
|
||||
provider: CalendarProvider,
|
||||
status: ProviderStatus,
|
||||
notConfiguredMsg: string,
|
||||
icon: Snippet,
|
||||
name: Snippet,
|
||||
)}
|
||||
<div class="bsplus-cal-provider" role="none">
|
||||
<div class="bsplus-cal-provider-row">
|
||||
<span class="bsplus-cal-provider-icon" aria-hidden="true">
|
||||
{@render icon()}
|
||||
</span>
|
||||
<div class="bsplus-cal-provider-copy">
|
||||
<span class="bsplus-cal-provider-name">{@render name()}</span>
|
||||
<span class="bsplus-cal-provider-status">
|
||||
{providerStatusText(status, notConfiguredMsg)}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bsplus-cal-provider-actions">
|
||||
{#if !status.connected}
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-action bsplus-cal-action--primary"
|
||||
style:--bsplus-cal-accent={accent}
|
||||
role="menuitem"
|
||||
disabled={!status.configured || isBusy}
|
||||
onclick={() => void connectProvider(provider)}
|
||||
>
|
||||
{providerPhase(provider) === "connect" ? "Connecting…" : "Connect & sync"}
|
||||
</button>
|
||||
{:else}
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-action bsplus-cal-action--primary"
|
||||
style:--bsplus-cal-accent={accent}
|
||||
role="menuitem"
|
||||
disabled={isBusy}
|
||||
onclick={() => void syncProvider(provider)}
|
||||
>
|
||||
{providerPhase(provider) === "sync" ? "Updating…" : "Update calendar"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-action bsplus-cal-action--ghost"
|
||||
role="menuitem"
|
||||
disabled={isBusy}
|
||||
onclick={() => openModal(provider, "delete")}
|
||||
>
|
||||
{providerPhase(provider) === "delete" ? "Deleting…" : "Delete synced classes"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-action bsplus-cal-action--ghost"
|
||||
role="menuitem"
|
||||
disabled={isBusy}
|
||||
onclick={() => openModal(provider, "disconnect")}
|
||||
>
|
||||
Disconnect account
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{/snippet}
|
||||
|
||||
<div class="bsplus-cal-sync" bind:this={rootEl}>
|
||||
<button
|
||||
type="button"
|
||||
@@ -473,7 +517,7 @@
|
||||
role="menu"
|
||||
bind:this={menuEl}
|
||||
style={menuStyle}
|
||||
use:portalMenu
|
||||
use:portalToBody
|
||||
transition:fly={{ y: -6, duration: 160 }}
|
||||
>
|
||||
<div class="bsplus-cal-menu-header">
|
||||
@@ -481,155 +525,20 @@
|
||||
<span class="bsplus-cal-menu-sub">Copy your SEQTA timetable classes to Google or Outlook</span>
|
||||
</div>
|
||||
|
||||
<div class="bsplus-cal-provider" role="none">
|
||||
<div class="bsplus-cal-provider-row">
|
||||
<span class="bsplus-cal-provider-icon" aria-hidden="true">
|
||||
<svg viewBox="0 0 24 24">
|
||||
<path
|
||||
fill="#4285F4"
|
||||
d="M22 12c0-.96-.08-1.88-.24-2.76H12v5.22h5.68c-.24 1.28-.96 2.44-2.04 3.18v2.64h3.3c1.92-1.76 3.06-4.36 3.06-7.28z"
|
||||
/>
|
||||
<path
|
||||
fill="#34A853"
|
||||
d="M12 22c2.76 0 5.08-.92 6.78-2.5l-3.3-2.64c-.92.62-2.1.98-3.48.98-2.68 0-4.96-1.8-5.78-4.22H2.18v2.72A10 10 0 0 0 12 22z"
|
||||
/>
|
||||
<path
|
||||
fill="#FBBC05"
|
||||
d="M6.22 13.62A5.98 5.98 0 0 1 5.82 12c0-.56.1-1.1.28-1.62V7.66H2.18A10 10 0 0 0 2 12c0 1.62.38 3.16 1.06 4.52l3.16-2.9z"
|
||||
/>
|
||||
<path
|
||||
fill="#EA4335"
|
||||
d="M12 5.38c1.5 0 2.84.52 3.9 1.54l2.92-2.92C17.08 2.34 14.76 1.2 12 1.2 7.54 1.2 3.72 3.94 2.18 7.66l4.04 3.14c.82-2.42 3.1-4.22 5.78-4.22z"
|
||||
/>
|
||||
</svg>
|
||||
</span>
|
||||
<div class="bsplus-cal-provider-copy">
|
||||
<span class="bsplus-cal-provider-name">
|
||||
<span class="bsplus-google-word bsplus-google-word--sm">
|
||||
<span class="bsplus-google-g">G</span><span class="bsplus-google-o1">o</span><span class="bsplus-google-o2">o</span><span class="bsplus-google-g2">g</span><span class="bsplus-google-l">l</span><span class="bsplus-google-e">e</span>
|
||||
</span>
|
||||
<span> Calendar</span>
|
||||
</span>
|
||||
<span class="bsplus-cal-provider-status">
|
||||
{#if !googleStatus.configured}
|
||||
Not available in this build
|
||||
{:else if googleStatus.connected}
|
||||
Connected{formatLastSync(googleStatus.lastSyncAt) ? ` · ${formatLastSync(googleStatus.lastSyncAt)}` : ""}
|
||||
{:else}
|
||||
Not connected
|
||||
{/if}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bsplus-cal-provider-actions">
|
||||
{#if !googleStatus.connected}
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-action bsplus-cal-action--primary"
|
||||
style:--bsplus-cal-accent={accent}
|
||||
role="menuitem"
|
||||
disabled={!googleStatus.configured || isBusy}
|
||||
onclick={() => void connectProvider("google")}
|
||||
>
|
||||
{providerPhase("google") === "connect" ? "Connecting…" : "Connect & sync"}
|
||||
</button>
|
||||
{:else}
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-action bsplus-cal-action--primary"
|
||||
style:--bsplus-cal-accent={accent}
|
||||
role="menuitem"
|
||||
disabled={isBusy}
|
||||
onclick={() => void syncProvider("google")}
|
||||
>
|
||||
{providerPhase("google") === "sync" ? "Updating…" : "Update calendar"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-action bsplus-cal-action--ghost"
|
||||
role="menuitem"
|
||||
disabled={isBusy}
|
||||
onclick={() => openDeleteModal("google")}
|
||||
>
|
||||
{providerPhase("google") === "delete" ? "Deleting…" : "Delete synced classes"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-action bsplus-cal-action--ghost"
|
||||
role="menuitem"
|
||||
disabled={isBusy}
|
||||
onclick={() => openDisconnectModal("google")}
|
||||
>
|
||||
Disconnect account
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bsplus-cal-provider" role="none">
|
||||
<div class="bsplus-cal-provider-row">
|
||||
<span class="bsplus-cal-provider-icon" aria-hidden="true">
|
||||
<OutlookCalendarIcon />
|
||||
</span>
|
||||
<div class="bsplus-cal-provider-copy">
|
||||
<span class="bsplus-cal-provider-name">Outlook Calendar</span>
|
||||
<span class="bsplus-cal-provider-status">
|
||||
{#if !outlookStatus.configured}
|
||||
Set OUTLOOK_OAUTH_CLIENT_ID to enable
|
||||
{:else if outlookStatus.connected}
|
||||
Connected{formatLastSync(outlookStatus.lastSyncAt) ? ` · ${formatLastSync(outlookStatus.lastSyncAt)}` : ""}
|
||||
{:else}
|
||||
Not connected
|
||||
{/if}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="bsplus-cal-provider-actions">
|
||||
{#if !outlookStatus.connected}
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-action bsplus-cal-action--primary"
|
||||
style:--bsplus-cal-accent={accent}
|
||||
role="menuitem"
|
||||
disabled={!outlookStatus.configured || isBusy}
|
||||
onclick={() => void connectProvider("outlook")}
|
||||
>
|
||||
{providerPhase("outlook") === "connect" ? "Connecting…" : "Connect & sync"}
|
||||
</button>
|
||||
{:else}
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-action bsplus-cal-action--primary"
|
||||
style:--bsplus-cal-accent={accent}
|
||||
role="menuitem"
|
||||
disabled={isBusy}
|
||||
onclick={() => void syncProvider("outlook")}
|
||||
>
|
||||
{providerPhase("outlook") === "sync" ? "Updating…" : "Update calendar"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-action bsplus-cal-action--ghost"
|
||||
role="menuitem"
|
||||
disabled={isBusy}
|
||||
onclick={() => openDeleteModal("outlook")}
|
||||
>
|
||||
{providerPhase("outlook") === "delete" ? "Deleting…" : "Delete synced classes"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-action bsplus-cal-action--ghost"
|
||||
role="menuitem"
|
||||
disabled={isBusy}
|
||||
onclick={() => openDisconnectModal("outlook")}
|
||||
>
|
||||
Disconnect account
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{@render providerPanel(
|
||||
"google",
|
||||
googleStatus,
|
||||
"Not available in this build",
|
||||
googleIcon,
|
||||
googleName,
|
||||
)}
|
||||
{@render providerPanel(
|
||||
"outlook",
|
||||
outlookStatus,
|
||||
"Set OUTLOOK_OAUTH_CLIENT_ID to enable",
|
||||
outlookIcon,
|
||||
outlookName,
|
||||
)}
|
||||
|
||||
{#if anyConnected}
|
||||
<div class="bsplus-cal-settings" role="group" aria-label="Sync options">
|
||||
@@ -670,25 +579,82 @@
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<CalendarDeleteEventsModal
|
||||
open={showDeleteEvents}
|
||||
busy={busy?.phase === "delete"}
|
||||
providerLabel={modalProvider === "outlook" ? "Outlook" : "Google"}
|
||||
onCancel={() => {
|
||||
if (busy?.phase !== "delete") showDeleteEvents = false;
|
||||
{#if modalOpen}
|
||||
<div
|
||||
class="bsplus-cal-modal-backdrop"
|
||||
use:portalToBody
|
||||
onclick={(e) => {
|
||||
if (e.target === e.currentTarget && !modalBusy) closeModal();
|
||||
}}
|
||||
onConfirm={confirmDeleteEvents}
|
||||
/>
|
||||
|
||||
<CalendarDisconnectModal
|
||||
open={showDisconnect}
|
||||
busy={busy?.phase === "disconnect"}
|
||||
providerLabel={modalProvider === "outlook" ? "Outlook" : "Google"}
|
||||
onCancel={() => {
|
||||
if (busy?.phase !== "disconnect") showDisconnect = false;
|
||||
onkeydown={(e) => {
|
||||
if (e.key === "Escape" && !modalBusy) closeModal();
|
||||
}}
|
||||
onConfirm={confirmDisconnect}
|
||||
/>
|
||||
role="presentation"
|
||||
transition:fade={{ duration: 150 }}
|
||||
>
|
||||
<div
|
||||
class="bsplus-cal-modal"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-labelledby="bsplus-cal-modal-title"
|
||||
transition:fade={{ duration: 180 }}
|
||||
>
|
||||
{#if showDisconnect}
|
||||
<h2 id="bsplus-cal-modal-title" class="bsplus-cal-modal-title">
|
||||
Disconnect {providerLabel} Calendar?
|
||||
</h2>
|
||||
<p class="bsplus-cal-modal-body">
|
||||
Stops BetterSEQTA+ from updating your calendar. Synced classes stay in {providerLabel} Calendar
|
||||
until you delete them or connect again.
|
||||
</p>
|
||||
<div class="bsplus-cal-modal-actions">
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-btn bsplus-cal-btn--ghost"
|
||||
disabled={modalBusy}
|
||||
onclick={closeModal}
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-btn bsplus-cal-btn--danger"
|
||||
disabled={modalBusy}
|
||||
onclick={() => void confirmDisconnect()}
|
||||
>
|
||||
{modalBusy ? "Disconnecting…" : "Disconnect account"}
|
||||
</button>
|
||||
</div>
|
||||
{:else}
|
||||
<h2 id="bsplus-cal-modal-title" class="bsplus-cal-modal-title">
|
||||
Delete synced classes?
|
||||
</h2>
|
||||
<p class="bsplus-cal-modal-body">
|
||||
Removes every BetterSEQTA+ timetable event from your {providerLabel} Calendar for this school.
|
||||
Your account stays connected — use Update calendar to sync again.
|
||||
</p>
|
||||
<div class="bsplus-cal-modal-actions">
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-btn bsplus-cal-btn--ghost"
|
||||
disabled={modalBusy}
|
||||
onclick={closeModal}
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="bsplus-cal-btn bsplus-cal-btn--danger"
|
||||
disabled={modalBusy}
|
||||
onclick={() => void confirmDeleteEvents()}
|
||||
>
|
||||
{modalBusy ? "Deleting…" : "Delete synced classes"}
|
||||
</button>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
{#if toast}
|
||||
<div
|
||||
@@ -702,6 +668,39 @@
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#snippet googleIcon()}
|
||||
<svg viewBox="0 0 24 24">
|
||||
<path
|
||||
fill="#4285F4"
|
||||
d="M22 12c0-.96-.08-1.88-.24-2.76H12v5.22h5.68c-.24 1.28-.96 2.44-2.04 3.18v2.64h3.3c1.92-1.76 3.06-4.36 3.06-7.28z"
|
||||
/>
|
||||
<path
|
||||
fill="#34A853"
|
||||
d="M12 22c2.76 0 5.08-.92 6.78-2.5l-3.3-2.64c-.92.62-2.1.98-3.48.98-2.68 0-4.96-1.8-5.78-4.22H2.18v2.72A10 10 0 0 0 12 22z"
|
||||
/>
|
||||
<path
|
||||
fill="#FBBC05"
|
||||
d="M6.22 13.62A5.98 5.98 0 0 1 5.82 12c0-.56.1-1.1.28-1.62V7.66H2.18A10 10 0 0 0 2 12c0 1.62.38 3.16 1.06 4.52l3.16-2.9z"
|
||||
/>
|
||||
<path
|
||||
fill="#EA4335"
|
||||
d="M12 5.38c1.5 0 2.84.52 3.9 1.54l2.92-2.92C17.08 2.34 14.76 1.2 12 1.2 7.54 1.2 3.72 3.94 2.18 7.66l4.04 3.14c.82-2.42 3.1-4.22 5.78-4.22z"
|
||||
/>
|
||||
</svg>
|
||||
{/snippet}
|
||||
|
||||
{#snippet googleName()}
|
||||
Google Calendar
|
||||
{/snippet}
|
||||
|
||||
{#snippet outlookIcon()}
|
||||
<OutlookCalendarIcon />
|
||||
{/snippet}
|
||||
|
||||
{#snippet outlookName()}
|
||||
Outlook Calendar
|
||||
{/snippet}
|
||||
|
||||
<style>
|
||||
.bsplus-cal-sync {
|
||||
position: relative;
|
||||
@@ -765,35 +764,6 @@
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.bsplus-google-word {
|
||||
display: inline-flex;
|
||||
font-size: 13px;
|
||||
font-weight: 700;
|
||||
letter-spacing: -0.02em;
|
||||
}
|
||||
|
||||
.bsplus-google-word--sm {
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.bsplus-google-g,
|
||||
.bsplus-google-g2 {
|
||||
color: #4285f4;
|
||||
}
|
||||
|
||||
.bsplus-google-o1,
|
||||
.bsplus-google-e {
|
||||
color: #ea4335;
|
||||
}
|
||||
|
||||
.bsplus-google-o2 {
|
||||
color: #fbbc05;
|
||||
}
|
||||
|
||||
.bsplus-google-l {
|
||||
color: #34a853;
|
||||
}
|
||||
|
||||
.bsplus-cal-trigger:hover:not(.bsplus-cal-trigger--busy) {
|
||||
transform: scale(1.03);
|
||||
}
|
||||
@@ -999,6 +969,81 @@
|
||||
background: color-mix(in srgb, var(--bsplus-cal-text) 14%, var(--bsplus-cal-surface));
|
||||
}
|
||||
|
||||
.bsplus-cal-modal-backdrop {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: var(--bsplus-cal-z-modal, 2147483647);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 16px;
|
||||
background: rgba(0, 0, 0, 0.5);
|
||||
backdrop-filter: blur(4px);
|
||||
}
|
||||
|
||||
.bsplus-cal-modal {
|
||||
width: min(100%, 400px);
|
||||
padding: 20px;
|
||||
border-radius: 16px;
|
||||
background: var(--bsplus-cal-surface, #fff);
|
||||
color: var(--bsplus-cal-text, #111);
|
||||
box-shadow: 0 24px 48px rgba(0, 0, 0, 0.22);
|
||||
border: 1px solid color-mix(in srgb, var(--bsplus-cal-text, #111) 12%, transparent);
|
||||
}
|
||||
|
||||
.bsplus-cal-modal-title {
|
||||
margin: 0 0 8px;
|
||||
font-size: 18px;
|
||||
font-weight: 700;
|
||||
line-height: 1.3;
|
||||
}
|
||||
|
||||
.bsplus-cal-modal-body {
|
||||
margin: 0 0 20px;
|
||||
font-size: 14px;
|
||||
line-height: 1.5;
|
||||
color: color-mix(in srgb, var(--bsplus-cal-text, #111) 72%, transparent);
|
||||
}
|
||||
|
||||
.bsplus-cal-modal-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.bsplus-cal-btn {
|
||||
padding: 8px 14px;
|
||||
border: none;
|
||||
border-radius: 10px;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
}
|
||||
|
||||
.bsplus-cal-btn:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.bsplus-cal-btn--ghost {
|
||||
background: color-mix(in srgb, var(--bsplus-cal-text, #111) 8%, transparent);
|
||||
color: var(--bsplus-cal-text, #111);
|
||||
}
|
||||
|
||||
.bsplus-cal-btn--ghost:hover:not(:disabled) {
|
||||
background: color-mix(in srgb, var(--bsplus-cal-text, #111) 14%, transparent);
|
||||
}
|
||||
|
||||
.bsplus-cal-btn--danger {
|
||||
background: #dc2626;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.bsplus-cal-btn--danger:hover:not(:disabled) {
|
||||
background: #b91c1c;
|
||||
}
|
||||
|
||||
.bsplus-cal-toast {
|
||||
position: fixed;
|
||||
right: 16px;
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
<script lang="ts">
|
||||
import type { GoogleCalendarSyncProgress } from "@/seqta/utils/googleCalendar/types";
|
||||
|
||||
let {
|
||||
progress = null,
|
||||
} = $props<{
|
||||
progress?: GoogleCalendarSyncProgress | null;
|
||||
}>();
|
||||
|
||||
const percent = $derived(
|
||||
progress && progress.total > 0
|
||||
? Math.min(100, Math.round((progress.current / progress.total) * 100))
|
||||
: progress?.phase === "preparing"
|
||||
? 8
|
||||
: 0,
|
||||
);
|
||||
</script>
|
||||
|
||||
{#if progress && progress.phase !== "done"}
|
||||
<div class="bsplus-cal-progress" role="status" aria-live="polite" aria-busy="true">
|
||||
<div class="bsplus-cal-progress-label">{progress.message}</div>
|
||||
<div class="bsplus-cal-progress-track" aria-hidden="true">
|
||||
<div class="bsplus-cal-progress-bar" style:width={`${percent}%`}></div>
|
||||
</div>
|
||||
{#if progress.total > 0}
|
||||
<div class="bsplus-cal-progress-meta">{progress.current} / {progress.total}</div>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<style>
|
||||
.bsplus-cal-progress {
|
||||
margin-top: 8px;
|
||||
padding: 10px;
|
||||
border-radius: 10px;
|
||||
background: color-mix(in srgb, var(--bsplus-cal-accent, var(--better-main, #3b82f6)) 10%, transparent);
|
||||
border: 1px solid color-mix(in srgb, var(--bsplus-cal-accent, var(--better-main, #3b82f6)) 22%, transparent);
|
||||
}
|
||||
|
||||
.bsplus-cal-progress-label {
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
color: var(--bsplus-cal-text, var(--text-primary, #111));
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.bsplus-cal-progress-track {
|
||||
height: 8px;
|
||||
border-radius: 999px;
|
||||
overflow: hidden;
|
||||
background: color-mix(in srgb, var(--bsplus-cal-text, #111) 10%, transparent);
|
||||
}
|
||||
|
||||
.bsplus-cal-progress-bar {
|
||||
height: 100%;
|
||||
border-radius: 999px;
|
||||
background: var(--bsplus-cal-accent, var(--better-main, #3b82f6));
|
||||
transition: width 0.25s ease;
|
||||
}
|
||||
|
||||
.bsplus-cal-progress-meta {
|
||||
margin-top: 6px;
|
||||
font-size: 10px;
|
||||
color: color-mix(in srgb, var(--bsplus-cal-text, #111) 62%, transparent);
|
||||
text-align: right;
|
||||
}
|
||||
</style>
|
||||
@@ -25,5 +25,4 @@
|
||||
|
||||
.bsplus-calendar-sync-mount {
|
||||
display: inline-flex;
|
||||
font-family: var(--bsplus-cal-font-family, var(--betterseqta-font-family, Rubik), sans-serif);
|
||||
}
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
/** Layer order: toolbar boost < menu < modal (all portaled UI uses the upper layers). */
|
||||
export const CALENDAR_SYNC_Z_MENU = 2_147_483_646;
|
||||
export const CALENDAR_SYNC_Z_MODAL = 2_147_483_647;
|
||||
|
||||
export function portalToBody(node: HTMLElement) {
|
||||
document.body.appendChild(node);
|
||||
return {
|
||||
destroy() {
|
||||
node.remove();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function isCalendarSyncModalTarget(target: EventTarget | null): boolean {
|
||||
return target instanceof Element && Boolean(target.closest(".bsplus-cal-modal-backdrop"));
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
import { settingsState } from "@/seqta/utils/listeners/SettingsState";
|
||||
import { extractSolidColor } from "@/seqta/ui/colors/parseCssColor";
|
||||
import { ensureFontLoaded } from "@/seqta/ui/fonts/Manager";
|
||||
import { getFontPreset } from "@/seqta/ui/fonts/presets";
|
||||
|
||||
export const CALENDAR_THEME_CSS_VARS = [
|
||||
"--better-main",
|
||||
"--better-pale",
|
||||
"--better-light",
|
||||
"--text-color",
|
||||
"--background-primary",
|
||||
"--background-secondary",
|
||||
"--text-primary",
|
||||
"--theme-offset-bg",
|
||||
"--better-sub",
|
||||
] as const;
|
||||
|
||||
const ACCENT_CSS_VARS = [
|
||||
"--better-main",
|
||||
"--accent-color-value",
|
||||
"--accentColor",
|
||||
"--colour-betterseqta-blue",
|
||||
] as const;
|
||||
|
||||
export function isCalendarDarkMode(): boolean {
|
||||
return !!settingsState.DarkMode || document.documentElement.classList.contains("dark");
|
||||
}
|
||||
|
||||
function resolvePageAccentColor(): string {
|
||||
const computed = getComputedStyle(document.documentElement);
|
||||
for (const name of ACCENT_CSS_VARS) {
|
||||
const solid = extractSolidColor(computed.getPropertyValue(name));
|
||||
if (solid) return solid;
|
||||
}
|
||||
const fromSettings = settingsState.selectedColor?.trim();
|
||||
if (fromSettings) {
|
||||
const solid = extractSolidColor(fromSettings);
|
||||
if (solid) return solid;
|
||||
}
|
||||
return "#3b82f6";
|
||||
}
|
||||
|
||||
/** Sync extension theme (including dark mode) onto a calendar UI host or portaled menu. */
|
||||
export function syncCalendarSyncTheme(target: HTMLElement): void {
|
||||
const computed = getComputedStyle(document.documentElement);
|
||||
const dark = isCalendarDarkMode();
|
||||
const fontPreset = getFontPreset(settingsState.selectedFont);
|
||||
|
||||
ensureFontLoaded(fontPreset);
|
||||
target.style.setProperty("--bsplus-cal-font-family", fontPreset.stack);
|
||||
|
||||
for (const name of CALENDAR_THEME_CSS_VARS) {
|
||||
const value =
|
||||
document.documentElement.style.getPropertyValue(name).trim() ||
|
||||
computed.getPropertyValue(name).trim();
|
||||
if (value) target.style.setProperty(name, value);
|
||||
}
|
||||
|
||||
const accent = resolvePageAccentColor();
|
||||
target.style.setProperty("--bsplus-cal-accent", accent);
|
||||
target.style.setProperty("--better-main", accent);
|
||||
target.classList.toggle("dark", dark);
|
||||
|
||||
const textPrimary =
|
||||
computed.getPropertyValue("--text-primary").trim() ||
|
||||
computed.getPropertyValue("--text-color").trim();
|
||||
const bgPrimary =
|
||||
computed.getPropertyValue("--background-primary").trim() ||
|
||||
computed.getPropertyValue("--background-secondary").trim() ||
|
||||
computed.getPropertyValue("--theme-offset-bg").trim();
|
||||
|
||||
target.style.setProperty(
|
||||
"--bsplus-cal-text",
|
||||
textPrimary || (dark ? "#f4f4f5" : "#18181b"),
|
||||
);
|
||||
target.style.setProperty(
|
||||
"--bsplus-cal-surface",
|
||||
bgPrimary || (dark ? "#27272a" : "#ffffff"),
|
||||
);
|
||||
target.style.setProperty(
|
||||
"--bsplus-cal-surface-muted",
|
||||
dark ? "#3f3f46" : "color-mix(in srgb, var(--bsplus-cal-text) 5%, var(--bsplus-cal-surface))",
|
||||
);
|
||||
target.style.setProperty(
|
||||
"--bsplus-cal-border",
|
||||
dark ? "color-mix(in srgb, #ffffff 14%, transparent)" : "color-mix(in srgb, var(--bsplus-cal-text) 12%, transparent)",
|
||||
);
|
||||
}
|
||||
@@ -1,14 +1,133 @@
|
||||
import { mount, unmount } from "svelte";
|
||||
import { settingsState } from "@/seqta/utils/listeners/SettingsState";
|
||||
import { extractSolidColor } from "@/seqta/ui/colors/parseCssColor";
|
||||
import { ensureFontLoaded } from "@/seqta/ui/fonts/Manager";
|
||||
import { getFontPreset } from "@/seqta/ui/fonts/presets";
|
||||
import type { GoogleCalendarSyncProgress } from "@/seqta/utils/googleCalendar/types";
|
||||
import CalendarSyncControl from "./CalendarSyncControl.svelte";
|
||||
import { syncCalendarSyncTheme } from "./calendarSyncTheme";
|
||||
import { registerCalendarContentHandlers } from "@/seqta/utils/googleCalendar/calendarSyncListener";
|
||||
import hostStyles from "./calendarSyncHost.css?inline";
|
||||
|
||||
export type CalendarProvider = "google" | "outlook";
|
||||
|
||||
export function providerLabel(provider: CalendarProvider): string {
|
||||
return provider === "google" ? "Google" : "Outlook";
|
||||
}
|
||||
|
||||
export function formatLastSync(ts?: number): string | null {
|
||||
if (!ts) return null;
|
||||
const diff = Date.now() - ts;
|
||||
if (diff < 60_000) return "Synced just now";
|
||||
if (diff < 3_600_000) return `Synced ${Math.floor(diff / 60_000)}m ago`;
|
||||
if (diff < 86_400_000) return `Synced ${Math.floor(diff / 3_600_000)}h ago`;
|
||||
return `Synced ${new Date(ts).toLocaleDateString()}`;
|
||||
}
|
||||
|
||||
export function syncProgressPercent(progress: GoogleCalendarSyncProgress | null): number {
|
||||
if (!progress || progress.phase === "done") return 0;
|
||||
if (progress.total > 0) {
|
||||
return Math.min(100, Math.round((progress.current / progress.total) * 100));
|
||||
}
|
||||
return progress.phase === "preparing" ? 8 : 0;
|
||||
}
|
||||
|
||||
const CONTROLS_CLASS = "timetable-calendar-controls";
|
||||
const HOST_STYLE_ID = "bsplus-calendar-sync-host-styles";
|
||||
|
||||
const THEME_CSS_VARS = [
|
||||
"--better-main",
|
||||
"--better-pale",
|
||||
"--better-light",
|
||||
"--text-color",
|
||||
"--background-primary",
|
||||
"--background-secondary",
|
||||
"--text-primary",
|
||||
"--theme-offset-bg",
|
||||
"--better-sub",
|
||||
] as const;
|
||||
|
||||
const ACCENT_CSS_VARS = [
|
||||
"--better-main",
|
||||
"--accent-color-value",
|
||||
"--accentColor",
|
||||
"--colour-betterseqta-blue",
|
||||
] as const;
|
||||
|
||||
let currentApp: ReturnType<typeof mount> | null = null;
|
||||
let mountRoot: HTMLElement | null = null;
|
||||
|
||||
export function portalToBody(node: HTMLElement) {
|
||||
document.body.appendChild(node);
|
||||
return {
|
||||
destroy() {
|
||||
node.remove();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function isCalendarSyncModalTarget(target: EventTarget | null): boolean {
|
||||
return target instanceof Element && Boolean(target.closest(".bsplus-cal-modal-backdrop"));
|
||||
}
|
||||
|
||||
/** Sync extension theme (including dark mode) onto a calendar UI host or portaled menu. */
|
||||
export function syncCalendarSyncTheme(target: HTMLElement): void {
|
||||
const computed = getComputedStyle(document.documentElement);
|
||||
const dark = !!settingsState.DarkMode || document.documentElement.classList.contains("dark");
|
||||
const fontPreset = getFontPreset(settingsState.selectedFont);
|
||||
|
||||
ensureFontLoaded(fontPreset);
|
||||
target.style.setProperty("--bsplus-cal-font-family", fontPreset.stack);
|
||||
|
||||
for (const name of THEME_CSS_VARS) {
|
||||
const value =
|
||||
document.documentElement.style.getPropertyValue(name).trim() ||
|
||||
computed.getPropertyValue(name).trim();
|
||||
if (value) target.style.setProperty(name, value);
|
||||
}
|
||||
|
||||
let accent = "#3b82f6";
|
||||
for (const name of ACCENT_CSS_VARS) {
|
||||
const solid = extractSolidColor(computed.getPropertyValue(name));
|
||||
if (solid) {
|
||||
accent = solid;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (accent === "#3b82f6") {
|
||||
const fromSettings = settingsState.selectedColor?.trim();
|
||||
if (fromSettings) {
|
||||
const solid = extractSolidColor(fromSettings);
|
||||
if (solid) accent = solid;
|
||||
}
|
||||
}
|
||||
target.style.setProperty("--bsplus-cal-accent", accent);
|
||||
target.style.setProperty("--better-main", accent);
|
||||
target.classList.toggle("dark", dark);
|
||||
|
||||
const textPrimary =
|
||||
computed.getPropertyValue("--text-primary").trim() ||
|
||||
computed.getPropertyValue("--text-color").trim();
|
||||
const bgPrimary =
|
||||
computed.getPropertyValue("--background-primary").trim() ||
|
||||
computed.getPropertyValue("--background-secondary").trim() ||
|
||||
computed.getPropertyValue("--theme-offset-bg").trim();
|
||||
|
||||
target.style.setProperty(
|
||||
"--bsplus-cal-text",
|
||||
textPrimary || (dark ? "#f4f4f5" : "#18181b"),
|
||||
);
|
||||
target.style.setProperty(
|
||||
"--bsplus-cal-surface",
|
||||
bgPrimary || (dark ? "#27272a" : "#ffffff"),
|
||||
);
|
||||
target.style.setProperty(
|
||||
"--bsplus-cal-surface-muted",
|
||||
dark ? "#3f3f46" : "color-mix(in srgb, var(--bsplus-cal-text) 5%, var(--bsplus-cal-surface))",
|
||||
);
|
||||
target.style.setProperty(
|
||||
"--bsplus-cal-border",
|
||||
dark ? "color-mix(in srgb, #ffffff 14%, transparent)" : "color-mix(in srgb, var(--bsplus-cal-text) 12%, transparent)",
|
||||
);
|
||||
}
|
||||
|
||||
function ensureHostStyles() {
|
||||
if (document.getElementById(HOST_STYLE_ID)) return;
|
||||
@@ -23,8 +142,6 @@ function teardown() {
|
||||
unmount(currentApp);
|
||||
currentApp = null;
|
||||
}
|
||||
|
||||
mountRoot = null;
|
||||
document.querySelector(`.${CONTROLS_CLASS}`)?.remove();
|
||||
document.getElementById(HOST_STYLE_ID)?.remove();
|
||||
}
|
||||
@@ -42,7 +159,7 @@ export async function mountGoogleCalendarButton(): Promise<void> {
|
||||
controls.className = `${CONTROLS_CLASS} bsplus-timetable-control`;
|
||||
toolbar.appendChild(controls);
|
||||
|
||||
mountRoot = document.createElement("div");
|
||||
const mountRoot = document.createElement("div");
|
||||
mountRoot.className = "bsplus-calendar-sync-mount";
|
||||
syncCalendarSyncTheme(mountRoot);
|
||||
controls.appendChild(mountRoot);
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
import {
|
||||
extractSolidColor,
|
||||
normalizeCssColorString,
|
||||
parseCssColor,
|
||||
} from "./parseCssColor";
|
||||
import { extractSolidColor, normalizeCssColorString } from "./parseCssColor";
|
||||
|
||||
describe("normalizeCssColorString", () => {
|
||||
it("lowercases uppercase RGBA/RGB function names", () => {
|
||||
@@ -28,19 +24,3 @@ describe("extractSolidColor", () => {
|
||||
).toBe("rgba(201,61,0,1)");
|
||||
});
|
||||
});
|
||||
|
||||
describe("parseCssColor", () => {
|
||||
it("parses uppercase RGBA without throwing", () => {
|
||||
const parsed = parseCssColor("RGBA(3, 29, 11, 0.58)");
|
||||
expect(parsed.alpha()).toBeCloseTo(0.58, 2);
|
||||
expect(parsed.red()).toBe(3);
|
||||
expect(parsed.green()).toBe(29);
|
||||
expect(parsed.blue()).toBe(11);
|
||||
});
|
||||
|
||||
it("falls back when the value is not a colour", () => {
|
||||
expect(parseCssColor("not-a-color", "#007bff").hex().toLowerCase()).toBe(
|
||||
"#007bff",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
import Color from "color";
|
||||
|
||||
type ColorInstance = ReturnType<typeof Color>;
|
||||
|
||||
/**
|
||||
* SEQTA themes and user gradients often use uppercase `RGBA()` / `RGB()`.
|
||||
* The `color` package only accepts lowercase function names.
|
||||
@@ -32,34 +28,3 @@ export function extractSolidColor(value: string): string | null {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Parse a CSS colour for the `color` library; never throws. */
|
||||
export function parseCssColor(value: string, fallback = "#007bff"): ColorInstance {
|
||||
const candidates = [
|
||||
extractSolidColor(value),
|
||||
normalizeCssColorString(value),
|
||||
].filter((candidate): candidate is string => Boolean(candidate));
|
||||
|
||||
for (const candidate of candidates) {
|
||||
try {
|
||||
return Color(candidate);
|
||||
} catch {
|
||||
// try next strategy
|
||||
}
|
||||
|
||||
const rgbaMatch = candidate.match(
|
||||
/rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)(?:\s*,\s*([\d.]+))?\s*\)/i,
|
||||
);
|
||||
if (rgbaMatch) {
|
||||
try {
|
||||
const [, r, g, b, a] = rgbaMatch;
|
||||
const rgb = Color.rgb(Number(r), Number(g), Number(b));
|
||||
return a !== undefined ? rgb.alpha(Number(a)) : rgb;
|
||||
} catch {
|
||||
// fall through
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Color(fallback);
|
||||
}
|
||||
|
||||
+27
-21
@@ -1,10 +1,4 @@
|
||||
import {
|
||||
OUTLOOK_CALENDAR_ACCOUNTS_NOT_READY_HINT,
|
||||
OUTLOOK_CALENDAR_REFRESH_URL,
|
||||
OUTLOOK_CALENDAR_TOKEN_URL,
|
||||
} from "@/config/outlookCalendar";
|
||||
|
||||
type OutlookTokenPayload = {
|
||||
export type AccountsTokenPayload = {
|
||||
access_token: string;
|
||||
refresh_token?: string;
|
||||
expires_in?: number;
|
||||
@@ -19,7 +13,7 @@ async function parseAccountsJson(res: Response): Promise<Record<string, unknown>
|
||||
}
|
||||
}
|
||||
|
||||
function extractTokens(json: Record<string, unknown>): OutlookTokenPayload {
|
||||
function extractTokens(json: Record<string, unknown>): AccountsTokenPayload {
|
||||
const access_token = json.access_token;
|
||||
if (typeof access_token !== "string" || !access_token) {
|
||||
throw new Error("Token response missing access_token");
|
||||
@@ -31,21 +25,30 @@ function extractTokens(json: Record<string, unknown>): OutlookTokenPayload {
|
||||
};
|
||||
}
|
||||
|
||||
function formatAccountsTokenError(res: Response, json: Record<string, unknown>): string {
|
||||
if (res.status === 404 || res.status === 501) {
|
||||
return OUTLOOK_CALENDAR_ACCOUNTS_NOT_READY_HINT;
|
||||
}
|
||||
function formatAccountsTokenError(
|
||||
res: Response,
|
||||
json: Record<string, unknown>,
|
||||
notReadyHint: string,
|
||||
includeErrorDescription: boolean,
|
||||
): string {
|
||||
if (res.status === 404 || res.status === 501) return notReadyHint;
|
||||
const err = typeof json.error === "string" ? json.error : "";
|
||||
const desc = typeof json.error_description === "string" ? json.error_description : "";
|
||||
const desc =
|
||||
includeErrorDescription && typeof json.error_description === "string"
|
||||
? json.error_description
|
||||
: "";
|
||||
return desc || err || `Accounts token API failed (${res.status})`;
|
||||
}
|
||||
|
||||
export async function exchangeOutlookCodeViaAccounts(
|
||||
export async function exchangeAccountsCode(
|
||||
tokenUrl: string,
|
||||
code: string,
|
||||
redirectUri: string,
|
||||
codeVerifier: string,
|
||||
): Promise<OutlookTokenPayload> {
|
||||
const res = await fetch(OUTLOOK_CALENDAR_TOKEN_URL, {
|
||||
notReadyHint: string,
|
||||
includeErrorDescription = false,
|
||||
): Promise<AccountsTokenPayload> {
|
||||
const res = await fetch(tokenUrl, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
@@ -56,22 +59,25 @@ export async function exchangeOutlookCodeViaAccounts(
|
||||
});
|
||||
const json = await parseAccountsJson(res);
|
||||
if (!res.ok) {
|
||||
throw new Error(formatAccountsTokenError(res, json));
|
||||
throw new Error(formatAccountsTokenError(res, json, notReadyHint, includeErrorDescription));
|
||||
}
|
||||
return extractTokens(json);
|
||||
}
|
||||
|
||||
export async function refreshOutlookTokenViaAccounts(
|
||||
export async function refreshAccountsToken(
|
||||
refreshUrl: string,
|
||||
refreshToken: string,
|
||||
): Promise<OutlookTokenPayload> {
|
||||
const res = await fetch(OUTLOOK_CALENDAR_REFRESH_URL, {
|
||||
notReadyHint: string,
|
||||
includeErrorDescription = false,
|
||||
): Promise<AccountsTokenPayload> {
|
||||
const res = await fetch(refreshUrl, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ refresh_token: refreshToken }),
|
||||
});
|
||||
const json = await parseAccountsJson(res);
|
||||
if (!res.ok) {
|
||||
throw new Error(formatAccountsTokenError(res, json));
|
||||
throw new Error(formatAccountsTokenError(res, json, notReadyHint, includeErrorDescription));
|
||||
}
|
||||
return extractTokens(json);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import browser from "webextension-polyfill";
|
||||
|
||||
export interface EventMapEntry {
|
||||
id: string;
|
||||
date: string;
|
||||
}
|
||||
|
||||
export type EventMapRecord = Record<string, string | EventMapEntry>;
|
||||
|
||||
export function eventMapKey(origin: string, seqtaKey: string): string {
|
||||
return `${origin}::${seqtaKey}`;
|
||||
}
|
||||
|
||||
export function normalizeEventMapEntry(
|
||||
value: string | EventMapEntry | undefined,
|
||||
): EventMapEntry | undefined {
|
||||
if (value == null) return undefined;
|
||||
if (typeof value === "string") return { id: value, date: "" };
|
||||
if (typeof value.id === "string" && value.id.length > 0) {
|
||||
return { id: value.id, date: value.date ?? "" };
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function getStoredEventId(
|
||||
value: string | EventMapEntry | undefined,
|
||||
): string | undefined {
|
||||
return normalizeEventMapEntry(value)?.id;
|
||||
}
|
||||
|
||||
export function lessonDateFromSeqtaKey(seqtaKey: string): string | undefined {
|
||||
for (const part of seqtaKey.split(":")) {
|
||||
if (/^\d{4}-\d{2}-\d{2}$/.test(part)) return part;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function createCalendarStateStorage<T extends object>(storageKey: string) {
|
||||
async function read(): Promise<T> {
|
||||
const got = await browser.storage.local.get(storageKey);
|
||||
const raw = got[storageKey];
|
||||
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return {} as T;
|
||||
return raw as T;
|
||||
}
|
||||
|
||||
async function write(patch: Partial<T>): Promise<T> {
|
||||
const current = await read();
|
||||
const next = { ...current, ...patch };
|
||||
await browser.storage.local.set({ [storageKey]: next });
|
||||
return next;
|
||||
}
|
||||
|
||||
async function clear(): Promise<void> {
|
||||
await browser.storage.local.remove(storageKey);
|
||||
}
|
||||
|
||||
return { read, write, clear };
|
||||
}
|
||||
@@ -1,138 +1,26 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, jest } from "@jest/globals";
|
||||
import { describe, expect, it, jest } from "@jest/globals";
|
||||
|
||||
jest.mock("@/utils/verboseLog", () => ({
|
||||
verboseLog: jest.fn(),
|
||||
}));
|
||||
|
||||
import {
|
||||
reportSyncProgress,
|
||||
resetSyncProgressThrottle,
|
||||
SYNC_PROGRESS_THROTTLE_MS,
|
||||
} from "./lessonSyncShared";
|
||||
import { reportSyncProgress } from "./lessonSyncShared";
|
||||
import type { GoogleCalendarSyncProgress } from "@/seqta/utils/googleCalendar/types";
|
||||
|
||||
describe("reportSyncProgress", () => {
|
||||
beforeEach(() => {
|
||||
jest.useFakeTimers();
|
||||
jest.setSystemTime(new Date("2026-06-28T12:00:00.000Z"));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
it("reports preparing and done immediately", () => {
|
||||
it("calls onProgress when provided", () => {
|
||||
const onProgress = jest.fn();
|
||||
const preparing: GoogleCalendarSyncProgress = {
|
||||
phase: "preparing",
|
||||
current: 0,
|
||||
total: 10,
|
||||
message: "Preparing…",
|
||||
};
|
||||
const done: GoogleCalendarSyncProgress = {
|
||||
phase: "done",
|
||||
current: 10,
|
||||
total: 10,
|
||||
message: "Done",
|
||||
const progress: GoogleCalendarSyncProgress = {
|
||||
phase: "upserting",
|
||||
current: 1,
|
||||
total: 5,
|
||||
message: "Syncing events (1/5)…",
|
||||
};
|
||||
|
||||
reportSyncProgress(onProgress, preparing);
|
||||
reportSyncProgress(onProgress, done);
|
||||
|
||||
expect(onProgress).toHaveBeenCalledTimes(2);
|
||||
expect(onProgress).toHaveBeenNthCalledWith(1, preparing);
|
||||
expect(onProgress).toHaveBeenNthCalledWith(2, done);
|
||||
});
|
||||
|
||||
it("throttles upserting progress to at most once per second", () => {
|
||||
const onProgress = jest.fn();
|
||||
|
||||
for (let i = 1; i <= 5; i++) {
|
||||
reportSyncProgress(onProgress, {
|
||||
phase: "upserting",
|
||||
current: i,
|
||||
total: 5,
|
||||
message: `Syncing events (${i}/5)…`,
|
||||
});
|
||||
}
|
||||
|
||||
expect(onProgress).toHaveBeenCalledTimes(1);
|
||||
expect(onProgress).toHaveBeenCalledWith({
|
||||
phase: "upserting",
|
||||
current: 1,
|
||||
total: 5,
|
||||
message: "Syncing events (1/5)…",
|
||||
});
|
||||
|
||||
jest.advanceTimersByTime(SYNC_PROGRESS_THROTTLE_MS);
|
||||
|
||||
expect(onProgress).toHaveBeenCalledTimes(2);
|
||||
expect(onProgress).toHaveBeenLastCalledWith({
|
||||
phase: "upserting",
|
||||
current: 5,
|
||||
total: 5,
|
||||
message: "Syncing events (5/5)…",
|
||||
});
|
||||
});
|
||||
|
||||
it("flushes pending progress before reporting done", () => {
|
||||
const onProgress = jest.fn();
|
||||
|
||||
reportSyncProgress(onProgress, {
|
||||
phase: "upserting",
|
||||
current: 1,
|
||||
total: 5,
|
||||
message: "Syncing events (1/5)…",
|
||||
});
|
||||
|
||||
reportSyncProgress(onProgress, {
|
||||
phase: "upserting",
|
||||
current: 4,
|
||||
total: 5,
|
||||
message: "Syncing events (4/5)…",
|
||||
});
|
||||
|
||||
reportSyncProgress(onProgress, {
|
||||
phase: "done",
|
||||
current: 5,
|
||||
total: 5,
|
||||
message: "Sync complete",
|
||||
});
|
||||
|
||||
expect(onProgress).toHaveBeenCalledTimes(3);
|
||||
expect(onProgress).toHaveBeenNthCalledWith(1, {
|
||||
phase: "upserting",
|
||||
current: 1,
|
||||
total: 5,
|
||||
message: "Syncing events (1/5)…",
|
||||
});
|
||||
expect(onProgress).toHaveBeenNthCalledWith(2, {
|
||||
phase: "upserting",
|
||||
current: 4,
|
||||
total: 5,
|
||||
message: "Syncing events (4/5)…",
|
||||
});
|
||||
expect(onProgress).toHaveBeenNthCalledWith(3, {
|
||||
phase: "done",
|
||||
current: 5,
|
||||
total: 5,
|
||||
message: "Sync complete",
|
||||
});
|
||||
});
|
||||
|
||||
it("resetSyncProgressThrottle clears queued updates", () => {
|
||||
const onProgress = jest.fn();
|
||||
|
||||
reportSyncProgress(onProgress, {
|
||||
phase: "upserting",
|
||||
current: 1,
|
||||
total: 3,
|
||||
message: "Syncing events (1/3)…",
|
||||
});
|
||||
|
||||
resetSyncProgressThrottle(onProgress);
|
||||
jest.advanceTimersByTime(SYNC_PROGRESS_THROTTLE_MS);
|
||||
reportSyncProgress(onProgress, progress);
|
||||
reportSyncProgress(undefined, progress);
|
||||
|
||||
expect(onProgress).toHaveBeenCalledTimes(1);
|
||||
expect(onProgress).toHaveBeenCalledWith(progress);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,109 +3,31 @@ import {
|
||||
getStoredEventId,
|
||||
lessonDateFromSeqtaKey,
|
||||
normalizeEventMapEntry,
|
||||
} from "@/seqta/utils/googleCalendar/eventMapEntry";
|
||||
type EventMapRecord,
|
||||
} from "@/seqta/utils/calendarSync/eventMap";
|
||||
import {
|
||||
isDateInRange,
|
||||
syncWindowRange,
|
||||
} from "@/seqta/utils/googleCalendar/syncDateRange";
|
||||
import type {
|
||||
GoogleCalendarDeleteResult,
|
||||
GoogleCalendarSyncOptions,
|
||||
GoogleCalendarSyncProgress,
|
||||
GoogleCalendarSyncResult,
|
||||
} from "@/seqta/utils/googleCalendar/types";
|
||||
|
||||
export const EVENT_MAP_PERSIST_EVERY = 10;
|
||||
/** Max UI progress refresh rate during bulk delete/upsert (reduces Svelte re-renders). */
|
||||
export const SYNC_PROGRESS_THROTTLE_MS = 1000;
|
||||
|
||||
export type EventMapRecord = Record<string, string | { id: string; date: string }>;
|
||||
|
||||
export type MappedLessonEvent = {
|
||||
seqtaKey: string;
|
||||
startDateTime: string;
|
||||
};
|
||||
|
||||
type ProgressThrottleState = {
|
||||
lastReportAt: number;
|
||||
pending: GoogleCalendarSyncProgress | null;
|
||||
timer: ReturnType<typeof setTimeout> | null;
|
||||
};
|
||||
|
||||
const progressThrottleByCallback = new WeakMap<
|
||||
NonNullable<GoogleCalendarSyncOptions["onProgress"]>,
|
||||
ProgressThrottleState
|
||||
>();
|
||||
|
||||
function getProgressThrottleState(
|
||||
onProgress: NonNullable<GoogleCalendarSyncOptions["onProgress"]>,
|
||||
): ProgressThrottleState {
|
||||
let state = progressThrottleByCallback.get(onProgress);
|
||||
if (!state) {
|
||||
state = { lastReportAt: 0, pending: null, timer: null };
|
||||
progressThrottleByCallback.set(onProgress, state);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
function flushPendingSyncProgress(
|
||||
onProgress: NonNullable<GoogleCalendarSyncOptions["onProgress"]>,
|
||||
state: ProgressThrottleState,
|
||||
) {
|
||||
if (state.timer) {
|
||||
clearTimeout(state.timer);
|
||||
state.timer = null;
|
||||
}
|
||||
if (!state.pending) return;
|
||||
onProgress(state.pending);
|
||||
state.pending = null;
|
||||
state.lastReportAt = Date.now();
|
||||
}
|
||||
|
||||
/** Clears any queued progress for a callback (e.g. when a sync run ends). */
|
||||
export function resetSyncProgressThrottle(
|
||||
onProgress: GoogleCalendarSyncOptions["onProgress"],
|
||||
) {
|
||||
if (!onProgress) return;
|
||||
const state = progressThrottleByCallback.get(onProgress);
|
||||
if (!state) return;
|
||||
if (state.timer) {
|
||||
clearTimeout(state.timer);
|
||||
state.timer = null;
|
||||
}
|
||||
state.pending = null;
|
||||
}
|
||||
|
||||
export function reportSyncProgress(
|
||||
onProgress: GoogleCalendarSyncOptions["onProgress"],
|
||||
progress: GoogleCalendarSyncProgress,
|
||||
) {
|
||||
if (!onProgress) return;
|
||||
|
||||
const state = getProgressThrottleState(onProgress);
|
||||
|
||||
if (progress.phase === "preparing" || progress.phase === "done") {
|
||||
flushPendingSyncProgress(onProgress, state);
|
||||
onProgress(progress);
|
||||
state.lastReportAt = Date.now();
|
||||
if (progress.phase === "done") {
|
||||
resetSyncProgressThrottle(onProgress);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
state.pending = progress;
|
||||
const elapsed = Date.now() - state.lastReportAt;
|
||||
if (elapsed >= SYNC_PROGRESS_THROTTLE_MS) {
|
||||
flushPendingSyncProgress(onProgress, state);
|
||||
return;
|
||||
}
|
||||
|
||||
if (state.timer) return;
|
||||
|
||||
state.timer = setTimeout(() => {
|
||||
state.timer = null;
|
||||
flushPendingSyncProgress(onProgress, state);
|
||||
}, SYNC_PROGRESS_THROTTLE_MS - elapsed);
|
||||
onProgress?.(progress);
|
||||
}
|
||||
|
||||
export function lessonDateForEvent(startDateTime: string, seqtaKey: string): string {
|
||||
@@ -126,18 +48,6 @@ export function originEventMapEntries(
|
||||
return entries;
|
||||
}
|
||||
|
||||
function shouldPruneEntry(
|
||||
mode: "full" | "incremental",
|
||||
entry: { id: string; date: string },
|
||||
mapKey: string,
|
||||
window: ReturnType<typeof syncWindowRange>,
|
||||
currentMapKeys: Set<string>,
|
||||
): boolean {
|
||||
if (mode === "incremental") return false;
|
||||
if (entry.date) return !isDateInRange(entry.date, window);
|
||||
return !currentMapKeys.has(mapKey);
|
||||
}
|
||||
|
||||
export function entriesToPrune(
|
||||
eventMap: EventMapRecord,
|
||||
origin: string,
|
||||
@@ -145,6 +55,8 @@ export function entriesToPrune(
|
||||
weeksAhead: number,
|
||||
currentMapKeys: Set<string>,
|
||||
): Array<[string, string]> {
|
||||
if (mode === "incremental") return [];
|
||||
|
||||
const window = syncWindowRange(weeksAhead);
|
||||
const prefix = `${origin}::`;
|
||||
const entries: Array<[string, string]> = [];
|
||||
@@ -153,9 +65,10 @@ export function entriesToPrune(
|
||||
if (!mapKey.startsWith(prefix)) continue;
|
||||
const entry = normalizeEventMapEntry(raw);
|
||||
if (!entry) continue;
|
||||
if (shouldPruneEntry(mode, entry, mapKey, window, currentMapKeys)) {
|
||||
entries.push([mapKey, entry.id]);
|
||||
}
|
||||
const stale = entry.date
|
||||
? !isDateInRange(entry.date, window)
|
||||
: !currentMapKeys.has(mapKey);
|
||||
if (stale) entries.push([mapKey, entry.id]);
|
||||
}
|
||||
|
||||
return entries;
|
||||
@@ -178,6 +91,98 @@ export function emptyLessonsSyncResult(): GoogleCalendarSyncResult {
|
||||
};
|
||||
}
|
||||
|
||||
export function formatLessonSyncResultMessage(
|
||||
result: GoogleCalendarSyncResult,
|
||||
calendarLabel: string,
|
||||
): string {
|
||||
const created = result.created ?? 0;
|
||||
const updated = result.updated ?? 0;
|
||||
const deleted = result.deleted ?? 0;
|
||||
const parts: string[] = [];
|
||||
if (created > 0) parts.push(`${created} new`);
|
||||
if (updated > 0) parts.push(`${updated} updated`);
|
||||
if (deleted > 0) parts.push(`${deleted} removed`);
|
||||
if (parts.length === 0) return `${calendarLabel} is up to date.`;
|
||||
return `${calendarLabel} updated (${parts.join(", ")}).`;
|
||||
}
|
||||
|
||||
export function buildDeleteSyncResult(
|
||||
deleted: number,
|
||||
failed: number,
|
||||
): GoogleCalendarDeleteResult {
|
||||
return {
|
||||
success: failed === 0,
|
||||
configured: true,
|
||||
connected: true,
|
||||
deleted,
|
||||
failed,
|
||||
error:
|
||||
failed > 0
|
||||
? `Removed ${deleted} event${deleted === 1 ? "" : "s"} with ${failed} error${failed === 1 ? "" : "s"}.`
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
export async function deleteTrackedLessonEvents(
|
||||
entries: Array<[string, string]>,
|
||||
eventMap: EventMapRecord,
|
||||
getAccessToken: () => Promise<string>,
|
||||
deleteEvent: (
|
||||
accessToken: string,
|
||||
eventId: string,
|
||||
refreshAccessToken: () => Promise<string>,
|
||||
) => Promise<void>,
|
||||
writeState: (patch: { eventMap: EventMapRecord }) => Promise<unknown>,
|
||||
options: {
|
||||
persistProgress?: boolean;
|
||||
onProgress?: GoogleCalendarSyncOptions["onProgress"];
|
||||
progressOffset?: number;
|
||||
progressTotal?: number;
|
||||
logLabel: string;
|
||||
},
|
||||
): Promise<{ deleted: number; failed: number }> {
|
||||
if (entries.length === 0) return { deleted: 0, failed: 0 };
|
||||
|
||||
const {
|
||||
persistProgress = false,
|
||||
onProgress,
|
||||
progressOffset = 0,
|
||||
progressTotal = 0,
|
||||
logLabel,
|
||||
} = options;
|
||||
|
||||
let accessToken = await getAccessToken();
|
||||
let deleted = 0;
|
||||
let failed = 0;
|
||||
|
||||
for (const [mapKey, eventId] of entries) {
|
||||
try {
|
||||
await deleteEvent(accessToken, eventId, async () => {
|
||||
accessToken = await getAccessToken();
|
||||
return accessToken;
|
||||
});
|
||||
delete eventMap[mapKey];
|
||||
deleted += 1;
|
||||
} catch (err) {
|
||||
verboseLog(`[BetterSEQTA+] ${logLabel} event delete failed:`, err);
|
||||
failed += 1;
|
||||
}
|
||||
|
||||
reportSyncProgress(onProgress, {
|
||||
phase: "deleting",
|
||||
current: progressOffset + deleted + failed,
|
||||
total: progressTotal,
|
||||
message: `Removing old events (${deleted + failed}/${entries.length})…`,
|
||||
});
|
||||
|
||||
if (persistProgress && (deleted + failed) % EVENT_MAP_PERSIST_EVERY === 0) {
|
||||
await writeState({ eventMap });
|
||||
}
|
||||
}
|
||||
|
||||
return { deleted, failed };
|
||||
}
|
||||
|
||||
export function buildLessonSyncResult(
|
||||
created: number,
|
||||
updated: number,
|
||||
@@ -202,24 +207,6 @@ export function buildLessonSyncResult(
|
||||
};
|
||||
}
|
||||
|
||||
export async function persistFinalSyncState(
|
||||
writeState: (patch: {
|
||||
eventMap: EventMapRecord;
|
||||
lastSyncAt: number;
|
||||
lastSyncOrigin: string;
|
||||
}) => Promise<unknown>,
|
||||
eventMap: EventMapRecord,
|
||||
lastSyncAt: number,
|
||||
origin: string,
|
||||
staleDeleted: number,
|
||||
staleEntryCount: number,
|
||||
eventCount: number,
|
||||
): Promise<void> {
|
||||
if (staleDeleted > 0 || staleEntryCount > 0 || eventCount > 0) {
|
||||
await writeState({ eventMap, lastSyncAt, lastSyncOrigin: origin });
|
||||
}
|
||||
}
|
||||
|
||||
type UpsertLessonEventsParams<TEvent extends MappedLessonEvent> = {
|
||||
events: TEvent[];
|
||||
eventMap: EventMapRecord;
|
||||
@@ -288,19 +275,14 @@ export async function upsertLessonEvents<TEvent extends MappedLessonEvent>(
|
||||
date: lessonDateForEvent(event.startDateTime, event.seqtaKey),
|
||||
};
|
||||
|
||||
reportSyncProgress(onProgress, {
|
||||
phase: "upserting",
|
||||
current: progressCurrent,
|
||||
total: totalSteps,
|
||||
message: progressMessage,
|
||||
});
|
||||
|
||||
if ((i + 1) % EVENT_MAP_PERSIST_EVERY === 0 || i === events.length - 1) {
|
||||
await writeState({ eventMap, lastSyncAt, lastSyncOrigin: origin });
|
||||
}
|
||||
} catch (err) {
|
||||
verboseLog(`[BetterSEQTA+] ${logLabel} event sync failed:`, err);
|
||||
failed += 1;
|
||||
}
|
||||
|
||||
reportSyncProgress(onProgress, {
|
||||
phase: "upserting",
|
||||
current: progressCurrent,
|
||||
@@ -308,7 +290,6 @@ export async function upsertLessonEvents<TEvent extends MappedLessonEvent>(
|
||||
message: progressMessage,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return { created, updated, failed, accessToken };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import type { EventMapRecord } from "@/seqta/utils/calendarSync/eventMap";
|
||||
import { createCalendarStateStorage } from "@/seqta/utils/calendarSync/eventMap";
|
||||
|
||||
export const BSPLUS_GOOGLE_CALENDAR_STORAGE_KEY = "bsplus_google_calendar";
|
||||
export const BSPLUS_OUTLOOK_CALENDAR_STORAGE_KEY = "bsplus_outlook_calendar";
|
||||
|
||||
export interface GoogleCalendarStoredState {
|
||||
accessToken?: string;
|
||||
refreshToken?: string;
|
||||
expiresAt?: number;
|
||||
connectedAt?: number;
|
||||
lastSyncAt?: number;
|
||||
lastWeeklySyncAt?: number;
|
||||
lastSyncOrigin?: string;
|
||||
syncWeeksAhead?: number;
|
||||
autoSyncWeekly?: boolean;
|
||||
pendingWeeklySync?: boolean;
|
||||
eventMap?: EventMapRecord;
|
||||
}
|
||||
|
||||
export interface OutlookCalendarStatus {
|
||||
configured: boolean;
|
||||
connected: boolean;
|
||||
lastSyncAt?: number;
|
||||
lastSyncOrigin?: string;
|
||||
}
|
||||
|
||||
export interface OutlookCalendarStoredState {
|
||||
accessToken?: string;
|
||||
refreshToken?: string;
|
||||
expiresAt?: number;
|
||||
connectedAt?: number;
|
||||
lastSyncAt?: number;
|
||||
lastSyncOrigin?: string;
|
||||
eventMap?: EventMapRecord;
|
||||
}
|
||||
|
||||
const googleStorage = createCalendarStateStorage<GoogleCalendarStoredState>(
|
||||
BSPLUS_GOOGLE_CALENDAR_STORAGE_KEY,
|
||||
);
|
||||
const outlookStorage = createCalendarStateStorage<OutlookCalendarStoredState>(
|
||||
BSPLUS_OUTLOOK_CALENDAR_STORAGE_KEY,
|
||||
);
|
||||
|
||||
export const readGoogleCalendarState = googleStorage.read;
|
||||
export const writeGoogleCalendarState = googleStorage.write;
|
||||
export const clearGoogleCalendarState = googleStorage.clear;
|
||||
|
||||
export const readOutlookCalendarState = outlookStorage.read;
|
||||
export const writeOutlookCalendarState = outlookStorage.write;
|
||||
export const clearOutlookCalendarState = outlookStorage.clear;
|
||||
@@ -0,0 +1,144 @@
|
||||
import { GOOGLE_CALENDAR_API } from "@/config/googleCalendar";
|
||||
import { OUTLOOK_GRAPH_API } from "@/config/outlookCalendar";
|
||||
|
||||
async function authorizedFetch(
|
||||
accessToken: string,
|
||||
url: string,
|
||||
init: RequestInit,
|
||||
refreshAccessToken?: () => Promise<string>,
|
||||
): Promise<Response> {
|
||||
const res = await fetch(url, {
|
||||
...init,
|
||||
headers: { Authorization: `Bearer ${accessToken}`, ...init.headers },
|
||||
});
|
||||
if (res.status === 401 && refreshAccessToken) {
|
||||
return authorizedFetch(await refreshAccessToken(), url, init);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
async function upsertRemoteEvent(
|
||||
accessToken: string,
|
||||
existingEventId: string | undefined,
|
||||
body: Record<string, unknown>,
|
||||
paths: { update: (id: string) => string; create: string },
|
||||
label: string,
|
||||
refreshAccessToken?: () => Promise<string>,
|
||||
): Promise<string> {
|
||||
const headers = { "Content-Type": "application/json" };
|
||||
|
||||
if (existingEventId) {
|
||||
const res = await authorizedFetch(
|
||||
accessToken,
|
||||
paths.update(existingEventId),
|
||||
{ method: "PATCH", headers, body: JSON.stringify(body) },
|
||||
refreshAccessToken,
|
||||
);
|
||||
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 ?? `${label} update failed (${res.status})`);
|
||||
}
|
||||
}
|
||||
|
||||
const res = await authorizedFetch(
|
||||
accessToken,
|
||||
paths.create,
|
||||
{ method: "POST", headers, body: JSON.stringify(body) },
|
||||
refreshAccessToken,
|
||||
);
|
||||
const json = (await res.json().catch(() => ({}))) as { id?: string; error?: { message?: string } };
|
||||
if (!res.ok || !json.id) {
|
||||
throw new Error(json?.error?.message ?? `${label} create failed (${res.status})`);
|
||||
}
|
||||
return json.id;
|
||||
}
|
||||
|
||||
async function deleteRemoteEvent(
|
||||
accessToken: string,
|
||||
eventUrl: string,
|
||||
label: string,
|
||||
refreshAccessToken?: () => Promise<string>,
|
||||
): Promise<void> {
|
||||
const res = await authorizedFetch(
|
||||
accessToken,
|
||||
eventUrl,
|
||||
{ method: "DELETE" },
|
||||
refreshAccessToken,
|
||||
);
|
||||
if (res.ok || res.status === 404 || res.status === 410) return;
|
||||
const err = (await res.json().catch(() => ({}))) as { error?: { message?: string } };
|
||||
throw new Error(err?.error?.message ?? `${label} delete failed (${res.status})`);
|
||||
}
|
||||
|
||||
const GOOGLE_CALENDAR_ID = "primary";
|
||||
|
||||
export function upsertGoogleCalendarEvent(
|
||||
accessToken: string,
|
||||
calendarId: string,
|
||||
existingEventId: string | undefined,
|
||||
body: Record<string, unknown>,
|
||||
refreshAccessToken?: () => Promise<string>,
|
||||
): Promise<string> {
|
||||
const encodedCalendar = encodeURIComponent(calendarId);
|
||||
return upsertRemoteEvent(
|
||||
accessToken,
|
||||
existingEventId,
|
||||
body,
|
||||
{
|
||||
update: (id) =>
|
||||
`${GOOGLE_CALENDAR_API}/calendars/${encodedCalendar}/events/${encodeURIComponent(id)}`,
|
||||
create: `${GOOGLE_CALENDAR_API}/calendars/${encodedCalendar}/events`,
|
||||
},
|
||||
"Google Calendar",
|
||||
refreshAccessToken,
|
||||
);
|
||||
}
|
||||
|
||||
export function deleteGoogleCalendarEvent(
|
||||
accessToken: string,
|
||||
calendarId: string,
|
||||
eventId: string,
|
||||
refreshAccessToken?: () => Promise<string>,
|
||||
): Promise<void> {
|
||||
return deleteRemoteEvent(
|
||||
accessToken,
|
||||
`${GOOGLE_CALENDAR_API}/calendars/${encodeURIComponent(calendarId)}/events/${encodeURIComponent(eventId)}`,
|
||||
"Google Calendar",
|
||||
refreshAccessToken,
|
||||
);
|
||||
}
|
||||
|
||||
export function upsertOutlookCalendarEvent(
|
||||
accessToken: string,
|
||||
existingEventId: string | undefined,
|
||||
body: Record<string, unknown>,
|
||||
refreshAccessToken?: () => Promise<string>,
|
||||
): Promise<string> {
|
||||
return upsertRemoteEvent(
|
||||
accessToken,
|
||||
existingEventId,
|
||||
body,
|
||||
{
|
||||
update: (id) => `${OUTLOOK_GRAPH_API}/me/events/${encodeURIComponent(id)}`,
|
||||
create: `${OUTLOOK_GRAPH_API}/me/events`,
|
||||
},
|
||||
"Outlook Calendar",
|
||||
refreshAccessToken,
|
||||
);
|
||||
}
|
||||
|
||||
export function deleteOutlookCalendarEvent(
|
||||
accessToken: string,
|
||||
eventId: string,
|
||||
refreshAccessToken?: () => Promise<string>,
|
||||
): Promise<void> {
|
||||
return deleteRemoteEvent(
|
||||
accessToken,
|
||||
`${OUTLOOK_GRAPH_API}/me/events/${encodeURIComponent(eventId)}`,
|
||||
"Outlook Calendar",
|
||||
refreshAccessToken,
|
||||
);
|
||||
}
|
||||
|
||||
export { GOOGLE_CALENDAR_ID };
|
||||
@@ -1,20 +1,53 @@
|
||||
import browser from "webextension-polyfill";
|
||||
import {
|
||||
GOOGLE_CALENDAR_SYNC_WEEKS_DEFAULT,
|
||||
GOOGLE_CALENDAR_SYNC_WEEKS,
|
||||
GOOGLE_CALENDAR_SYNC_WEEKS_MAX,
|
||||
GOOGLE_CALENDAR_SYNC_WEEKS_MIN,
|
||||
} from "@/config/googleCalendar";
|
||||
import { readOutlookCalendarState } from "@/seqta/utils/outlookCalendar/storage";
|
||||
import { readGoogleCalendarState } from "@/seqta/utils/googleCalendar/storage";
|
||||
import {
|
||||
readSharedCalendarSyncSettings,
|
||||
WEEKLY_SYNC_INTERVAL_MS,
|
||||
writeSharedCalendarSyncSettings,
|
||||
} from "./sharedSettings";
|
||||
import { readOutlookCalendarState } from "@/seqta/utils/outlookCalendar/storage";
|
||||
|
||||
export { CALENDAR_WEEKLY_ALARM, WEEKLY_SYNC_INTERVAL_MS } from "./sharedSettings";
|
||||
export const BSPLUS_CALENDAR_SYNC_SETTINGS_KEY = "bsplus_calendar_sync_settings";
|
||||
export const CALENDAR_WEEKLY_ALARM = "bsplus_calendar_weekly";
|
||||
export const WEEKLY_SYNC_INTERVAL_MS = 7 * 24 * 60 * 60 * 1000;
|
||||
|
||||
export interface SharedCalendarSyncSettings {
|
||||
syncWeeksAhead?: number;
|
||||
autoSyncWeekly?: boolean;
|
||||
lastWeeklySyncAt?: number;
|
||||
pendingWeeklySync?: boolean;
|
||||
}
|
||||
|
||||
export async function readSharedCalendarSyncSettings(): Promise<SharedCalendarSyncSettings> {
|
||||
const got = await browser.storage.local.get(BSPLUS_CALENDAR_SYNC_SETTINGS_KEY);
|
||||
const raw = got[BSPLUS_CALENDAR_SYNC_SETTINGS_KEY];
|
||||
const shared =
|
||||
raw && typeof raw === "object" && !Array.isArray(raw)
|
||||
? (raw as SharedCalendarSyncSettings)
|
||||
: {};
|
||||
|
||||
if (Object.keys(shared).length > 0) return shared;
|
||||
|
||||
const legacy = await readGoogleCalendarState();
|
||||
return {
|
||||
syncWeeksAhead: legacy.syncWeeksAhead,
|
||||
autoSyncWeekly: legacy.autoSyncWeekly,
|
||||
lastWeeklySyncAt: legacy.lastWeeklySyncAt,
|
||||
pendingWeeklySync: legacy.pendingWeeklySync,
|
||||
};
|
||||
}
|
||||
|
||||
export async function writeSharedCalendarSyncSettings(
|
||||
patch: Partial<SharedCalendarSyncSettings>,
|
||||
): Promise<SharedCalendarSyncSettings> {
|
||||
const current = await readSharedCalendarSyncSettings();
|
||||
const next = { ...current, ...patch };
|
||||
await browser.storage.local.set({ [BSPLUS_CALENDAR_SYNC_SETTINGS_KEY]: next });
|
||||
return next;
|
||||
}
|
||||
|
||||
export function clampSyncWeeks(weeks: number): number {
|
||||
if (!Number.isFinite(weeks)) return GOOGLE_CALENDAR_SYNC_WEEKS_DEFAULT;
|
||||
if (!Number.isFinite(weeks)) return GOOGLE_CALENDAR_SYNC_WEEKS;
|
||||
return Math.min(
|
||||
GOOGLE_CALENDAR_SYNC_WEEKS_MAX,
|
||||
Math.max(GOOGLE_CALENDAR_SYNC_WEEKS_MIN, Math.round(weeks)),
|
||||
@@ -23,7 +56,7 @@ export function clampSyncWeeks(weeks: number): number {
|
||||
|
||||
export async function getSyncWeeksAhead(): Promise<number> {
|
||||
const settings = await readSharedCalendarSyncSettings();
|
||||
return clampSyncWeeks(settings.syncWeeksAhead ?? GOOGLE_CALENDAR_SYNC_WEEKS_DEFAULT);
|
||||
return clampSyncWeeks(settings.syncWeeksAhead ?? GOOGLE_CALENDAR_SYNC_WEEKS);
|
||||
}
|
||||
|
||||
export async function getAutoSyncWeekly(): Promise<boolean> {
|
||||
@@ -31,7 +64,7 @@ export async function getAutoSyncWeekly(): Promise<boolean> {
|
||||
return settings.autoSyncWeekly !== false;
|
||||
}
|
||||
|
||||
async function isAnyCalendarConnected(): Promise<boolean> {
|
||||
export async function isAnyCalendarConnected(): Promise<boolean> {
|
||||
const [google, outlook] = await Promise.all([
|
||||
readGoogleCalendarState(),
|
||||
readOutlookCalendarState(),
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
import browser from "webextension-polyfill";
|
||||
import {
|
||||
GOOGLE_CALENDAR_SYNC_WEEKS_DEFAULT,
|
||||
} from "@/config/googleCalendar";
|
||||
import { readGoogleCalendarState } from "@/seqta/utils/googleCalendar/storage";
|
||||
|
||||
export const BSPLUS_CALENDAR_SYNC_SETTINGS_KEY = "bsplus_calendar_sync_settings";
|
||||
export const CALENDAR_WEEKLY_ALARM = "bsplus_calendar_weekly";
|
||||
export const WEEKLY_SYNC_INTERVAL_MS = 7 * 24 * 60 * 60 * 1000;
|
||||
|
||||
export interface SharedCalendarSyncSettings {
|
||||
syncWeeksAhead?: number;
|
||||
autoSyncWeekly?: boolean;
|
||||
lastWeeklySyncAt?: number;
|
||||
pendingWeeklySync?: boolean;
|
||||
}
|
||||
|
||||
export async function readSharedCalendarSyncSettings(): Promise<SharedCalendarSyncSettings> {
|
||||
const got = await browser.storage.local.get(BSPLUS_CALENDAR_SYNC_SETTINGS_KEY);
|
||||
const raw = got[BSPLUS_CALENDAR_SYNC_SETTINGS_KEY];
|
||||
const shared =
|
||||
raw && typeof raw === "object" && !Array.isArray(raw)
|
||||
? (raw as SharedCalendarSyncSettings)
|
||||
: {};
|
||||
|
||||
if (Object.keys(shared).length > 0) return shared;
|
||||
|
||||
const legacy = await readGoogleCalendarState();
|
||||
return {
|
||||
syncWeeksAhead: legacy.syncWeeksAhead,
|
||||
autoSyncWeekly: legacy.autoSyncWeekly,
|
||||
lastWeeklySyncAt: legacy.lastWeeklySyncAt,
|
||||
pendingWeeklySync: legacy.pendingWeeklySync,
|
||||
};
|
||||
}
|
||||
|
||||
export async function writeSharedCalendarSyncSettings(
|
||||
patch: Partial<SharedCalendarSyncSettings>,
|
||||
): Promise<SharedCalendarSyncSettings> {
|
||||
const current = await readSharedCalendarSyncSettings();
|
||||
const next = { ...current, ...patch };
|
||||
await browser.storage.local.set({ [BSPLUS_CALENDAR_SYNC_SETTINGS_KEY]: next });
|
||||
return next;
|
||||
}
|
||||
|
||||
export function defaultSyncWeeksAhead(): number {
|
||||
return GOOGLE_CALENDAR_SYNC_WEEKS_DEFAULT;
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
import { isGoogleCalendarConfigured } from "@/config/googleCalendar";
|
||||
import { isOutlookCalendarConfigured } from "@/config/outlookCalendar";
|
||||
import { getSyncWeeksAhead } from "@/seqta/utils/calendarSync/settings";
|
||||
import { eventMapKey } from "@/seqta/utils/calendarSync/eventMap";
|
||||
import type { EventMapRecord } from "@/seqta/utils/calendarSync/eventMap";
|
||||
import {
|
||||
buildDeleteSyncResult,
|
||||
buildLessonSyncResult,
|
||||
deleteTrackedLessonEvents,
|
||||
emptyLessonsSyncResult,
|
||||
entriesToPrune,
|
||||
notConfiguredSyncResult,
|
||||
notConnectedSyncResult,
|
||||
originEventMapEntries,
|
||||
reportSyncProgress,
|
||||
upsertLessonEvents,
|
||||
} from "@/seqta/utils/calendarSync/lessonSyncShared";
|
||||
import { googleApiEventBody, mapLessonsToGoogleEvents, outlookGraphEventBody } from "@/seqta/utils/googleCalendar/eventMapper";
|
||||
import {
|
||||
readGoogleCalendarState,
|
||||
writeGoogleCalendarState,
|
||||
} from "@/seqta/utils/googleCalendar/storage";
|
||||
import type {
|
||||
GoogleCalendarDeleteResult,
|
||||
GoogleCalendarEventInput,
|
||||
GoogleCalendarSyncOptions,
|
||||
GoogleCalendarSyncRequest,
|
||||
GoogleCalendarSyncResult,
|
||||
} from "@/seqta/utils/googleCalendar/types";
|
||||
import {
|
||||
deleteGoogleCalendarEvent,
|
||||
deleteOutlookCalendarEvent,
|
||||
GOOGLE_CALENDAR_ID,
|
||||
upsertGoogleCalendarEvent,
|
||||
upsertOutlookCalendarEvent,
|
||||
} from "@/seqta/utils/calendarSync/remoteEvents";
|
||||
import {
|
||||
readOutlookCalendarState,
|
||||
writeOutlookCalendarState,
|
||||
} from "@/seqta/utils/outlookCalendar/storage";
|
||||
|
||||
type CalendarStoredState = {
|
||||
refreshToken?: string;
|
||||
accessToken?: string;
|
||||
eventMap?: EventMapRecord;
|
||||
};
|
||||
|
||||
export type CalendarLessonSyncProvider = {
|
||||
label: string;
|
||||
isConfigured: () => boolean;
|
||||
notConfiguredError: string;
|
||||
notConnectedError: string;
|
||||
readState: () => Promise<CalendarStoredState>;
|
||||
writeState: (patch: {
|
||||
eventMap?: EventMapRecord;
|
||||
lastSyncAt?: number;
|
||||
lastSyncOrigin?: string;
|
||||
}) => Promise<unknown>;
|
||||
deleteEvent: (
|
||||
accessToken: string,
|
||||
eventId: string,
|
||||
refreshAccessToken: () => Promise<string>,
|
||||
) => Promise<void>;
|
||||
upsertEvent: (
|
||||
accessToken: string,
|
||||
existingId: string | undefined,
|
||||
body: Record<string, unknown>,
|
||||
refreshAccessToken: () => Promise<string>,
|
||||
) => Promise<string>;
|
||||
toApiBody: (event: GoogleCalendarEventInput) => Record<string, unknown>;
|
||||
};
|
||||
|
||||
export const googleLessonSyncProvider: CalendarLessonSyncProvider = {
|
||||
label: "Google Calendar",
|
||||
isConfigured: isGoogleCalendarConfigured,
|
||||
notConfiguredError: "Google Calendar is not configured in this extension build.",
|
||||
notConnectedError: "Connect Google Calendar first.",
|
||||
readState: readGoogleCalendarState,
|
||||
writeState: writeGoogleCalendarState,
|
||||
deleteEvent: (accessToken, eventId, refreshAccessToken) =>
|
||||
deleteGoogleCalendarEvent(accessToken, GOOGLE_CALENDAR_ID, eventId, refreshAccessToken),
|
||||
upsertEvent: (accessToken, existingId, body, refreshAccessToken) =>
|
||||
upsertGoogleCalendarEvent(
|
||||
accessToken,
|
||||
GOOGLE_CALENDAR_ID,
|
||||
existingId,
|
||||
body,
|
||||
refreshAccessToken,
|
||||
),
|
||||
toApiBody: googleApiEventBody,
|
||||
};
|
||||
|
||||
export const outlookLessonSyncProvider: CalendarLessonSyncProvider = {
|
||||
label: "Outlook Calendar",
|
||||
isConfigured: isOutlookCalendarConfigured,
|
||||
notConfiguredError: "Outlook Calendar is not configured in this extension build.",
|
||||
notConnectedError: "Connect Outlook Calendar first.",
|
||||
readState: readOutlookCalendarState,
|
||||
writeState: writeOutlookCalendarState,
|
||||
deleteEvent: (accessToken, eventId, refreshAccessToken) =>
|
||||
deleteOutlookCalendarEvent(accessToken, eventId, refreshAccessToken),
|
||||
upsertEvent: (accessToken, existingId, body, refreshAccessToken) =>
|
||||
upsertOutlookCalendarEvent(accessToken, existingId, body, refreshAccessToken),
|
||||
toApiBody: outlookGraphEventBody,
|
||||
};
|
||||
|
||||
/** Runs in the content script tab so long syncs are not killed by the MV3 service worker. */
|
||||
export async function syncLessonsToCalendar(
|
||||
provider: CalendarLessonSyncProvider,
|
||||
request: GoogleCalendarSyncRequest,
|
||||
getAccessToken: () => Promise<string>,
|
||||
options: GoogleCalendarSyncOptions = {},
|
||||
): Promise<GoogleCalendarSyncResult> {
|
||||
if (!provider.isConfigured()) {
|
||||
return notConfiguredSyncResult(provider.notConfiguredError);
|
||||
}
|
||||
|
||||
const state = await provider.readState();
|
||||
if (!state.refreshToken && !state.accessToken) {
|
||||
return notConnectedSyncResult(provider.notConnectedError);
|
||||
}
|
||||
|
||||
const mode = request.mode ?? "full";
|
||||
const weeksAhead = request.weeksAhead ?? (await getSyncWeeksAhead());
|
||||
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
|
||||
const events = mapLessonsToGoogleEvents(request.origin, request.lessons, timeZone);
|
||||
|
||||
if (events.length === 0 && mode === "full") {
|
||||
return emptyLessonsSyncResult();
|
||||
}
|
||||
|
||||
reportSyncProgress(options.onProgress, {
|
||||
phase: "preparing",
|
||||
current: 0,
|
||||
total: Math.max(events.length, 1),
|
||||
message: mode === "incremental" ? "Preparing weekly sync…" : "Preparing sync…",
|
||||
});
|
||||
|
||||
const eventMap = { ...(state.eventMap ?? {}) };
|
||||
const currentMapKeys = new Set(events.map((event) => eventMapKey(request.origin, event.seqtaKey)));
|
||||
const staleEntries = entriesToPrune(eventMap, request.origin, mode, weeksAhead, currentMapKeys);
|
||||
const totalSteps = staleEntries.length + events.length;
|
||||
const lastSyncAt = Date.now();
|
||||
|
||||
const staleResult = await deleteTrackedLessonEvents(
|
||||
staleEntries,
|
||||
eventMap,
|
||||
getAccessToken,
|
||||
provider.deleteEvent,
|
||||
provider.writeState,
|
||||
{
|
||||
onProgress: options.onProgress,
|
||||
progressTotal: totalSteps,
|
||||
logLabel: provider.label,
|
||||
},
|
||||
);
|
||||
|
||||
const upsertResult = await upsertLessonEvents({
|
||||
events,
|
||||
eventMap,
|
||||
origin: request.origin,
|
||||
staleEntryCount: staleEntries.length,
|
||||
totalSteps,
|
||||
lastSyncAt,
|
||||
initialFailed: staleResult.failed,
|
||||
getAccessToken,
|
||||
mapKey: eventMapKey,
|
||||
upsert: (accessToken, existingId, event, refreshAccessToken) =>
|
||||
provider.upsertEvent(
|
||||
accessToken,
|
||||
existingId,
|
||||
provider.toApiBody(event),
|
||||
refreshAccessToken,
|
||||
),
|
||||
writeState: provider.writeState,
|
||||
onProgress: options.onProgress,
|
||||
logLabel: provider.label,
|
||||
});
|
||||
|
||||
if (staleResult.deleted > 0 || staleEntries.length > 0 || events.length > 0) {
|
||||
await provider.writeState({
|
||||
eventMap,
|
||||
lastSyncAt,
|
||||
lastSyncOrigin: request.origin,
|
||||
});
|
||||
}
|
||||
|
||||
reportSyncProgress(options.onProgress, {
|
||||
phase: "done",
|
||||
current: totalSteps,
|
||||
total: totalSteps,
|
||||
message: "Sync complete",
|
||||
});
|
||||
|
||||
return buildLessonSyncResult(
|
||||
upsertResult.created,
|
||||
upsertResult.updated,
|
||||
staleResult.deleted,
|
||||
upsertResult.failed,
|
||||
lastSyncAt,
|
||||
);
|
||||
}
|
||||
|
||||
/** Delete all tracked BetterSEQTA+ events for this SEQTA origin from the provider calendar. */
|
||||
export async function deleteSyncedEventsFromCalendar(
|
||||
provider: CalendarLessonSyncProvider,
|
||||
origin: string,
|
||||
getAccessToken: () => Promise<string>,
|
||||
options: GoogleCalendarSyncOptions = {},
|
||||
): Promise<GoogleCalendarDeleteResult> {
|
||||
if (!provider.isConfigured()) {
|
||||
return {
|
||||
success: false,
|
||||
configured: false,
|
||||
error: provider.notConfiguredError,
|
||||
};
|
||||
}
|
||||
|
||||
const state = await provider.readState();
|
||||
if (!state.refreshToken && !state.accessToken) {
|
||||
return { success: false, configured: true, connected: false, error: provider.notConnectedError };
|
||||
}
|
||||
|
||||
const entries = originEventMapEntries(state.eventMap ?? {}, origin);
|
||||
if (entries.length === 0) {
|
||||
return { success: true, configured: true, connected: true, deleted: 0, failed: 0 };
|
||||
}
|
||||
|
||||
reportSyncProgress(options.onProgress, {
|
||||
phase: "preparing",
|
||||
current: 0,
|
||||
total: entries.length,
|
||||
message: "Preparing removal…",
|
||||
});
|
||||
|
||||
const eventMap = { ...(state.eventMap ?? {}) };
|
||||
const { deleted, failed } = await deleteTrackedLessonEvents(
|
||||
entries,
|
||||
eventMap,
|
||||
getAccessToken,
|
||||
provider.deleteEvent,
|
||||
provider.writeState,
|
||||
{
|
||||
persistProgress: true,
|
||||
onProgress: options.onProgress,
|
||||
progressTotal: entries.length,
|
||||
logLabel: provider.label,
|
||||
},
|
||||
);
|
||||
|
||||
await provider.writeState({ eventMap });
|
||||
|
||||
reportSyncProgress(options.onProgress, {
|
||||
phase: "done",
|
||||
current: entries.length,
|
||||
total: entries.length,
|
||||
message: "Removal complete",
|
||||
});
|
||||
|
||||
return buildDeleteSyncResult(deleted, failed);
|
||||
}
|
||||
|
||||
export const syncLessonsToGoogleCalendar = (
|
||||
request: GoogleCalendarSyncRequest,
|
||||
getAccessToken: () => Promise<string>,
|
||||
options?: GoogleCalendarSyncOptions,
|
||||
) => syncLessonsToCalendar(googleLessonSyncProvider, request, getAccessToken, options);
|
||||
|
||||
export const deleteSyncedEventsFromGoogleCalendar = (
|
||||
origin: string,
|
||||
getAccessToken: () => Promise<string>,
|
||||
options?: GoogleCalendarSyncOptions,
|
||||
) => deleteSyncedEventsFromCalendar(googleLessonSyncProvider, origin, getAccessToken, options);
|
||||
|
||||
export const syncLessonsToOutlookCalendar = (
|
||||
request: GoogleCalendarSyncRequest,
|
||||
getAccessToken: () => Promise<string>,
|
||||
options?: GoogleCalendarSyncOptions,
|
||||
) => syncLessonsToCalendar(outlookLessonSyncProvider, request, getAccessToken, options);
|
||||
|
||||
export const deleteSyncedEventsFromOutlookCalendar = (
|
||||
origin: string,
|
||||
getAccessToken: () => Promise<string>,
|
||||
options?: GoogleCalendarSyncOptions,
|
||||
) => deleteSyncedEventsFromCalendar(outlookLessonSyncProvider, origin, getAccessToken, options);
|
||||
@@ -0,0 +1,86 @@
|
||||
import browser from "webextension-polyfill";
|
||||
import { fetchTimetableForSync, fetchTimetableLessons } from "@/seqta/utils/googleCalendar/fetchTimetable";
|
||||
import { trailingWeekRange } from "@/seqta/utils/googleCalendar/syncDateRange";
|
||||
import { getSyncWeeksAhead } from "@/seqta/utils/calendarSync/settings";
|
||||
import { reportSyncProgress } from "@/seqta/utils/calendarSync/lessonSyncShared";
|
||||
import {
|
||||
googleLessonSyncProvider,
|
||||
outlookLessonSyncProvider,
|
||||
syncLessonsToCalendar,
|
||||
type CalendarLessonSyncProvider,
|
||||
} from "@/seqta/utils/calendarSync/syncEngine";
|
||||
import type {
|
||||
GoogleCalendarSyncProgress,
|
||||
GoogleCalendarSyncResult,
|
||||
} from "@/seqta/utils/googleCalendar/types";
|
||||
|
||||
export interface RunCalendarSyncParams {
|
||||
mode?: "full" | "incremental";
|
||||
onProgress?: (progress: GoogleCalendarSyncProgress) => void;
|
||||
}
|
||||
|
||||
type CalendarSyncRunnerConfig = {
|
||||
accessTokenMessageType: string;
|
||||
accessTokenError: string;
|
||||
lessonSyncProvider: CalendarLessonSyncProvider;
|
||||
};
|
||||
|
||||
const GOOGLE_SYNC_RUNNER: CalendarSyncRunnerConfig = {
|
||||
accessTokenMessageType: "googleCalendarGetAccessToken",
|
||||
accessTokenError: "Could not get Google Calendar access token.",
|
||||
lessonSyncProvider: googleLessonSyncProvider,
|
||||
};
|
||||
|
||||
const OUTLOOK_SYNC_RUNNER: CalendarSyncRunnerConfig = {
|
||||
accessTokenMessageType: "outlookCalendarGetAccessToken",
|
||||
accessTokenError: "Could not get Outlook Calendar access token.",
|
||||
lessonSyncProvider: outlookLessonSyncProvider,
|
||||
};
|
||||
|
||||
async function getAccessTokenFromBackground(
|
||||
messageType: string,
|
||||
errorMessage: string,
|
||||
): Promise<string> {
|
||||
const res = (await browser.runtime.sendMessage({ type: messageType })) as {
|
||||
success?: boolean;
|
||||
accessToken?: string;
|
||||
error?: string;
|
||||
};
|
||||
if (!res?.success || !res.accessToken) {
|
||||
throw new Error(res?.error ?? errorMessage);
|
||||
}
|
||||
return res.accessToken;
|
||||
}
|
||||
|
||||
export async function runCalendarSync(
|
||||
config: CalendarSyncRunnerConfig,
|
||||
params: RunCalendarSyncParams = {},
|
||||
): Promise<GoogleCalendarSyncResult> {
|
||||
const mode = params.mode ?? "full";
|
||||
const weeksAhead = await getSyncWeeksAhead();
|
||||
|
||||
reportSyncProgress(params.onProgress, {
|
||||
phase: "preparing",
|
||||
current: 0,
|
||||
total: 1,
|
||||
message: mode === "incremental" ? "Fetching new week…" : "Fetching timetable…",
|
||||
});
|
||||
|
||||
const lessons =
|
||||
mode === "incremental"
|
||||
? await fetchTimetableLessons(trailingWeekRange(weeksAhead))
|
||||
: await fetchTimetableForSync(weeksAhead);
|
||||
|
||||
return syncLessonsToCalendar(
|
||||
config.lessonSyncProvider,
|
||||
{ origin: location.origin, lessons, mode, weeksAhead },
|
||||
() => getAccessTokenFromBackground(config.accessTokenMessageType, config.accessTokenError),
|
||||
{ onProgress: params.onProgress },
|
||||
);
|
||||
}
|
||||
|
||||
export const runGoogleCalendarSync = (params?: RunCalendarSyncParams) =>
|
||||
runCalendarSync(GOOGLE_SYNC_RUNNER, params);
|
||||
|
||||
export const runOutlookCalendarSync = (params?: RunCalendarSyncParams) =>
|
||||
runCalendarSync(OUTLOOK_SYNC_RUNNER, params);
|
||||
@@ -1,51 +0,0 @@
|
||||
import type { GoogleCalendarSyncResult } from "@/seqta/utils/googleCalendar/types";
|
||||
import { formatSyncResultMessage } from "@/seqta/utils/googleCalendar/syncRunner";
|
||||
import { formatOutlookSyncResultMessage } from "@/seqta/utils/outlookCalendar/syncRunner";
|
||||
|
||||
type ProviderCalendarState = {
|
||||
refreshToken?: string;
|
||||
accessToken?: string;
|
||||
};
|
||||
|
||||
function isProviderConnected(state: ProviderCalendarState): boolean {
|
||||
return Boolean(state.refreshToken || state.accessToken);
|
||||
}
|
||||
|
||||
function providerHadChanges(result: GoogleCalendarSyncResult): boolean {
|
||||
return (
|
||||
(result.created ?? 0) + (result.updated ?? 0) + (result.deleted ?? 0) > 0
|
||||
);
|
||||
}
|
||||
|
||||
export function weeklySyncErrorMessage(
|
||||
results: GoogleCalendarSyncResult[],
|
||||
): string | undefined {
|
||||
const failed = results.find((result) => !result.success);
|
||||
if (!failed) return undefined;
|
||||
return failed.error ?? "Weekly calendar sync failed.";
|
||||
}
|
||||
|
||||
export function formatWeeklySyncMessages(
|
||||
google: ProviderCalendarState,
|
||||
outlook: ProviderCalendarState,
|
||||
results: GoogleCalendarSyncResult[],
|
||||
): string[] {
|
||||
const messages: string[] = [];
|
||||
let index = 0;
|
||||
|
||||
if (isProviderConnected(google)) {
|
||||
const result = results[index++];
|
||||
if (result && providerHadChanges(result)) {
|
||||
messages.push(formatSyncResultMessage(result));
|
||||
}
|
||||
}
|
||||
|
||||
if (isProviderConnected(outlook)) {
|
||||
const result = results[index++];
|
||||
if (result && providerHadChanges(result)) {
|
||||
messages.push(formatOutlookSyncResultMessage(result));
|
||||
}
|
||||
}
|
||||
|
||||
return messages;
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
import {
|
||||
GOOGLE_CALENDAR_ACCOUNTS_NOT_READY_HINT,
|
||||
GOOGLE_CALENDAR_REFRESH_URL,
|
||||
GOOGLE_CALENDAR_TOKEN_URL,
|
||||
} from "@/config/googleCalendar";
|
||||
|
||||
type GoogleTokenPayload = {
|
||||
access_token: string;
|
||||
refresh_token?: string;
|
||||
expires_in?: number;
|
||||
};
|
||||
|
||||
async function parseAccountsJson(res: Response): Promise<Record<string, unknown>> {
|
||||
const text = await res.text();
|
||||
try {
|
||||
return text ? (JSON.parse(text) as Record<string, unknown>) : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function extractTokens(json: Record<string, unknown>): GoogleTokenPayload {
|
||||
const access_token = json.access_token;
|
||||
if (typeof access_token !== "string" || !access_token) {
|
||||
throw new Error("Token response missing access_token");
|
||||
}
|
||||
return {
|
||||
access_token,
|
||||
refresh_token: typeof json.refresh_token === "string" ? json.refresh_token : undefined,
|
||||
expires_in: typeof json.expires_in === "number" ? json.expires_in : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function formatAccountsTokenError(res: Response, json: Record<string, unknown>): string {
|
||||
if (res.status === 404 || res.status === 501) {
|
||||
return GOOGLE_CALENDAR_ACCOUNTS_NOT_READY_HINT;
|
||||
}
|
||||
const err = typeof json.error === "string" ? json.error : "";
|
||||
return err || `Accounts token API failed (${res.status})`;
|
||||
}
|
||||
|
||||
export async function exchangeGoogleCodeViaAccounts(
|
||||
code: string,
|
||||
redirectUri: string,
|
||||
codeVerifier: string,
|
||||
): Promise<GoogleTokenPayload> {
|
||||
const res = await fetch(GOOGLE_CALENDAR_TOKEN_URL, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
code,
|
||||
redirect_uri: redirectUri,
|
||||
code_verifier: codeVerifier,
|
||||
}),
|
||||
});
|
||||
const json = await parseAccountsJson(res);
|
||||
if (!res.ok) {
|
||||
throw new Error(formatAccountsTokenError(res, json));
|
||||
}
|
||||
return extractTokens(json);
|
||||
}
|
||||
|
||||
export async function refreshGoogleTokenViaAccounts(
|
||||
refreshToken: string,
|
||||
): Promise<GoogleTokenPayload> {
|
||||
const res = await fetch(GOOGLE_CALENDAR_REFRESH_URL, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ refresh_token: refreshToken }),
|
||||
});
|
||||
const json = await parseAccountsJson(res);
|
||||
if (!res.ok) {
|
||||
throw new Error(formatAccountsTokenError(res, json));
|
||||
}
|
||||
return extractTokens(json);
|
||||
}
|
||||
@@ -3,33 +3,38 @@ import {
|
||||
markWeeklySyncComplete,
|
||||
shouldRunWeeklySync,
|
||||
} from "@/seqta/utils/calendarSync/settings";
|
||||
import {
|
||||
formatWeeklySyncMessages,
|
||||
weeklySyncErrorMessage,
|
||||
} from "@/seqta/utils/calendarSync/weeklySyncMessages";
|
||||
import { runGoogleCalendarSync } from "@/seqta/utils/googleCalendar/syncRunner";
|
||||
import { runOutlookCalendarSync } from "@/seqta/utils/outlookCalendar/syncRunner";
|
||||
import { formatLessonSyncResultMessage } from "@/seqta/utils/calendarSync/lessonSyncShared";
|
||||
import { runGoogleCalendarSync, runOutlookCalendarSync } from "@/seqta/utils/calendarSync/syncRunner";
|
||||
import { readGoogleCalendarState } from "@/seqta/utils/googleCalendar/storage";
|
||||
import { readOutlookCalendarState } from "@/seqta/utils/outlookCalendar/storage";
|
||||
import type { GoogleCalendarSyncResult } from "@/seqta/utils/googleCalendar/types";
|
||||
|
||||
let listenerRegistered = false;
|
||||
|
||||
async function runWeeklySyncForConnectedProviders(): Promise<GoogleCalendarSyncResult[]> {
|
||||
const [google, outlook] = await Promise.all([
|
||||
readGoogleCalendarState(),
|
||||
readOutlookCalendarState(),
|
||||
]);
|
||||
const results: GoogleCalendarSyncResult[] = [];
|
||||
const WEEKLY_PROVIDERS = [
|
||||
{ label: "Google Calendar", read: readGoogleCalendarState, run: runGoogleCalendarSync },
|
||||
{ label: "Outlook Calendar", read: readOutlookCalendarState, run: runOutlookCalendarSync },
|
||||
] as const;
|
||||
|
||||
if (google.refreshToken || google.accessToken) {
|
||||
results.push(await runGoogleCalendarSync({ mode: "incremental", silent: true }));
|
||||
}
|
||||
if (outlook.refreshToken || outlook.accessToken) {
|
||||
results.push(await runOutlookCalendarSync({ mode: "incremental", silent: true }));
|
||||
function isConnected(state: { refreshToken?: string; accessToken?: string }): boolean {
|
||||
return Boolean(state.refreshToken || state.accessToken);
|
||||
}
|
||||
|
||||
if (results.some((r) => r.success)) {
|
||||
function hadChanges(result: GoogleCalendarSyncResult): boolean {
|
||||
return (result.created ?? 0) + (result.updated ?? 0) + (result.deleted ?? 0) > 0;
|
||||
}
|
||||
|
||||
async function runWeeklySyncForConnectedProviders(): Promise<
|
||||
Array<{ label: string; result: GoogleCalendarSyncResult }>
|
||||
> {
|
||||
const results: Array<{ label: string; result: GoogleCalendarSyncResult }> = [];
|
||||
|
||||
for (const provider of WEEKLY_PROVIDERS) {
|
||||
if (!isConnected(await provider.read())) continue;
|
||||
results.push({ label: provider.label, result: await provider.run({ mode: "incremental" }) });
|
||||
}
|
||||
|
||||
if (results.some(({ result }) => result.success)) {
|
||||
await markWeeklySyncComplete();
|
||||
}
|
||||
|
||||
@@ -61,24 +66,20 @@ export async function maybeRunDueWeeklySync(
|
||||
): Promise<void> {
|
||||
if (!(await shouldRunWeeklySync())) return;
|
||||
|
||||
const [google, outlook] = await Promise.all([
|
||||
readGoogleCalendarState(),
|
||||
readOutlookCalendarState(),
|
||||
]);
|
||||
const results = await runWeeklySyncForConnectedProviders();
|
||||
if (!onComplete) return;
|
||||
|
||||
const errorMessage = weeklySyncErrorMessage(results);
|
||||
if (errorMessage) {
|
||||
onComplete(errorMessage, true);
|
||||
const failed = results.find(({ result }) => !result.success);
|
||||
if (failed) {
|
||||
onComplete(failed.result.error ?? "Weekly calendar sync failed.", true);
|
||||
return;
|
||||
}
|
||||
|
||||
const messages = formatWeeklySyncMessages(google, outlook, results);
|
||||
const messages = results
|
||||
.filter(({ result }) => hadChanges(result))
|
||||
.map(({ label, result }) => formatLessonSyncResultMessage(result, label));
|
||||
|
||||
if (messages.length > 0) {
|
||||
onComplete(messages.join(" "));
|
||||
}
|
||||
}
|
||||
|
||||
/** @deprecated use registerCalendarContentHandlers */
|
||||
export const registerGoogleCalendarContentHandlers = registerCalendarContentHandlers;
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
export interface GoogleCalendarEventMapEntry {
|
||||
id: string;
|
||||
date: string;
|
||||
}
|
||||
|
||||
export function normalizeEventMapEntry(
|
||||
value: string | GoogleCalendarEventMapEntry | undefined,
|
||||
): GoogleCalendarEventMapEntry | undefined {
|
||||
if (value == null) return undefined;
|
||||
if (typeof value === "string") return { id: value, date: "" };
|
||||
if (typeof value.id === "string" && value.id.length > 0) {
|
||||
return { id: value.id, date: value.date ?? "" };
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function getStoredEventId(
|
||||
value: string | GoogleCalendarEventMapEntry | undefined,
|
||||
): string | undefined {
|
||||
return normalizeEventMapEntry(value)?.id;
|
||||
}
|
||||
|
||||
export function lessonDateFromSeqtaKey(seqtaKey: string): string | undefined {
|
||||
const parts = seqtaKey.split(":");
|
||||
for (const part of parts) {
|
||||
if (/^\d{4}-\d{2}-\d{2}$/.test(part)) return part;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { BSPLUS_GOOGLE_CALENDAR_EVENT_PROP } from "@/config/googleCalendar";
|
||||
import { BSPLUS_OUTLOOK_CALENDAR_EVENT_CATEGORY } from "@/config/outlookCalendar";
|
||||
import type { GoogleCalendarEventInput, SeqtaTimetableLesson } from "./types";
|
||||
|
||||
const SKIP_TYPES = new Set(["note", "holiday", "assembly-note"]);
|
||||
@@ -93,3 +94,20 @@ export function googleApiEventBody(event: GoogleCalendarEventInput): Record<stri
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function outlookGraphEventBody(event: GoogleCalendarEventInput): Record<string, unknown> {
|
||||
const body: Record<string, unknown> = {
|
||||
subject: event.summary,
|
||||
body: {
|
||||
contentType: "text",
|
||||
content: event.description ?? "Synced by BetterSEQTA+",
|
||||
},
|
||||
start: { dateTime: event.startDateTime, timeZone: event.timeZone },
|
||||
end: { dateTime: event.endDateTime, timeZone: event.timeZone },
|
||||
categories: [BSPLUS_OUTLOOK_CALENDAR_EVENT_CATEGORY],
|
||||
};
|
||||
if (event.location) {
|
||||
body.location = { displayName: event.location };
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
@@ -76,5 +76,3 @@ export async function fetchTimetableLessons(
|
||||
export async function fetchTimetableForSync(weeksAhead?: number): Promise<SeqtaTimetableLesson[]> {
|
||||
return fetchTimetableLessons(syncWindowRange(weeksAhead));
|
||||
}
|
||||
|
||||
export { syncWindowRange, trailingWeekRange, droppedWeekRange } from "./syncDateRange";
|
||||
|
||||
@@ -1,44 +1,9 @@
|
||||
import browser from "webextension-polyfill";
|
||||
import type { GoogleCalendarEventMapEntry } from "./eventMapEntry";
|
||||
export {
|
||||
BSPLUS_GOOGLE_CALENDAR_STORAGE_KEY,
|
||||
clearGoogleCalendarState,
|
||||
readGoogleCalendarState,
|
||||
writeGoogleCalendarState,
|
||||
type GoogleCalendarStoredState,
|
||||
} from "@/seqta/utils/calendarSync/providerStorage";
|
||||
|
||||
/** Never uploaded to BetterSEQTA Cloud (OAuth tokens + per-device event map). */
|
||||
export const BSPLUS_GOOGLE_CALENDAR_STORAGE_KEY = "bsplus_google_calendar";
|
||||
|
||||
export interface GoogleCalendarStoredState {
|
||||
accessToken?: string;
|
||||
refreshToken?: string;
|
||||
expiresAt?: number;
|
||||
connectedAt?: number;
|
||||
lastSyncAt?: number;
|
||||
lastWeeklySyncAt?: number;
|
||||
lastSyncOrigin?: string;
|
||||
syncWeeksAhead?: number;
|
||||
autoSyncWeekly?: boolean;
|
||||
pendingWeeklySync?: boolean;
|
||||
/** `${origin}::${seqtaKey}` → Google event id (+ lesson date when known) */
|
||||
eventMap?: Record<string, string | GoogleCalendarEventMapEntry>;
|
||||
}
|
||||
|
||||
export async function readGoogleCalendarState(): Promise<GoogleCalendarStoredState> {
|
||||
const got = await browser.storage.local.get(BSPLUS_GOOGLE_CALENDAR_STORAGE_KEY);
|
||||
const raw = got[BSPLUS_GOOGLE_CALENDAR_STORAGE_KEY];
|
||||
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return {};
|
||||
return raw as GoogleCalendarStoredState;
|
||||
}
|
||||
|
||||
export async function writeGoogleCalendarState(
|
||||
patch: Partial<GoogleCalendarStoredState>,
|
||||
): Promise<GoogleCalendarStoredState> {
|
||||
const current = await readGoogleCalendarState();
|
||||
const next: GoogleCalendarStoredState = { ...current, ...patch };
|
||||
await browser.storage.local.set({ [BSPLUS_GOOGLE_CALENDAR_STORAGE_KEY]: next });
|
||||
return next;
|
||||
}
|
||||
|
||||
export async function clearGoogleCalendarState(): Promise<void> {
|
||||
await browser.storage.local.remove(BSPLUS_GOOGLE_CALENDAR_STORAGE_KEY);
|
||||
}
|
||||
|
||||
export function eventMapKey(origin: string, seqtaKey: string): string {
|
||||
return `${origin}::${seqtaKey}`;
|
||||
}
|
||||
export type { EventMapRecord as GoogleCalendarEventMapRecord } from "@/seqta/utils/calendarSync/eventMap";
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
import {
|
||||
GOOGLE_CALENDAR_SYNC_WEEKS_DEFAULT,
|
||||
} from "@/config/googleCalendar";
|
||||
import { GOOGLE_CALENDAR_SYNC_WEEKS } from "@/config/googleCalendar";
|
||||
import { toISODate, weekRangeContaining } from "@/seqta/utils/Loaders/engageParentTimetable";
|
||||
|
||||
export interface SyncDateRange {
|
||||
@@ -12,8 +10,15 @@ function parseLocalDate(iso: string): Date {
|
||||
return new Date(`${iso}T12:00:00`);
|
||||
}
|
||||
|
||||
function weekEndingOn(until: string): SyncDateRange {
|
||||
const end = parseLocalDate(until);
|
||||
const start = new Date(end);
|
||||
start.setDate(start.getDate() - 6);
|
||||
return { from: toISODate(start), until };
|
||||
}
|
||||
|
||||
/** Full rolling sync window from the start of the current week. */
|
||||
export function syncWindowRange(weeksAhead = GOOGLE_CALENDAR_SYNC_WEEKS_DEFAULT): SyncDateRange {
|
||||
export function syncWindowRange(weeksAhead = GOOGLE_CALENDAR_SYNC_WEEKS): SyncDateRange {
|
||||
const { from } = weekRangeContaining(new Date());
|
||||
const end = parseLocalDate(from);
|
||||
end.setDate(end.getDate() + weeksAhead * 7 - 1);
|
||||
@@ -21,23 +26,15 @@ export function syncWindowRange(weeksAhead = GOOGLE_CALENDAR_SYNC_WEEKS_DEFAULT)
|
||||
}
|
||||
|
||||
/** The trailing week at the end of the sync window (added each weekly roll). */
|
||||
export function trailingWeekRange(weeksAhead = GOOGLE_CALENDAR_SYNC_WEEKS_DEFAULT): SyncDateRange {
|
||||
const { from: windowStart } = syncWindowRange(weeksAhead);
|
||||
const start = parseLocalDate(windowStart);
|
||||
start.setDate(start.getDate() + (weeksAhead - 1) * 7);
|
||||
const end = new Date(start);
|
||||
end.setDate(end.getDate() + 6);
|
||||
return { from: toISODate(start), until: toISODate(end) };
|
||||
export function trailingWeekRange(weeksAhead = GOOGLE_CALENDAR_SYNC_WEEKS): SyncDateRange {
|
||||
return weekEndingOn(syncWindowRange(weeksAhead).until);
|
||||
}
|
||||
|
||||
/** The week that rolled off when the window advances (removed each weekly roll). */
|
||||
export function droppedWeekRange(weeksAhead = GOOGLE_CALENDAR_SYNC_WEEKS_DEFAULT): SyncDateRange {
|
||||
const { from: windowStart } = syncWindowRange(weeksAhead);
|
||||
const end = parseLocalDate(windowStart);
|
||||
export function droppedWeekRange(weeksAhead = GOOGLE_CALENDAR_SYNC_WEEKS): SyncDateRange {
|
||||
const end = parseLocalDate(syncWindowRange(weeksAhead).from);
|
||||
end.setDate(end.getDate() - 1);
|
||||
const start = new Date(end);
|
||||
start.setDate(start.getDate() - 6);
|
||||
return { from: toISODate(start), until: toISODate(end) };
|
||||
return weekEndingOn(toISODate(end));
|
||||
}
|
||||
|
||||
export function isDateInRange(date: string, range: SyncDateRange): boolean {
|
||||
|
||||
@@ -9,17 +9,22 @@ jest.mock("@/utils/verboseLog", () => ({
|
||||
verboseLog: jest.fn(),
|
||||
}));
|
||||
|
||||
jest.mock("@/seqta/utils/googleCalendar/storage", () => ({
|
||||
eventMapKey: (origin: string, seqtaKey: string) => `${origin}::${seqtaKey}`,
|
||||
jest.mock("@/seqta/utils/googleCalendar/storage", () => {
|
||||
const actual = jest.requireActual<typeof import("@/seqta/utils/googleCalendar/storage")>(
|
||||
"@/seqta/utils/googleCalendar/storage",
|
||||
);
|
||||
return {
|
||||
...actual,
|
||||
readGoogleCalendarState: jest.fn(),
|
||||
writeGoogleCalendarState: jest.fn(async (patch: unknown) => patch),
|
||||
}));
|
||||
};
|
||||
});
|
||||
|
||||
jest.mock("@/seqta/utils/calendarSync/settings", () => ({
|
||||
getSyncWeeksAhead: jest.fn(async () => 12),
|
||||
}));
|
||||
|
||||
jest.mock("@/seqta/utils/googleCalendar/upsertEvent", () => ({
|
||||
jest.mock("@/seqta/utils/calendarSync/remoteEvents", () => ({
|
||||
upsertGoogleCalendarEvent: jest.fn(),
|
||||
deleteGoogleCalendarEvent: jest.fn(),
|
||||
}));
|
||||
@@ -28,8 +33,8 @@ import { readGoogleCalendarState } from "@/seqta/utils/googleCalendar/storage";
|
||||
import {
|
||||
deleteGoogleCalendarEvent,
|
||||
upsertGoogleCalendarEvent,
|
||||
} from "@/seqta/utils/googleCalendar/upsertEvent";
|
||||
import { deleteSyncedEventsFromGoogleCalendar, syncLessonsToGoogleCalendar } from "./syncEngine";
|
||||
} from "@/seqta/utils/calendarSync/remoteEvents";
|
||||
import { deleteSyncedEventsFromGoogleCalendar, syncLessonsToGoogleCalendar } from "@/seqta/utils/calendarSync/syncEngine";
|
||||
|
||||
const ORIGIN = "https://school.seqta.com.au";
|
||||
const getAccessToken = async () => "test-token";
|
||||
|
||||
@@ -1,250 +0,0 @@
|
||||
import { verboseLog } from "@/utils/verboseLog";
|
||||
import { isGoogleCalendarConfigured } from "@/config/googleCalendar";
|
||||
import { googleApiEventBody, mapLessonsToGoogleEvents } from "@/seqta/utils/googleCalendar/eventMapper";
|
||||
import { getSyncWeeksAhead } from "@/seqta/utils/calendarSync/settings";
|
||||
import {
|
||||
buildLessonSyncResult,
|
||||
emptyLessonsSyncResult,
|
||||
entriesToPrune,
|
||||
EVENT_MAP_PERSIST_EVERY,
|
||||
notConfiguredSyncResult,
|
||||
notConnectedSyncResult,
|
||||
originEventMapEntries,
|
||||
persistFinalSyncState,
|
||||
reportSyncProgress,
|
||||
upsertLessonEvents,
|
||||
} from "@/seqta/utils/calendarSync/lessonSyncShared";
|
||||
import {
|
||||
eventMapKey,
|
||||
readGoogleCalendarState,
|
||||
writeGoogleCalendarState,
|
||||
} from "@/seqta/utils/googleCalendar/storage";
|
||||
import type {
|
||||
GoogleCalendarDeleteResult,
|
||||
GoogleCalendarSyncOptions,
|
||||
GoogleCalendarSyncRequest,
|
||||
GoogleCalendarSyncResult,
|
||||
} from "@/seqta/utils/googleCalendar/types";
|
||||
import {
|
||||
deleteGoogleCalendarEvent,
|
||||
upsertGoogleCalendarEvent,
|
||||
} from "@/seqta/utils/googleCalendar/upsertEvent";
|
||||
|
||||
const CALENDAR_ID = "primary";
|
||||
|
||||
type DeleteTrackedEventsResult = {
|
||||
deleted: number;
|
||||
failed: number;
|
||||
};
|
||||
|
||||
async function deleteTrackedEventsFromGoogle(
|
||||
entries: Array<[string, string]>,
|
||||
eventMap: Record<string, string | { id: string; date: string }>,
|
||||
getAccessToken: () => Promise<string>,
|
||||
persistProgress = false,
|
||||
onProgress?: GoogleCalendarSyncOptions["onProgress"],
|
||||
progressOffset = 0,
|
||||
progressTotal = 0,
|
||||
): Promise<DeleteTrackedEventsResult> {
|
||||
if (entries.length === 0) return { deleted: 0, failed: 0 };
|
||||
|
||||
let accessToken = await getAccessToken();
|
||||
let deleted = 0;
|
||||
let failed = 0;
|
||||
|
||||
for (let i = 0; i < entries.length; i++) {
|
||||
const [mapKey, eventId] = entries[i];
|
||||
try {
|
||||
await deleteGoogleCalendarEvent(accessToken, CALENDAR_ID, eventId, async () => {
|
||||
accessToken = await getAccessToken();
|
||||
return accessToken;
|
||||
});
|
||||
delete eventMap[mapKey];
|
||||
deleted += 1;
|
||||
|
||||
reportSyncProgress(onProgress, {
|
||||
phase: "deleting",
|
||||
current: progressOffset + deleted + failed,
|
||||
total: progressTotal,
|
||||
message: `Removing old events (${deleted + failed}/${entries.length})…`,
|
||||
});
|
||||
|
||||
if (persistProgress && (deleted + failed) % EVENT_MAP_PERSIST_EVERY === 0) {
|
||||
await writeGoogleCalendarState({ eventMap });
|
||||
}
|
||||
} catch (err) {
|
||||
verboseLog("[BetterSEQTA+] Google Calendar event delete failed:", err);
|
||||
failed += 1;
|
||||
reportSyncProgress(onProgress, {
|
||||
phase: "deleting",
|
||||
current: progressOffset + deleted + failed,
|
||||
total: progressTotal,
|
||||
message: `Removing old events (${deleted + failed}/${entries.length})…`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return { deleted, failed };
|
||||
}
|
||||
|
||||
/** Runs in the content script tab so long syncs are not killed by the MV3 service worker. */
|
||||
export async function syncLessonsToGoogleCalendar(
|
||||
request: GoogleCalendarSyncRequest,
|
||||
getAccessToken: () => Promise<string>,
|
||||
options: GoogleCalendarSyncOptions = {},
|
||||
): Promise<GoogleCalendarSyncResult> {
|
||||
if (!isGoogleCalendarConfigured()) {
|
||||
return notConfiguredSyncResult(
|
||||
"Google Calendar is not configured in this extension build.",
|
||||
);
|
||||
}
|
||||
|
||||
const state = await readGoogleCalendarState();
|
||||
if (!state.refreshToken && !state.accessToken) {
|
||||
return notConnectedSyncResult("Connect Google Calendar first.");
|
||||
}
|
||||
|
||||
const mode = request.mode ?? "full";
|
||||
const weeksAhead = request.weeksAhead ?? (await getSyncWeeksAhead());
|
||||
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
|
||||
const events = mapLessonsToGoogleEvents(request.origin, request.lessons, timeZone);
|
||||
|
||||
if (events.length === 0 && mode === "full") {
|
||||
return emptyLessonsSyncResult();
|
||||
}
|
||||
|
||||
reportSyncProgress(options.onProgress, {
|
||||
phase: "preparing",
|
||||
current: 0,
|
||||
total: Math.max(events.length, 1),
|
||||
message: mode === "incremental" ? "Preparing weekly sync…" : "Preparing sync…",
|
||||
});
|
||||
|
||||
const eventMap = { ...(state.eventMap ?? {}) };
|
||||
const currentMapKeys = new Set(events.map((event) => eventMapKey(request.origin, event.seqtaKey)));
|
||||
const staleEntries = entriesToPrune(eventMap, request.origin, mode, weeksAhead, currentMapKeys);
|
||||
const totalSteps = staleEntries.length + events.length;
|
||||
const lastSyncAt = Date.now();
|
||||
|
||||
const staleResult = await deleteTrackedEventsFromGoogle(
|
||||
staleEntries,
|
||||
eventMap,
|
||||
getAccessToken,
|
||||
false,
|
||||
options.onProgress,
|
||||
0,
|
||||
totalSteps,
|
||||
);
|
||||
|
||||
const upsertResult = await upsertLessonEvents({
|
||||
events,
|
||||
eventMap,
|
||||
origin: request.origin,
|
||||
staleEntryCount: staleEntries.length,
|
||||
totalSteps,
|
||||
lastSyncAt,
|
||||
initialFailed: staleResult.failed,
|
||||
getAccessToken,
|
||||
mapKey: eventMapKey,
|
||||
upsert: (accessToken, existingId, event, refreshAccessToken) =>
|
||||
upsertGoogleCalendarEvent(
|
||||
accessToken,
|
||||
CALENDAR_ID,
|
||||
existingId,
|
||||
googleApiEventBody(event),
|
||||
refreshAccessToken,
|
||||
),
|
||||
writeState: writeGoogleCalendarState,
|
||||
onProgress: options.onProgress,
|
||||
logLabel: "Google Calendar",
|
||||
});
|
||||
|
||||
await persistFinalSyncState(
|
||||
writeGoogleCalendarState,
|
||||
eventMap,
|
||||
lastSyncAt,
|
||||
request.origin,
|
||||
staleResult.deleted,
|
||||
staleEntries.length,
|
||||
events.length,
|
||||
);
|
||||
|
||||
reportSyncProgress(options.onProgress, {
|
||||
phase: "done",
|
||||
current: totalSteps,
|
||||
total: totalSteps,
|
||||
message: "Sync complete",
|
||||
});
|
||||
|
||||
return buildLessonSyncResult(
|
||||
upsertResult.created,
|
||||
upsertResult.updated,
|
||||
staleResult.deleted,
|
||||
upsertResult.failed,
|
||||
lastSyncAt,
|
||||
);
|
||||
}
|
||||
|
||||
/** Delete all tracked BetterSEQTA+ events for this SEQTA origin from Google Calendar. */
|
||||
export async function deleteSyncedEventsFromGoogleCalendar(
|
||||
origin: string,
|
||||
getAccessToken: () => Promise<string>,
|
||||
options: GoogleCalendarSyncOptions = {},
|
||||
): Promise<GoogleCalendarDeleteResult> {
|
||||
if (!isGoogleCalendarConfigured()) {
|
||||
return {
|
||||
success: false,
|
||||
configured: false,
|
||||
error: "Google Calendar is not configured in this extension build.",
|
||||
};
|
||||
}
|
||||
|
||||
const state = await readGoogleCalendarState();
|
||||
if (!state.refreshToken && !state.accessToken) {
|
||||
return { success: false, configured: true, connected: false, error: "Connect Google Calendar first." };
|
||||
}
|
||||
|
||||
const entries = originEventMapEntries(state.eventMap ?? {}, origin);
|
||||
if (entries.length === 0) {
|
||||
return { success: true, configured: true, connected: true, deleted: 0, failed: 0 };
|
||||
}
|
||||
|
||||
reportSyncProgress(options.onProgress, {
|
||||
phase: "preparing",
|
||||
current: 0,
|
||||
total: entries.length,
|
||||
message: "Preparing removal…",
|
||||
});
|
||||
|
||||
const eventMap = { ...(state.eventMap ?? {}) };
|
||||
const { deleted, failed } = await deleteTrackedEventsFromGoogle(
|
||||
entries,
|
||||
eventMap,
|
||||
getAccessToken,
|
||||
true,
|
||||
options.onProgress,
|
||||
0,
|
||||
entries.length,
|
||||
);
|
||||
|
||||
await writeGoogleCalendarState({ eventMap });
|
||||
|
||||
reportSyncProgress(options.onProgress, {
|
||||
phase: "done",
|
||||
current: entries.length,
|
||||
total: entries.length,
|
||||
message: "Removal complete",
|
||||
});
|
||||
|
||||
return {
|
||||
success: failed === 0,
|
||||
configured: true,
|
||||
connected: true,
|
||||
deleted,
|
||||
failed,
|
||||
error:
|
||||
failed > 0
|
||||
? `Removed ${deleted} event${deleted === 1 ? "" : "s"} with ${failed} error${failed === 1 ? "" : "s"}.`
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
import browser from "webextension-polyfill";
|
||||
import {
|
||||
fetchTimetableForSync,
|
||||
fetchTimetableLessons,
|
||||
trailingWeekRange,
|
||||
} from "@/seqta/utils/googleCalendar/fetchTimetable";
|
||||
import { getSyncWeeksAhead } from "@/seqta/utils/calendarSync/settings";
|
||||
import { reportSyncProgress } from "@/seqta/utils/calendarSync/lessonSyncShared";
|
||||
import { syncLessonsToGoogleCalendar } from "@/seqta/utils/googleCalendar/syncEngine";
|
||||
import type {
|
||||
GoogleCalendarSyncOptions,
|
||||
GoogleCalendarSyncProgress,
|
||||
GoogleCalendarSyncResult,
|
||||
} from "@/seqta/utils/googleCalendar/types";
|
||||
|
||||
export type GoogleCalendarRunMode = "full" | "incremental";
|
||||
|
||||
export interface RunGoogleCalendarSyncParams {
|
||||
mode?: GoogleCalendarRunMode;
|
||||
silent?: boolean;
|
||||
onProgress?: (progress: GoogleCalendarSyncProgress) => void;
|
||||
}
|
||||
|
||||
async function getAccessTokenFromBackground(): Promise<string> {
|
||||
const res = (await browser.runtime.sendMessage({
|
||||
type: "googleCalendarGetAccessToken",
|
||||
})) as { success?: boolean; accessToken?: string; error?: string };
|
||||
if (!res?.success || !res.accessToken) {
|
||||
throw new Error(res?.error ?? "Could not get Google Calendar access token.");
|
||||
}
|
||||
return res.accessToken;
|
||||
}
|
||||
|
||||
export async function runGoogleCalendarSync(
|
||||
params: RunGoogleCalendarSyncParams = {},
|
||||
): Promise<GoogleCalendarSyncResult> {
|
||||
const mode = params.mode ?? "full";
|
||||
const weeksAhead = await getSyncWeeksAhead();
|
||||
|
||||
reportSyncProgress(params.onProgress, {
|
||||
phase: "preparing",
|
||||
current: 0,
|
||||
total: 1,
|
||||
message: mode === "incremental" ? "Fetching new week…" : "Fetching timetable…",
|
||||
});
|
||||
|
||||
const lessons =
|
||||
mode === "incremental"
|
||||
? await fetchTimetableLessons(trailingWeekRange(weeksAhead))
|
||||
: await fetchTimetableForSync(weeksAhead);
|
||||
|
||||
const options: GoogleCalendarSyncOptions = { onProgress: params.onProgress };
|
||||
const result = await syncLessonsToGoogleCalendar(
|
||||
{
|
||||
origin: location.origin,
|
||||
lessons,
|
||||
mode,
|
||||
weeksAhead,
|
||||
},
|
||||
getAccessTokenFromBackground,
|
||||
options,
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function formatSyncResultMessage(result: GoogleCalendarSyncResult): string {
|
||||
const created = result.created ?? 0;
|
||||
const updated = result.updated ?? 0;
|
||||
const deleted = result.deleted ?? 0;
|
||||
const parts: string[] = [];
|
||||
if (created > 0) parts.push(`${created} new`);
|
||||
if (updated > 0) parts.push(`${updated} updated`);
|
||||
if (deleted > 0) parts.push(`${deleted} removed`);
|
||||
if (parts.length === 0) return "Google Calendar is up to date.";
|
||||
return `Google Calendar updated (${parts.join(", ")}).`;
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
export {
|
||||
CALENDAR_WEEKLY_ALARM as GOOGLE_CALENDAR_WEEKLY_ALARM,
|
||||
WEEKLY_SYNC_INTERVAL_MS,
|
||||
clampSyncWeeks,
|
||||
getAutoSyncWeekly,
|
||||
getSyncWeeksAhead,
|
||||
markWeeklySyncComplete,
|
||||
markWeeklySyncPending,
|
||||
shouldRunWeeklySync,
|
||||
} from "@/seqta/utils/calendarSync/settings";
|
||||
@@ -1,63 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
export async function deleteGoogleCalendarEvent(
|
||||
accessToken: string,
|
||||
calendarId: string,
|
||||
eventId: string,
|
||||
refreshAccessToken?: () => Promise<string>,
|
||||
): Promise<void> {
|
||||
const res = await fetch(
|
||||
`${GOOGLE_CALENDAR_API}/calendars/${encodeURIComponent(calendarId)}/events/${encodeURIComponent(eventId)}`,
|
||||
{ method: "DELETE", headers: { Authorization: `Bearer ${accessToken}` } },
|
||||
);
|
||||
if (res.status === 401 && refreshAccessToken) {
|
||||
const nextToken = await refreshAccessToken();
|
||||
return deleteGoogleCalendarEvent(nextToken, calendarId, eventId);
|
||||
}
|
||||
if (res.ok || res.status === 404 || res.status === 410) return;
|
||||
const err = (await res.json().catch(() => ({}))) as { error?: { message?: string } };
|
||||
throw new Error(err?.error?.message ?? `Google Calendar delete failed (${res.status})`);
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
import { BSPLUS_OUTLOOK_CALENDAR_EVENT_CATEGORY } from "@/config/outlookCalendar";
|
||||
import type {
|
||||
GoogleCalendarEventInput,
|
||||
SeqtaTimetableLesson,
|
||||
} from "@/seqta/utils/googleCalendar/types";
|
||||
import {
|
||||
lessonToGoogleEvent,
|
||||
mapLessonsToGoogleEvents,
|
||||
} from "@/seqta/utils/googleCalendar/eventMapper";
|
||||
|
||||
export { mapLessonsToGoogleEvents, lessonToGoogleEvent, seqtaLessonKey } from "@/seqta/utils/googleCalendar/eventMapper";
|
||||
|
||||
export function outlookGraphEventBody(event: GoogleCalendarEventInput): Record<string, unknown> {
|
||||
const body: Record<string, unknown> = {
|
||||
subject: event.summary,
|
||||
body: {
|
||||
contentType: "text",
|
||||
content: event.description ?? "Synced by BetterSEQTA+",
|
||||
},
|
||||
start: { dateTime: event.startDateTime, timeZone: event.timeZone },
|
||||
end: { dateTime: event.endDateTime, timeZone: event.timeZone },
|
||||
categories: [BSPLUS_OUTLOOK_CALENDAR_EVENT_CATEGORY],
|
||||
};
|
||||
if (event.location) {
|
||||
body.location = { displayName: event.location };
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
export function mapLessonsToOutlookEvents(
|
||||
origin: string,
|
||||
lessons: SeqtaTimetableLesson[],
|
||||
timeZone: string,
|
||||
): GoogleCalendarEventInput[] {
|
||||
return mapLessonsToGoogleEvents(origin, lessons, timeZone);
|
||||
}
|
||||
@@ -1,38 +1,8 @@
|
||||
import browser from "webextension-polyfill";
|
||||
import type { GoogleCalendarEventMapEntry } from "@/seqta/utils/googleCalendar/eventMapEntry";
|
||||
|
||||
export const BSPLUS_OUTLOOK_CALENDAR_STORAGE_KEY = "bsplus_outlook_calendar";
|
||||
|
||||
export interface OutlookCalendarStoredState {
|
||||
accessToken?: string;
|
||||
refreshToken?: string;
|
||||
expiresAt?: number;
|
||||
connectedAt?: number;
|
||||
lastSyncAt?: number;
|
||||
lastSyncOrigin?: string;
|
||||
eventMap?: Record<string, string | GoogleCalendarEventMapEntry>;
|
||||
}
|
||||
|
||||
export async function readOutlookCalendarState(): Promise<OutlookCalendarStoredState> {
|
||||
const got = await browser.storage.local.get(BSPLUS_OUTLOOK_CALENDAR_STORAGE_KEY);
|
||||
const raw = got[BSPLUS_OUTLOOK_CALENDAR_STORAGE_KEY];
|
||||
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return {};
|
||||
return raw as OutlookCalendarStoredState;
|
||||
}
|
||||
|
||||
export async function writeOutlookCalendarState(
|
||||
patch: Partial<OutlookCalendarStoredState>,
|
||||
): Promise<OutlookCalendarStoredState> {
|
||||
const current = await readOutlookCalendarState();
|
||||
const next: OutlookCalendarStoredState = { ...current, ...patch };
|
||||
await browser.storage.local.set({ [BSPLUS_OUTLOOK_CALENDAR_STORAGE_KEY]: next });
|
||||
return next;
|
||||
}
|
||||
|
||||
export async function clearOutlookCalendarState(): Promise<void> {
|
||||
await browser.storage.local.remove(BSPLUS_OUTLOOK_CALENDAR_STORAGE_KEY);
|
||||
}
|
||||
|
||||
export function outlookEventMapKey(origin: string, seqtaKey: string): string {
|
||||
return `${origin}::${seqtaKey}`;
|
||||
}
|
||||
export {
|
||||
BSPLUS_OUTLOOK_CALENDAR_STORAGE_KEY,
|
||||
clearOutlookCalendarState,
|
||||
readOutlookCalendarState,
|
||||
writeOutlookCalendarState,
|
||||
type OutlookCalendarStatus,
|
||||
type OutlookCalendarStoredState,
|
||||
} from "@/seqta/utils/calendarSync/providerStorage";
|
||||
|
||||
@@ -1,249 +0,0 @@
|
||||
import { verboseLog } from "@/utils/verboseLog";
|
||||
import { isOutlookCalendarConfigured } from "@/config/outlookCalendar";
|
||||
import { getSyncWeeksAhead } from "@/seqta/utils/calendarSync/settings";
|
||||
import {
|
||||
buildLessonSyncResult,
|
||||
emptyLessonsSyncResult,
|
||||
entriesToPrune,
|
||||
EVENT_MAP_PERSIST_EVERY,
|
||||
notConfiguredSyncResult,
|
||||
notConnectedSyncResult,
|
||||
originEventMapEntries,
|
||||
persistFinalSyncState,
|
||||
reportSyncProgress,
|
||||
upsertLessonEvents,
|
||||
} from "@/seqta/utils/calendarSync/lessonSyncShared";
|
||||
import type {
|
||||
GoogleCalendarDeleteResult,
|
||||
GoogleCalendarSyncOptions,
|
||||
GoogleCalendarSyncRequest,
|
||||
GoogleCalendarSyncResult,
|
||||
} from "@/seqta/utils/googleCalendar/types";
|
||||
import {
|
||||
mapLessonsToOutlookEvents,
|
||||
outlookGraphEventBody,
|
||||
} from "@/seqta/utils/outlookCalendar/eventMapper";
|
||||
import {
|
||||
outlookEventMapKey,
|
||||
readOutlookCalendarState,
|
||||
writeOutlookCalendarState,
|
||||
} from "@/seqta/utils/outlookCalendar/storage";
|
||||
import {
|
||||
deleteOutlookCalendarEvent,
|
||||
upsertOutlookCalendarEvent,
|
||||
} from "@/seqta/utils/outlookCalendar/upsertEvent";
|
||||
|
||||
type DeleteTrackedEventsResult = {
|
||||
deleted: number;
|
||||
failed: number;
|
||||
};
|
||||
|
||||
async function deleteTrackedEventsFromOutlook(
|
||||
entries: Array<[string, string]>,
|
||||
eventMap: Record<string, string | { id: string; date: string }>,
|
||||
getAccessToken: () => Promise<string>,
|
||||
persistProgress = false,
|
||||
onProgress?: GoogleCalendarSyncOptions["onProgress"],
|
||||
progressOffset = 0,
|
||||
progressTotal = 0,
|
||||
): Promise<DeleteTrackedEventsResult> {
|
||||
if (entries.length === 0) return { deleted: 0, failed: 0 };
|
||||
|
||||
let accessToken = await getAccessToken();
|
||||
let deleted = 0;
|
||||
let failed = 0;
|
||||
|
||||
for (const [mapKey, eventId] of entries) {
|
||||
try {
|
||||
await deleteOutlookCalendarEvent(accessToken, eventId, async () => {
|
||||
accessToken = await getAccessToken();
|
||||
return accessToken;
|
||||
});
|
||||
delete eventMap[mapKey];
|
||||
deleted += 1;
|
||||
|
||||
reportSyncProgress(onProgress, {
|
||||
phase: "deleting",
|
||||
current: progressOffset + deleted + failed,
|
||||
total: progressTotal,
|
||||
message: `Removing old events (${deleted + failed}/${entries.length})…`,
|
||||
});
|
||||
|
||||
if (persistProgress && (deleted + failed) % EVENT_MAP_PERSIST_EVERY === 0) {
|
||||
await writeOutlookCalendarState({ eventMap });
|
||||
}
|
||||
} catch (err) {
|
||||
verboseLog("[BetterSEQTA+] Outlook Calendar event delete failed:", err);
|
||||
failed += 1;
|
||||
reportSyncProgress(onProgress, {
|
||||
phase: "deleting",
|
||||
current: progressOffset + deleted + failed,
|
||||
total: progressTotal,
|
||||
message: `Removing old events (${deleted + failed}/${entries.length})…`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return { deleted, failed };
|
||||
}
|
||||
|
||||
export async function syncLessonsToOutlookCalendar(
|
||||
request: GoogleCalendarSyncRequest,
|
||||
getAccessToken: () => Promise<string>,
|
||||
options: GoogleCalendarSyncOptions = {},
|
||||
): Promise<GoogleCalendarSyncResult> {
|
||||
if (!isOutlookCalendarConfigured()) {
|
||||
return notConfiguredSyncResult(
|
||||
"Outlook Calendar is not configured in this extension build.",
|
||||
);
|
||||
}
|
||||
|
||||
const state = await readOutlookCalendarState();
|
||||
if (!state.refreshToken && !state.accessToken) {
|
||||
return notConnectedSyncResult("Connect Outlook Calendar first.");
|
||||
}
|
||||
|
||||
const mode = request.mode ?? "full";
|
||||
const weeksAhead = request.weeksAhead ?? (await getSyncWeeksAhead());
|
||||
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
|
||||
const events = mapLessonsToOutlookEvents(request.origin, request.lessons, timeZone);
|
||||
|
||||
if (events.length === 0 && mode === "full") {
|
||||
return emptyLessonsSyncResult();
|
||||
}
|
||||
|
||||
reportSyncProgress(options.onProgress, {
|
||||
phase: "preparing",
|
||||
current: 0,
|
||||
total: Math.max(events.length, 1),
|
||||
message: mode === "incremental" ? "Preparing weekly sync…" : "Preparing sync…",
|
||||
});
|
||||
|
||||
const eventMap = { ...(state.eventMap ?? {}) };
|
||||
const currentMapKeys = new Set(
|
||||
events.map((event) => outlookEventMapKey(request.origin, event.seqtaKey)),
|
||||
);
|
||||
const staleEntries = entriesToPrune(eventMap, request.origin, mode, weeksAhead, currentMapKeys);
|
||||
const totalSteps = staleEntries.length + events.length;
|
||||
const lastSyncAt = Date.now();
|
||||
|
||||
const staleResult = await deleteTrackedEventsFromOutlook(
|
||||
staleEntries,
|
||||
eventMap,
|
||||
getAccessToken,
|
||||
false,
|
||||
options.onProgress,
|
||||
0,
|
||||
totalSteps,
|
||||
);
|
||||
|
||||
const upsertResult = await upsertLessonEvents({
|
||||
events,
|
||||
eventMap,
|
||||
origin: request.origin,
|
||||
staleEntryCount: staleEntries.length,
|
||||
totalSteps,
|
||||
lastSyncAt,
|
||||
initialFailed: staleResult.failed,
|
||||
getAccessToken,
|
||||
mapKey: outlookEventMapKey,
|
||||
upsert: (accessToken, existingId, event, refreshAccessToken) =>
|
||||
upsertOutlookCalendarEvent(
|
||||
accessToken,
|
||||
existingId,
|
||||
outlookGraphEventBody(event),
|
||||
refreshAccessToken,
|
||||
),
|
||||
writeState: writeOutlookCalendarState,
|
||||
onProgress: options.onProgress,
|
||||
logLabel: "Outlook Calendar",
|
||||
});
|
||||
|
||||
await persistFinalSyncState(
|
||||
writeOutlookCalendarState,
|
||||
eventMap,
|
||||
lastSyncAt,
|
||||
request.origin,
|
||||
staleResult.deleted,
|
||||
staleEntries.length,
|
||||
events.length,
|
||||
);
|
||||
|
||||
reportSyncProgress(options.onProgress, {
|
||||
phase: "done",
|
||||
current: totalSteps,
|
||||
total: totalSteps,
|
||||
message: "Sync complete",
|
||||
});
|
||||
|
||||
return buildLessonSyncResult(
|
||||
upsertResult.created,
|
||||
upsertResult.updated,
|
||||
staleResult.deleted,
|
||||
upsertResult.failed,
|
||||
lastSyncAt,
|
||||
);
|
||||
}
|
||||
|
||||
export async function deleteSyncedEventsFromOutlookCalendar(
|
||||
origin: string,
|
||||
getAccessToken: () => Promise<string>,
|
||||
options: GoogleCalendarSyncOptions = {},
|
||||
): Promise<GoogleCalendarDeleteResult> {
|
||||
if (!isOutlookCalendarConfigured()) {
|
||||
return {
|
||||
success: false,
|
||||
configured: false,
|
||||
error: "Outlook Calendar is not configured in this extension build.",
|
||||
};
|
||||
}
|
||||
|
||||
const state = await readOutlookCalendarState();
|
||||
if (!state.refreshToken && !state.accessToken) {
|
||||
return { success: false, configured: true, connected: false, error: "Connect Outlook Calendar first." };
|
||||
}
|
||||
|
||||
const entries = originEventMapEntries(state.eventMap ?? {}, origin);
|
||||
if (entries.length === 0) {
|
||||
return { success: true, configured: true, connected: true, deleted: 0, failed: 0 };
|
||||
}
|
||||
|
||||
reportSyncProgress(options.onProgress, {
|
||||
phase: "preparing",
|
||||
current: 0,
|
||||
total: entries.length,
|
||||
message: "Preparing removal…",
|
||||
});
|
||||
|
||||
const eventMap = { ...(state.eventMap ?? {}) };
|
||||
const { deleted, failed } = await deleteTrackedEventsFromOutlook(
|
||||
entries,
|
||||
eventMap,
|
||||
getAccessToken,
|
||||
true,
|
||||
options.onProgress,
|
||||
0,
|
||||
entries.length,
|
||||
);
|
||||
|
||||
await writeOutlookCalendarState({ eventMap });
|
||||
|
||||
reportSyncProgress(options.onProgress, {
|
||||
phase: "done",
|
||||
current: entries.length,
|
||||
total: entries.length,
|
||||
message: "Removal complete",
|
||||
});
|
||||
|
||||
return {
|
||||
success: failed === 0,
|
||||
configured: true,
|
||||
connected: true,
|
||||
deleted,
|
||||
failed,
|
||||
error:
|
||||
failed > 0
|
||||
? `Removed ${deleted} event${deleted === 1 ? "" : "s"} with ${failed} error${failed === 1 ? "" : "s"}.`
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
import browser from "webextension-polyfill";
|
||||
import {
|
||||
fetchTimetableForSync,
|
||||
fetchTimetableLessons,
|
||||
trailingWeekRange,
|
||||
} from "@/seqta/utils/googleCalendar/fetchTimetable";
|
||||
import { getSyncWeeksAhead } from "@/seqta/utils/calendarSync/settings";
|
||||
import { reportSyncProgress } from "@/seqta/utils/calendarSync/lessonSyncShared";
|
||||
import { syncLessonsToOutlookCalendar } from "@/seqta/utils/outlookCalendar/syncEngine";
|
||||
import type {
|
||||
GoogleCalendarSyncOptions,
|
||||
GoogleCalendarSyncProgress,
|
||||
GoogleCalendarSyncResult,
|
||||
} from "@/seqta/utils/googleCalendar/types";
|
||||
|
||||
export type OutlookCalendarRunMode = "full" | "incremental";
|
||||
|
||||
export interface RunOutlookCalendarSyncParams {
|
||||
mode?: OutlookCalendarRunMode;
|
||||
silent?: boolean;
|
||||
onProgress?: (progress: GoogleCalendarSyncProgress) => void;
|
||||
}
|
||||
|
||||
async function getAccessTokenFromBackground(): Promise<string> {
|
||||
const res = (await browser.runtime.sendMessage({
|
||||
type: "outlookCalendarGetAccessToken",
|
||||
})) as { success?: boolean; accessToken?: string; error?: string };
|
||||
if (!res?.success || !res.accessToken) {
|
||||
throw new Error(res?.error ?? "Could not get Outlook Calendar access token.");
|
||||
}
|
||||
return res.accessToken;
|
||||
}
|
||||
|
||||
export async function runOutlookCalendarSync(
|
||||
params: RunOutlookCalendarSyncParams = {},
|
||||
): Promise<GoogleCalendarSyncResult> {
|
||||
const mode = params.mode ?? "full";
|
||||
const weeksAhead = await getSyncWeeksAhead();
|
||||
|
||||
reportSyncProgress(params.onProgress, {
|
||||
phase: "preparing",
|
||||
current: 0,
|
||||
total: 1,
|
||||
message: mode === "incremental" ? "Fetching new week…" : "Fetching timetable…",
|
||||
});
|
||||
|
||||
const lessons =
|
||||
mode === "incremental"
|
||||
? await fetchTimetableLessons(trailingWeekRange(weeksAhead))
|
||||
: await fetchTimetableForSync(weeksAhead);
|
||||
|
||||
const options: GoogleCalendarSyncOptions = { onProgress: params.onProgress };
|
||||
const result = await syncLessonsToOutlookCalendar(
|
||||
{
|
||||
origin: location.origin,
|
||||
lessons,
|
||||
mode,
|
||||
weeksAhead,
|
||||
},
|
||||
getAccessTokenFromBackground,
|
||||
options,
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export function formatOutlookSyncResultMessage(result: GoogleCalendarSyncResult): string {
|
||||
const created = result.created ?? 0;
|
||||
const updated = result.updated ?? 0;
|
||||
const deleted = result.deleted ?? 0;
|
||||
const parts: string[] = [];
|
||||
if (created > 0) parts.push(`${created} new`);
|
||||
if (updated > 0) parts.push(`${updated} updated`);
|
||||
if (deleted > 0) parts.push(`${deleted} removed`);
|
||||
if (parts.length === 0) return "Outlook Calendar is up to date.";
|
||||
return `Outlook Calendar updated (${parts.join(", ")}).`;
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
export interface OutlookCalendarStatus {
|
||||
configured: boolean;
|
||||
connected: boolean;
|
||||
lastSyncAt?: number;
|
||||
lastSyncOrigin?: string;
|
||||
}
|
||||
@@ -1,48 +1,31 @@
|
||||
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
|
||||
import { upsertOutlookCalendarEvent, deleteOutlookCalendarEvent } from "./upsertEvent";
|
||||
|
||||
const fetchMock = jest.fn();
|
||||
global.fetch = fetchMock as unknown as typeof fetch;
|
||||
const mockFetch = jest.fn<typeof fetch>();
|
||||
global.fetch = mockFetch as typeof fetch;
|
||||
|
||||
describe("upsertOutlookCalendarEvent", () => {
|
||||
import {
|
||||
deleteOutlookCalendarEvent,
|
||||
upsertOutlookCalendarEvent,
|
||||
} from "@/seqta/utils/calendarSync/remoteEvents";
|
||||
|
||||
describe("outlook calendar remote events", () => {
|
||||
beforeEach(() => {
|
||||
fetchMock.mockReset();
|
||||
mockFetch.mockReset();
|
||||
});
|
||||
|
||||
it("creates a new event when no existing id", async () => {
|
||||
fetchMock.mockResolvedValue({
|
||||
ok: true,
|
||||
status: 200,
|
||||
json: async () => ({ id: "evt-1" }),
|
||||
});
|
||||
it("creates a new event when none exists", async () => {
|
||||
mockFetch.mockResolvedValueOnce(
|
||||
new Response(JSON.stringify({ id: "evt-1" }), { status: 201 }),
|
||||
);
|
||||
|
||||
const id = await upsertOutlookCalendarEvent("token", undefined, { subject: "Math" });
|
||||
expect(id).toBe("evt-1");
|
||||
expect(fetchMock).toHaveBeenCalledWith(
|
||||
"https://graph.microsoft.com/v1.0/me/events",
|
||||
expect.objectContaining({ method: "POST" }),
|
||||
);
|
||||
expect(mockFetch).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("patches when an existing id is provided", async () => {
|
||||
fetchMock.mockResolvedValue({ ok: true, status: 200, json: async () => ({}) });
|
||||
it("deletes an event", async () => {
|
||||
mockFetch.mockResolvedValueOnce(new Response(null, { status: 204 }));
|
||||
|
||||
const id = await upsertOutlookCalendarEvent("token", "evt-1", { subject: "Math" });
|
||||
expect(id).toBe("evt-1");
|
||||
expect(fetchMock).toHaveBeenCalledWith(
|
||||
"https://graph.microsoft.com/v1.0/me/events/evt-1",
|
||||
expect.objectContaining({ method: "PATCH" }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("deleteOutlookCalendarEvent", () => {
|
||||
beforeEach(() => {
|
||||
fetchMock.mockReset();
|
||||
});
|
||||
|
||||
it("treats 404 as success", async () => {
|
||||
fetchMock.mockResolvedValue({ ok: false, status: 404, json: async () => ({}) });
|
||||
await expect(deleteOutlookCalendarEvent("token", "evt-1")).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
import { OUTLOOK_GRAPH_API } from "@/config/outlookCalendar";
|
||||
|
||||
export async function upsertOutlookCalendarEvent(
|
||||
accessToken: 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(`${OUTLOOK_GRAPH_API}/me/events/${encodeURIComponent(existingEventId)}`, {
|
||||
method: "PATCH",
|
||||
headers,
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
if (res.status === 401 && refreshAccessToken) {
|
||||
const nextToken = await refreshAccessToken();
|
||||
return upsertOutlookCalendarEvent(nextToken, 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 ?? `Outlook Calendar update failed (${res.status})`);
|
||||
}
|
||||
}
|
||||
|
||||
const res = await fetch(`${OUTLOOK_GRAPH_API}/me/events`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
if (res.status === 401 && refreshAccessToken) {
|
||||
const nextToken = await refreshAccessToken();
|
||||
return upsertOutlookCalendarEvent(nextToken, 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 ?? `Outlook Calendar create failed (${res.status})`);
|
||||
}
|
||||
return json.id;
|
||||
}
|
||||
|
||||
export async function deleteOutlookCalendarEvent(
|
||||
accessToken: string,
|
||||
eventId: string,
|
||||
refreshAccessToken?: () => Promise<string>,
|
||||
): Promise<void> {
|
||||
const res = await fetch(`${OUTLOOK_GRAPH_API}/me/events/${encodeURIComponent(eventId)}`, {
|
||||
method: "DELETE",
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
});
|
||||
if (res.status === 401 && refreshAccessToken) {
|
||||
const nextToken = await refreshAccessToken();
|
||||
return deleteOutlookCalendarEvent(nextToken, eventId);
|
||||
}
|
||||
if (res.ok || res.status === 404 || res.status === 410) return;
|
||||
const err = (await res.json().catch(() => ({}))) as { error?: { message?: string } };
|
||||
throw new Error(err?.error?.message ?? `Outlook Calendar delete failed (${res.status})`);
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
type ColorChannels = {
|
||||
r: number;
|
||||
g: number;
|
||||
b: number;
|
||||
a: number;
|
||||
};
|
||||
|
||||
function clampByte(value: number): number {
|
||||
return Math.max(0, Math.min(255, Math.round(value)));
|
||||
}
|
||||
|
||||
function clampAlpha(value: number): number {
|
||||
return Math.max(0, Math.min(1, value));
|
||||
}
|
||||
|
||||
function toHexByte(value: number): string {
|
||||
return clampByte(value).toString(16).padStart(2, "0");
|
||||
}
|
||||
|
||||
function createColor(channels: ColorChannels) {
|
||||
const color = {
|
||||
red: () => clampByte(channels.r),
|
||||
green: () => clampByte(channels.g),
|
||||
blue: () => clampByte(channels.b),
|
||||
alpha: () => clampAlpha(channels.a),
|
||||
hex: () =>
|
||||
`#${toHexByte(channels.r)}${toHexByte(channels.g)}${toHexByte(channels.b)}`,
|
||||
};
|
||||
|
||||
return {
|
||||
...color,
|
||||
alpha: (value?: number) => {
|
||||
if (value === undefined) return clampAlpha(channels.a);
|
||||
return createColor({ ...channels, a: value });
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function parseHex(input: string): ColorChannels | null {
|
||||
const short = input.match(/^#([0-9a-f]{3})$/i);
|
||||
if (short) {
|
||||
const [r, g, b] = short[1].split("");
|
||||
return {
|
||||
r: parseInt(`${r}${r}`, 16),
|
||||
g: parseInt(`${g}${g}`, 16),
|
||||
b: parseInt(`${b}${b}`, 16),
|
||||
a: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const long = input.match(/^#([0-9a-f]{6})$/i);
|
||||
if (!long) return null;
|
||||
|
||||
return {
|
||||
r: parseInt(long[1].slice(0, 2), 16),
|
||||
g: parseInt(long[1].slice(2, 4), 16),
|
||||
b: parseInt(long[1].slice(4, 6), 16),
|
||||
a: 1,
|
||||
};
|
||||
}
|
||||
|
||||
function parseRgb(input: string): ColorChannels | null {
|
||||
const match = input.match(
|
||||
/rgba?\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)(?:\s*,\s*([\d.]+))?\s*\)/i,
|
||||
);
|
||||
if (!match) return null;
|
||||
|
||||
return {
|
||||
r: Number(match[1]),
|
||||
g: Number(match[2]),
|
||||
b: Number(match[3]),
|
||||
a: match[4] !== undefined ? Number(match[4]) : 1,
|
||||
};
|
||||
}
|
||||
|
||||
function Color(input: string) {
|
||||
const channels = parseHex(input) ?? parseRgb(input);
|
||||
if (!channels) {
|
||||
throw new Error(`Unable to parse color: ${input}`);
|
||||
}
|
||||
return createColor(channels);
|
||||
}
|
||||
|
||||
Color.rgb = (r: number, g: number, b: number) => createColor({ r, g, b, a: 1 });
|
||||
|
||||
export default Color;
|
||||
+1
-34
@@ -1,38 +1,5 @@
|
||||
import { settingsState } from "@/seqta/utils/listeners/SettingsState";
|
||||
|
||||
const VERBOSE_LOG_ATTR = "data-bsplus-verbose-log";
|
||||
|
||||
export function isVerboseLoggingEnabled(): boolean {
|
||||
return Boolean(settingsState.devMode && settingsState.verboseLogging);
|
||||
}
|
||||
|
||||
export function syncVerboseLogDomFlag(): void {
|
||||
if (typeof document === "undefined") return;
|
||||
document.documentElement.toggleAttribute(
|
||||
VERBOSE_LOG_ATTR,
|
||||
isVerboseLoggingEnabled(),
|
||||
);
|
||||
}
|
||||
|
||||
let initialized = false;
|
||||
|
||||
/** Register DOM flag sync when dev / verbose toggles change. Call after settings load. */
|
||||
export function initVerboseLogging(): void {
|
||||
if (initialized) return;
|
||||
initialized = true;
|
||||
syncVerboseLogDomFlag();
|
||||
settingsState.register("devMode", () => syncVerboseLogDomFlag());
|
||||
settingsState.register("verboseLogging", () => syncVerboseLogDomFlag());
|
||||
}
|
||||
|
||||
export function verboseDebug(...args: unknown[]): void {
|
||||
if (isVerboseLoggingEnabled()) console.debug(...args);
|
||||
}
|
||||
|
||||
export function verboseInfo(...args: unknown[]): void {
|
||||
if (isVerboseLoggingEnabled()) console.info(...args);
|
||||
}
|
||||
|
||||
export function verboseLog(...args: unknown[]): void {
|
||||
if (isVerboseLoggingEnabled()) console.log(...args);
|
||||
if (settingsState.devMode) console.log(...args);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user