-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.js
105 lines (90 loc) · 2.1 KB
/
feed.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
102
103
104
105
const Feed = require('feed').Feed;
const express = require('express');
const jobs = require('./jobs.js');
const notify = require('./notify.js');
const config = require('./config.js');
const router = express.Router();
const feed = new Feed({
title: 'Web Monitor',
description: 'Get updates on monitored values',
id: config.domain,
link: config.domain,
copyright: 'All rights reserved 2018',
feedLinks: {
rss: `${config.domain}/feed/rss`,
atom: `${config.domain}/feed/atom`,
json: `${config.domain}/feed/json`
}
});
let rss2 = null;
let atom1 = null;
let json1 = null;
router.get('/rss', (req, res) => {
if (!rss2) {
loadAllItems().then(() => {
res.status(200);
res.send(feed.rss2());
});
return;
}
res.status(200);
res.send(feed.rss2());
});
router.get('/atom', (req, res) => {
if (!atom1) {
loadAllItems().then(() => {
res.status(200);
res.send(feed.atom1());
});
return;
}
res.status(200);
res.send(feed.atom1());
});
router.get('/json', (req, res) => {
if (!json1) {
loadAllItems().then(() => {
res.status(200);
res.json(feed.json1());
});
return;
}
res.status(200);
res.json(feed.json1());
});
Object.assign(exports, {
router: router,
addItem: addItem
});
function addItem(job, oldValue, newValue, updateCache = true) {
feed.addItem({
title: job.title,
id: `${config.domain}/manage/${job.id}#${newValue.time}`,
link: `${config.domain}/manage/${job.id}`,
description: `Value changed from "${oldValue.value}" to "${
newValue.value
}"`,
content: notify.formatHTML(job, oldValue.value, newValue.value),
date: newValue.time
});
if (updateCache) {
updateFeedCache();
}
}
function updateFeedCache() {
rss2 = feed.rss2();
atom1 = feed.atom1();
json1 = feed.json1();
}
async function loadAllItems() {
return jobs.getAll().then((jobs) => {
for (const job of jobs) {
let oldValue = {};
for (const newValue of job.values) {
addItem(job, oldValue, newValue, false);
oldValue = newValue;
}
}
updateFeedCache();
});
}