Skip to content

Commit

Permalink
Action !takedown labels in hydrator (#2270)
Browse files Browse the repository at this point in the history
* action takedown labels in hydrator

* wip tests

* fix tests & better hydration

* tidy up includeTakedowns

* update labelers header semantics

* add response header

* pds: pipe through res headers

* fix up tests

* tidy tests

* re-include example in dev-env

* fix test

* revamp parsing

* fix build err in hydrator

* fix more build errs

* fix test
  • Loading branch information
dholms authored Mar 12, 2024
1 parent 7b14660 commit 3d32d1c
Show file tree
Hide file tree
Showing 14 changed files with 285 additions and 104 deletions.
21 changes: 8 additions & 13 deletions packages/bsky/src/api/app/bsky/actor/getProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ export default function (server: Server, ctx: AppContext) {
server.app.bsky.actor.getProfile({
auth: ctx.authVerifier.optionalStandardOrRole,
handler: async ({ auth, params, req }) => {
const { viewer, canViewTakedowns } = ctx.authVerifier.parseCreds(auth)
const { viewer, includeTakedowns } = ctx.authVerifier.parseCreds(auth)
const labelers = ctx.reqLabelers(req)
const hydrateCtx = { labelers, viewer }
const hydrateCtx = { labelers, viewer, includeTakedowns }

const result = await getProfile(
{ ...params, hydrateCtx, canViewTakedowns },
ctx,
)
const result = await getProfile({ ...params, hydrateCtx }, ctx)

const repoRev = await ctx.hydrator.actor.getRepoRevSafe(viewer)

Expand Down Expand Up @@ -57,11 +54,10 @@ const hydration = async (input: {
skeleton: SkeletonState
}) => {
const { ctx, params, skeleton } = input
return ctx.hydrator.hydrateProfilesDetailed(
[skeleton.did],
params.hydrateCtx,
true,
)
return ctx.hydrator.hydrateProfilesDetailed([skeleton.did], {
...params.hydrateCtx,
includeTakedowns: true,
})
}

const presentation = (input: {
Expand All @@ -75,7 +71,7 @@ const presentation = (input: {
if (!profile) {
throw new InvalidRequestError('Profile not found')
} else if (
!params.canViewTakedowns &&
!params.hydrateCtx.includeTakedowns &&
ctx.views.actorIsTakendown(skeleton.did, hydration)
) {
throw new InvalidRequestError(
Expand All @@ -93,7 +89,6 @@ type Context = {

type Params = QueryParams & {
hydrateCtx: HydrateCtx
canViewTakedowns: boolean
}

type SkeletonState = { did: string }
6 changes: 1 addition & 5 deletions packages/bsky/src/api/app/bsky/actor/getSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ const hydration = async (input: {
skeleton: Skeleton
}) => {
const { ctx, params, skeleton } = input
return ctx.hydrator.hydrateProfilesDetailed(
skeleton.dids,
params.hydrateCtx,
true,
)
return ctx.hydrator.hydrateProfilesDetailed(skeleton.dids, params.hydrateCtx)
}

const noBlocksOrMutes = (input: {
Expand Down
18 changes: 5 additions & 13 deletions packages/bsky/src/api/app/bsky/feed/getAuthorFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getAuthorFeed({
auth: ctx.authVerifier.optionalStandardOrRole,
handler: async ({ params, auth, req }) => {
const { viewer, canViewTakedowns } = ctx.authVerifier.parseCreds(auth)
const { viewer, includeTakedowns } = ctx.authVerifier.parseCreds(auth)
const labelers = ctx.reqLabelers(req)
const hydrateCtx = { labelers, viewer }
const hydrateCtx = { labelers, viewer, includeTakedowns }

const result = await getAuthorFeed(
{ ...params, hydrateCtx, includeTakedowns: canViewTakedowns },
ctx,
)
const result = await getAuthorFeed({ ...params, hydrateCtx }, ctx)

const repoRev = await ctx.hydrator.actor.getRepoRevSafe(viewer)

Expand Down Expand Up @@ -69,7 +66,7 @@ export const skeleton = async (inputs: {
}
const actors = await ctx.hydrator.actor.getActors(
[did],
params.includeTakedowns,
params.hydrateCtx.includeTakedowns,
)
const actor = actors.get(did)
if (!actor) {
Expand Down Expand Up @@ -103,11 +100,7 @@ const hydration = async (inputs: {
}): Promise<HydrationState> => {
const { ctx, params, skeleton } = inputs
const [feedPostState, profileViewerState] = await Promise.all([
ctx.hydrator.hydrateFeedItems(
skeleton.items,
params.hydrateCtx,
params.includeTakedowns,
),
ctx.hydrator.hydrateFeedItems(skeleton.items, params.hydrateCtx),
ctx.hydrator.hydrateProfileViewers([skeleton.actor.did], params.hydrateCtx),
])
return mergeStates(feedPostState, profileViewerState)
Expand Down Expand Up @@ -163,7 +156,6 @@ type Context = {

type Params = QueryParams & {
hydrateCtx: HydrateCtx
includeTakedowns: boolean
}

type Skeleton = {
Expand Down
18 changes: 8 additions & 10 deletions packages/bsky/src/api/app/bsky/graph/getFollowers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,11 @@ export default function (server: Server, ctx: AppContext) {
server.app.bsky.graph.getFollowers({
auth: ctx.authVerifier.optionalStandardOrRole,
handler: async ({ params, auth, req }) => {
const { viewer, canViewTakedowns } = ctx.authVerifier.parseCreds(auth)
const { viewer, includeTakedowns } = ctx.authVerifier.parseCreds(auth)
const labelers = ctx.reqLabelers(req)
const hydrateCtx = { labelers, viewer }
const hydrateCtx = { labelers, viewer, includeTakedowns }

const result = await getFollowers(
{ ...params, hydrateCtx, canViewTakedowns },
ctx,
)
const result = await getFollowers({ ...params, hydrateCtx }, ctx)

return {
encoding: 'application/json',
Expand Down Expand Up @@ -85,7 +82,6 @@ const hydration = async (
const profileState = await ctx.hydrator.hydrateProfiles(
dids,
params.hydrateCtx,
params.canViewTakedowns,
)
return mergeStates(followState, profileState)
}
Expand All @@ -112,13 +108,16 @@ const presentation = (
ctx.views.actorIsTakendown(did, hydration)

const subject = ctx.views.profile(subjectDid, hydration)
if (!subject || (!params.canViewTakedowns && isTakendown(subjectDid))) {
if (
!subject ||
(!params.hydrateCtx.includeTakedowns && isTakendown(subjectDid))
) {
throw new InvalidRequestError(`Actor not found: ${params.actor}`)
}

const followers = mapDefined(followUris, (followUri) => {
const followerDid = didFromUri(followUri)
if (!params.canViewTakedowns && isTakendown(followerDid)) {
if (!params.hydrateCtx.includeTakedowns && isTakendown(followerDid)) {
return
}
return ctx.views.profile(didFromUri(followUri), hydration)
Expand All @@ -134,7 +133,6 @@ type Context = {

type Params = QueryParams & {
hydrateCtx: HydrateCtx
canViewTakedowns: boolean
}

type SkeletonState = {
Expand Down
18 changes: 8 additions & 10 deletions packages/bsky/src/api/app/bsky/graph/getFollows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,12 @@ export default function (server: Server, ctx: AppContext) {
server.app.bsky.graph.getFollows({
auth: ctx.authVerifier.optionalStandardOrRole,
handler: async ({ params, auth, req }) => {
const { viewer, canViewTakedowns } = ctx.authVerifier.parseCreds(auth)
const { viewer, includeTakedowns } = ctx.authVerifier.parseCreds(auth)
const labelers = ctx.reqLabelers(req)
const hydrateCtx = { labelers, viewer }
const hydrateCtx = { labelers, viewer, includeTakedowns }

// @TODO ensure canViewTakedowns gets threaded through and applied properly
const result = await getFollows(
{ ...params, hydrateCtx, canViewTakedowns },
ctx,
)
const result = await getFollows({ ...params, hydrateCtx }, ctx)

return {
encoding: 'application/json',
Expand Down Expand Up @@ -80,7 +77,6 @@ const hydration = async (
const profileState = await ctx.hydrator.hydrateProfiles(
dids,
params.hydrateCtx,
params.canViewTakedowns,
)
return mergeStates(followState, profileState)
}
Expand Down Expand Up @@ -109,14 +105,17 @@ const presentation = (
ctx.views.actorIsTakendown(did, hydration)

const subject = ctx.views.profile(subjectDid, hydration)
if (!subject || (!params.canViewTakedowns && isTakendown(subjectDid))) {
if (
!subject ||
(!params.hydrateCtx.includeTakedowns && isTakendown(subjectDid))
) {
throw new InvalidRequestError(`Actor not found: ${params.actor}`)
}

const follows = mapDefined(followUris, (followUri) => {
const followDid = hydration.follows?.get(followUri)?.record.subject
if (!followDid) return
if (!params.canViewTakedowns && isTakendown(followDid)) {
if (!params.hydrateCtx.includeTakedowns && isTakendown(followDid)) {
return
}
return ctx.views.profile(followDid, hydration)
Expand All @@ -132,7 +131,6 @@ type Context = {

type Params = QueryParams & {
hydrateCtx: HydrateCtx
canViewTakedowns: boolean
}

type SkeletonState = {
Expand Down
6 changes: 3 additions & 3 deletions packages/bsky/src/api/com/atproto/admin/getAccountInfos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ export default function (server: Server, ctx: AppContext) {
auth: ctx.authVerifier.optionalStandardOrRole,
handler: async ({ params, auth }) => {
const { dids } = params
const { canViewTakedowns } = ctx.authVerifier.parseCreds(auth)
const { includeTakedowns } = ctx.authVerifier.parseCreds(auth)

const actors = await ctx.hydrator.actor.getActors(dids, true)

const infos = mapDefined(dids, (did) => {
const info = actors.get(did)
if (!info) return
if (info.takedownRef && !canViewTakedowns) return
if (info.takedownRef && !includeTakedowns) return
const profileRecord =
!info.profileTakedownRef || canViewTakedowns
!info.profileTakedownRef || includeTakedowns
? info.profile
: undefined
return {
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/auth-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export class AuthVerifier {
) {
const viewer =
creds.credentials.type === 'standard' ? creds.credentials.iss : null
const canViewTakedowns =
const includeTakedowns =
(creds.credentials.type === 'role' && creds.credentials.admin) ||
creds.credentials.type === 'mod_service' ||
(creds.credentials.type === 'standard' &&
Expand All @@ -269,7 +269,7 @@ export class AuthVerifier {

return {
viewer,
canViewTakedowns,
includeTakedowns,
canPerformTakedown,
}
}
Expand Down
Loading

0 comments on commit 3d32d1c

Please sign in to comment.