1- import type { AtprotoProfile } from '~~/server/api/atproto/pds-users.get'
1+ import type { AtprotoProfile } from '#shared/types/atproto'
2+
3+ import {
4+ ONE_THOUSAND_NPMX_USER_ACCOUNTS_XRPC ,
5+ BSKY_APP_VIEW_USER_PROFILES_XRPC ,
6+ ERROR_PDS_FETCH_FAILED ,
7+ } from '#shared/utils/constants'
28
39interface GraphLink {
410 source : string
511 target : string
612}
713
14+ const USER_BATCH_AMOUNT = 25
15+
816export default defineCachedEventHandler (
917 async ( ) : Promise < { nodes : AtprotoProfile [ ] ; links : GraphLink [ ] } > => {
10- const response = await fetch ( 'https://npmx.social/xrpc/com.atproto.sync.listRepos?limit=1000' )
18+ const response = await fetch ( ONE_THOUSAND_NPMX_USER_ACCOUNTS_XRPC )
1119
1220 if ( ! response . ok ) {
1321 throw createError ( {
1422 statusCode : response . status ,
15- message : 'Failed to fetch PDS repos' ,
23+ message : ERROR_PDS_FETCH_FAILED ,
1624 } )
1725 }
1826
1927 const listRepos = ( await response . json ( ) ) as { repos : { did : string } [ ] }
2028 const dids = listRepos . repos . map ( repo => repo . did )
2129 const localDids = new Set ( dids )
2230
23- const getProfilesUrl = 'https://public.api.bsky.app/xrpc/app.bsky.actor.getProfiles'
2431 const nodes : AtprotoProfile [ ] = [ ]
2532 const links : GraphLink [ ] = [ ]
2633
27- for ( let i = 0 ; i < dids . length ; i += 25 ) {
28- const batch = dids . slice ( i , i + 25 )
34+ for ( let i = 0 ; i < dids . length ; i += USER_BATCH_AMOUNT ) {
35+ const batch = dids . slice ( i , i + USER_BATCH_AMOUNT )
2936
30- const params = new URLSearchParams ( )
37+ const url = new URL ( BSKY_APP_VIEW_USER_PROFILES_XRPC )
3138 for ( const did of batch ) {
32- params . append ( 'actors' , did )
39+ url . searchParams . append ( 'actors' , did )
3340 }
3441
3542 try {
36- const profilesResponse = await fetch ( ` ${ getProfilesUrl } ? ${ params . toString ( ) } ` )
43+ const profilesResponse = await fetch ( url . toString ( ) )
3744
3845 if ( ! profilesResponse . ok ) {
3946 console . warn ( `Failed to fetch atproto profiles: ${ profilesResponse . status } ` )
@@ -56,7 +63,7 @@ export default defineCachedEventHandler(
5663 )
5764
5865 if ( ! followResponse . ok ) {
59- console . warn ( `Failed to fetch atproto profiles : ${ followResponse . status } ` )
66+ console . warn ( `Failed to fetch follows : ${ followResponse . status } ` )
6067 continue
6168 }
6269
@@ -73,4 +80,9 @@ export default defineCachedEventHandler(
7380 links,
7481 }
7582 } ,
83+ {
84+ maxAge : 3600 ,
85+ name : 'pds-graphs' ,
86+ getKey : ( ) => 'pds-graphs' ,
87+ } ,
7688)
0 commit comments