@@ -5,36 +5,31 @@ import type { Services } from '#/service'
55import type { Environment } from '#/types'
66import type { IncludeValidator , LimitValidator } from '../validators'
77
8- /**
9- * TODO: add support for whether :addressOrENS is followed, is following, is muting, is blocking, is blocked by
10- */
118export function followers (
129 leaderboard : Hono < { Bindings : Environment } > ,
1310 services : Services ,
1411 limitValidator : LimitValidator ,
1512 includeValidator : IncludeValidator
1613) {
17- /**
18- * By default, only returns leaderboard with address and followers_count/following_count of each user.
19- * If include=ens, also returns ens profile of each user.
20- * If include=muted, also returns how many users each user has muted.
21- * If include=blocked, also returns how many users each user has blocked.
22- * If addressOrENS path param is provided AND include=mutuals query param is provided, returns mutuals between addressOrENS and each user.
23- */
24- leaderboard . get ( '/followers/:addressOrENS?' , limitValidator , includeValidator , async context => {
25- const { include, limit } = context . req . valid ( 'query' )
14+ leaderboard . get ( '/followers' , limitValidator , includeValidator , async context => {
15+ const { limit, offset, cache } = context . req . valid ( 'query' )
2616 const parsedLimit = Number . parseInt ( limit ?. toString ( ) || '10' , 10 )
27- let mostFollowers : { address : string ; followers_count : number } [ ] = await services
17+ const parsedOffset = Number . parseInt ( offset ?. toString ( ) || '0' , 10 )
18+
19+ const cacheService = services . cache ( env ( context ) )
20+ const cacheTarget = `leaderboard/followers?limit=${ parsedLimit } &offset=${ parsedOffset } `
21+ if ( cache !== 'fresh' ) {
22+ const cacheHit = await cacheService . get ( cacheTarget )
23+ if ( cacheHit ) {
24+ return context . json ( { ...cacheHit } , 200 )
25+ }
26+ }
27+ const mostFollowers : { address : string ; followers_count : number } [ ] = await services
2828 . efp ( env ( context ) )
2929 . getLeaderboardFollowers ( parsedLimit )
30- if ( include ?. includes ( 'ens' ) ) {
31- const ens = services . ens ( env ( context ) )
32- const ensProfiles = await Promise . all ( mostFollowers . map ( user => ens . getENSProfile ( user . address ) ) )
33- mostFollowers = mostFollowers . map ( ( user , index ) => ( {
34- ...user ,
35- ens : ensProfiles [ index ]
36- } ) )
37- }
38- return context . json ( mostFollowers , 200 )
30+
31+ const packagedResponse = mostFollowers
32+ await cacheService . put ( cacheTarget , JSON . stringify ( packagedResponse ) )
33+ return context . json ( packagedResponse , 200 )
3934 } )
4035}
0 commit comments