From 3c32284271032901ee92767587a7a7a49dfd1f88 Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Mon, 20 Mar 2023 10:12:38 +0000 Subject: [PATCH] more fix properties parsing with Neon --- backend/src/activitypub/objects/index.ts | 20 ++++++++++++++++++-- backend/src/mastodon/idempotency.ts | 10 +++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/backend/src/activitypub/objects/index.ts b/backend/src/activitypub/objects/index.ts index 70b3e36..db736d1 100644 --- a/backend/src/activitypub/objects/index.ts +++ b/backend/src/activitypub/objects/index.ts @@ -128,7 +128,15 @@ export async function cacheObject( } { - const properties = JSON.parse(row.properties) + let properties + if (typeof row.properties === 'object') { + // neon uses JSONB for properties which is returned as a deserialized + // object. + properties = row.properties + } else { + // D1 uses a string for JSON properties + properties = JSON.parse(row.properties) + } const object = { published: new Date(row.cdate).toISOString(), ...properties, @@ -206,7 +214,15 @@ export async function getObjectBy(db: Database, key: ObjectByKey, value: string) } const result: any = results[0] - const properties = JSON.parse(result.properties) + let properties + if (typeof result.properties === 'object') { + // neon uses JSONB for properties which is returned as a deserialized + // object. + properties = result.properties + } else { + // D1 uses a string for JSON properties + properties = JSON.parse(result.properties) + } return { published: new Date(result.cdate).toISOString(), diff --git a/backend/src/mastodon/idempotency.ts b/backend/src/mastodon/idempotency.ts index f5c1bee..c9f5b98 100644 --- a/backend/src/mastodon/idempotency.ts +++ b/backend/src/mastodon/idempotency.ts @@ -36,7 +36,15 @@ export async function hasKey(db: Database, key: string): Promise