Refactor article fetching

* Apply different limits to profile and feed
* Ensure the limit is the returned amount
* Pre-sort articles
This commit is contained in:
2024-10-26 12:04:06 +02:00
parent 0b1eca87b2
commit 010eb3f291
4 changed files with 13 additions and 11 deletions

View File

@@ -2,6 +2,7 @@ import { NostrEvent, NostrFilter, NPool, NRelay1 } from "@nostrify/nostrify";
import { nip19 } from "@nostr/tools";
import { lookupUsernameByPubkey } from "./directory.ts";
import config from "./config.ts";
import Article from "./models/article.ts";
const relayPool = new NPool({
open: (url) => new NRelay1(url),
@@ -49,14 +50,20 @@ export async function fetchReplaceableEvent(
}
}
export async function fetchArticlesByAuthor(pubkey: string) {
export async function fetchArticlesByAuthor(
pubkey: string,
limit: number = 10,
) {
const events = await fetchWithTimeout([{
authors: [pubkey],
kinds: [30023],
limit: 210,
limit: limit,
}]) as NostrEvent[];
return events;
const articles = events.map((a) => new Article(a));
const sortedArticles = articles.sort((a, b) => b.publishedAt - a.publishedAt);
// The limit seems to apply per relay, not per pool query
return sortedArticles.slice(0, limit);
}
export async function fetchProfileEvent(pubkey: string) {