From 010eb3f291067ea0ac611d61140bab93620376fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Sat, 26 Oct 2024 12:04:06 +0200 Subject: [PATCH] Refactor article fetching * Apply different limits to profile and feed * Ensure the limit is the returned amount * Pre-sort articles --- handlers/user-atom-feed.ts | 4 +--- handlers/user-profile.ts | 4 +--- html.ts | 3 +-- nostr.ts | 13 ++++++++++--- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/handlers/user-atom-feed.ts b/handlers/user-atom-feed.ts index a78553e..95fb3b6 100644 --- a/handlers/user-atom-feed.ts +++ b/handlers/user-atom-feed.ts @@ -2,7 +2,6 @@ import { Context } from "@oak/oak"; import { lookupPubkeyByUsername } from "../directory.ts"; import { fetchArticlesByAuthor, fetchProfileEvent } from "../nostr.ts"; import { profileAtomFeed } from "../feeds.ts"; -import Article from "../models/article.ts"; import Profile from "../models/profile.ts"; import { notFoundHandler } from "../handlers/errors.ts"; @@ -21,8 +20,7 @@ const userAtomFeedHandler = async function (ctx: Context) { const profile = new Profile(profileEvent, username); if (profile.nip05) { - const articleEvents = await fetchArticlesByAuthor(pubkey); - const articles = articleEvents.map((a) => new Article(a)); + const articles = await fetchArticlesByAuthor(pubkey, 10); const atom = await profileAtomFeed(profile, articles); ctx.response.headers.set("Content-Type", "application/atom+xml"); diff --git a/handlers/user-profile.ts b/handlers/user-profile.ts index e561ee6..5721f07 100644 --- a/handlers/user-profile.ts +++ b/handlers/user-profile.ts @@ -1,7 +1,6 @@ import { Context } from "@oak/oak"; import { lookupPubkeyByUsername } from "../directory.ts"; import { fetchArticlesByAuthor, fetchProfileEvent } from "../nostr.ts"; -import Article from "../models/article.ts"; import Profile from "../models/profile.ts"; import { profilePageHtml } from "../html.ts"; import { notFoundHandler } from "../handlers/errors.ts"; @@ -20,8 +19,7 @@ const userProfileHandler = async function (ctx: Context) { if (profileEvent) { const profile = new Profile(profileEvent, username); - const articleEvents = await fetchArticlesByAuthor(pubkey); - const articles = articleEvents.map((a) => new Article(a)); + const articles = await fetchArticlesByAuthor(pubkey, 210); const html = await profilePageHtml(profile, articles); generateOgProfileImage(profile); diff --git a/html.ts b/html.ts index 9f42b31..39602e2 100644 --- a/html.ts +++ b/html.ts @@ -92,10 +92,9 @@ function articleListItemHtml(article: Article): string { export function articleListHtml(articles: Article[]): string { if (articles.length === 0) return ""; - const sortedArticles = articles.sort((a, b) => b.publishedAt - a.publishedAt); let html = ""; - for (const article of sortedArticles) { + for (const article of articles) { html += articleListItemHtml(article); } diff --git a/nostr.ts b/nostr.ts index 1995fe0..7d1884c 100644 --- a/nostr.ts +++ b/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) {