Skip to content

Commit

Permalink
fix(ipns): correct db query on article table
Browse files Browse the repository at this point in the history
  • Loading branch information
gitwoz committed Oct 31, 2024
1 parent 513d2b5 commit 8015d74
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 26 deletions.
1 change: 0 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
npm run format:check
npm run lint
npm run build
npm test
17 changes: 1 addition & 16 deletions handlers/refresh-ipns-gw3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
22 changes: 13 additions & 9 deletions lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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({
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 8015d74

Please sign in to comment.