[Enhancement] Limit Comment Retrieval When Building Index
Created by: MarkMoretto
I noticed in build-index.ts, the getLastActivity() function calls the /posts endpoint and subsequently takes the first value via array indexing.
async function getLastActivity(c: Community) {
type Response = {
posts: Post[];
};
const posts = (await fetch(
`https://discuit.net/api/posts?communityId=${c.id}&sort=activity`,
).then((r) => r.json())) as Response;
if (posts.posts) {
c.lastActivityAt = posts.posts[0]?.lastActivityAt || "";
} else {
c.lastActivityAt = c.createdAt;
}
return c
}
What about adding the query parameter limit
with a value of one and just taking that result? would it improve performance any?
suggested update:
const getLastActivity = async(c: Community): Promise<Community> => {
// Create URL with' limit=1' key-value pair in query string.
const url = `https://discuit.net/api/posts?communityId=${c.id}&sort=activity&limit=1`
const post: Post = await fetch(url).then((r) => r.json())
// if post is not null or undefined, update Comment 'lastActivityAt' value.
// Otherwise, use 'createdAt'
c.lastActivityAt = post.lastActivityAt ?? c.createdAt
return c
}