A frontend for your PDS
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: add batching configuration for posts and accounts fetching

tophhie.cloud 146a3f50 426ad323

verified
+47 -6
+14
config.ts
··· 30 30 static readonly MAX_POSTS: number = 100; 31 31 32 32 /** 33 + * Number of posts to request per-user per fetch. 34 + * Keeps individual requests small to improve latency. 35 + * @default 5 36 + */ 37 + static readonly POSTS_BATCH_SIZE: number = 5; 38 + 39 + /** 40 + * Number of accounts to fetch per getNextPosts invocation. 41 + * This spaces work across multiple infinite-scroll calls. 42 + * @default 10 43 + */ 44 + static readonly ACCOUNTS_PER_BATCH: number = 10; 45 + 46 + /** 33 47 * Footer text for the dashboard, you probably want to change this. Supports HTML. 34 48 * @default "<a href='https://git.witchcraft.systems/scientific-witchery/pds-dash' target='_blank'>Source</a> (<a href='https://github.com/witchcraft-systems/pds-dash/' target='_blank'>github mirror</a>)" 35 49 */
+33 -6
src/lib/pdsfetch.ts
··· 28 28 } 29 29 30 30 let accountsMetadata: AccountMetadata[] = []; 31 + let accountFetchIndex = 0; 32 + let initialFetchDone = false; 31 33 32 34 interface atUriObject { 33 35 repo: string; ··· 316 318 if (!accountsMetadata.length) { 317 319 accountsMetadata = await getAllMetadataFromPds(); 318 320 } 321 + if (!accountsMetadata.length) { 322 + return []; 323 + } 324 + // Fetch posts for a subset of accounts (batch) to reduce per-call load 325 + const accountsPerBatch = Config.ACCOUNTS_PER_BATCH || 10; 326 + let accountsToFetch: AccountMetadata[]; 319 327 320 - // Fetch posts for all accounts 328 + // On the first fetch, query all accounts but with a small per-account limit 329 + if (!initialFetchDone) { 330 + accountsToFetch = accountsMetadata.slice(); 331 + initialFetchDone = true; 332 + } else { 333 + const start = accountFetchIndex; 334 + const end = start + accountsPerBatch; 335 + if (end <= accountsMetadata.length) { 336 + accountsToFetch = accountsMetadata.slice(start, end); 337 + } else { 338 + accountsToFetch = accountsMetadata.slice(start).concat( 339 + accountsMetadata.slice(0, end % accountsMetadata.length), 340 + ); 341 + } 342 + 343 + // Advance the rotating index for the next invocation 344 + accountFetchIndex = (accountFetchIndex + accountsToFetch.length) % accountsMetadata.length; 345 + } 346 + 321 347 const postsAcc: PostsAcc[] = await Promise.all( 322 - accountsMetadata.map(async (account) => { 348 + accountsToFetch.map(async (account) => { 323 349 const result = await fetchPostsForUser( 324 350 account.did, 325 - account.currentCursor || null 351 + account.currentCursor || null, 352 + Config.POSTS_BATCH_SIZE, 326 353 ); 327 354 328 355 const records = result?.records ?? []; ··· 336 363 posts: records, 337 364 account, 338 365 }; 339 - }) 366 + }), 340 367 ); 341 368 342 369 // Flatten posts ··· 388 415 } 389 416 }; 390 417 391 - const fetchPostsForUser = async (did: At.Did, cursor: string | null) => { 418 + const fetchPostsForUser = async (did: At.Did, cursor: string | null, limit: number = Config.POSTS_BATCH_SIZE) => { 392 419 try { 393 420 const { data } = await rpc.get("com.atproto.repo.listRecords", { 394 421 params: { 395 422 repo: did as At.Identifier, 396 423 collection: "app.bsky.feed.post", 397 - limit: Config.MAX_POSTS, 424 + limit: limit, 398 425 cursor: cursor || undefined, 399 426 }, 400 427 });