-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement getBlocks, block application fix
- Loading branch information
Showing
4 changed files
with
86 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,85 @@ | ||
import { mapDefined } from '@atproto/common' | ||
import { Server } from '../../../../lexicon' | ||
import { paginate, TimeCidKeyset } from '../../../../db/pagination' | ||
import { QueryParams } from '../../../../lexicon/types/app/bsky/graph/getBlocks' | ||
import AppContext from '../../../../context' | ||
import { notSoftDeletedClause } from '../../../../db/util' | ||
import { | ||
createPipelineNew, | ||
HydrationFnInput, | ||
noRulesNew, | ||
PresentationFnInput, | ||
SkeletonFnInput, | ||
} from '../../../../pipeline' | ||
import { Hydrator } from '../../../../hydration/hydrator' | ||
import { Views } from '../../../../views' | ||
|
||
export default function (server: Server, ctx: AppContext) { | ||
const getBlocks = createPipelineNew( | ||
skeleton, | ||
hydration, | ||
noRulesNew, | ||
presentation, | ||
) | ||
server.app.bsky.graph.getBlocks({ | ||
auth: ctx.authVerifier, | ||
handler: async ({ params, auth }) => { | ||
const { limit, cursor } = params | ||
const requester = auth.credentials.did | ||
const db = ctx.db.getReplica() | ||
const { ref } = db.db.dynamic | ||
|
||
let blocksReq = db.db | ||
.selectFrom('actor_block') | ||
.where('actor_block.creator', '=', requester) | ||
.innerJoin('actor as subject', 'subject.did', 'actor_block.subjectDid') | ||
.where(notSoftDeletedClause(ref('subject'))) | ||
.selectAll('subject') | ||
.select(['actor_block.cid as cid', 'actor_block.sortAt as sortAt']) | ||
|
||
const keyset = new TimeCidKeyset( | ||
ref('actor_block.sortAt'), | ||
ref('actor_block.cid'), | ||
) | ||
blocksReq = paginate(blocksReq, { | ||
limit, | ||
cursor, | ||
keyset, | ||
}) | ||
|
||
const blocksRes = await blocksReq.execute() | ||
|
||
const actorService = ctx.services.actor(db) | ||
const blocks = await actorService.views.profilesList(blocksRes, requester) | ||
|
||
const viewer = auth.credentials.did | ||
const result = await getBlocks({ ...params, viewer }, ctx) | ||
return { | ||
encoding: 'application/json', | ||
body: { | ||
blocks, | ||
cursor: keyset.packFromResult(blocksRes), | ||
}, | ||
body: result, | ||
} | ||
}, | ||
}) | ||
} | ||
|
||
const skeleton = async (input: SkeletonFnInput<Context, Params>) => { | ||
const { params, ctx } = input | ||
const { blockUris, cursor } = await ctx.hydrator.dataplane.getBlocks({ | ||
actorDid: params.viewer, | ||
cursor: params.cursor, | ||
limit: params.limit, | ||
}) | ||
const blocks = await ctx.hydrator.graph.getBlocks(blockUris) | ||
const blockedDids = mapDefined( | ||
blockUris, | ||
(uri) => blocks.get(uri)?.record.subject, | ||
) | ||
return { | ||
blockedDids, | ||
cursor: cursor || undefined, | ||
} | ||
} | ||
|
||
const hydration = async ( | ||
input: HydrationFnInput<Context, Params, SkeletonState>, | ||
) => { | ||
const { ctx, params, skeleton } = input | ||
const { viewer } = params | ||
const { blockedDids } = skeleton | ||
return ctx.hydrator.hydrateProfiles(blockedDids, viewer) | ||
} | ||
|
||
const presentation = ( | ||
input: PresentationFnInput<Context, Params, SkeletonState>, | ||
) => { | ||
const { ctx, hydration, skeleton } = input | ||
const { blockedDids, cursor } = skeleton | ||
const blocks = mapDefined(blockedDids, (did) => { | ||
return ctx.views.profile(did, hydration) | ||
}) | ||
return { blocks, cursor } | ||
} | ||
|
||
type Context = { | ||
hydrator: Hydrator | ||
views: Views | ||
} | ||
|
||
type Params = QueryParams & { | ||
viewer: string | ||
} | ||
|
||
type SkeletonState = { | ||
blockedDids: string[] | ||
cursor?: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters