Repository navigation
Expand file tree
/
Copy pathFeedFilterer.js
More file actions
47 lines (40 loc) · 1.92 KB
/
Copy pathFeedFilterer.js
File metadata and controls
47 lines (40 loc) · 1.92 KB
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
module.exports = class FeedFilterer {
constructor(config) {
if (!config.client) throw new Error('Must provide "client"');
if (!config.rules) throw new Error('Must provide "rules"');
this.client = config.client;
this.rules = config.rules;
this.isDryRun = config.isDryRun;
this.verboseMode = config.verboseMode;
}
execute() {
console.log('FeedFilterer: getting unread items');
this.client.getUnreadItems()
.then((items) => {
console.log('FeedFilterer: got ' + items.length + ' items');
let itemsToMarkAsRead = items.filter((item) => {
if (this.verboseMode) {
console.log(`#${item.itemId} ''${(item.title || '').replace(/\s+/g, ' ').trim()}'' <${item.itemUrl}>`);
}
return this.rules.some((rule) => rule(item));
});
if (itemsToMarkAsRead.length > 0) {
console.log('FeedFilterer: will mark ' + itemsToMarkAsRead.length + ' items as read');
let promises = itemsToMarkAsRead.map((item) => {
console.log('Marking item as read: #' + item.itemId + ' ' + item.title + ' ' + item.itemUrl + '\n' + item.content + '\n\n\n\n');
if (this.isDryRun) {
console.log('FeedFilterer: (not actually marking as read)');
return Promise.resolve();
} else {
return this.client.markItemAsRead(item);
}
});
Promise.all(promises).then(() => {
console.log('FeedFilter: finished marking items as read');
});
} else {
console.log('FeedFilterer: nothing to mark as read');
}
});
}
}