+44
-2
netlify/functions/batch-search-actors.ts
+44
-2
netlify/functions/batch-search-actors.ts
···
119
119
return {
120
120
...actor,
121
121
matchScore: score,
122
-
postCount: actor.postCount || 0,
123
-
followerCount: actor.followerCount || 0
122
+
did: actor.did
124
123
};
125
124
})
126
125
.filter((actor: any) => actor.matchScore > 0)
···
142
141
});
143
142
144
143
const results = await Promise.all(searchPromises);
144
+
145
+
// Enrich results with follower and post counts using getProfiles
146
+
const allDids = results
147
+
.flatMap(r => r.actors.map((a: any) => a.did))
148
+
.filter((did): did is string => !!did);
149
+
150
+
if (allDids.length > 0) {
151
+
// Create a map to store enriched profile data
152
+
const profileDataMap = new Map<string, { postCount: number; followerCount: number }>();
153
+
154
+
// Batch fetch profiles (25 at a time - API limit)
155
+
const PROFILE_BATCH_SIZE = 25;
156
+
for (let i = 0; i < allDids.length; i += PROFILE_BATCH_SIZE) {
157
+
const batch = allDids.slice(i, i + PROFILE_BATCH_SIZE);
158
+
try {
159
+
const profilesResponse = await agent.app.bsky.actor.getProfiles({
160
+
actors: batch
161
+
});
162
+
163
+
profilesResponse.data.profiles.forEach((profile: any) => {
164
+
profileDataMap.set(profile.did, {
165
+
postCount: profile.postsCount || 0,
166
+
followerCount: profile.followersCount || 0
167
+
});
168
+
});
169
+
} catch (error) {
170
+
console.error('Failed to fetch profile batch:', error);
171
+
// Continue even if one batch fails
172
+
}
173
+
}
174
+
175
+
// Merge enriched data back into results
176
+
results.forEach(result => {
177
+
result.actors = result.actors.map((actor: any) => {
178
+
const enrichedData = profileDataMap.get(actor.did);
179
+
return {
180
+
...actor,
181
+
postCount: enrichedData?.postCount || 0,
182
+
followerCount: enrichedData?.followerCount || 0
183
+
};
184
+
});
185
+
});
186
+
}
145
187
146
188
return {
147
189
statusCode: 200,