From 7096fe03d02f3702577bd52be085bbdea7266f17 Mon Sep 17 00:00:00 2001 From: devin ivy Date: Fri, 11 Oct 2024 14:16:23 -0400 Subject: [PATCH] Appview: temp fix for bad pinned post values (#2875) appview: temp fix for bad pinned post values --- .../bsky/src/api/app/bsky/feed/getAuthorFeed.ts | 13 +++++++------ packages/bsky/src/util/uris.ts | 17 ++++++++++++++++- packages/bsky/src/views/index.ts | 6 +++--- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/bsky/src/api/app/bsky/feed/getAuthorFeed.ts b/packages/bsky/src/api/app/bsky/feed/getAuthorFeed.ts index ea353bb0625..d58500c07c4 100644 --- a/packages/bsky/src/api/app/bsky/feed/getAuthorFeed.ts +++ b/packages/bsky/src/api/app/bsky/feed/getAuthorFeed.ts @@ -14,7 +14,7 @@ import { import { Views } from '../../../../views' import { DataPlaneClient } from '../../../../data-plane' import { parseString } from '../../../../hydration/util' -import { uriToDid } from '../../../../util/uris' +import { safePinnedPost, uriToDid } from '../../../../util/uris' import { Actor } from '../../../../hydration/actor' import { FeedItem, Post } from '../../../../hydration/feed' import { FeedType } from '../../../../proto/bsky_pb' @@ -81,12 +81,13 @@ export const skeleton = async (inputs: { return { actor, filter: params.filter, items: [] } } + const pinnedPost = safePinnedPost(actor.profile?.pinnedPost) const isFirstPageRequest = !params.cursor const shouldInsertPinnedPost = isFirstPageRequest && params.includePins && - !!actor.profile?.pinnedPost && - uriToDid(actor.profile.pinnedPost.uri) === actor.did + pinnedPost && + uriToDid(pinnedPost.uri) === actor.did const res = await ctx.dataplane.getAuthorFeed({ actorDid: did, @@ -102,11 +103,11 @@ export const skeleton = async (inputs: { : undefined, })) - if (shouldInsertPinnedPost && actor.profile?.pinnedPost) { + if (shouldInsertPinnedPost && pinnedPost) { const pinnedItem = { post: { - uri: actor.profile.pinnedPost.uri, - cid: actor.profile.pinnedPost.cid, + uri: pinnedPost.uri, + cid: pinnedPost.cid, }, authorPinned: true, } diff --git a/packages/bsky/src/util/uris.ts b/packages/bsky/src/util/uris.ts index 2884f65df54..fca731e098d 100644 --- a/packages/bsky/src/util/uris.ts +++ b/packages/bsky/src/util/uris.ts @@ -1,6 +1,9 @@ -import assert from 'node:assert' import { AtUri } from '@atproto/syntax' import { ids } from '../lexicon/lexicons' +import { + validateMain as validateStrongRef, + Main as StrongRef, +} from '../lexicon/types/com/atproto/repo/strongRef' /** * Convert a post URI to a threadgate URI. If the URI is not a valid @@ -29,3 +32,15 @@ export function postUriToPostgateUri(postUri: string) { export function uriToDid(uri: string) { return new AtUri(uri).hostname } + +// @TODO temp fix for proliferation of invalid pinned post values +export function safePinnedPost(value: unknown) { + if (!value) { + return + } + const validated = validateStrongRef(value) + if (!validated.success) { + return + } + return validated.value as StrongRef +} diff --git a/packages/bsky/src/views/index.ts b/packages/bsky/src/views/index.ts index 248e343a37f..69983870756 100644 --- a/packages/bsky/src/views/index.ts +++ b/packages/bsky/src/views/index.ts @@ -35,7 +35,7 @@ import { VideoUriBuilder, parsePostgate, } from './util' -import { uriToDid as creatorFromUri } from '../util/uris' +import { uriToDid as creatorFromUri, safePinnedPost } from '../util/uris' import { isListRule } from '../lexicon/types/app/bsky/feed/threadgate' import { isSelfLabels } from '../lexicon/types/com/atproto/label/defs' import { @@ -170,7 +170,7 @@ export class Views { joinedViaStarterPack: actor.profile?.joinedViaStarterPack ? this.starterPackBasic(actor.profile.joinedViaStarterPack.uri, state) : undefined, - pinnedPost: actor.profile?.pinnedPost, + pinnedPost: safePinnedPost(actor.profile?.pinnedPost), } } @@ -1143,7 +1143,7 @@ export class Views { if (!state.ctx?.viewer || state.ctx.viewer !== authorDid) return const actor = state.actors?.get(authorDid) if (!actor) return - const pinnedPost = actor.profile?.pinnedPost + const pinnedPost = safePinnedPost(actor.profile?.pinnedPost) if (!pinnedPost) return undefined return pinnedPost.uri === uri }