You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/notifications.js

316 lines
8.0 KiB
JavaScript

11 years ago
'use strict';
11 years ago
var async = require('async'),
winston = require('winston'),
cron = require('cron').CronJob,
nconf = require('nconf'),
11 years ago
db = require('./database'),
utils = require('../public/src/utils'),
events = require('./events'),
User = require('./user'),
groups = require('./groups'),
meta = require('./meta');
11 years ago
(function(Notifications) {
11 years ago
Notifications.init = function() {
if (process.env.NODE_ENV === 'development') {
winston.info('[notifications.init] Registering jobs.');
}
new cron('0 0 * * *', Notifications.prune, null, true);
};
11 years ago
11 years ago
Notifications.get = function(nid, uid, callback) {
db.exists('notifications:' + nid, function(err, exists) {
11 years ago
if (err) {
winston.error('[notifications.get] Could not retrieve nid ' + nid + ': ' + err.message);
return callback(null);
}
11 years ago
if (exists) {
db.sortedSetRank('uid:' + uid + ':notifications:read', nid, function(err, rank) {
11 years ago
db.getObjectFields('notifications:' + nid, ['nid', 'from', 'text', 'image', 'importance', 'score', 'path', 'datetime', 'uniqueId'], function(err, notification) {
11 years ago
notification.read = rank !== null ? true:false;
if (notification.from && !notification.image) {
User.getUserField(notification.from, 'picture', function(err, picture) {
notification.image = picture;
callback(notification);
});
} else if (notification.image) {
switch(notification.image) {
case 'brand:logo':
notification.image = meta.config['brand:logo'] || nconf.get('relative_path') + '/logo.png';
break;
}
callback(notification);
} else {
callback(notification);
}
11 years ago
});
11 years ago
});
11 years ago
} else {
// Remove from the user's boxes
if (process.env.NODE_ENV === 'development') {
winston.info('[notifications.get] nid ' + nid + ' not found. Removing.');
}
async.parallel([
function(next) {
db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid, next);
},
function(next) {
db.sortedSetRemove('uid:' + uid + ':notifications:read', nid, next);
}
], function(err) {
callback(null);
});
}
});
11 years ago
};
11 years ago
Notifications.create = function(data, callback) {
11 years ago
/**
11 years ago
*data.uniqueId is used solely to override stale nids.
* If a new nid is pushed to a user and an existing nid in the user's
* (un)read list contains the same uniqueId, it will be removed, and
* the new one put in its place.
*/
// Add default values to data Object if not already set
var defaults = {
text: '',
path: null,
11 years ago
importance: 5,
datetime: Date.now(),
uniqueId: utils.generateUUID()
};
for(var v in defaults) {
if (defaults.hasOwnProperty(v) && !data[v]) {
data[v] = defaults[v];
}
}
11 years ago
db.incrObjectField('global', 'nextNid', function(err, nid) {
11 years ago
data.nid = nid;
11 years ago
db.setAdd('notifications', nid);
db.setObject('notifications:' + nid, data, function(err, status) {
11 years ago
if (!err) {
callback(nid);
}
});
11 years ago
});
};
11 years ago
11 years ago
Notifications.push = function(nid, uids, callback) {
var websockets = require('./socket.io');
11 years ago
if (!Array.isArray(uids)) {
uids = [uids];
}
11 years ago
var numUids = uids.length,
x;
11 years ago
Notifications.get(nid, null, function(notif_data) {
11 years ago
async.each(uids, function(uid, next) {
11 years ago
if (!parseInt(uid, 10)) {
return next();
11 years ago
}
11 years ago
11 years ago
checkReplace(notif_data.uniqueId, uid, notif_data, function(err, replace) {
if (replace) {
db.sortedSetAdd('uid:' + uid + ':notifications:unread', notif_data.datetime, nid);
websockets.in('uid_' + uid).emit('event:new_notification', notif_data);
}
next();
});
}, function(err) {
if (callback) {
callback(null, true);
}
11 years ago
});
11 years ago
});
};
11 years ago
// Note: This method used in mentions plugin. If still unused in core as of 0.6.0, please move to mentions plugin itself.
Notifications.pushGroup = function(nid, groupName, callback) {
if (!callback) {
callback = function() {};
}
groups.get(groupName, {}, function(err, groupObj) {
if (!err && groupObj) {
if (groupObj.memberCount > 0) {
Notifications.push(nid, groupObj.members, callback);
}
} else {
callback(err);
}
});
};
11 years ago
function checkReplace(uniqueId, uid, newNotifObj, callback) {
var replace = false, matched = false;
function checkAndRemove(set, next) {
db.getSortedSetRange(set, 0, -1, function(err, nids) {
if (err || !nids || !nids.length) {
return next(err);
}
var keys = nids.map(function(nid) {
return 'notifications:' + nid;
});
db.getObjectsFields(keys, ['nid', 'uniqueId', 'importance'], function(err, nid_infos) {
if (err) {
return next(err);
11 years ago
}
nid_infos.forEach(function(nid_info) {
if (nid_info && nid_info.uniqueId === uniqueId) {
matched = true;
if ((nid_info.importance || 5) >= newNotifObj.importance) {
replace = true;
db.sortedSetRemove(set, nid_info.nid);
}
}
});
next();
11 years ago
});
});
}
async.parallel([
function(next) {
checkAndRemove('uid:' + uid + ':notifications:unread', next);
11 years ago
},
function(next) {
11 years ago
checkAndRemove('uid:' + uid + ':notifications:read', next);
11 years ago
}
], function(err) {
if (!err) {
11 years ago
if (replace === false && matched === false) {
replace = true;
}
callback(null, replace);
11 years ago
}
});
}
11 years ago
11 years ago
Notifications.mark_read = function(nid, uid, callback) {
if (parseInt(uid, 10) > 0) {
Notifications.get(nid, uid, function(notif_data) {
async.parallel([
function(next) {
db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid, next);
},
function(next) {
db.sortedSetAdd('uid:' + uid + ':notifications:read', notif_data.datetime, nid, next);
}
], function(err) {
if (callback) {
callback();
}
});
});
11 years ago
}
};
11 years ago
11 years ago
Notifications.mark_read_multiple = function(nids, uid, callback) {
if (!Array.isArray(nids) && parseInt(nids, 10) > 0) {
nids = [nids];
}
11 years ago
async.each(nids, function(nid, next) {
Notifications.mark_read(nid, uid, function(err) {
if (!err) {
next(null);
}
});
11 years ago
}, function(err) {
if (callback) {
callback(err);
}
});
};
11 years ago
11 years ago
Notifications.mark_all_read = function(uid, callback) {
11 years ago
db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, 10, function(err, nids) {
11 years ago
if (err) {
return callback(err);
}
11 years ago
if (nids.length > 0) {
Notifications.mark_read_multiple(nids, uid, function(err) {
callback(err);
});
} else {
callback();
}
11 years ago
});
};
11 years ago
11 years ago
Notifications.prune = function(cutoff) {
var start = process.hrtime();
11 years ago
if (process.env.NODE_ENV === 'development') {
winston.info('[notifications.prune] Removing expired notifications from the database.');
}
11 years ago
var today = new Date(),
numPruned = 0;
11 years ago
if (!cutoff) {
cutoff = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7);
}
11 years ago
11 years ago
var cutoffTime = cutoff.getTime();
11 years ago
11 years ago
db.getSetMembers('notifications', function(err, nids) {
async.filter(nids, function(nid, next) {
db.getObjectField('notifications:' + nid, 'datetime', function(err, datetime) {
if (parseInt(datetime, 10) < cutoffTime) {
next(true);
} else {
next(false);
}
11 years ago
});
11 years ago
}, function(expiredNids) {
async.each(expiredNids, function(nid, next) {
async.parallel([
function(next) {
11 years ago
db.setRemove('notifications', nid, next);
11 years ago
},
function(next) {
11 years ago
db.delete('notifications:' + nid, next);
11 years ago
}
], function(err) {
numPruned++;
11 years ago
next(err);
});
}, function(err) {
if (!err) {
if (process.env.NODE_ENV === 'development') {
winston.info('[notifications.prune] Notification pruning completed. ' + numPruned + ' expired notification' + (numPruned !== 1 ? 's' : '') + ' removed.');
}
var diff = process.hrtime(start);
events.log('Pruning notifications took : ' + (diff[0] * 1e3 + diff[1] / 1e6) + ' ms');
} else {
winston.error('Encountered error pruning notifications: ' + err.message);
11 years ago
}
11 years ago
});
});
11 years ago
});
};
11 years ago
11 years ago
}(exports));