Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appview v2 handling clearly bad cursors #2092

Merged
merged 4 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/bsky/src/api/app/bsky/actor/getSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const skeleton = async (input: {
}): Promise<Skeleton> => {
const { ctx, params } = input
let dids: string[] = []
// @NOTE for appview swap moving to rkey-based cursors which are somewhat permissive, should not hard-break pagination
let cursor: string | undefined = params.cursor
// filter out follows and re-fetch if left with an empty page
while (dids.length === 0) {
Expand Down
1 change: 1 addition & 0 deletions packages/bsky/src/api/app/bsky/actor/searchActors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const skeleton = async (inputs: SkeletonFnInput<Context, Params>) => {
// add hits total

if (ctx.searchAgent) {
// @NOTE cursors wont change on appview swap
const { data: res } =
await ctx.searchAgent.api.app.bsky.unspecced.searchActorsSkeleton({
q: term,
Expand Down
4 changes: 4 additions & 0 deletions packages/bsky/src/api/app/bsky/feed/getActorFeeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { HydrationState, Hydrator } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { DataPlaneClient } from '../../../../data-plane'
import { parseString } from '../../../../hydration/util'
import { clearlyBadCursor } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getActorFeeds = createPipeline(
Expand All @@ -34,6 +35,9 @@ const skeleton = async (inputs: {
params: Params
}): Promise<Skeleton> => {
const { ctx, params } = inputs
if (clearlyBadCursor(params.cursor)) {
return { feedUris: [] }
}
const [did] = await ctx.hydrator.actor.getDids([params.actor])
if (!did) {
throw new InvalidRequestError('Profile not found')
Expand Down
6 changes: 4 additions & 2 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 { setRepoRev } from '../../../util'
import { clearlyBadCursor, setRepoRev } from '../../../util'
import { createPipeline } from '../../../../pipeline'
import { HydrationState, Hydrator } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
Expand Down Expand Up @@ -45,7 +45,9 @@ const skeleton = async (inputs: {
}): Promise<Skeleton> => {
const { ctx, params } = inputs
const { actor, limit, cursor, viewer } = params

if (clearlyBadCursor(cursor)) {
return { items: [] }
}
const [actorDid] = await ctx.hydrator.actor.getDids([actor])
if (!actorDid || !viewer || viewer !== actorDid) {
throw new InvalidRequestError('Profile not found')
Expand Down
5 changes: 4 additions & 1 deletion 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 { setRepoRev } from '../../../util'
import { clearlyBadCursor, setRepoRev } from '../../../util'
import { createPipeline } from '../../../../pipeline'
import {
HydrationState,
Expand Down Expand Up @@ -65,6 +65,9 @@ export const skeleton = async (inputs: {
if (!actor) {
throw new InvalidRequestError('Profile not found')
}
if (clearlyBadCursor(params.cursor)) {
return { actor, items: [] }
}
const res = await ctx.dataplane.getAuthorFeed({
actorDid: did,
limit: params.limit,
Expand Down
1 change: 1 addition & 0 deletions packages/bsky/src/api/app/bsky/feed/getFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ 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, viewer, headers },
ctx,
Expand Down
4 changes: 4 additions & 0 deletions packages/bsky/src/api/app/bsky/feed/getLikes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { HydrationState, Hydrator } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { parseString } from '../../../../hydration/util'
import { creatorFromUri } from '../../../../views/util'
import { clearlyBadCursor } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getLikes = createPipeline(skeleton, hydration, noBlocks, presentation)
Expand All @@ -30,6 +31,9 @@ const skeleton = async (inputs: {
params: Params
}): Promise<Skeleton> => {
const { ctx, params } = inputs
if (clearlyBadCursor(params.cursor)) {
return { likes: [] }
}
const likesRes = await ctx.hydrator.dataplane.getLikesBySubject({
subject: { uri: params.uri, cid: params.cid },
cursor: params.cursor,
Expand Down
5 changes: 4 additions & 1 deletion 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 { setRepoRev } from '../../../util'
import { clearlyBadCursor, setRepoRev } from '../../../util'
import { createPipeline } from '../../../../pipeline'
import { HydrationState, Hydrator } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
Expand Down Expand Up @@ -42,6 +42,9 @@ export const skeleton = async (inputs: {
params: Params
}): Promise<Skeleton> => {
const { ctx, params } = inputs
if (clearlyBadCursor(params.cursor)) {
return { items: [] }
}
const res = await ctx.dataplane.getListFeed({
listUri: params.list,
limit: params.limit,
Expand Down
4 changes: 4 additions & 0 deletions packages/bsky/src/api/app/bsky/feed/getRepostedBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { HydrationState, Hydrator } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { parseString } from '../../../../hydration/util'
import { creatorFromUri } from '../../../../views/util'
import { clearlyBadCursor } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getRepostedBy = createPipeline(
Expand Down Expand Up @@ -34,6 +35,9 @@ const skeleton = async (inputs: {
params: Params
}): Promise<Skeleton> => {
const { ctx, params } = inputs
if (clearlyBadCursor(params.cursor)) {
return { reposts: [] }
}
const res = await ctx.hydrator.dataplane.getRepostsBySubject({
subject: { uri: params.uri, cid: params.cid },
cursor: params.cursor,
Expand Down
1 change: 1 addition & 0 deletions packages/bsky/src/api/app/bsky/feed/getSuggestedFeeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default function (server: Server, ctx: AppContext) {
handler: async ({ auth, params }) => {
const viewer = auth.credentials.iss

// @NOTE no need to coordinate the cursor for appview swap, as v1 doesn't use the cursor
const suggestedRes = await ctx.dataplane.getSuggestedFeeds({
actorDid: viewer ?? undefined,
limit: params.limit,
Expand Down
5 changes: 4 additions & 1 deletion packages/bsky/src/api/app/bsky/feed/getTimeline.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Server } from '../../../../lexicon'
import AppContext from '../../../../context'
import { QueryParams } from '../../../../lexicon/types/app/bsky/feed/getTimeline'
import { setRepoRev } from '../../../util'
import { clearlyBadCursor, setRepoRev } from '../../../util'
import { createPipeline } from '../../../../pipeline'
import { HydrationState, Hydrator } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
Expand Down Expand Up @@ -42,6 +42,9 @@ export const skeleton = async (inputs: {
params: Params
}): Promise<Skeleton> => {
const { ctx, params } = inputs
if (clearlyBadCursor(params.cursor)) {
return { items: [] }
}
const res = await ctx.dataplane.getTimeline({
actorDid: params.viewer,
limit: params.limit,
Expand Down
1 change: 1 addition & 0 deletions packages/bsky/src/api/app/bsky/feed/searchPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const skeleton = async (inputs: SkeletonFnInput<Context, Params>) => {
const { ctx, params } = inputs

if (ctx.searchAgent) {
// @NOTE cursors wont change on appview swap
const { data: res } =
await ctx.searchAgent.api.app.bsky.unspecced.searchPostsSkeleton({
q: params.q,
Expand Down
4 changes: 4 additions & 0 deletions packages/bsky/src/api/app/bsky/graph/getBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '../../../../pipeline'
import { Hydrator } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { clearlyBadCursor } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getBlocks = createPipeline(skeleton, hydration, noRules, presentation)
Expand All @@ -29,6 +30,9 @@ export default function (server: Server, ctx: AppContext) {

const skeleton = async (input: SkeletonFnInput<Context, Params>) => {
const { params, ctx } = input
if (clearlyBadCursor(params.cursor)) {
return { blockedDids: [] }
}
const { blockUris, cursor } = await ctx.hydrator.dataplane.getBlocks({
actorDid: params.viewer,
cursor: params.cursor,
Expand Down
4 changes: 4 additions & 0 deletions packages/bsky/src/api/app/bsky/graph/getFollowers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { didFromUri } from '../../../../hydration/util'
import { Hydrator, mergeStates } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { clearlyBadCursor } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getFollowers = createPipeline(
Expand Down Expand Up @@ -45,6 +46,9 @@ const skeleton = async (input: SkeletonFnInput<Context, Params>) => {
if (!subjectDid) {
throw new InvalidRequestError(`Actor not found: ${params.actor}`)
}
if (clearlyBadCursor(params.cursor)) {
return { subjectDid, followUris: [] }
}
const { followers, cursor } = await ctx.hydrator.graph.getActorFollowers({
did: subjectDid,
cursor: params.cursor,
Expand Down
4 changes: 4 additions & 0 deletions packages/bsky/src/api/app/bsky/graph/getFollows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '../../../../pipeline'
import { Hydrator, mergeStates } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { clearlyBadCursor } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getFollows = createPipeline(skeleton, hydration, noBlocks, presentation)
Expand Down Expand Up @@ -40,6 +41,9 @@ const skeleton = async (input: SkeletonFnInput<Context, Params>) => {
if (!subjectDid) {
throw new InvalidRequestError(`Actor not found: ${params.actor}`)
}
if (clearlyBadCursor(params.cursor)) {
return { subjectDid, followUris: [] }
}
const { follows, cursor } = await ctx.hydrator.graph.getActorFollows({
did: subjectDid,
cursor: params.cursor,
Expand Down
4 changes: 4 additions & 0 deletions packages/bsky/src/api/app/bsky/graph/getList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '../../../../pipeline'
import { Hydrator, mergeStates } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { clearlyBadCursor } from '../../../util'
import { ListItemInfo } from '../../../../proto/bsky_pb'

export default function (server: Server, ctx: AppContext) {
Expand All @@ -33,6 +34,9 @@ const skeleton = async (
input: SkeletonFnInput<Context, Params>,
): Promise<SkeletonState> => {
const { ctx, params } = input
if (clearlyBadCursor(params.cursor)) {
return { listUri: params.list, listitems: [] }
}
const { listitems, cursor } = await ctx.hydrator.dataplane.getListMembers({
listUri: params.list,
limit: params.limit,
Expand Down
4 changes: 4 additions & 0 deletions packages/bsky/src/api/app/bsky/graph/getListBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '../../../../pipeline'
import { Hydrator } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { clearlyBadCursor } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getListBlocks = createPipeline(
Expand All @@ -36,6 +37,9 @@ const skeleton = async (
input: SkeletonFnInput<Context, Params>,
): Promise<SkeletonState> => {
const { ctx, params } = input
if (clearlyBadCursor(params.cursor)) {
return { listUris: [] }
}
const { listUris, cursor } =
await ctx.hydrator.dataplane.getBlocklistSubscriptions({
actorDid: params.viewer,
Expand Down
4 changes: 4 additions & 0 deletions packages/bsky/src/api/app/bsky/graph/getListMutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '../../../../pipeline'
import { Hydrator } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { clearlyBadCursor } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getListMutes = createPipeline(
Expand All @@ -36,6 +37,9 @@ const skeleton = async (
input: SkeletonFnInput<Context, Params>,
): Promise<SkeletonState> => {
const { ctx, params } = input
if (clearlyBadCursor(params.cursor)) {
return { listUris: [] }
}
const { listUris, cursor } =
await ctx.hydrator.dataplane.getMutelistSubscriptions({
actorDid: params.viewer,
Expand Down
4 changes: 4 additions & 0 deletions packages/bsky/src/api/app/bsky/graph/getLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '../../../../pipeline'
import { Hydrator } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { clearlyBadCursor } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getLists = createPipeline(skeleton, hydration, noRules, presentation)
Expand All @@ -32,6 +33,9 @@ const skeleton = async (
input: SkeletonFnInput<Context, Params>,
): Promise<SkeletonState> => {
const { ctx, params } = input
if (clearlyBadCursor(params.cursor)) {
return { listUris: [] }
}
const { listUris, cursor } = await ctx.hydrator.dataplane.getActorLists({
actorDid: params.actor,
cursor: params.cursor,
Expand Down
4 changes: 4 additions & 0 deletions packages/bsky/src/api/app/bsky/graph/getMutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
createPipeline,
noRules,
} from '../../../../pipeline'
import { clearlyBadCursor } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const getMutes = createPipeline(skeleton, hydration, noRules, presentation)
Expand All @@ -29,6 +30,9 @@ export default function (server: Server, ctx: AppContext) {

const skeleton = async (input: SkeletonFnInput<Context, Params>) => {
const { params, ctx } = input
if (clearlyBadCursor(params.cursor)) {
return { mutedDids: [] }
}
const { dids, cursor } = await ctx.hydrator.dataplane.getMutes({
actorDid: params.viewer,
cursor: params.cursor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Hydrator } from '../../../../hydration/hydrator'
import { Views } from '../../../../views'
import { Notification } from '../../../../proto/bsky_pb'
import { didFromUri } from '../../../../hydration/util'
import { clearlyBadCursor } from '../../../util'

export default function (server: Server, ctx: AppContext) {
const listNotifications = createPipeline(
Expand Down Expand Up @@ -42,6 +43,9 @@ const skeleton = async (
if (params.seenAt) {
throw new InvalidRequestError('The seenAt parameter is unsupported')
}
if (clearlyBadCursor(params.cursor)) {
return { notifs: [] }
}
const [res, lastSeenRes] = await Promise.all([
ctx.hydrator.dataplane.getNotifications({
actorDid: params.viewer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { mapDefined } from '@atproto/common'
import { Server } from '../../../../lexicon'
import AppContext from '../../../../context'
import { parseString } from '../../../../hydration/util'
import { clearlyBadCursor } from '../../../util'

// THIS IS A TEMPORARY UNSPECCED ROUTE
// @TODO currently mirrors getSuggestedFeeds and ignores the "query" param.
Expand All @@ -12,6 +13,13 @@ export default function (server: Server, ctx: AppContext) {
handler: async ({ auth, params }) => {
const viewer = auth.credentials.iss

if (clearlyBadCursor(params.cursor)) {
return {
encoding: 'application/json',
body: { feeds: [] },
}
}

const suggestedRes = await ctx.dataplane.getSuggestedFeeds({
actorDid: viewer ?? undefined,
limit: params.limit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default function (server: Server, ctx: AppContext) {
auth: ctx.authVerifier.standard,
handler: async ({ auth, params }) => {
const viewer = auth.credentials.iss
// @NOTE bad cursor during appview swap handled within skeleton()
const result = await skeleton({ ctx, params: { ...params, viewer } })
const feed = result.items.map((item) => {
return toSkeletonItem({
Expand Down
5 changes: 5 additions & 0 deletions packages/bsky/src/api/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ export const setRepoRev = (res: express.Response, rev: string | null) => {
res.setHeader('Atproto-Repo-Rev', rev)
}
}

export const clearlyBadCursor = (cursor?: string) => {
// hallmark of v1 cursor, highly unlikely in v2 cursors based on time or rkeys
return !!cursor?.includes('::')
}
6 changes: 3 additions & 3 deletions packages/bsky/src/data-plane/server/db/pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type LabeledResult = {
* - LabeledResult: a Result processed such that the "primary" and "secondary" parts of the cursor are labeled.
* - E.g. { primary: '2022-01-01T12:00:00Z', secondary: 'bafyx' }
* - Cursor: the two string parts that make-up the packed/string cursor.
* - E.g. packed cursor '1641038400000::bafyx' in parts { primary: '1641038400000', secondary: 'bafyx' }
* - E.g. packed cursor '1641038400000__bafyx' in parts { primary: '1641038400000', secondary: 'bafyx' }
*
* These types relate as such. Implementers define the relations marked with a *:
* Result -*-> LabeledResult <-*-> Cursor <--> packed/string cursor
Expand Down Expand Up @@ -44,11 +44,11 @@ export abstract class GenericKeyset<R, LR extends LabeledResult> {
}
packCursor(cursor?: Cursor): string | undefined {
if (!cursor) return
return `${cursor.primary}::${cursor.secondary}`
return `${cursor.primary}__${cursor.secondary}`
}
unpackCursor(cursorStr?: string): Cursor | undefined {
if (!cursorStr) return
const result = cursorStr.split('::')
const result = cursorStr.split('__')
const [primary, secondary, ...others] = result
if (!primary || !secondary || others.length > 0) {
throw new InvalidRequestError('Malformed cursor')
Expand Down
Loading
Loading