Skip to content

Commit

Permalink
Appview: handle out-of-date cursor with feed.getLikes (#2474)
Browse files Browse the repository at this point in the history
* appview: temporarily revert move to GetLikesBySubjectSorted

* appview: provide error when using old cursor value on getLikes
  • Loading branch information
devinivy authored May 9, 2024
1 parent 499fabe commit 7cdcd58
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packages/bsky/src/api/app/bsky/feed/getLikes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Views } from '../../../../views'
import { parseString } from '../../../../hydration/util'
import { creatorFromUri } from '../../../../views/util'
import { clearlyBadCursor, resHeaders } from '../../../util'
import { InvalidRequestError } from '@atproto/xrpc-server'

export default function (server: Server, ctx: AppContext) {
const getLikes = createPipeline(skeleton, hydration, noBlocks, presentation)
Expand Down Expand Up @@ -41,6 +42,11 @@ const skeleton = async (inputs: {
if (clearlyBadCursor(params.cursor)) {
return { likes: [] }
}
if (looksLikeNonSortedCursor(params.cursor)) {
throw new InvalidRequestError(
'Cursor appear to be out of date, please try reloading.',
)
}
const likesRes = await ctx.hydrator.dataplane.getLikesBySubjectSorted({
subject: { uri: params.uri, cid: params.cid },
cursor: params.cursor,
Expand Down Expand Up @@ -116,3 +122,9 @@ type Skeleton = {
likes: string[]
cursor?: string
}

const looksLikeNonSortedCursor = (cursor: string | undefined) => {
// the old cursor values used with getLikesBySubject() were dids.
// we now use getLikesBySubjectSorted(), whose cursors look like timestamps.
return cursor?.startsWith('did:')
}
9 changes: 6 additions & 3 deletions packages/bsky/src/data-plane/server/routes/likes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import assert from 'node:assert'
import { keyBy } from '@atproto/common'
import { ServiceImpl } from '@connectrpc/connect'
import { Service } from '../../../proto/bsky_connect'
import { Database } from '../db'
import { TimeCidKeyset, paginate } from '../db/pagination'
import { keyBy } from '@atproto/common'

export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
async getLikesBySubjectSorted(req) {
Expand Down Expand Up @@ -34,8 +35,10 @@ export default (db: Database): Partial<ServiceImpl<typeof Service>> => ({
}
},

async getLikesBySubject(_req) {
throw new Error('deprecated in favor of getLikesBySubjectSorted')
// @NOTE deprecated in favor of getLikesBySubjectSorted
async getLikesBySubject(req, context) {
assert(this.getLikesBySubjectSorted)
return this.getLikesBySubjectSorted(req, context)
},

async getLikesByActorAndSubjects(req) {
Expand Down

0 comments on commit 7cdcd58

Please sign in to comment.