Skip to content

Commit

Permalink
feat: implement pinned posts functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-ext committed May 2, 2024
1 parent 426079f commit d4f059e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
14 changes: 14 additions & 0 deletions packages/bsky/src/api/app/bsky/feed/getAuthorFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ export const skeleton = async (inputs: {
if (clearlyBadCursor(params.cursor)) {
return { actor, filter: params.filter, items: [] }
}

if (params.filter === 'pinned_posts') {
return {
actor,
filter: params.filter,
items:
actor.profile?.pinnedPosts?.map((uri) => ({
post: { uri: uri, cid: undefined },
repost: undefined,
})) || [],
cursor: undefined,
}
}

const res = await ctx.dataplane.getAuthorFeed({
actorDid: did,
limit: params.limit,
Expand Down
8 changes: 5 additions & 3 deletions packages/bsky/src/hydration/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type Posts = HydrationMap<Post>
export type PostViewerState = {
like?: string
repost?: string
pinned?: boolean
}

export type PostViewerStates = HydrationMap<PostViewerState>
Expand Down Expand Up @@ -86,23 +87,24 @@ export class FeedHydrator {

async getPostViewerStates(
refs: ItemRef[],
viewer: string,
{ did, pinnedPosts }: { did: string; pinnedPosts?: string[] },
): Promise<PostViewerStates> {
if (!refs.length) return new HydrationMap<PostViewerState>()
const [likes, reposts] = await Promise.all([
this.dataplane.getLikesByActorAndSubjects({
actorDid: viewer,
actorDid: did,
refs,
}),
this.dataplane.getRepostsByActorAndSubjects({
actorDid: viewer,
actorDid: did,
refs,
}),
])
return refs.reduce((acc, { uri }, i) => {
return acc.set(uri, {
like: parseString(likes.uris[i]),
repost: parseString(reposts.uris[i]),
pinned: pinnedPosts?.includes(uri) || undefined,
})
}, new HydrationMap<PostViewerState>())
}
Expand Down
21 changes: 15 additions & 6 deletions packages/bsky/src/hydration/hydrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Actors,
ProfileViewerStates,
ProfileViewerState,
Actor,
} from './actor'
import {
Follows,
Expand Down Expand Up @@ -269,11 +270,14 @@ export class Hydrator {
state: HydrationState = {},
): Promise<HydrationState> {
const uris = refs.map((ref) => ref.uri)
const postsLayer0 = await this.feed.getPosts(
uris,
ctx.includeTakedowns,
state.posts,
)
const [postsLayer0, viewer] = await Promise.all([
this.feed.getPosts(uris, ctx.includeTakedowns, state.posts),
ctx.viewer
? this.actor
.getActors([ctx.viewer])
.then((v) => v.get(ctx.viewer as string))
: undefined,
])
// first level embeds plus thread roots we haven't fetched yet
const urisLayer1 = nestedRecordUrisFromPosts(postsLayer0)
const additionalRootUris = rootUrisFromPosts(postsLayer0) // supports computing threadgates
Expand Down Expand Up @@ -329,7 +333,12 @@ export class Hydrator {
...postUrisLayer1.map(uriToRef), // supports aggregates on embed #viewRecords
...postUrisLayer2.map(uriToRef),
]),
ctx.viewer ? this.feed.getPostViewerStates(refs, ctx.viewer) : undefined,
ctx.viewer
? this.feed.getPostViewerStates(refs, {
did: ctx.viewer,
pinnedPosts: viewer?.profile?.pinnedPosts,
})
: undefined,
this.label.getLabelsForSubjects(allPostUris, ctx.labelers),
this.hydratePostBlocks(posts),
this.hydrateProfiles(allPostUris.map(didFromUri), ctx),
Expand Down
2 changes: 2 additions & 0 deletions packages/bsky/src/views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export class Views {
lists: profileAggs?.lists,
feedgens: profileAggs?.feeds,
labeler: actor?.isLabeler,
pinnedPosts: actor.profile?.pinnedPosts?.length,
},
}
}
Expand Down Expand Up @@ -443,6 +444,7 @@ export class Views {
? {
repost: viewer.repost,
like: viewer.like,
pinned: viewer.pinned,
replyDisabled: this.userReplyDisabled(uri, state),
}
: undefined,
Expand Down

0 comments on commit d4f059e

Please sign in to comment.