From 18fefb05bec5b7512bb884a4599450e704bd47af Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Sat, 15 Jun 2024 23:20:50 -0400 Subject: [PATCH 1/4] feat: call internal topics api instead of topics lib directly when creating a topic from feed --- lib/pull.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/pull.js b/lib/pull.js index 3ddb582..ff70ca0 100644 --- a/lib/pull.js +++ b/lib/pull.js @@ -3,8 +3,8 @@ const winston = require.main.require('winston'); const db = require.main.require('./src/database'); const meta = require.main.require('./src/meta'); -const topics = require.main.require('./src/topics'); const user = require.main.require('./src/user'); +const api = require.main.require('./src/api'); const feedAPI = require('./feed'); @@ -53,7 +53,7 @@ async function postEntry(feed, entry) { const isNew = await isEntryNew(feed, entry); if (!isNew) { - winston.info(`[plugin-rss] entry is not new, id: ${entry.id}, title: ${entry.title}, link: ${entry.link && entry.link.href}`); + winston.verbose(`[plugin-rss] entry is not new, id: ${entry.id}, title: ${entry.title}, link: ${entry.link && entry.link.href}`); return; } @@ -73,9 +73,9 @@ async function postEntry(feed, entry) { tags = tags.concat(entryTags); } - winston.info(`[plugin-rss] posting, ${feed.url} - title: ${entry.title}, published date: ${getEntryDate(entry)}`); + winston.verbose(`[plugin-rss] posting, ${feed.url} - title: ${entry.title}, published date: ${getEntryDate(entry)}`); - const result = await topics.post({ + const result = await api.topics.create({ uid: posterUid, ip: '127.0.0.1' }, { uid: posterUid, title: entry.title, content: entry.link && entry.link.href, From 162ed6fa26fa0f67a6446b230e7309577c00ac27 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Sun, 16 Jun 2024 00:09:48 -0400 Subject: [PATCH 2/4] refactor: just pass published date into topic creation call directly --- lib/pull.js | 28 +--------------------------- 1 file changed, 1 insertion(+), 27 deletions(-) diff --git a/lib/pull.js b/lib/pull.js index 3ddb582..c6cbdfb 100644 --- a/lib/pull.js +++ b/lib/pull.js @@ -81,40 +81,14 @@ async function postEntry(feed, entry) { content: entry.link && entry.link.href, cid: feed.category, tags: tags, + timestamp: feed.timestamp === 'feed' ? new Date(getEntryDate(entry)).getTime() : undefined, }); const { topicData } = result; - if (feed.timestamp === 'feed') { - setTimestampToFeedPublishedDate(result, entry); - } - const max = Math.max(parseInt(meta.config.postDelay, 10) || 10, parseInt(meta.config.newbiePostDelay, 10) || 10) + 1; await user.setUserField(posterUid, 'lastposttime', Date.now() - (max * 1000)); const uuid = entry.id || (entry.link && entry.link.href) || entry.title; await db.sortedSetAdd(`nodebb-plugin-rss:feed:${feed.url}:uuid`, topicData.tid, uuid); } - -function setTimestampToFeedPublishedDate(data, entry) { - const { topicData } = data; - const { postData } = data; - const { tid } = topicData; - const { pid } = postData; - const timestamp = new Date(getEntryDate(entry)).getTime(); - - db.setObjectField(`topic:${tid}`, 'timestamp', timestamp); - db.sortedSetsAdd([ - 'topics:tid', - `cid:${topicData.cid}:tids`, - `cid:${topicData.cid}:uid:${topicData.uid}:tids`, - `uid:${topicData.uid}:topics`, - ], timestamp, tid); - - db.setObjectField(`post:${pid}`, 'timestamp', timestamp); - db.sortedSetsAdd([ - 'posts:pid', - `cid:${topicData.cid}:pids`, - ], timestamp, pid); -} - From 2e87265eca0fddee49813d10a1ff4773b6d1c477 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Sun, 16 Jun 2024 09:06:19 -0400 Subject: [PATCH 3/4] fix: undefined topicData --- lib/pull.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/pull.js b/lib/pull.js index 7f5b1bc..e936d89 100644 --- a/lib/pull.js +++ b/lib/pull.js @@ -75,7 +75,7 @@ async function postEntry(feed, entry) { winston.verbose(`[plugin-rss] posting, ${feed.url} - title: ${entry.title}, published date: ${getEntryDate(entry)}`); - const result = await api.topics.create({ uid: posterUid, ip: '127.0.0.1' }, { + const topicData = await api.topics.create({ uid: posterUid, ip: '127.0.0.1' }, { uid: posterUid, title: entry.title, content: entry.link && entry.link.href, @@ -84,8 +84,6 @@ async function postEntry(feed, entry) { timestamp: feed.timestamp === 'feed' ? new Date(getEntryDate(entry)).getTime() : undefined, }); - const { topicData } = result; - const max = Math.max(parseInt(meta.config.postDelay, 10) || 10, parseInt(meta.config.newbiePostDelay, 10) || 10) + 1; await user.setUserField(posterUid, 'lastposttime', Date.now() - (max * 1000)); From f629174c75c275e59b32c35e733a226691be7d62 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 28 Jun 2024 12:00:29 -0400 Subject: [PATCH 4/4] fix: return early on topic purge if tid is not a number --- index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/index.js b/index.js index 0e11e8f..0576806 100644 --- a/index.js +++ b/index.js @@ -30,6 +30,10 @@ RssPlugin.init = async function (params) { * Called on `action:topic.purge` */ RssPlugin.onTopicPurge = async function (data) { + if (!utils.isNumber(data.topic.tid)) { + return; + } + const feedUrls = await db.getSetMembers('nodebb-plugin-rss:feeds'); const keys = feedUrls.map(url => `nodebb-plugin-rss:feed:${url}:uuid`); await db.sortedSetsRemoveRangeByScore(keys, data.topic.tid, data.topic.tid);