Skip to content

Commit

Permalink
Appview: temp fix for bad pinned post values (#2875)
Browse files Browse the repository at this point in the history
appview: temp fix for bad pinned post values
  • Loading branch information
devinivy authored Oct 11, 2024
1 parent fabc8a9 commit 7096fe0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
13 changes: 7 additions & 6 deletions packages/bsky/src/api/app/bsky/feed/getAuthorFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}
Expand Down
17 changes: 16 additions & 1 deletion packages/bsky/src/util/uris.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions packages/bsky/src/views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 7096fe0

Please sign in to comment.