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:
13
nostr.ts
13
nostr.ts
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user