This repository was archived by the owner on Aug 17, 2020. It is now read-only.
forked from fightforthefuture/votebot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifier.js
executable file
·131 lines (102 loc) · 3.93 KB
/
notifier.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var config = require('./config');
var db = require('./lib/db');
var l10n = require('./lib/l10n');
var convo_model = require('./models/conversation');
var user_model = require('./models/user');
var notifications = require('./lib/notifications');
var log = require('./lib/logger');
var Promise = require('bluebird');
var moment = require('moment-timezone');
var RUN_DELAY = 60000;
var NOTIFY_TIMEOUT = 10000;
var QUERY = [
'SELECT *',
'FROM users',
'WHERE active = true',
'AND id > 1',
/*
'AND created > \'2016-10-12\'',
'AND created < \'2016-10-13\'',
*/
// 'AND created < now() - \'24 hours\'::interval',
/*
'AND (',
' last_notified IS NULL',
' OR',
' last_notified < now() - \'24 hours\'::interval',
' )',
*/
'ORDER BY id',
];
var run = function() {
var time = moment().tz('America/New_York');
log.notice('CURRENT time is (America/New_York): ', time.toString());
log.notice('... the hour is: ', time.hour());
if (time.hour() < 8 || time.hour() > 21) {
log.notice(' - Time is not between 5am PST and 10pm EST! Waiting...');
return setTimeout(run, RUN_DELAY);
}
db.query(
QUERY.join('\n'))
.each(executeUserNotification)
.then(function() {
log.notice(' - Got to the end of the database. Yay!');
return setTimeout(run, RUN_DELAY);
});
};
var executeUserNotification = function(user) {
log.notice('Processing notifications for user: ', user.id);
return Promise.each(notifications, function(notification) {
log.notice(' - Notification: ', notification.type);
// Check that the notification hasn't been sent
if (
user.notifications
&&
user.notifications.sent
&&
user.notifications.sent.indexOf(notification.type) > -1
) {
log.notice(' - USER TAGGED WITH THIS NOTIFICATION. NEXT!');
return;
}
var result = notification.process(user)
var skipToNextUser = function() {
throw('(not an error)');
}
if (!result.chain && !result.mark_sent) {
return;
} else {
// first mark the user as having been sent this notification
if (!user.notifications)
user.notifications = {};
if (!user.notifications.sent)
user.notifications.sent = [];
user.notifications.sent.push(notification.type);
log.notice(' - Marking user as sent: ', notification.type);
return user_model.update(user.id, {
notifications: user.notifications,
last_notified: db.now()
}).catch(function(err) {
log.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1!!!1!11!1');
log.error('FAILED UPDATE USER NOTIFICATIONS FLAG', err);
log.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1!!!1!1!!1');
return process.exit(1);
}).then(function(_user) {
if (result.chain) {
log.notice(' - Switch chain: ', _user.id, result.chain);
return convo_model.switch_chain(result.chain, user)
.timeout(NOTIFY_TIMEOUT)
.then(function() {
log.notice(' - Switch chain: SWITCHING TO NEXT USER LOL');
return skipToNextUser();
})
} else {
return skipToNextUser();
}
})
}
}).catch(function(error) {
log.notice(' - Triggered!!! Skipping to next:', error);
});
}
run();