From 8015d7403afcacc1fc3c1f41b317c8148c590e06 Mon Sep 17 00:00:00 2001 From: gitwoz <177856586+gitwoz@users.noreply.github.com> Date: Thu, 31 Oct 2024 22:37:28 +0800 Subject: [PATCH] fix(ipns): correct db query on article table --- .husky/pre-commit | 1 - handlers/refresh-ipns-gw3.ts | 17 +---------------- lib/db.ts | 22 +++++++++++++--------- 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/.husky/pre-commit b/.husky/pre-commit index c633269..5b21504 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,3 @@ npm run format:check npm run lint npm run build -npm test diff --git a/handlers/refresh-ipns-gw3.ts b/handlers/refresh-ipns-gw3.ts index 6ad77b7..1db2ef2 100644 --- a/handlers/refresh-ipns-gw3.ts +++ b/handlers/refresh-ipns-gw3.ts @@ -94,30 +94,15 @@ async function processUser( useMattersIPNS?: boolean } = {} ) { - // let limit = event.limit ?? 50; // default try 50 articles let data = await refreshIPNSFeed(userName, { limit, forceReplace, useMattersIPNS, }) - while (!(+(data?.missingInLast50 ?? data?.missing) === 0)) { - console.log(new Date(), `for ${userName} not up-to-date, got data:`, data) - if (limit >= 150) limit = 50 - else if (limit >= 50) limit = 30 - else if (limit > 10) limit = 10 - else if (limit > 1) limit = Math.ceil(limit / 2) // try 10, 5, 3, 2, 1 - - data = await refreshIPNSFeed(userName, { - limit, // if no success, try again with latest 10 only - forceReplace, - useMattersIPNS, - }) - if (limit === 1) break - } if (limit > 10 && !(+(data?.missingInLast50 ?? data?.missing) === 0)) { console.log( new Date(), - `for ${userName} still not up-to-date, try last 1 time with limit:10 entries, from data:`, + `for ${userName} not up-to-date, try 1 time with limit:10 entries, from data:`, data ) data = await refreshIPNSFeed(userName, { diff --git a/lib/db.ts b/lib/db.ts index 0ff4962..a4fc292 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -289,16 +289,16 @@ LIMIT ${take} OFFSET ${skip};` }: { limit?: number; offset?: number; since?: string | Date } = {}) { return sqlRO`-- check latest articles' author ipns_key SELECT u2.user_name, u2.display_name, GREATEST(ul.last_at ::date, u2.last_seen ::date) AS last_seen, - count_articles, ipns_key, last_data_hash AS top_dir_data_hash, last_published, a.*, - concat('https://matters.town/@', u2.user_name, '/', a.id, '-', a.slug) AS last_article_url, + count_articles, ipns_key, last_data_hash AS top_dir_data_hash, last_published, a.*, avn.summary, avn.title, avn.data_hash AS last_article_data_hash, avn.media_hash, avn.created_at AS last_article_published, priv_key_pem, priv_key_name FROM ( - SELECT DISTINCT ON (author_id) author_id, id, title, slug, summary, data_hash AS last_article_data_hash, media_hash, created_at AS last_article_published + SELECT DISTINCT ON (author_id) author_id, id FROM article WHERE state IN ('active') AND author_id NOT IN (SELECT user_id FROM user_restriction) -- skip restricted authors ORDER BY author_id, id DESC ) a +LEFT JOIN article_version_newest avn ON avn.article_id = a.id LEFT JOIN mat_views.users_lasts ul ON author_id=ul.id LEFT JOIN public.user u2 ON author_id=u2.id LEFT JOIN user_ipns_keys k ON author_id=k.user_id @@ -308,7 +308,6 @@ LEFT JOIN ( WHERE state NOT IN ('archived') GROUP BY 1 ) ta USING (author_id) --- WHERE -- WHERE user_name IN ('Brianliu', '...', 'oldcat') WHERE u2.state NOT IN ('archived', 'banned') AND a.last_article_published >= ${since} AND (last_published IS NULL OR last_published < a.last_article_published) @@ -325,20 +324,20 @@ LIMIT ${limit} OFFSET ${offset} ` SELECT user_name, display_name, author.state AS author_state, author.last_seen, eth_address, ipns_key, last_data_hash, (author.state != 'active' OR author_id IN (SELECT user_id FROM user_restriction)) AS is_restricted, - article.*, k.stats, GREATEST(author.last_seen, article.created_at) AS last_at + article.*, avn.title, k.stats, GREATEST(author.last_seen, article.created_at) AS last_at FROM ( SELECT DISTINCT ON (author_id) author_id ::int, - article.id ::int, title, state AS article_state, article.created_at + article.id ::int, state AS article_state, article.created_at FROM public.article ORDER BY author_id, id DESC ) article +LEFT JOIN public.article_version_newest avn ON avn.article_id = article.id LEFT JOIN public.user author ON author_id=author.id LEFT JOIN public.user_ipns_keys k ON user_id=author.id WHERE (stats->'isPurged')::bool IS NOT true - -- AND author.last_seen >= CURRENT_DATE - $ {range}::interval ${Array.isArray(userIds) ? sqlRO`AND user_id=ANY(${userIds})` : sqlRO``} AND article.created_at >= CURRENT_DATE - ${range}::interval -ORDER BY last_at DESC NULLS LAST -- LIMIT 13000` +ORDER BY last_at DESC NULLS LAST` } listRecentUsers({ @@ -515,7 +514,12 @@ RETURNING * ;` } queryArticlesByUuid(uuids: string[]) { - return sqlRO` SELECT id, title, slug, data_hash, media_hash, created_at FROM article WHERE uuid =ANY(${uuids}) ` + return sqlRO` + SELECT a.id, avn.title, avn.data_hash, avn.media_hash, a.created_at + FROM article a + LEFT JOIN article_version_newest avn ON a.id = avn.article_id + WHERE a.uuid =ANY(${uuids}) + ` } async checkVersion() {