mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-07 14:35:47 +01:00
added cronjob for notifications
This commit is contained in:
5
app.js
5
app.js
@@ -87,7 +87,8 @@
|
|||||||
SocketIO = require('socket.io').listen(global.server, { log: false, transports: ['websocket', 'xhr-polling', 'jsonp-polling', 'flashsocket']}),
|
SocketIO = require('socket.io').listen(global.server, { log: false, transports: ['websocket', 'xhr-polling', 'jsonp-polling', 'flashsocket']}),
|
||||||
websockets = require('./src/websockets.js'),
|
websockets = require('./src/websockets.js'),
|
||||||
posts = require('./src/posts.js'),
|
posts = require('./src/posts.js'),
|
||||||
plugins = require('./src/plugins'); // Don't remove this - plugins initializes itself
|
plugins = require('./src/plugins'), // Don't remove this - plugins initializes itself
|
||||||
|
Notifications = require('./src/notifications');
|
||||||
|
|
||||||
websockets.init(SocketIO);
|
websockets.init(SocketIO);
|
||||||
|
|
||||||
@@ -106,6 +107,8 @@
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
templates.ready(webserver.init);
|
templates.ready(webserver.init);
|
||||||
|
|
||||||
|
Notifications.init();
|
||||||
});
|
});
|
||||||
|
|
||||||
} else if (nconf.get('upgrade')) {
|
} else if (nconf.get('upgrade')) {
|
||||||
|
|||||||
@@ -44,7 +44,8 @@
|
|||||||
"nodebb-plugin-mentions": "~0.1.13",
|
"nodebb-plugin-mentions": "~0.1.13",
|
||||||
"nodebb-plugin-markdown": "~0.1.7",
|
"nodebb-plugin-markdown": "~0.1.7",
|
||||||
"nodebb-theme-vanilla": "designcreateplay/nodebb-theme-vanilla",
|
"nodebb-theme-vanilla": "designcreateplay/nodebb-theme-vanilla",
|
||||||
"nodebb-theme-cerulean": "0.0.3"
|
"nodebb-theme-cerulean": "0.0.3",
|
||||||
|
"cron": "~1.0.1"
|
||||||
},
|
},
|
||||||
"optionalDependencies": {
|
"optionalDependencies": {
|
||||||
"hiredis": "~0.1.15"
|
"hiredis": "~0.1.15"
|
||||||
|
|||||||
@@ -1,18 +1,29 @@
|
|||||||
var RDB = require('./redis.js'),
|
var RDB = require('./redis.js'),
|
||||||
async = require('async'),
|
async = require('async'),
|
||||||
utils = require('../public/src/utils.js'),
|
utils = require('../public/src/utils.js'),
|
||||||
|
winston = require('winston'),
|
||||||
|
cron = require('cron').CronJob,
|
||||||
|
|
||||||
notifications = {
|
notifications = {
|
||||||
|
init: function() {
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
winston.info('[notifications.init] Registering jobs.');
|
||||||
|
}
|
||||||
|
|
||||||
|
new cron('0 0 * * *', notifications.prune, null, true);
|
||||||
|
},
|
||||||
get: function(nid, uid, callback) {
|
get: function(nid, uid, callback) {
|
||||||
RDB.multi()
|
RDB.multi()
|
||||||
.hmget('notifications:' + nid, 'text', 'score', 'path', 'datetime', 'uniqueId')
|
.hmget('notifications:' + nid, 'text', 'score', 'path', 'datetime', 'uniqueId')
|
||||||
.zrank('uid:' + uid + ':notifications:read', nid)
|
.zrank('uid:' + uid + ':notifications:read', nid)
|
||||||
.exists('notifications:' + nid)
|
.exists('notifications:' + nid)
|
||||||
.exec(function(err, results) {
|
.exec(function(err, results) {
|
||||||
var notification = results[0]
|
var notification = results[0],
|
||||||
readIdx = results[1];
|
readIdx = results[1];
|
||||||
|
|
||||||
if (!results[2]) return callback(null);
|
if (!results[2]) {
|
||||||
|
return callback(null);
|
||||||
|
}
|
||||||
|
|
||||||
callback({
|
callback({
|
||||||
nid: nid,
|
nid: nid,
|
||||||
@@ -40,7 +51,9 @@ var RDB = require('./redis.js'),
|
|||||||
datetime: Date.now(),
|
datetime: Date.now(),
|
||||||
uniqueId: uniqueId || utils.generateUUID()
|
uniqueId: uniqueId || utils.generateUUID()
|
||||||
}, function(err, status) {
|
}, function(err, status) {
|
||||||
if (!err) callback(nid);
|
if (!err) {
|
||||||
|
callback(nid);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -65,12 +78,14 @@ var RDB = require('./redis.js'),
|
|||||||
|
|
||||||
notifications.get(nid, null, function(notif_data) {
|
notifications.get(nid, null, function(notif_data) {
|
||||||
for (x = 0; x < numUids; x++) {
|
for (x = 0; x < numUids; x++) {
|
||||||
if (parseInt(uids[x]) > 0) {
|
if (parseInt(uids[x], 10) > 0) {
|
||||||
(function(uid) {
|
(function(uid) {
|
||||||
notifications.remove_by_uniqueId(notif_data.uniqueId, uid, function() {
|
notifications.remove_by_uniqueId(notif_data.uniqueId, uid, function() {
|
||||||
RDB.zadd('uid:' + uid + ':notifications:unread', notif_data.datetime, nid);
|
RDB.zadd('uid:' + uid + ':notifications:unread', notif_data.datetime, nid);
|
||||||
global.io.sockets.in('uid_' + uid).emit('event:new_notification');
|
global.io.sockets.in('uid_' + uid).emit('event:new_notification');
|
||||||
if (callback) callback(true);
|
if (callback) {
|
||||||
|
callback(true);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
})(uids[x]);
|
})(uids[x]);
|
||||||
}
|
}
|
||||||
@@ -84,13 +99,18 @@ var RDB = require('./redis.js'),
|
|||||||
if (nids && nids.length > 0) {
|
if (nids && nids.length > 0) {
|
||||||
async.each(nids, function(nid, next) {
|
async.each(nids, function(nid, next) {
|
||||||
notifications.get(nid, uid, function(nid_info) {
|
notifications.get(nid, uid, function(nid_info) {
|
||||||
if (nid_info.uniqueId === uniqueId) RDB.zrem('uid:' + uid + ':notifications:unread', nid);
|
if (nid_info.uniqueId === uniqueId) {
|
||||||
|
RDB.zrem('uid:' + uid + ':notifications:unread', nid);
|
||||||
|
}
|
||||||
|
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
} else next();
|
} else {
|
||||||
|
next();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
@@ -98,17 +118,24 @@ var RDB = require('./redis.js'),
|
|||||||
if (nids && nids.length > 0) {
|
if (nids && nids.length > 0) {
|
||||||
async.each(nids, function(nid, next) {
|
async.each(nids, function(nid, next) {
|
||||||
notifications.get(nid, uid, function(nid_info) {
|
notifications.get(nid, uid, function(nid_info) {
|
||||||
if (nid_info.uniqueId === uniqueId) RDB.zrem('uid:' + uid + ':notifications:read', nid);
|
if (nid_info.uniqueId === uniqueId) {
|
||||||
|
RDB.zrem('uid:' + uid + ':notifications:read', nid);
|
||||||
|
}
|
||||||
|
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
} else next();
|
} else {
|
||||||
|
next();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
], function(err) {
|
], function(err) {
|
||||||
if (!err) callback(true);
|
if (!err) {
|
||||||
|
callback(true);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
mark_read: function(nid, uid, callback) {
|
mark_read: function(nid, uid, callback) {
|
||||||
@@ -116,35 +143,55 @@ var RDB = require('./redis.js'),
|
|||||||
notifications.get(nid, uid, function(notif_data) {
|
notifications.get(nid, uid, function(notif_data) {
|
||||||
RDB.zrem('uid:' + uid + ':notifications:unread', nid);
|
RDB.zrem('uid:' + uid + ':notifications:unread', nid);
|
||||||
RDB.zadd('uid:' + uid + ':notifications:read', notif_data.datetime, nid);
|
RDB.zadd('uid:' + uid + ':notifications:read', notif_data.datetime, nid);
|
||||||
if (callback) callback();
|
if (callback) {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
mark_read_multiple: function(nids, uid, callback) {
|
mark_read_multiple: function(nids, uid, callback) {
|
||||||
if (!Array.isArray(nids) && parseInt(nids, 10) > 0) nids = [nids];
|
if (!Array.isArray(nids) && parseInt(nids, 10) > 0) {
|
||||||
|
nids = [nids];
|
||||||
|
}
|
||||||
|
|
||||||
async.each(nids, function(nid, next) {
|
async.each(nids, function(nid, next) {
|
||||||
notifications.mark_read(nid, uid, function(err) {
|
notifications.mark_read(nid, uid, function(err) {
|
||||||
if (!err) next(null);
|
if (!err) {
|
||||||
|
next(null);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
if (callback) callback(err);
|
if (callback) {
|
||||||
|
callback(err);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
mark_all_read: function(uid, callback) {
|
mark_all_read: function(uid, callback) {
|
||||||
RDB.zrange('uid:' + uid + ':notifications:unread', 0, 10, function(err, nids) {
|
RDB.zrange('uid:' + uid + ':notifications:unread', 0, 10, function(err, nids) {
|
||||||
if (err) return callback(err);
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
if (nids.length > 0) {
|
if (nids.length > 0) {
|
||||||
notifications.mark_read_multiple(nids, uid, function(err) {
|
notifications.mark_read_multiple(nids, uid, function(err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
});
|
});
|
||||||
} else callback();
|
} else {
|
||||||
|
callback();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
prune: function(cutoff, callback) {
|
prune: function(cutoff) {
|
||||||
var today = new Date();
|
if (process.env.NODE_ENV === 'development') {
|
||||||
if (!cutoff) cutoff = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7);
|
winston.info('[notifications.prune] Removing expired notifications from the database.');
|
||||||
|
}
|
||||||
|
|
||||||
|
var today = new Date(),
|
||||||
|
numPruned = 0;
|
||||||
|
|
||||||
|
if (!cutoff) {
|
||||||
|
cutoff = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7);
|
||||||
|
}
|
||||||
|
|
||||||
var cutoffTime = cutoff.getTime();
|
var cutoffTime = cutoff.getTime();
|
||||||
|
|
||||||
@@ -156,8 +203,11 @@ var RDB = require('./redis.js'),
|
|||||||
RDB.smembers('notifications', function(err, nids) {
|
RDB.smembers('notifications', function(err, nids) {
|
||||||
async.filter(nids, function(nid, next) {
|
async.filter(nids, function(nid, next) {
|
||||||
RDB.hget('notifications:' + nid, 'datetime', function(err, datetime) {
|
RDB.hget('notifications:' + nid, 'datetime', function(err, datetime) {
|
||||||
if (parseInt(datetime, 10) < cutoffTime) next(true);
|
if (parseInt(datetime, 10) < cutoffTime) {
|
||||||
else next(false);
|
next(true);
|
||||||
|
} else {
|
||||||
|
next(false);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}, function(expiredNids) {
|
}, function(expiredNids) {
|
||||||
next(null, expiredNids);
|
next(null, expiredNids);
|
||||||
@@ -179,17 +229,22 @@ var RDB = require('./redis.js'),
|
|||||||
multi.exec(function(err, results) {
|
multi.exec(function(err, results) {
|
||||||
// If the notification is not present in any inbox, delete it altogether
|
// If the notification is not present in any inbox, delete it altogether
|
||||||
var expired = results.every(function(present) {
|
var expired = results.every(function(present) {
|
||||||
if (present === null) return true;
|
if (present === null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (expired) {
|
if (expired) {
|
||||||
notifications.destroy(nid);
|
notifications.destroy(nid);
|
||||||
|
numPruned++;
|
||||||
}
|
}
|
||||||
|
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
winston.info('[notifications.prune] Notification pruning completed. ' + numPruned + ' expired notification' + (numPruned !== 1 ? 's' : '') + ' removed.');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (process.env.NODE_ENV === 'development') {
|
if (process.env.NODE_ENV === 'development') {
|
||||||
@@ -197,16 +252,17 @@ var RDB = require('./redis.js'),
|
|||||||
winston.error(err.stack);
|
winston.error(err.stack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
init: notifications.init,
|
||||||
get: notifications.get,
|
get: notifications.get,
|
||||||
create: notifications.create,
|
create: notifications.create,
|
||||||
push: notifications.push,
|
push: notifications.push,
|
||||||
mark_read: notifications.mark_read_multiple,
|
mark_read: notifications.mark_read_multiple,
|
||||||
mark_all_read: notifications.mark_all_read,
|
mark_all_read: notifications.mark_all_read,
|
||||||
prune: notifications.prune
|
prune: notifications.prune
|
||||||
}
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user