The recipes.blue monorepo recipes.blue
recipes appview atproto

feat: improve logic for getrecipes

hayden.moe aaa11a23 1fda649a

verified
Changed files
+21 -7
apps
+21 -7
apps/api/src/xrpc/blue.recipes.feed.getRecipes.ts
··· 1 - import { db, eq } from '@cookware/database'; 1 + import { db, and, eq } from '@cookware/database'; 2 2 import { recipeTable } from '@cookware/database/schema'; 3 3 import { BlueRecipesActorDefs, BlueRecipesFeedGetRecipes, BlueRecipesFeedRecipe } from '@cookware/lexicons'; 4 4 import { json, XRPCRouter } from '@atcute/xrpc-server'; 5 5 import { parseDid } from '../util/api.js'; 6 - import { AtprotoDid, ResourceUri } from '@atcute/lexicons/syntax'; 6 + import { ResourceUri } from '@atcute/lexicons/syntax'; 7 7 import { Logger } from 'pino'; 8 8 import { isLegacyBlob } from '@atcute/lexicons/interfaces'; 9 9 10 10 export const registerGetRecipes = (router: XRPCRouter, _logger: Logger) => { 11 11 router.addQuery(BlueRecipesFeedGetRecipes.mainSchema, { 12 - async handler({ params: { author, limit } }) { 13 - let foundDid: AtprotoDid | null = null; 14 - if (author) foundDid = await parseDid(author); 12 + async handler({ params: { author, limit, cursor } }) { 13 + const whereClauses = []; 14 + if (author) { 15 + const did = await parseDid(author); 16 + whereClauses.push(eq(recipeTable.did, did)); 17 + } 18 + 19 + if (cursor) { 20 + const { c } = JSON.parse(atob(cursor)) as { c: string }; 21 + whereClauses.push(eq(recipeTable.createdAt, new Date(c))); 22 + } 15 23 16 24 const recipes = await db.query.recipeTable.findMany({ 17 25 orderBy: recipeTable.createdAt, 18 26 limit: limit, 19 - where: foundDid ? eq(recipeTable.did, foundDid) : undefined, 27 + where: whereClauses ? and(...whereClauses) : undefined, 20 28 }); 21 29 30 + let nextCursor = ''; 31 + if (recipes.length == limit) { 32 + const { createdAt } = recipes[limit - 1]!; 33 + nextCursor = atob(JSON.stringify({ c: createdAt })); 34 + } 35 + 22 36 return json({ 23 - nextCursor: '', 37 + nextCursor, 24 38 recipes: recipes.map((recipe) => ({ 25 39 author: { 26 40 $type: BlueRecipesActorDefs.profileViewBasicSchema.shape.$type.wrapped.expected,