|
|
|
@ -23,75 +23,58 @@ var async = require('async'),
|
|
|
|
|
new cron('0 0 * * *', Notifications.prune, null, true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Notifications.get = function(nid, uid, callback) {
|
|
|
|
|
Notifications.get = function(nid, callback) {
|
|
|
|
|
db.exists('notifications:' + nid, function(err, exists) {
|
|
|
|
|
if (err) {
|
|
|
|
|
winston.error('[notifications.get] Could not retrieve nid ' + nid + ': ' + err.message);
|
|
|
|
|
return callback(null);
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
|
db.sortedSetRank('uid:' + uid + ':notifications:read', nid, function(err, rank) {
|
|
|
|
|
db.getObject('notifications:' + nid, function(err, notification) {
|
|
|
|
|
notification.read = rank !== null ? true:false;
|
|
|
|
|
|
|
|
|
|
// Backwards compatibility for old notification schema
|
|
|
|
|
// Remove this block when NodeBB v0.6.0 is released.
|
|
|
|
|
if (notification.hasOwnProperty('text')) {
|
|
|
|
|
notification.bodyShort = notification.text;
|
|
|
|
|
notification.bodyLong = '';
|
|
|
|
|
notification.text = S(notification.text).escapeHTML().s;
|
|
|
|
|
}
|
|
|
|
|
if (!exists) {
|
|
|
|
|
return callback(null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.getObject('notifications:' + nid, function(err, notification) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Backwards compatibility for old notification schema
|
|
|
|
|
// Remove this block when NodeBB v0.6.0 is released.
|
|
|
|
|
if (notification.hasOwnProperty('text')) {
|
|
|
|
|
notification.bodyShort = notification.text;
|
|
|
|
|
notification.bodyLong = '';
|
|
|
|
|
notification.text = S(notification.text).escapeHTML().s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notification.bodyShort = S(notification.bodyShort).escapeHTML().s;
|
|
|
|
|
notification.bodyLong = S(notification.bodyLong).escapeHTML().s;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
notification.bodyShort = S(notification.bodyShort).escapeHTML().s;
|
|
|
|
|
notification.bodyLong = S(notification.bodyLong).escapeHTML().s;
|
|
|
|
|
|
|
|
|
|
if (notification.from && !notification.image) {
|
|
|
|
|
User.getUserField(notification.from, 'picture', function(err, picture) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
notification.image = picture;
|
|
|
|
|
callback(null, notification);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// Remove from the user's boxes
|
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
|
winston.info('[notifications.get] nid ' + nid + ' not found. Removing.');
|
|
|
|
|
return;
|
|
|
|
|
} else if (notification.image) {
|
|
|
|
|
switch(notification.image) {
|
|
|
|
|
case 'brand:logo':
|
|
|
|
|
notification.image = meta.config['brand:logo'] || nconf.get('relative_path') + '/logo.png';
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return callback(null, notification);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
callback(null, notification);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Notifications.create = function(data, callback) {
|
|
|
|
|
/**
|
|
|
|
|
*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 = {
|
|
|
|
|
bodyShort: '',
|
|
|
|
@ -117,36 +100,47 @@ var async = require('async'),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.incrObjectField('global', 'nextNid', function(err, nid) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data.nid = nid;
|
|
|
|
|
db.setAdd('notifications', nid);
|
|
|
|
|
db.setObject('notifications:' + nid, data, function(err, status) {
|
|
|
|
|
if (!err) {
|
|
|
|
|
callback(nid);
|
|
|
|
|
} else {
|
|
|
|
|
winston.error('[notifications.create] ' + err.message);
|
|
|
|
|
}
|
|
|
|
|
db.setObject('notifications:' + nid, data, function(err) {
|
|
|
|
|
callback(err, nid);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Notifications.push = function(nid, uids, callback) {
|
|
|
|
|
callback = callback || function() {};
|
|
|
|
|
var websockets = require('./socket.io');
|
|
|
|
|
if (!Array.isArray(uids)) {
|
|
|
|
|
uids = [uids];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var numUids = uids.length,
|
|
|
|
|
x;
|
|
|
|
|
Notifications.get(nid, function(err, notif_data) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Notifications.get(nid, null, function(notif_data) {
|
|
|
|
|
async.each(uids, function(uid, next) {
|
|
|
|
|
if (!parseInt(uid, 10)) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkReplace(notif_data.uniqueId, uid, notif_data, function(err, replace) {
|
|
|
|
|
if (replace) {
|
|
|
|
|
db.sortedSetAdd('uid:' + uid + ':notifications:unread', notif_data.datetime, nid);
|
|
|
|
|
shouldPush(uid, notif_data, function(err, shouldPush) {
|
|
|
|
|
if (err || !shouldPush) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.parallel([
|
|
|
|
|
async.apply(db.sortedSetAdd, 'uid:' + uid + ':notifications:unread', notif_data.datetime, notif_data.uniqueId),
|
|
|
|
|
async.apply(db.sortedSetRemove, 'uid:' + uid + ':notifications:read', notif_data.uniqueId)
|
|
|
|
|
], function(err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Client-side
|
|
|
|
|
websockets.in('uid_' + uid).emit('event:new_notification', notif_data);
|
|
|
|
@ -154,17 +148,56 @@ var async = require('async'),
|
|
|
|
|
// Plugins
|
|
|
|
|
notif_data.uid = uid;
|
|
|
|
|
plugins.fireHook('action:notification.pushed', notif_data);
|
|
|
|
|
}
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
|
|
db.setObjectField('uid:' + uid + ':notifications:uniqueId:nid', notif_data.uniqueId, nid, next);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}, callback);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function shouldPush(uid, newNotifObj, callback) {
|
|
|
|
|
hasNotification(newNotifObj.uniqueId, uid, function(err, hasNotification) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!hasNotification) {
|
|
|
|
|
return callback(null, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, function(err) {
|
|
|
|
|
if (callback) {
|
|
|
|
|
callback(null, true);
|
|
|
|
|
db.getObjectField('uid:' + uid + ':notifications:uniqueId:nid', newNotifObj.uniqueId, function(err, nid) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.getObjectFields('notifications:' + nid, ['nid', 'uniqueId', 'importance'], function(err, oldNotifObj) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!oldNotifObj || newNotifObj.uniqueId !== oldNotifObj.uniqueId) {
|
|
|
|
|
return callback(null, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null, parseInt(newNotifObj.importance, 10) >= parseInt(oldNotifObj.importance, 10));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function hasNotification(uniqueId, uid, callback) {
|
|
|
|
|
async.parallel([
|
|
|
|
|
async.apply(db.isSortedSetMember, 'uid:' + uid + ':notifications:unread', uniqueId),
|
|
|
|
|
async.apply(db.isSortedSetMember, 'uid:' + uid + ':notifications:read', uniqueId)
|
|
|
|
|
], function(err, results) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null, results[0] || results[1]);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Notifications.pushGroup = function(nid, groupName, callback) {
|
|
|
|
|
if (!callback) {
|
|
|
|
@ -182,129 +215,55 @@ var async = require('async'),
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.parallel([
|
|
|
|
|
function(next) {
|
|
|
|
|
checkAndRemove('uid:' + uid + ':notifications:unread', next);
|
|
|
|
|
},
|
|
|
|
|
function(next) {
|
|
|
|
|
checkAndRemove('uid:' + uid + ':notifications:read', next);
|
|
|
|
|
}
|
|
|
|
|
], function(err) {
|
|
|
|
|
if (!err) {
|
|
|
|
|
if (replace === false && matched === false) {
|
|
|
|
|
replace = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null, replace);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Notifications.mark_read = function(nid, uid, callback) {
|
|
|
|
|
Notifications.markRead = function(nid, uid, callback) {
|
|
|
|
|
callback = callback || function() {};
|
|
|
|
|
if (!parseInt(uid, 10)) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Notifications.get(nid, uid, function(notif_data) {
|
|
|
|
|
Notifications.get(nid, function(err, notificationData) {
|
|
|
|
|
if (err || !notificationData) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.parallel([
|
|
|
|
|
function(next) {
|
|
|
|
|
db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid, next);
|
|
|
|
|
},
|
|
|
|
|
function(next) {
|
|
|
|
|
if (!notif_data) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
db.sortedSetAdd('uid:' + uid + ':notifications:read', notif_data.datetime, nid, next);
|
|
|
|
|
}
|
|
|
|
|
async.apply(db.sortedSetRemove, 'uid:' + uid + ':notifications:unread', notificationData.uniqueId),
|
|
|
|
|
async.apply(db.sortedSetAdd, 'uid:' + uid + ':notifications:read', notificationData.datetime, notificationData.uniqueId)
|
|
|
|
|
], callback);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Notifications.mark_read_multiple = function(nids, uid, callback) {
|
|
|
|
|
Notifications.markReadMultiple = function(nids, uid, callback) {
|
|
|
|
|
callback = callback || function() {};
|
|
|
|
|
if (!Array.isArray(nids) && parseInt(nids, 10) > 0) {
|
|
|
|
|
nids = [nids];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.each(nids, function(nid, next) {
|
|
|
|
|
Notifications.mark_read(nid, uid, function(err) {
|
|
|
|
|
if (!err) {
|
|
|
|
|
next(null);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, function(err) {
|
|
|
|
|
if (callback) {
|
|
|
|
|
callback(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
Notifications.markRead(nid, uid, next);
|
|
|
|
|
}, callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Notifications.mark_all_read = function(uid, callback) {
|
|
|
|
|
db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, 10, function(err, nids) {
|
|
|
|
|
Notifications.markAllRead = function(uid, callback) {
|
|
|
|
|
db.getObjectValues('uid:' + uid + ':notifications:uniqueId:nid', function(err, nids) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nids.length > 0) {
|
|
|
|
|
Notifications.mark_read_multiple(nids, uid, function(err) {
|
|
|
|
|
callback(err);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
callback();
|
|
|
|
|
if (!Array.isArray(nids) || !nids.length) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Notifications.markReadMultiple(nids, uid, callback);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// why_are_we_using_underscores_here_?
|
|
|
|
|
// maybe_camel_case_ALL_THE_THINGS
|
|
|
|
|
Notifications.mark_read_by_uniqueid = function(uid, uniqueId, callback) {
|
|
|
|
|
Notifications.markReadByUniqueId = function(uid, uniqueId, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
async.apply(db.getSortedSetRange, 'uid:' + uid + ':notifications:unread', 0, 10),
|
|
|
|
|
function(nids, next) {
|
|
|
|
|
async.filter(nids, function(nid, next) {
|
|
|
|
|
db.getObjectField('notifications:' + nid, 'uniqueId', function(err, value) {
|
|
|
|
|
next(uniqueId === value);
|
|
|
|
|
});
|
|
|
|
|
}, function(nids) {
|
|
|
|
|
next(null, nids);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
function(nids, next) {
|
|
|
|
|
Notifications.mark_read_multiple(nids, uid, next);
|
|
|
|
|
async.apply(db.getObjectField, 'uid:' + uid + ':notifications:uniqueId:nid', uniqueId),
|
|
|
|
|
function(nid, next) {
|
|
|
|
|
Notifications.markRead(nid, uid, next);
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
@ -326,13 +285,13 @@ var async = require('async'),
|
|
|
|
|
var cutoffTime = cutoff.getTime();
|
|
|
|
|
|
|
|
|
|
db.getSetMembers('notifications', function(err, nids) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return winston.error(err.message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.filter(nids, function(nid, next) {
|
|
|
|
|
db.getObjectField('notifications:' + nid, 'datetime', function(err, datetime) {
|
|
|
|
|
if (parseInt(datetime, 10) < cutoffTime) {
|
|
|
|
|
next(true);
|
|
|
|
|
} else {
|
|
|
|
|
next(false);
|
|
|
|
|
}
|
|
|
|
|
next(!err && parseInt(datetime, 10) < cutoffTime);
|
|
|
|
|
});
|
|
|
|
|
}, function(expiredNids) {
|
|
|
|
|
async.each(expiredNids, function(nid, next) {
|
|
|
|
|