|
|
|
@ -10,16 +10,17 @@ var meta = require('../meta');
|
|
|
|
|
var notifications = require('../notifications');
|
|
|
|
|
var privileges = require('../privileges');
|
|
|
|
|
|
|
|
|
|
(function (UserNotifications) {
|
|
|
|
|
UserNotifications.get = function (uid, callback) {
|
|
|
|
|
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,11 +29,12 @@ var privileges = require('../privileges');
|
|
|
|
|
notifications.read.length = maxNotifs - notifications.unread.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback(null, notifications);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
next(null, notifications);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function filterNotifications(nids, filter, callback) {
|
|
|
|
|
function filterNotifications(nids, filter, callback) {
|
|
|
|
|
if (!filter) {
|
|
|
|
|
return setImmediate(callback, null, nids);
|
|
|
|
|
}
|
|
|
|
@ -52,9 +54,9 @@ var privileges = require('../privileges');
|
|
|
|
|
next(null, nids);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UserNotifications.getAll = function (uid, filter, callback) {
|
|
|
|
|
UserNotifications.getAll = function (uid, filter, callback) {
|
|
|
|
|
var nids;
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
@ -87,9 +89,9 @@ var privileges = require('../privileges');
|
|
|
|
|
filterNotifications(nids, filter, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function deleteUserNids(nids, uid, callback) {
|
|
|
|
|
function deleteUserNids(nids, uid, callback) {
|
|
|
|
|
callback = callback || function () {};
|
|
|
|
|
if (!nids.length) {
|
|
|
|
|
return setImmediate(callback);
|
|
|
|
@ -104,9 +106,9 @@ var privileges = require('../privileges');
|
|
|
|
|
], function (err) {
|
|
|
|
|
callback(err);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getNotifications(uid, start, stop, callback) {
|
|
|
|
|
function getNotifications(uid, start, stop, callback) {
|
|
|
|
|
async.parallel({
|
|
|
|
|
unread: function (next) {
|
|
|
|
|
getNotificationsFromSet('uid:' + uid + ':notifications:unread', false, uid, start, stop, next);
|
|
|
|
@ -115,9 +117,9 @@ var privileges = require('../privileges');
|
|
|
|
|
getNotificationsFromSet('uid:' + uid + ':notifications:read', true, uid, start, stop, next);
|
|
|
|
|
},
|
|
|
|
|
}, callback);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getNotificationsFromSet(set, read, uid, start, stop, callback) {
|
|
|
|
|
function getNotificationsFromSet(set, read, uid, start, stop, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.getSortedSetRevRange(set, start, stop, next);
|
|
|
|
@ -130,9 +132,9 @@ var privileges = require('../privileges');
|
|
|
|
|
UserNotifications.getNotifications(nids, uid, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UserNotifications.getNotifications = function (nids, uid, callback) {
|
|
|
|
|
UserNotifications.getNotifications = function (nids, uid, callback) {
|
|
|
|
|
var notificationData = [];
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
@ -165,33 +167,38 @@ var privileges = require('../privileges');
|
|
|
|
|
notifications.merge(notificationData, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserNotifications.getDailyUnread = function (uid, callback) {
|
|
|
|
|
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) {
|
|
|
|
|
UserNotifications.getUnreadCount = function (uid, callback) {
|
|
|
|
|
if (!parseInt(uid, 10)) {
|
|
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
@ -214,14 +222,16 @@ var privileges = require('../privileges');
|
|
|
|
|
}, 0));
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserNotifications.getUnreadByField = function (uid, field, values, callback) {
|
|
|
|
|
db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99, function (err, nids) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserNotifications.getUnreadByField = function (uid, field, values, callback) {
|
|
|
|
|
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,12 +250,12 @@ var privileges = require('../privileges');
|
|
|
|
|
return notification.nid;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
callback(null, nids);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
next(null, nids);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserNotifications.deleteAll = function (uid, callback) {
|
|
|
|
|
UserNotifications.deleteAll = function (uid, callback) {
|
|
|
|
|
if (!parseInt(uid, 10)) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
@ -259,9 +267,9 @@ var privileges = require('../privileges');
|
|
|
|
|
db.delete('uid:' + uid + ':notifications:read', next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserNotifications.sendTopicNotificationToFollowers = function (uid, topicData, postData) {
|
|
|
|
|
UserNotifications.sendTopicNotificationToFollowers = function (uid, topicData, postData) {
|
|
|
|
|
var followers;
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
@ -304,9 +312,9 @@ var privileges = require('../privileges');
|
|
|
|
|
notifications.push(notification, followers);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserNotifications.sendWelcomeNotification = function (uid, callback) {
|
|
|
|
|
UserNotifications.sendWelcomeNotification = function (uid, callback) {
|
|
|
|
|
callback = callback || function () {};
|
|
|
|
|
if (!meta.config.welcomeNotification) {
|
|
|
|
|
return callback();
|
|
|
|
@ -314,20 +322,24 @@ 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], next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
notifications.push(notification, [uid], callback);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserNotifications.sendNameChangeNotification = function (uid, username) {
|
|
|
|
|
UserNotifications.sendNameChangeNotification = function (uid, username) {
|
|
|
|
|
notifications.create({
|
|
|
|
|
bodyShort: '[[user:username_taken_workaround, ' + username + ']]',
|
|
|
|
|
image: 'brand:logo',
|
|
|
|
@ -338,9 +350,9 @@ var privileges = require('../privileges');
|
|
|
|
|
notifications.push(notification, uid);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UserNotifications.pushCount = function (uid) {
|
|
|
|
|
UserNotifications.pushCount = function (uid) {
|
|
|
|
|
var websockets = require('./../socket.io');
|
|
|
|
|
UserNotifications.getUnreadCount(uid, function (err, count) {
|
|
|
|
|
if (err) {
|
|
|
|
@ -349,5 +361,4 @@ var privileges = require('../privileges');
|
|
|
|
|
|
|
|
|
|
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}(exports));
|
|
|
|
|
};
|
|
|
|
|