|
|
|
@ -10,16 +10,17 @@ var meta = require('../meta');
|
|
|
|
|
var notifications = require('../notifications');
|
|
|
|
|
var privileges = require('../privileges');
|
|
|
|
|
|
|
|
|
|
(function (UserNotifications) {
|
|
|
|
|
var UserNotifications = module.exports;
|
|
|
|
|
|
|
|
|
|
UserNotifications.get = function (uid, callback) {
|
|
|
|
|
if (!parseInt(uid, 10)) {
|
|
|
|
|
return callback(null, { read: [], unread: [] });
|
|
|
|
|
}
|
|
|
|
|
getNotifications(uid, 0, 9, function (err, notifications) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
getNotifications(uid, 0, 9, next);
|
|
|
|
|
},
|
|
|
|
|
function (notifications, next) {
|
|
|
|
|
notifications.read = notifications.read.filter(Boolean);
|
|
|
|
|
notifications.unread = notifications.unread.filter(Boolean);
|
|
|
|
|
|
|
|
|
@ -28,8 +29,9 @@ var privileges = require('../privileges');
|
|
|
|
|
notifications.read.length = maxNotifs - notifications.unread.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null, notifications);
|
|
|
|
|
});
|
|
|
|
|
next(null, notifications);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function filterNotifications(nids, filter, callback) {
|
|
|
|
@ -170,17 +172,18 @@ var privileges = require('../privileges');
|
|
|
|
|
UserNotifications.getDailyUnread = function (uid, callback) {
|
|
|
|
|
var yesterday = Date.now() - (1000 * 60 * 60 * 24); // Approximate, can be more or less depending on time changes, makes no difference really.
|
|
|
|
|
|
|
|
|
|
db.getSortedSetRevRangeByScore('uid:' + uid + ':notifications:unread', 0, 20, '+inf', yesterday, function (err, nids) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.getSortedSetRevRangeByScore('uid:' + uid + ':notifications:unread', 0, 20, '+inf', yesterday, next);
|
|
|
|
|
},
|
|
|
|
|
function (nids, next) {
|
|
|
|
|
if (!Array.isArray(nids) || !nids.length) {
|
|
|
|
|
return callback(null, []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UserNotifications.getNotifications(nids, uid, callback);
|
|
|
|
|
});
|
|
|
|
|
UserNotifications.getNotifications(nids, uid, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserNotifications.getUnreadCount = function (uid, callback) {
|
|
|
|
@ -188,10 +191,14 @@ var privileges = require('../privileges');
|
|
|
|
|
return callback(null, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Collapse any notifications with identical mergeIds
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
async.apply(db.getSortedSetRevRange, 'uid:' + uid + ':notifications:unread', 0, 99),
|
|
|
|
|
async.apply(notifications.filterExists),
|
|
|
|
|
function (next) {
|
|
|
|
|
db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99, next);
|
|
|
|
|
},
|
|
|
|
|
function (nids, next) {
|
|
|
|
|
notifications.filterExists(nids, next);
|
|
|
|
|
},
|
|
|
|
|
function (nids, next) {
|
|
|
|
|
var keys = nids.map(function (nid) {
|
|
|
|
|
return 'notifications:' + nid;
|
|
|
|
@ -200,6 +207,7 @@ var privileges = require('../privileges');
|
|
|
|
|
db.getObjectsFields(keys, ['mergeId'], next);
|
|
|
|
|
},
|
|
|
|
|
function (mergeIds, next) {
|
|
|
|
|
// Collapse any notifications with identical mergeIds
|
|
|
|
|
mergeIds = mergeIds.map(function (set) {
|
|
|
|
|
return set.mergeId;
|
|
|
|
|
});
|
|
|
|
@ -217,11 +225,13 @@ var privileges = require('../privileges');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserNotifications.getUnreadByField = function (uid, field, values, callback) {
|
|
|
|
|
db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99, function (err, nids) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var nids;
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99, next);
|
|
|
|
|
},
|
|
|
|
|
function (_nids, next) {
|
|
|
|
|
nids = _nids;
|
|
|
|
|
if (!Array.isArray(nids) || !nids.length) {
|
|
|
|
|
return callback(null, []);
|
|
|
|
|
}
|
|
|
|
@ -230,11 +240,9 @@ var privileges = require('../privileges');
|
|
|
|
|
return 'notifications:' + nid;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
db.getObjectsFields(keys, ['nid', field], function (err, notifications) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.getObjectsFields(keys, ['nid', field], next);
|
|
|
|
|
},
|
|
|
|
|
function (notifications, next) {
|
|
|
|
|
values = values.map(function () { return values.toString(); });
|
|
|
|
|
nids = notifications.filter(function (notification) {
|
|
|
|
|
return notification && notification[field] && values.indexOf(notification[field].toString()) !== -1;
|
|
|
|
@ -242,9 +250,9 @@ var privileges = require('../privileges');
|
|
|
|
|
return notification.nid;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
callback(null, nids);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
next(null, nids);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserNotifications.deleteAll = function (uid, callback) {
|
|
|
|
@ -314,17 +322,21 @@ var privileges = require('../privileges');
|
|
|
|
|
|
|
|
|
|
var path = meta.config.welcomeLink ? meta.config.welcomeLink : '#';
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
notifications.create({
|
|
|
|
|
bodyShort: meta.config.welcomeNotification,
|
|
|
|
|
path: path,
|
|
|
|
|
nid: 'welcome_' + uid,
|
|
|
|
|
}, function (err, notification) {
|
|
|
|
|
if (err || !notification) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}, next);
|
|
|
|
|
},
|
|
|
|
|
function (notification, next) {
|
|
|
|
|
if (!notification) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notifications.push(notification, [uid], callback);
|
|
|
|
|
});
|
|
|
|
|
notifications.push(notification, [uid], next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserNotifications.sendNameChangeNotification = function (uid, username) {
|
|
|
|
@ -350,4 +362,3 @@ var privileges = require('../privileges');
|
|
|
|
|
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}(exports));
|
|
|
|
|