import { fetchTwitterUser } from "https://esm.town/v/andreterron/fetchTwitterUser";
export async function sortedPosts({ accessToken, posts }: {
accessToken: string;
posts: {
id: string;
text: string;
author_id: string;
}[];
}) {
const q = new URLSearchParams({
"user.fields": "public_metrics",
}).toString();
const detailed = (await Promise.all(
posts.slice(0, 4).map(async (post) => {
try {
const author = await fetchTwitterUser({
id: post.author_id,
accessToken,
query: q,
});
return {
...post,
author,
};
}
catch (e) {
console.log("Failed to fetch user", post.author_id, q);
console.error(e);
return undefined;
}
}),
)).filter((a) => !!a);
detailed.sort((a, b) =>
b.author.public_metrics.followers_count -
a.author.public_metrics.followers_count
);
return detailed;
}