-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (39 loc) · 1.25 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
const JOB_INTERVAL = process.env.JOB_INTERVAL || (1000 * 6);
const mongoUrl = require('./config').mongoUrl;
const escapeRegexp = require('escape-regexp');
const monk = require('monk');
const debug = require('debug')('job:main');
const db = monk(mongoUrl);
const todos = db.get('todos');
const suspicious = db.get('suspicious');
const prohibitedWords = require('./prohibitedWords.json');
const prohibitedWordsRegex = new RegExp(
prohibitedWords.map(t => `\\b${escapeRegexp(t)}\\b`).join('|'),
'i'
);
main();
function main(){
const timerId = setInterval(job, JOB_INTERVAL);
job();
}
function job(){
console.log(`Job run ${new Date()}`);
todos.find({}, function(err, todoLists){
todoLists.forEach(function(todoList){
todoList.todos.forEach(function(todo){
if(prohibitedWordsRegex.test(todo.text)){
console.warn(`Prohibited words detected '${todo.text}' in list '${todoList.name}', todo id '${todo.id}'`);
suspicious.findAndModify(
{ todoId : todo.id},
{ listId: todoList._id,
listName: todoList.name,
todoId: todo.id,
todoText: todo.text
},
{ upsert: true }
);
}
});
});
});
}