mirror of
https://github.com/kennethnym/aris.git
synced 2026-03-28 04:41:18 +00:00
feat(session): query enabled sources before providers
UserSessionManager now queries the user_sources table for enabled sources before calling any provider. Providers receive the per-user JSON config directly instead of querying the DB themselves, removing their db dependency and eliminating redundant round-trips. Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
@@ -1,15 +1,11 @@
|
||||
import { TflSource, type ITflApi, type TflLineId } from "@aelis/source-tfl"
|
||||
import { type } from "arktype"
|
||||
|
||||
import type { Database } from "../db/index.ts"
|
||||
import type { FeedSourceProvider } from "../session/feed-source-provider.ts"
|
||||
|
||||
import { SourceDisabledError } from "../sources/errors.ts"
|
||||
import { sources } from "../sources/user-sources.ts"
|
||||
|
||||
export type TflSourceProviderOptions =
|
||||
| { db: Database; apiKey: string; client?: never }
|
||||
| { db: Database; apiKey?: never; client: ITflApi }
|
||||
| { apiKey: string; client?: never }
|
||||
| { apiKey?: never; client: ITflApi }
|
||||
|
||||
const tflConfig = type({
|
||||
"lines?": "string[]",
|
||||
@@ -17,26 +13,18 @@ const tflConfig = type({
|
||||
|
||||
export class TflSourceProvider implements FeedSourceProvider {
|
||||
readonly sourceId = "aelis.tfl"
|
||||
private readonly db: Database
|
||||
private readonly apiKey: string | undefined
|
||||
private readonly client: ITflApi | undefined
|
||||
|
||||
constructor(options: TflSourceProviderOptions) {
|
||||
this.db = options.db
|
||||
this.apiKey = "apiKey" in options ? options.apiKey : undefined
|
||||
this.client = "client" in options ? options.client : undefined
|
||||
}
|
||||
|
||||
async feedSourceForUser(userId: string): Promise<TflSource> {
|
||||
const row = await sources(this.db, userId).find("aelis.tfl")
|
||||
|
||||
if (!row || !row.enabled) {
|
||||
throw new SourceDisabledError("aelis.tfl", userId)
|
||||
}
|
||||
|
||||
const parsed = tflConfig(row.config ?? {})
|
||||
async feedSourceForUser(_userId: string, config: unknown): Promise<TflSource> {
|
||||
const parsed = tflConfig(config)
|
||||
if (parsed instanceof type.errors) {
|
||||
throw new Error(`Invalid TFL config for user ${userId}: ${parsed.summary}`)
|
||||
throw new Error(`Invalid TFL config: ${parsed.summary}`)
|
||||
}
|
||||
|
||||
return new TflSource({
|
||||
|
||||
Reference in New Issue
Block a user