mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-27 17:16:14 +01:00
more notification tests
This commit is contained in:
@@ -57,7 +57,9 @@ module.exports = function (User) {
|
|||||||
], next);
|
], next);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
], callback);
|
], function (err) {
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
User.getFollowing = function (uid, start, stop, callback) {
|
User.getFollowing = function (uid, start, stop, callback) {
|
||||||
|
|||||||
@@ -10,16 +10,17 @@ var meta = require('../meta');
|
|||||||
var notifications = require('../notifications');
|
var notifications = require('../notifications');
|
||||||
var privileges = require('../privileges');
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
UserNotifications.get = function (uid, callback) {
|
||||||
|
if (!parseInt(uid, 10)) {
|
||||||
|
return callback(null, { read: [], unread: [] });
|
||||||
|
}
|
||||||
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
getNotifications(uid, 0, 9, next);
|
||||||
|
},
|
||||||
|
function (notifications, next) {
|
||||||
notifications.read = notifications.read.filter(Boolean);
|
notifications.read = notifications.read.filter(Boolean);
|
||||||
notifications.unread = notifications.unread.filter(Boolean);
|
notifications.unread = notifications.unread.filter(Boolean);
|
||||||
|
|
||||||
@@ -28,200 +29,209 @@ var privileges = require('../privileges');
|
|||||||
notifications.read.length = maxNotifs - notifications.unread.length;
|
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) {
|
if (!filter) {
|
||||||
return setImmediate(callback, null, nids);
|
return setImmediate(callback, null, nids);
|
||||||
}
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
var keys = nids.map(function (nid) {
|
|
||||||
return 'notifications:' + nid;
|
|
||||||
});
|
|
||||||
db.getObjectsFields(keys, ['nid', 'type'], next);
|
|
||||||
},
|
|
||||||
function (notifications, next) {
|
|
||||||
nids = notifications.filter(function (notification) {
|
|
||||||
return notification && notification.nid && notification.type === filter;
|
|
||||||
}).map(function (notification) {
|
|
||||||
return notification.nid;
|
|
||||||
});
|
|
||||||
next(null, nids);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
var keys = nids.map(function (nid) {
|
||||||
|
return 'notifications:' + nid;
|
||||||
|
});
|
||||||
|
db.getObjectsFields(keys, ['nid', 'type'], next);
|
||||||
|
},
|
||||||
|
function (notifications, next) {
|
||||||
|
nids = notifications.filter(function (notification) {
|
||||||
|
return notification && notification.nid && notification.type === filter;
|
||||||
|
}).map(function (notification) {
|
||||||
|
return notification.nid;
|
||||||
|
});
|
||||||
|
next(null, nids);
|
||||||
|
},
|
||||||
|
], callback);
|
||||||
|
}
|
||||||
|
|
||||||
UserNotifications.getAll = function (uid, filter, callback) {
|
UserNotifications.getAll = function (uid, filter, callback) {
|
||||||
var nids;
|
var nids;
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
function (next) {
|
function (next) {
|
||||||
async.parallel({
|
async.parallel({
|
||||||
unread: function (next) {
|
unread: function (next) {
|
||||||
db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, -1, next);
|
db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, -1, next);
|
||||||
},
|
},
|
||||||
read: function (next) {
|
read: function (next) {
|
||||||
db.getSortedSetRevRange('uid:' + uid + ':notifications:read', 0, -1, next);
|
db.getSortedSetRevRange('uid:' + uid + ':notifications:read', 0, -1, next);
|
||||||
},
|
},
|
||||||
}, next);
|
}, next);
|
||||||
},
|
},
|
||||||
function (results, next) {
|
function (results, next) {
|
||||||
nids = results.unread.concat(results.read);
|
nids = results.unread.concat(results.read);
|
||||||
db.isSortedSetMembers('notifications', nids, next);
|
db.isSortedSetMembers('notifications', nids, next);
|
||||||
},
|
},
|
||||||
function (exists, next) {
|
function (exists, next) {
|
||||||
var deleteNids = [];
|
var deleteNids = [];
|
||||||
|
|
||||||
nids = nids.filter(function (nid, index) {
|
nids = nids.filter(function (nid, index) {
|
||||||
if (!nid || !exists[index]) {
|
if (!nid || !exists[index]) {
|
||||||
deleteNids.push(nid);
|
deleteNids.push(nid);
|
||||||
}
|
|
||||||
return nid && exists[index];
|
|
||||||
});
|
|
||||||
|
|
||||||
deleteUserNids(deleteNids, uid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
filterNotifications(nids, filter, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
function deleteUserNids(nids, uid, callback) {
|
|
||||||
callback = callback || function () {};
|
|
||||||
if (!nids.length) {
|
|
||||||
return setImmediate(callback);
|
|
||||||
}
|
|
||||||
async.parallel([
|
|
||||||
function (next) {
|
|
||||||
db.sortedSetRemove('uid:' + uid + ':notifications:read', nids, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
db.sortedSetRemove('uid:' + uid + ':notifications:unread', nids, next);
|
|
||||||
},
|
|
||||||
], function (err) {
|
|
||||||
callback(err);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getNotifications(uid, start, stop, callback) {
|
|
||||||
async.parallel({
|
|
||||||
unread: function (next) {
|
|
||||||
getNotificationsFromSet('uid:' + uid + ':notifications:unread', false, uid, start, stop, next);
|
|
||||||
},
|
|
||||||
read: function (next) {
|
|
||||||
getNotificationsFromSet('uid:' + uid + ':notifications:read', true, uid, start, stop, next);
|
|
||||||
},
|
|
||||||
}, callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getNotificationsFromSet(set, read, uid, start, stop, callback) {
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
db.getSortedSetRevRange(set, start, stop, next);
|
|
||||||
},
|
|
||||||
function (nids, next) {
|
|
||||||
if (!Array.isArray(nids) || !nids.length) {
|
|
||||||
return callback(null, []);
|
|
||||||
}
|
}
|
||||||
|
return nid && exists[index];
|
||||||
|
});
|
||||||
|
|
||||||
UserNotifications.getNotifications(nids, uid, next);
|
deleteUserNids(deleteNids, uid, next);
|
||||||
},
|
},
|
||||||
], callback);
|
function (next) {
|
||||||
|
filterNotifications(nids, filter, next);
|
||||||
|
},
|
||||||
|
], callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
function deleteUserNids(nids, uid, callback) {
|
||||||
|
callback = callback || function () {};
|
||||||
|
if (!nids.length) {
|
||||||
|
return setImmediate(callback);
|
||||||
}
|
}
|
||||||
|
async.parallel([
|
||||||
|
function (next) {
|
||||||
|
db.sortedSetRemove('uid:' + uid + ':notifications:read', nids, next);
|
||||||
|
},
|
||||||
|
function (next) {
|
||||||
|
db.sortedSetRemove('uid:' + uid + ':notifications:unread', nids, next);
|
||||||
|
},
|
||||||
|
], function (err) {
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
UserNotifications.getNotifications = function (nids, uid, callback) {
|
function getNotifications(uid, start, stop, callback) {
|
||||||
var notificationData = [];
|
async.parallel({
|
||||||
async.waterfall([
|
unread: function (next) {
|
||||||
function (next) {
|
getNotificationsFromSet('uid:' + uid + ':notifications:unread', false, uid, start, stop, next);
|
||||||
async.parallel({
|
},
|
||||||
notifications: function (next) {
|
read: function (next) {
|
||||||
notifications.getMultiple(nids, next);
|
getNotificationsFromSet('uid:' + uid + ':notifications:read', true, uid, start, stop, next);
|
||||||
},
|
},
|
||||||
hasRead: function (next) {
|
}, callback);
|
||||||
db.isSortedSetMembers('uid:' + uid + ':notifications:read', nids, next);
|
}
|
||||||
},
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
var deletedNids = [];
|
|
||||||
notificationData = results.notifications.filter(function (notification, index) {
|
|
||||||
if (!notification || !notification.nid) {
|
|
||||||
deletedNids.push(nids[index]);
|
|
||||||
}
|
|
||||||
if (notification) {
|
|
||||||
notification.read = results.hasRead[index];
|
|
||||||
notification.readClass = !notification.read ? 'unread' : '';
|
|
||||||
}
|
|
||||||
|
|
||||||
return notification && notification.path;
|
|
||||||
});
|
|
||||||
|
|
||||||
deleteUserNids(deletedNids, uid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
notifications.merge(notificationData, next);
|
|
||||||
},
|
|
||||||
], 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
function getNotificationsFromSet(set, read, uid, start, stop, callback) {
|
||||||
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
db.getSortedSetRevRange(set, start, stop, next);
|
||||||
|
},
|
||||||
|
function (nids, next) {
|
||||||
if (!Array.isArray(nids) || !nids.length) {
|
if (!Array.isArray(nids) || !nids.length) {
|
||||||
return callback(null, []);
|
return callback(null, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
UserNotifications.getNotifications(nids, uid, callback);
|
UserNotifications.getNotifications(nids, uid, next);
|
||||||
});
|
},
|
||||||
};
|
], callback);
|
||||||
|
}
|
||||||
|
|
||||||
UserNotifications.getUnreadCount = function (uid, callback) {
|
UserNotifications.getNotifications = function (nids, uid, callback) {
|
||||||
if (!parseInt(uid, 10)) {
|
var notificationData = [];
|
||||||
return callback(null, 0);
|
async.waterfall([
|
||||||
}
|
function (next) {
|
||||||
|
async.parallel({
|
||||||
|
notifications: function (next) {
|
||||||
|
notifications.getMultiple(nids, next);
|
||||||
|
},
|
||||||
|
hasRead: function (next) {
|
||||||
|
db.isSortedSetMembers('uid:' + uid + ':notifications:read', nids, next);
|
||||||
|
},
|
||||||
|
}, next);
|
||||||
|
},
|
||||||
|
function (results, next) {
|
||||||
|
var deletedNids = [];
|
||||||
|
notificationData = results.notifications.filter(function (notification, index) {
|
||||||
|
if (!notification || !notification.nid) {
|
||||||
|
deletedNids.push(nids[index]);
|
||||||
|
}
|
||||||
|
if (notification) {
|
||||||
|
notification.read = results.hasRead[index];
|
||||||
|
notification.readClass = !notification.read ? 'unread' : '';
|
||||||
|
}
|
||||||
|
|
||||||
// Collapse any notifications with identical mergeIds
|
return notification && notification.path;
|
||||||
async.waterfall([
|
});
|
||||||
async.apply(db.getSortedSetRevRange, 'uid:' + uid + ':notifications:unread', 0, 99),
|
|
||||||
async.apply(notifications.filterExists),
|
|
||||||
function (nids, next) {
|
|
||||||
var keys = nids.map(function (nid) {
|
|
||||||
return 'notifications:' + nid;
|
|
||||||
});
|
|
||||||
|
|
||||||
db.getObjectsFields(keys, ['mergeId'], next);
|
deleteUserNids(deletedNids, uid, next);
|
||||||
},
|
},
|
||||||
function (mergeIds, next) {
|
function (next) {
|
||||||
mergeIds = mergeIds.map(function (set) {
|
notifications.merge(notificationData, next);
|
||||||
return set.mergeId;
|
},
|
||||||
});
|
], callback);
|
||||||
|
};
|
||||||
|
|
||||||
next(null, mergeIds.reduce(function (count, mergeId, idx, arr) {
|
UserNotifications.getDailyUnread = function (uid, callback) {
|
||||||
// A missing (null) mergeId means that notification is counted separately.
|
var yesterday = Date.now() - (1000 * 60 * 60 * 24); // Approximate, can be more or less depending on time changes, makes no difference really.
|
||||||
if (mergeId === null || idx === arr.indexOf(mergeId)) {
|
|
||||||
count += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return count;
|
async.waterfall([
|
||||||
}, 0));
|
function (next) {
|
||||||
},
|
db.getSortedSetRevRangeByScore('uid:' + uid + ':notifications:unread', 0, 20, '+inf', yesterday, next);
|
||||||
], callback);
|
},
|
||||||
};
|
function (nids, next) {
|
||||||
|
if (!Array.isArray(nids) || !nids.length) {
|
||||||
UserNotifications.getUnreadByField = function (uid, field, values, callback) {
|
return callback(null, []);
|
||||||
db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 99, function (err, nids) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UserNotifications.getNotifications(nids, uid, next);
|
||||||
|
},
|
||||||
|
], callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
UserNotifications.getUnreadCount = function (uid, callback) {
|
||||||
|
if (!parseInt(uid, 10)) {
|
||||||
|
return callback(null, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async.waterfall([
|
||||||
|
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;
|
||||||
|
});
|
||||||
|
|
||||||
|
db.getObjectsFields(keys, ['mergeId'], next);
|
||||||
|
},
|
||||||
|
function (mergeIds, next) {
|
||||||
|
// Collapse any notifications with identical mergeIds
|
||||||
|
mergeIds = mergeIds.map(function (set) {
|
||||||
|
return set.mergeId;
|
||||||
|
});
|
||||||
|
|
||||||
|
next(null, mergeIds.reduce(function (count, mergeId, idx, arr) {
|
||||||
|
// A missing (null) mergeId means that notification is counted separately.
|
||||||
|
if (mergeId === null || idx === arr.indexOf(mergeId)) {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}, 0));
|
||||||
|
},
|
||||||
|
], callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
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) {
|
if (!Array.isArray(nids) || !nids.length) {
|
||||||
return callback(null, []);
|
return callback(null, []);
|
||||||
}
|
}
|
||||||
@@ -230,124 +240,125 @@ var privileges = require('../privileges');
|
|||||||
return 'notifications:' + nid;
|
return 'notifications:' + nid;
|
||||||
});
|
});
|
||||||
|
|
||||||
db.getObjectsFields(keys, ['nid', field], function (err, notifications) {
|
db.getObjectsFields(keys, ['nid', field], next);
|
||||||
if (err) {
|
},
|
||||||
return callback(err);
|
function (notifications, next) {
|
||||||
}
|
values = values.map(function () { return values.toString(); });
|
||||||
|
nids = notifications.filter(function (notification) {
|
||||||
values = values.map(function () { return values.toString(); });
|
return notification && notification[field] && values.indexOf(notification[field].toString()) !== -1;
|
||||||
nids = notifications.filter(function (notification) {
|
}).map(function (notification) {
|
||||||
return notification && notification[field] && values.indexOf(notification[field].toString()) !== -1;
|
return notification.nid;
|
||||||
}).map(function (notification) {
|
|
||||||
return notification.nid;
|
|
||||||
});
|
|
||||||
|
|
||||||
callback(null, nids);
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
UserNotifications.deleteAll = function (uid, callback) {
|
next(null, nids);
|
||||||
if (!parseInt(uid, 10)) {
|
},
|
||||||
return callback();
|
], callback);
|
||||||
}
|
};
|
||||||
async.parallel([
|
|
||||||
function (next) {
|
|
||||||
db.delete('uid:' + uid + ':notifications:unread', next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
db.delete('uid:' + uid + ':notifications:read', next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
UserNotifications.sendTopicNotificationToFollowers = function (uid, topicData, postData) {
|
UserNotifications.deleteAll = function (uid, callback) {
|
||||||
var followers;
|
if (!parseInt(uid, 10)) {
|
||||||
async.waterfall([
|
return callback();
|
||||||
function (next) {
|
}
|
||||||
db.getSortedSetRange('followers:' + uid, 0, -1, next);
|
async.parallel([
|
||||||
},
|
function (next) {
|
||||||
function (followers, next) {
|
db.delete('uid:' + uid + ':notifications:unread', next);
|
||||||
if (!Array.isArray(followers) || !followers.length) {
|
},
|
||||||
return;
|
function (next) {
|
||||||
}
|
db.delete('uid:' + uid + ':notifications:read', next);
|
||||||
privileges.categories.filterUids('read', topicData.cid, followers, next);
|
},
|
||||||
},
|
], callback);
|
||||||
function (_followers, next) {
|
};
|
||||||
followers = _followers;
|
|
||||||
if (!followers.length) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var title = topicData.title;
|
UserNotifications.sendTopicNotificationToFollowers = function (uid, topicData, postData) {
|
||||||
if (title) {
|
var followers;
|
||||||
title = S(title).decodeHTMLEntities().s;
|
async.waterfall([
|
||||||
}
|
function (next) {
|
||||||
|
db.getSortedSetRange('followers:' + uid, 0, -1, next);
|
||||||
notifications.create({
|
},
|
||||||
type: 'new-topic',
|
function (followers, next) {
|
||||||
bodyShort: '[[notifications:user_posted_topic, ' + postData.user.username + ', ' + title + ']]',
|
if (!Array.isArray(followers) || !followers.length) {
|
||||||
bodyLong: postData.content,
|
return;
|
||||||
pid: postData.pid,
|
}
|
||||||
path: '/post/' + postData.pid,
|
privileges.categories.filterUids('read', topicData.cid, followers, next);
|
||||||
nid: 'tid:' + postData.tid + ':uid:' + uid,
|
},
|
||||||
tid: postData.tid,
|
function (_followers, next) {
|
||||||
from: uid,
|
followers = _followers;
|
||||||
}, next);
|
if (!followers.length) {
|
||||||
},
|
return;
|
||||||
], function (err, notification) {
|
|
||||||
if (err) {
|
|
||||||
return winston.error(err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (notification) {
|
var title = topicData.title;
|
||||||
notifications.push(notification, followers);
|
if (title) {
|
||||||
|
title = S(title).decodeHTMLEntities().s;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
UserNotifications.sendWelcomeNotification = function (uid, callback) {
|
notifications.create({
|
||||||
callback = callback || function () {};
|
type: 'new-topic',
|
||||||
if (!meta.config.welcomeNotification) {
|
bodyShort: '[[notifications:user_posted_topic, ' + postData.user.username + ', ' + title + ']]',
|
||||||
return callback();
|
bodyLong: postData.content,
|
||||||
|
pid: postData.pid,
|
||||||
|
path: '/post/' + postData.pid,
|
||||||
|
nid: 'tid:' + postData.tid + ':uid:' + uid,
|
||||||
|
tid: postData.tid,
|
||||||
|
from: uid,
|
||||||
|
}, next);
|
||||||
|
},
|
||||||
|
], function (err, notification) {
|
||||||
|
if (err) {
|
||||||
|
return winston.error(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
var path = meta.config.welcomeLink ? meta.config.welcomeLink : '#';
|
if (notification) {
|
||||||
|
notifications.push(notification, followers);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
notifications.create({
|
UserNotifications.sendWelcomeNotification = function (uid, callback) {
|
||||||
bodyShort: meta.config.welcomeNotification,
|
callback = callback || function () {};
|
||||||
path: path,
|
if (!meta.config.welcomeNotification) {
|
||||||
nid: 'welcome_' + uid,
|
return callback();
|
||||||
}, function (err, notification) {
|
}
|
||||||
if (err || !notification) {
|
|
||||||
return callback(err);
|
var path = meta.config.welcomeLink ? meta.config.welcomeLink : '#';
|
||||||
|
|
||||||
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
notifications.create({
|
||||||
|
bodyShort: meta.config.welcomeNotification,
|
||||||
|
path: path,
|
||||||
|
nid: 'welcome_' + uid,
|
||||||
|
}, next);
|
||||||
|
},
|
||||||
|
function (notification, next) {
|
||||||
|
if (!notification) {
|
||||||
|
return next();
|
||||||
}
|
}
|
||||||
|
notifications.push(notification, [uid], next);
|
||||||
|
},
|
||||||
|
], callback);
|
||||||
|
};
|
||||||
|
|
||||||
notifications.push(notification, [uid], callback);
|
UserNotifications.sendNameChangeNotification = function (uid, username) {
|
||||||
});
|
notifications.create({
|
||||||
};
|
bodyShort: '[[user:username_taken_workaround, ' + username + ']]',
|
||||||
|
image: 'brand:logo',
|
||||||
|
nid: 'username_taken:' + uid,
|
||||||
|
datetime: Date.now(),
|
||||||
|
}, function (err, notification) {
|
||||||
|
if (!err && notification) {
|
||||||
|
notifications.push(notification, uid);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
UserNotifications.sendNameChangeNotification = function (uid, username) {
|
UserNotifications.pushCount = function (uid) {
|
||||||
notifications.create({
|
var websockets = require('./../socket.io');
|
||||||
bodyShort: '[[user:username_taken_workaround, ' + username + ']]',
|
UserNotifications.getUnreadCount(uid, function (err, count) {
|
||||||
image: 'brand:logo',
|
if (err) {
|
||||||
nid: 'username_taken:' + uid,
|
return winston.error(err.stack);
|
||||||
datetime: Date.now(),
|
}
|
||||||
}, function (err, notification) {
|
|
||||||
if (!err && notification) {
|
|
||||||
notifications.push(notification, uid);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
UserNotifications.pushCount = function (uid) {
|
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
|
||||||
var websockets = require('./../socket.io');
|
});
|
||||||
UserNotifications.getUnreadCount(uid, function (err, count) {
|
};
|
||||||
if (err) {
|
|
||||||
return winston.error(err.stack);
|
|
||||||
}
|
|
||||||
|
|
||||||
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}(exports));
|
|
||||||
|
|||||||
@@ -5,7 +5,11 @@ var assert = require('assert');
|
|||||||
var async = require('async');
|
var async = require('async');
|
||||||
|
|
||||||
var db = require('./mocks/databasemock');
|
var db = require('./mocks/databasemock');
|
||||||
|
var meta = require('../src/meta');
|
||||||
var user = require('../src/user');
|
var user = require('../src/user');
|
||||||
|
var topics = require('../src/topics');
|
||||||
|
var categories = require('../src/categories');
|
||||||
|
var groups = require('../src/groups');
|
||||||
var notifications = require('../src/notifications');
|
var notifications = require('../src/notifications');
|
||||||
var socketNotifications = require('../src/socket.io/notifications');
|
var socketNotifications = require('../src/socket.io/notifications');
|
||||||
|
|
||||||
@@ -14,6 +18,7 @@ describe('Notifications', function () {
|
|||||||
var notification;
|
var notification;
|
||||||
|
|
||||||
before(function (done) {
|
before(function (done) {
|
||||||
|
groups.resetCache();
|
||||||
user.create({ username: 'poster' }, function (err, _uid) {
|
user.create({ username: 'poster' }, function (err, _uid) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return done(err);
|
return done(err);
|
||||||
@@ -329,6 +334,123 @@ describe('Notifications', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should return empty with falsy uid', function (done) {
|
||||||
|
user.notifications.get(0, function (err, data) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.equal(data.read.length, 0);
|
||||||
|
assert.equal(data.unread.length, 0);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should get all notifications and filter', function (done) {
|
||||||
|
var nid = 'willbefiltered';
|
||||||
|
notifications.create({
|
||||||
|
bodyShort: 'bodyShort',
|
||||||
|
nid: nid,
|
||||||
|
path: '/notification/path',
|
||||||
|
type: 'post',
|
||||||
|
}, function (err, notification) {
|
||||||
|
assert.ifError(err);
|
||||||
|
notifications.push(notification, [uid], function (err) {
|
||||||
|
assert.ifError(err);
|
||||||
|
setTimeout(function () {
|
||||||
|
user.notifications.getAll(uid, 'post', function (err, nids) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.notEqual(nids.indexOf(nid), -1);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
}, 1500);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not get anything if notifications does not exist', function (done) {
|
||||||
|
user.notifications.getNotifications(['doesnotexistnid1', 'doesnotexistnid2'], uid, function (err, data) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.deepEqual(data, []);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should get daily notifications', function (done) {
|
||||||
|
user.notifications.getDailyUnread(uid, function (err, data) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.equal(data[0].nid, 'willbefiltered');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return 0 for falsy uid', function (done) {
|
||||||
|
user.notifications.getUnreadCount(0, function (err, count) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.equal(count, 0);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not do anything if uid is falsy', function (done) {
|
||||||
|
user.notifications.deleteAll(0, function (err) {
|
||||||
|
assert.ifError(err);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should send notification to followers of user when he posts', function (done) {
|
||||||
|
var followerUid;
|
||||||
|
async.waterfall([
|
||||||
|
function (next) {
|
||||||
|
user.create({ username: 'follower' }, next);
|
||||||
|
},
|
||||||
|
function (_followerUid, next) {
|
||||||
|
followerUid = _followerUid;
|
||||||
|
user.follow(followerUid, uid, next);
|
||||||
|
},
|
||||||
|
function (next) {
|
||||||
|
categories.create({
|
||||||
|
name: 'Test Category',
|
||||||
|
description: 'Test category created by testing script',
|
||||||
|
}, next);
|
||||||
|
},
|
||||||
|
function (category, next) {
|
||||||
|
topics.post({
|
||||||
|
uid: uid,
|
||||||
|
cid: category.cid,
|
||||||
|
title: 'Test Topic Title',
|
||||||
|
content: 'The content of test topic',
|
||||||
|
}, next);
|
||||||
|
},
|
||||||
|
function (data, next) {
|
||||||
|
setTimeout(next, 1100);
|
||||||
|
},
|
||||||
|
function (next) {
|
||||||
|
user.notifications.getAll(followerUid, '', next);
|
||||||
|
},
|
||||||
|
], function (err, data) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert(data);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should send welcome notification', function (done) {
|
||||||
|
meta.config.welcomeNotification = 'welcome to the forums';
|
||||||
|
user.notifications.sendWelcomeNotification(uid, function (err) {
|
||||||
|
assert.ifError(err);
|
||||||
|
user.notifications.sendWelcomeNotification(uid, function (err) {
|
||||||
|
assert.ifError(err);
|
||||||
|
setTimeout(function () {
|
||||||
|
user.notifications.getAll(uid, '', function (err, data) {
|
||||||
|
meta.config.welcomeNotification = '';
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.notEqual(data.indexOf('welcome_' + uid), -1);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
}, 1100);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should prune notifications', function (done) {
|
it('should prune notifications', function (done) {
|
||||||
notifications.create({
|
notifications.create({
|
||||||
bodyShort: 'bodyShort',
|
bodyShort: 'bodyShort',
|
||||||
|
|||||||
Reference in New Issue
Block a user