mirror of
https://github.com/kennethnym/aris.git
synced 2026-04-15 22:31:19 +01:00
refactor: rename upsertSourceConfig to saveSourceConfig
Co-authored-by: Ona <no-reply@ona.com>
This commit is contained in:
@@ -79,11 +79,14 @@ export function SourceConfigPanel({ source, onUpdate }: SourceConfigPanelProps)
|
|||||||
(v) => typeof v === "string" && v.length > 0,
|
(v) => typeof v === "string" && v.length > 0,
|
||||||
)
|
)
|
||||||
|
|
||||||
await replaceSource(source.id, {
|
const body: Parameters<typeof replaceSource>[1] = {
|
||||||
enabled,
|
enabled,
|
||||||
config: getUserConfig(),
|
config: getUserConfig(),
|
||||||
...(hasCredentials && source.perUserCredentials ? { credentials: credentialFields } : {}),
|
}
|
||||||
})
|
if (hasCredentials && source.perUserCredentials) {
|
||||||
|
body.credentials = credentialFields
|
||||||
|
}
|
||||||
|
await replaceSource(source.id, body)
|
||||||
|
|
||||||
// For non-per-user credentials (provider-level), still use the admin endpoint.
|
// For non-per-user credentials (provider-level), still use the admin endpoint.
|
||||||
if (hasCredentials && !source.perUserCredentials) {
|
if (hasCredentials && !source.perUserCredentials) {
|
||||||
|
|||||||
@@ -828,7 +828,7 @@ describe("UserSessionManager.updateSourceCredentials", () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("UserSessionManager.upsertSourceConfig", () => {
|
describe("UserSessionManager.saveSourceConfig", () => {
|
||||||
test("upserts config without credentials (existing behavior)", async () => {
|
test("upserts config without credentials (existing behavior)", async () => {
|
||||||
setEnabledSources(["test"])
|
setEnabledSources(["test"])
|
||||||
const factory = mock(async () => createStubSource("test"))
|
const factory = mock(async () => createStubSource("test"))
|
||||||
@@ -842,7 +842,7 @@ describe("UserSessionManager.upsertSourceConfig", () => {
|
|||||||
// Create a session first so we can verify the source is refreshed
|
// Create a session first so we can verify the source is refreshed
|
||||||
await manager.getOrCreate("user-1")
|
await manager.getOrCreate("user-1")
|
||||||
|
|
||||||
await manager.upsertSourceConfig("user-1", "test", {
|
await manager.saveSourceConfig("user-1", "test", {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
config: { key: "value" },
|
config: { key: "value" },
|
||||||
})
|
})
|
||||||
@@ -871,7 +871,7 @@ describe("UserSessionManager.upsertSourceConfig", () => {
|
|||||||
await manager.getOrCreate("user-1")
|
await manager.getOrCreate("user-1")
|
||||||
|
|
||||||
const creds = { username: "alice", password: "s3cret" }
|
const creds = { username: "alice", password: "s3cret" }
|
||||||
await manager.upsertSourceConfig("user-1", "test", {
|
await manager.saveSourceConfig("user-1", "test", {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
config: { serverUrl: "https://example.com" },
|
config: { serverUrl: "https://example.com" },
|
||||||
credentials: creds,
|
credentials: creds,
|
||||||
@@ -901,7 +901,7 @@ describe("UserSessionManager.upsertSourceConfig", () => {
|
|||||||
expect(session.hasSource("test")).toBe(false)
|
expect(session.hasSource("test")).toBe(false)
|
||||||
|
|
||||||
// Set mockFindResult to undefined so find() returns a row (simulating the row was just created by upsertConfig)
|
// Set mockFindResult to undefined so find() returns a row (simulating the row was just created by upsertConfig)
|
||||||
await manager.upsertSourceConfig("user-1", "test", {
|
await manager.saveSourceConfig("user-1", "test", {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
config: {},
|
config: {},
|
||||||
credentials: { token: "abc" },
|
credentials: { token: "abc" },
|
||||||
@@ -922,7 +922,7 @@ describe("UserSessionManager.upsertSourceConfig", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
manager.upsertSourceConfig("user-1", "test", {
|
manager.saveSourceConfig("user-1", "test", {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
config: {},
|
config: {},
|
||||||
credentials: { token: "abc" },
|
credentials: { token: "abc" },
|
||||||
@@ -938,7 +938,7 @@ describe("UserSessionManager.upsertSourceConfig", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
manager.upsertSourceConfig("user-1", "unknown", {
|
manager.saveSourceConfig("user-1", "unknown", {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
config: {},
|
config: {},
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import type { FeedSourceProvider } from "./feed-source-provider.ts"
|
|||||||
import {
|
import {
|
||||||
CredentialStorageUnavailableError,
|
CredentialStorageUnavailableError,
|
||||||
InvalidSourceConfigError,
|
InvalidSourceConfigError,
|
||||||
InvalidSourceCredentialsError,
|
|
||||||
SourceNotFoundError,
|
SourceNotFoundError,
|
||||||
} from "../sources/errors.ts"
|
} from "../sources/errors.ts"
|
||||||
import { sources } from "../sources/user-sources.ts"
|
import { sources } from "../sources/user-sources.ts"
|
||||||
@@ -180,7 +179,7 @@ export class UserSessionManager {
|
|||||||
* @throws {InvalidSourceConfigError} if config fails schema validation
|
* @throws {InvalidSourceConfigError} if config fails schema validation
|
||||||
* @throws {CredentialStorageUnavailableError} if credentials are provided but no encryptor is configured
|
* @throws {CredentialStorageUnavailableError} if credentials are provided but no encryptor is configured
|
||||||
*/
|
*/
|
||||||
async upsertSourceConfig(
|
async saveSourceConfig(
|
||||||
userId: string,
|
userId: string,
|
||||||
sourceId: string,
|
sourceId: string,
|
||||||
data: { enabled: boolean; config?: unknown; credentials?: unknown },
|
data: { enabled: boolean; config?: unknown; credentials?: unknown },
|
||||||
@@ -224,12 +223,12 @@ export class UserSessionManager {
|
|||||||
session.removeSource(sourceId)
|
session.removeSource(sourceId)
|
||||||
} else {
|
} else {
|
||||||
// Prefer the just-provided credentials over what was in the DB.
|
// Prefer the just-provided credentials over what was in the DB.
|
||||||
const credentials =
|
let credentials: unknown = null
|
||||||
data.credentials !== undefined
|
if (data.credentials !== undefined) {
|
||||||
? data.credentials
|
credentials = data.credentials
|
||||||
: existingRow?.credentials
|
} else if (existingRow?.credentials) {
|
||||||
? this.decryptCredentials(existingRow.credentials)
|
credentials = this.decryptCredentials(existingRow.credentials)
|
||||||
: null
|
}
|
||||||
const source = await provider.feedSourceForUser(userId, config, credentials)
|
const source = await provider.feedSourceForUser(userId, config, credentials)
|
||||||
if (session.hasSource(sourceId)) {
|
if (session.hasSource(sourceId)) {
|
||||||
session.replaceSource(sourceId, source)
|
session.replaceSource(sourceId, source)
|
||||||
|
|||||||
@@ -168,7 +168,7 @@ async function handleReplaceSource(c: Context<Env>) {
|
|||||||
const user = c.get("user")!
|
const user = c.get("user")!
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await sessionManager.upsertSourceConfig(user.id, sourceId, {
|
await sessionManager.saveSourceConfig(user.id, sourceId, {
|
||||||
enabled,
|
enabled,
|
||||||
config,
|
config,
|
||||||
credentials,
|
credentials,
|
||||||
|
|||||||
Reference in New Issue
Block a user