forked from barisusakli/nodebb-plugin-rss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (78 loc) · 2.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
'use strict';
const db = require.main.require('./src/database');
const pubsub = require.main.require('./src/pubsub');
const database = require('./lib/database');
const controllers = require('./lib/controllers');
const feedAPI = require('./lib/feed');
const jobs = require('./lib/jobs');
const pull = require('./lib/pull');
const widget = require('./lib/widget');
const RssPlugin = {};
/**
* Called on `static:app.load`
*/
RssPlugin.init = async function (params) {
widget.init(params.app);
jobs.init(pullFeedsInterval);
params.router.get('/admin/plugins/rss', params.middleware.applyCSRF, params.middleware.admin.buildHeader, controllers.renderAdmin);
params.router.get('/api/admin/plugins/rss', params.middleware.applyCSRF, controllers.renderAdmin);
params.router.post('/api/admin/plugins/rss/save', params.middleware.applyCSRF, controllers.save);
params.router.get('/api/admin/plugins/rss/checkFeed', feedAPI.checkFeed);
};
/**
* Called on `action:topic.purge`
*/
RssPlugin.onTopicPurge = async function (data) {
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);
};
async function pullFeedsInterval(interval) {
let feeds = await database.getFeeds();
feeds = feeds.filter(item => item && parseInt(item.interval, 10) === interval);
if (!feeds.length) {
return;
}
for (const feed of feeds) {
// eslint-disable-next-line no-await-in-loop
await pull.pullFeed(feed);
}
}
RssPlugin.admin = {};
/**
* Called on `filter:admin.header.build`
*/
RssPlugin.admin.menu = async function (custom_header) {
custom_header.plugins.push({
route: '/plugins/rss',
icon: 'fa-rss',
name: 'RSS',
});
return custom_header;
};
/**
* Called on `action:plugin.deactivate`
*/
RssPlugin.admin.deactivate = function (data) {
if (data.id === 'nodebb-plugin-rss') {
pubsub.publish('nodebb-plugin-rss:deactivate');
}
};
/**
* Called on `action:plugin.uninstall`
*/
RssPlugin.admin.uninstall = function (data) {
if (data.id === 'nodebb-plugin-rss') {
database.deleteFeeds();
}
};
RssPlugin.widgets = {};
/**
* Called on `filter:widgets.getWidgets`
*/
RssPlugin.widgets.defineWidgets = widget.defineWidgets;
/**
* Called on `filter:widget.render:rss`
*/
RssPlugin.widgets.renderRssWidget = widget.render;
module.exports = RssPlugin;