From 9395b18214e3283f8b562fe49f026decff12e308 Mon Sep 17 00:00:00 2001 From: devin ivy Date: Fri, 19 May 2023 12:00:39 -0400 Subject: [PATCH] Add temp fix for blobref validation issue (#23) --- src/util/subscription.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/util/subscription.ts b/src/util/subscription.ts index e3c0111d..4061f3f3 100644 --- a/src/util/subscription.ts +++ b/src/util/subscription.ts @@ -1,5 +1,6 @@ import { Subscription } from '@atproto/xrpc-server' import { cborToLexRecord, readCar } from '@atproto/repo' +import { BlobRef } from '@atproto/lexicon' import { ids, lexicons } from '../lexicon/lexicons' import { Record as PostRecord } from '../lexicon/types/app/bsky/feed/post' import { Record as RepostRecord } from '../lexicon/types/app/bsky/feed/repost' @@ -156,9 +157,29 @@ export const isFollow = (obj: unknown): obj is FollowRecord => { const isType = (obj: unknown, nsid: string) => { try { - lexicons.assertValidRecord(nsid, obj) + lexicons.assertValidRecord(nsid, fixBlobRefs(obj)) return true } catch (err) { return false } } + +// @TODO right now record validation fails on BlobRefs +// simply because multiple packages have their own copy +// of the BlobRef class, causing instanceof checks to fail. +// This is a temporary solution. +const fixBlobRefs = (obj: unknown): unknown => { + if (Array.isArray(obj)) { + return obj.map(fixBlobRefs) + } + if (obj && typeof obj === 'object') { + if (obj.constructor.name === 'BlobRef') { + const blob = obj as BlobRef + return new BlobRef(blob.ref, blob.mimeType, blob.size, blob.original) + } + return Object.entries(obj).reduce((acc, [key, val]) => { + return Object.assign(acc, { [key]: fixBlobRefs(val) }) + }, {} as Record) + } + return obj +}