2026-03-10 19:19:23 +00:00
|
|
|
import { WeatherSource, type WeatherSourceOptions } from "@aelis/source-weatherkit"
|
2026-03-16 01:30:02 +00:00
|
|
|
import { type } from "arktype"
|
2026-02-18 00:41:20 +00:00
|
|
|
|
2026-03-16 01:30:02 +00:00
|
|
|
import type { Database } from "../db/index.ts"
|
2026-02-18 00:41:20 +00:00
|
|
|
import type { FeedSourceProvider } from "../session/feed-source-provider.ts"
|
|
|
|
|
|
2026-03-16 01:30:02 +00:00
|
|
|
import { SourceDisabledError } from "../sources/errors.ts"
|
|
|
|
|
import { sources } from "../sources/user-sources.ts"
|
|
|
|
|
|
|
|
|
|
export interface WeatherSourceProviderOptions {
|
|
|
|
|
db: Database
|
|
|
|
|
credentials: WeatherSourceOptions["credentials"]
|
|
|
|
|
client?: WeatherSourceOptions["client"]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const weatherConfig = type({
|
|
|
|
|
"units?": "'metric' | 'imperial'",
|
|
|
|
|
"hourlyLimit?": "number",
|
|
|
|
|
"dailyLimit?": "number",
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-18 00:41:20 +00:00
|
|
|
export class WeatherSourceProvider implements FeedSourceProvider {
|
2026-03-16 01:30:02 +00:00
|
|
|
private readonly db: Database
|
|
|
|
|
private readonly credentials: WeatherSourceOptions["credentials"]
|
|
|
|
|
private readonly client: WeatherSourceOptions["client"]
|
2026-02-18 00:41:20 +00:00
|
|
|
|
2026-03-16 01:30:02 +00:00
|
|
|
constructor(options: WeatherSourceProviderOptions) {
|
|
|
|
|
this.db = options.db
|
|
|
|
|
this.credentials = options.credentials
|
|
|
|
|
this.client = options.client
|
2026-02-18 00:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-16 01:30:02 +00:00
|
|
|
async feedSourceForUser(userId: string): Promise<WeatherSource> {
|
|
|
|
|
const row = await sources(this.db, userId).find("aelis.weather")
|
|
|
|
|
|
|
|
|
|
if (!row || !row.enabled) {
|
|
|
|
|
throw new SourceDisabledError("aelis.weather", userId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parsed = weatherConfig(row.config ?? {})
|
|
|
|
|
if (parsed instanceof type.errors) {
|
|
|
|
|
throw new Error(`Invalid weather config for user ${userId}: ${parsed.summary}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new WeatherSource({
|
|
|
|
|
credentials: this.credentials,
|
|
|
|
|
client: this.client,
|
|
|
|
|
units: parsed.units,
|
|
|
|
|
hourlyLimit: parsed.hourlyLimit,
|
|
|
|
|
dailyLimit: parsed.dailyLimit,
|
|
|
|
|
})
|
2026-02-18 00:41:20 +00:00
|
|
|
}
|
|
|
|
|
}
|