Skip to content

Commit

Permalink
Updated semantics for atproto labelers header (#2292)
Browse files Browse the repository at this point in the history
* update labelers header semantics

* add response header

* pds: pipe through res headers

* fix up tests

* revamp parsing
  • Loading branch information
dholms authored Mar 12, 2024
1 parent 8dd67f5 commit 7b14660
Show file tree
Hide file tree
Showing 39 changed files with 223 additions and 68 deletions.
1 change: 1 addition & 0 deletions packages/bsky/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"pino": "^8.15.0",
"pino-http": "^8.2.1",
"sharp": "^0.32.6",
"structured-headers": "^1.0.1",
"typed-emitter": "^2.1.0",
"uint8arrays": "3.0.0"
},
Expand Down
9 changes: 6 additions & 3 deletions packages/bsky/src/api/app/bsky/actor/getProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { InvalidRequestError } from '@atproto/xrpc-server'
import { Server } from '../../../../lexicon'
import { QueryParams } from '../../../../lexicon/types/app/bsky/actor/getProfile'
import AppContext from '../../../../context'
import { setRepoRev } from '../../../util'
import { resHeaders } from '../../../util'
import { createPipeline, noRules } from '../../../../pipeline'
import {
HydrateCtx,
Expand All @@ -15,7 +15,7 @@ export default function (server: Server, ctx: AppContext) {
const getProfile = createPipeline(skeleton, hydration, noRules, presentation)
server.app.bsky.actor.getProfile({
auth: ctx.authVerifier.optionalStandardOrRole,
handler: async ({ auth, params, req, res }) => {
handler: async ({ auth, params, req }) => {
const { viewer, canViewTakedowns } = ctx.authVerifier.parseCreds(auth)
const labelers = ctx.reqLabelers(req)
const hydrateCtx = { labelers, viewer }
Expand All @@ -26,11 +26,14 @@ export default function (server: Server, ctx: AppContext) {
)

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

return {
encoding: 'application/json',
body: result,
headers: resHeaders({
repoRev,
labelers,
}),
}
},
})
Expand Down
9 changes: 6 additions & 3 deletions packages/bsky/src/api/app/bsky/actor/getProfiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { mapDefined } from '@atproto/common'
import { Server } from '../../../../lexicon'
import { QueryParams } from '../../../../lexicon/types/app/bsky/actor/getProfiles'
import AppContext from '../../../../context'
import { setRepoRev } from '../../../util'
import { resHeaders } from '../../../util'
import { createPipeline, noRules } from '../../../../pipeline'
import {
HydrateCtx,
Expand All @@ -15,19 +15,22 @@ export default function (server: Server, ctx: AppContext) {
const getProfile = createPipeline(skeleton, hydration, noRules, presentation)
server.app.bsky.actor.getProfiles({
auth: ctx.authVerifier.standardOptional,
handler: async ({ auth, params, req, res }) => {
handler: async ({ auth, params, req }) => {
const viewer = auth.credentials.iss
const labelers = ctx.reqLabelers(req)
const hydrateCtx = { viewer, labelers }

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

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

return {
encoding: 'application/json',
body: result,
headers: resHeaders({
repoRev,
labelers,
}),
}
},
})
Expand Down
2 changes: 2 additions & 0 deletions packages/bsky/src/api/app/bsky/actor/getSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { Views } from '../../../../views'
import { DataPlaneClient } from '../../../../data-plane'
import { parseString } from '../../../../hydration/util'
import { resHeaders } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getSuggestions = createPipeline(
Expand All @@ -30,6 +31,7 @@ export default function (server: Server, ctx: AppContext) {
return {
encoding: 'application/json',
body: result,
headers: resHeaders({ labelers }),
}
},
})
Expand Down
2 changes: 2 additions & 0 deletions packages/bsky/src/api/app/bsky/actor/searchActors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { HydrateCtx, Hydrator } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { DataPlaneClient } from '../../../../data-plane'
import { parseString } from '../../../../hydration/util'
import { resHeaders } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const searchActors = createPipeline(
Expand All @@ -32,6 +33,7 @@ export default function (server: Server, ctx: AppContext) {
return {
encoding: 'application/json',
body: results,
headers: resHeaders({ labelers }),
}
},
})
Expand Down
2 changes: 2 additions & 0 deletions packages/bsky/src/api/app/bsky/actor/searchActorsTypeahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { HydrateCtx, Hydrator } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { DataPlaneClient } from '../../../../data-plane'
import { parseString } from '../../../../hydration/util'
import { resHeaders } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const searchActorsTypeahead = createPipeline(
Expand All @@ -35,6 +36,7 @@ export default function (server: Server, ctx: AppContext) {
return {
encoding: 'application/json',
body: results,
headers: resHeaders({ labelers }),
}
},
})
Expand Down
3 changes: 2 additions & 1 deletion packages/bsky/src/api/app/bsky/feed/getActorFeeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { Views } from '../../../../views'
import { DataPlaneClient } from '../../../../data-plane'
import { parseString } from '../../../../hydration/util'
import { clearlyBadCursor } from '../../../util'
import { clearlyBadCursor, resHeaders } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getActorFeeds = createPipeline(
Expand All @@ -31,6 +31,7 @@ export default function (server: Server, ctx: AppContext) {
return {
encoding: 'application/json',
body: result,
headers: resHeaders({ labelers }),
}
},
})
Expand Down
9 changes: 6 additions & 3 deletions packages/bsky/src/api/app/bsky/feed/getActorLikes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { mapDefined } from '@atproto/common'
import { Server } from '../../../../lexicon'
import { QueryParams } from '../../../../lexicon/types/app/bsky/feed/getActorLikes'
import AppContext from '../../../../context'
import { clearlyBadCursor, setRepoRev } from '../../../util'
import { clearlyBadCursor, resHeaders } from '../../../util'
import { createPipeline } from '../../../../pipeline'
import {
HydrateCtx,
Expand All @@ -25,19 +25,22 @@ export default function (server: Server, ctx: AppContext) {
)
server.app.bsky.feed.getActorLikes({
auth: ctx.authVerifier.standardOptional,
handler: async ({ params, auth, req, res }) => {
handler: async ({ params, auth, req }) => {
const viewer = auth.credentials.iss
const labelers = ctx.reqLabelers(req)
const hydrateCtx = { labelers, viewer }

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

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

return {
encoding: 'application/json',
body: result,
headers: resHeaders({
repoRev,
labelers,
}),
}
},
})
Expand Down
9 changes: 6 additions & 3 deletions packages/bsky/src/api/app/bsky/feed/getAuthorFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { InvalidRequestError } from '@atproto/xrpc-server'
import { Server } from '../../../../lexicon'
import { QueryParams } from '../../../../lexicon/types/app/bsky/feed/getAuthorFeed'
import AppContext from '../../../../context'
import { clearlyBadCursor, setRepoRev } from '../../../util'
import { clearlyBadCursor, resHeaders } from '../../../util'
import { createPipeline } from '../../../../pipeline'
import {
HydrateCtx,
Expand All @@ -27,7 +27,7 @@ export default function (server: Server, ctx: AppContext) {
)
server.app.bsky.feed.getAuthorFeed({
auth: ctx.authVerifier.optionalStandardOrRole,
handler: async ({ params, auth, req, res }) => {
handler: async ({ params, auth, req }) => {
const { viewer, canViewTakedowns } = ctx.authVerifier.parseCreds(auth)
const labelers = ctx.reqLabelers(req)
const hydrateCtx = { labelers, viewer }
Expand All @@ -38,11 +38,14 @@ export default function (server: Server, ctx: AppContext) {
)

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

return {
encoding: 'application/json',
body: result,
headers: resHeaders({
repoRev,
labelers,
}),
}
},
})
Expand Down
14 changes: 9 additions & 5 deletions packages/bsky/src/api/app/bsky/feed/getFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
isDataplaneError,
unpackIdentityServices,
} from '../../../../data-plane'
import { resHeaders } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getFeed = createPipeline(
Expand All @@ -47,16 +48,19 @@ export default function (server: Server, ctx: AppContext) {
'accept-language': req.headers['accept-language'],
})
// @NOTE feed cursors should not be affected by appview swap
const { timerSkele, timerHydr, resHeaders, ...result } = await getFeed(
{ ...params, hydrateCtx, headers },
ctx,
)
const {
timerSkele,
timerHydr,
resHeaders: feedResHeaders,
...result
} = await getFeed({ ...params, hydrateCtx, headers }, ctx)

return {
encoding: 'application/json',
body: result,
headers: {
...(resHeaders ?? {}),
...(feedResHeaders ?? {}),
...resHeaders({ labelers }),
'server-timing': serverTimingHeader([timerSkele, timerHydr]),
},
}
Expand Down
2 changes: 2 additions & 0 deletions packages/bsky/src/api/app/bsky/feed/getFeedGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isDataplaneError,
unpackIdentityServices,
} from '../../../../data-plane'
import { resHeaders } from '../../../util'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getFeedGenerator({
Expand Down Expand Up @@ -63,6 +64,7 @@ export default function (server: Server, ctx: AppContext) {
isOnline: true,
isValid: true,
},
headers: resHeaders({ labelers }),
}
},
})
Expand Down
2 changes: 2 additions & 0 deletions packages/bsky/src/api/app/bsky/feed/getFeedGenerators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Hydrator,
} from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { resHeaders } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getFeedGenerators = createPipeline(
Expand All @@ -27,6 +28,7 @@ export default function (server: Server, ctx: AppContext) {
return {
encoding: 'application/json',
body: view,
headers: resHeaders({ labelers }),
}
},
})
Expand Down
3 changes: 2 additions & 1 deletion packages/bsky/src/api/app/bsky/feed/getLikes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { Views } from '../../../../views'
import { parseString } from '../../../../hydration/util'
import { creatorFromUri } from '../../../../views/util'
import { clearlyBadCursor } from '../../../util'
import { clearlyBadCursor, resHeaders } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getLikes = createPipeline(skeleton, hydration, noBlocks, presentation)
Expand All @@ -27,6 +27,7 @@ export default function (server: Server, ctx: AppContext) {
return {
encoding: 'application/json',
body: result,
headers: resHeaders({ labelers }),
}
},
})
Expand Down
6 changes: 3 additions & 3 deletions packages/bsky/src/api/app/bsky/feed/getListFeed.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Server } from '../../../../lexicon'
import { QueryParams } from '../../../../lexicon/types/app/bsky/feed/getListFeed'
import AppContext from '../../../../context'
import { clearlyBadCursor, setRepoRev } from '../../../util'
import { clearlyBadCursor, resHeaders } from '../../../util'
import { createPipeline } from '../../../../pipeline'
import {
HydrateCtx,
Expand All @@ -23,19 +23,19 @@ export default function (server: Server, ctx: AppContext) {
)
server.app.bsky.feed.getListFeed({
auth: ctx.authVerifier.standardOptional,
handler: async ({ params, auth, req, res }) => {
handler: async ({ params, auth, req }) => {
const viewer = auth.credentials.iss
const labelers = ctx.reqLabelers(req)
const hydrateCtx = { labelers, viewer }

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

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

return {
encoding: 'application/json',
body: result,
headers: resHeaders({ labelers, repoRev }),
}
},
})
Expand Down
11 changes: 8 additions & 3 deletions packages/bsky/src/api/app/bsky/feed/getPostThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
OutputSchema,
} from '../../../../lexicon/types/app/bsky/feed/getPostThread'
import AppContext from '../../../../context'
import { setRepoRev } from '../../../util'
import { ATPROTO_REPO_REV, resHeaders } from '../../../util'
import {
HydrationFnInput,
PresentationFnInput,
Expand Down Expand Up @@ -37,16 +37,21 @@ export default function (server: Server, ctx: AppContext) {
result = await getPostThread({ ...params, hydrateCtx }, ctx)
} catch (err) {
const repoRev = await ctx.hydrator.actor.getRepoRevSafe(viewer)
setRepoRev(res, repoRev)
if (repoRev) {
res.setHeader(ATPROTO_REPO_REV, repoRev)
}
throw err
}

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

return {
encoding: 'application/json',
body: result,
headers: resHeaders({
repoRev,
labelers,
}),
}
},
})
Expand Down
2 changes: 2 additions & 0 deletions packages/bsky/src/api/app/bsky/feed/getPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { creatorFromUri } from '../../../../views/util'
import { resHeaders } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getPosts = createPipeline(skeleton, hydration, noBlocks, presentation)
Expand All @@ -25,6 +26,7 @@ export default function (server: Server, ctx: AppContext) {
return {
encoding: 'application/json',
body: results,
headers: resHeaders({ labelers }),
}
},
})
Expand Down
3 changes: 2 additions & 1 deletion packages/bsky/src/api/app/bsky/feed/getRepostedBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { Views } from '../../../../views'
import { parseString } from '../../../../hydration/util'
import { creatorFromUri } from '../../../../views/util'
import { clearlyBadCursor } from '../../../util'
import { clearlyBadCursor, resHeaders } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getRepostedBy = createPipeline(
Expand All @@ -31,6 +31,7 @@ export default function (server: Server, ctx: AppContext) {
return {
encoding: 'application/json',
body: result,
headers: resHeaders({ labelers }),
}
},
})
Expand Down
Loading

0 comments on commit 7b14660

Please sign in to comment.