Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 5 additions & 33 deletions lib/pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
}

Expand All @@ -73,48 +73,20 @@ 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 topicData = await api.topics.create({ uid: posterUid, ip: '127.0.0.1' }, {
uid: posterUid,
title: entry.title,
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);
}