2026-06-12 17:35:26 +01:00
|
|
|
import { CalDavSource } from "@freya/source-caldav"
|
2026-04-11 16:34:11 +01:00
|
|
|
import { type } from "arktype"
|
|
|
|
|
|
|
|
|
|
import type { FeedSourceProvider } from "../session/feed-source-provider.ts"
|
|
|
|
|
|
|
|
|
|
import { InvalidSourceCredentialsError } from "../sources/errors.ts"
|
|
|
|
|
|
|
|
|
|
const caldavConfig = type({
|
|
|
|
|
"+": "reject",
|
|
|
|
|
serverUrl: "string",
|
|
|
|
|
username: "string",
|
|
|
|
|
"lookAheadDays?": "number",
|
|
|
|
|
"timeZone?": "string",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const caldavCredentials = type({
|
|
|
|
|
"+": "reject",
|
|
|
|
|
password: "string",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export class CalDavSourceProvider implements FeedSourceProvider {
|
2026-06-12 17:35:26 +01:00
|
|
|
readonly sourceId = "freya.caldav"
|
2026-04-11 16:34:11 +01:00
|
|
|
readonly configSchema = caldavConfig
|
|
|
|
|
|
|
|
|
|
async feedSourceForUser(
|
|
|
|
|
_userId: string,
|
|
|
|
|
config: unknown,
|
|
|
|
|
credentials: unknown,
|
|
|
|
|
): Promise<CalDavSource> {
|
|
|
|
|
const parsed = caldavConfig(config)
|
|
|
|
|
if (parsed instanceof type.errors) {
|
|
|
|
|
throw new Error(`Invalid CalDAV config: ${parsed.summary}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!credentials) {
|
2026-06-12 17:35:26 +01:00
|
|
|
throw new InvalidSourceCredentialsError("freya.caldav", "No CalDAV credentials configured")
|
2026-04-11 16:34:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const creds = caldavCredentials(credentials)
|
|
|
|
|
if (creds instanceof type.errors) {
|
2026-06-12 17:35:26 +01:00
|
|
|
throw new InvalidSourceCredentialsError("freya.caldav", creds.summary)
|
2026-04-11 16:34:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new CalDavSource({
|
|
|
|
|
serverUrl: parsed.serverUrl,
|
|
|
|
|
authMethod: "basic",
|
|
|
|
|
username: parsed.username,
|
|
|
|
|
password: creds.password,
|
|
|
|
|
lookAheadDays: parsed.lookAheadDays,
|
|
|
|
|
timeZone: parsed.timeZone,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|