mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 12:05:57 +01:00
notifications tests
This commit is contained in:
@@ -34,7 +34,7 @@ module.exports = function (Messaging) {
|
||||
function (roomData, next) {
|
||||
modifyRoomData(roomData);
|
||||
next(null, roomData);
|
||||
}
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ Notifications.get = function (nid, callback) {
|
||||
};
|
||||
|
||||
Notifications.getMultiple = function (nids, callback) {
|
||||
if (!nids.length) {
|
||||
if (!Array.isArray(nids) || !nids.length) {
|
||||
return setImmediate(callback, null, []);
|
||||
}
|
||||
var keys = nids.map(function (nid) {
|
||||
@@ -106,37 +106,33 @@ Notifications.findRelated = function (mergeIds, set, callback) {
|
||||
|
||||
db.getObjectsFields(keys, ['mergeId'], next);
|
||||
},
|
||||
], function (err, sets) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
function (sets, next) {
|
||||
sets = sets.map(function (set) {
|
||||
return set.mergeId;
|
||||
});
|
||||
|
||||
callback(null, _nids.filter(function (nid, idx) {
|
||||
next(null, _nids.filter(function (nid, idx) {
|
||||
return mergeIds.indexOf(sets[idx]) !== -1;
|
||||
}));
|
||||
});
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
Notifications.create = function (data, callback) {
|
||||
if (!data.nid) {
|
||||
return callback(new Error('no-notification-id'));
|
||||
return callback(new Error('[[error:no-notification-id]]'));
|
||||
}
|
||||
data.importance = data.importance || 5;
|
||||
db.getObject('notifications:' + data.nid, function (err, oldNotification) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getObject('notifications:' + data.nid, next);
|
||||
},
|
||||
function (oldNotification, next) {
|
||||
if (oldNotification) {
|
||||
if (parseInt(oldNotification.pid, 10) === parseInt(data.pid, 10) && parseInt(oldNotification.importance, 10) > parseInt(data.importance, 10)) {
|
||||
return callback(null, null);
|
||||
}
|
||||
}
|
||||
|
||||
var now = Date.now();
|
||||
data.datetime = now;
|
||||
async.parallel([
|
||||
@@ -147,9 +143,10 @@ Notifications.create = function (data, callback) {
|
||||
db.setObject('notifications:' + data.nid, data, next);
|
||||
},
|
||||
], function (err) {
|
||||
callback(err, data);
|
||||
});
|
||||
next(err, data);
|
||||
});
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
Notifications.push = function (notification, uids, callback) {
|
||||
@@ -233,25 +230,31 @@ function pushToUids(uids, notification, callback) {
|
||||
|
||||
Notifications.pushGroup = function (notification, groupName, callback) {
|
||||
callback = callback || function () {};
|
||||
groups.getMembers(groupName, 0, -1, function (err, members) {
|
||||
if (err || !Array.isArray(members) || !members.length) {
|
||||
return callback(err);
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
groups.getMembers(groupName, 0, -1, next);
|
||||
},
|
||||
function (members, next) {
|
||||
if (!Array.isArray(members) || !members.length) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
Notifications.push(notification, members, callback);
|
||||
});
|
||||
Notifications.push(notification, members, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
Notifications.pushGroups = function (notification, groupNames, callback) {
|
||||
callback = callback || function () {};
|
||||
groups.getMembersOfGroups(groupNames, function (err, groupMembers) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
groups.getMembersOfGroups(groupNames, next);
|
||||
},
|
||||
function (groupMembers, next) {
|
||||
var members = _.unique(_.flatten(groupMembers));
|
||||
Notifications.push(notification, members, callback);
|
||||
});
|
||||
Notifications.push(notification, members, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
Notifications.rescind = function (nid, callback) {
|
||||
@@ -261,13 +264,7 @@ Notifications.rescind = function (nid, callback) {
|
||||
async.apply(db.sortedSetRemove, 'notifications', nid),
|
||||
async.apply(db.delete, 'notifications:' + nid),
|
||||
], function (err) {
|
||||
if (err) {
|
||||
winston.error('Encountered error rescinding notification (' + nid + '): ' + err.message);
|
||||
} else {
|
||||
winston.verbose('[notifications/rescind] Rescinded notification "' + nid + '"');
|
||||
}
|
||||
|
||||
callback(err, nid);
|
||||
callback(err);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -284,18 +281,22 @@ Notifications.markUnread = function (nid, uid, callback) {
|
||||
if (!parseInt(uid, 10) || !nid) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
db.getObject('notifications:' + nid, function (err, notification) {
|
||||
if (err || !notification) {
|
||||
return callback(err || new Error('[[error:no-notification]]'));
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getObject('notifications:' + nid, next);
|
||||
},
|
||||
function (notification, next) {
|
||||
if (!notification) {
|
||||
return callback(new Error('[[error:no-notification]]'));
|
||||
}
|
||||
notification.datetime = notification.datetime || Date.now();
|
||||
|
||||
async.parallel([
|
||||
async.apply(db.sortedSetRemove, 'uid:' + uid + ':notifications:read', nid),
|
||||
async.apply(db.sortedSetAdd, 'uid:' + uid + ':notifications:unread', notification.datetime, nid),
|
||||
], next);
|
||||
},
|
||||
], callback);
|
||||
});
|
||||
};
|
||||
|
||||
Notifications.markReadMultiple = function (nids, uid, callback) {
|
||||
|
||||
@@ -169,21 +169,26 @@ SocketHelpers.sendNotificationToTopicOwner = function (tid, fromuid, command, no
|
||||
};
|
||||
|
||||
SocketHelpers.rescindUpvoteNotification = function (pid, fromuid) {
|
||||
var nid = 'upvote:post:' + pid + ':uid:' + fromuid;
|
||||
notifications.rescind(nid);
|
||||
|
||||
posts.getPostField(pid, 'uid', function (err, uid) {
|
||||
if (err) {
|
||||
return winston.error(err);
|
||||
}
|
||||
|
||||
user.notifications.getUnreadCount(uid, function (err, count) {
|
||||
if (err) {
|
||||
return winston.error(err);
|
||||
}
|
||||
|
||||
var uid;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
notifications.rescind('upvote:post:' + pid + ':uid:' + fromuid, next);
|
||||
},
|
||||
function (next) {
|
||||
posts.getPostField(pid, 'uid', next);
|
||||
},
|
||||
function (_uid, next) {
|
||||
uid = _uid;
|
||||
user.notifications.getUnreadCount(uid, next);
|
||||
},
|
||||
function (count, next) {
|
||||
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
|
||||
});
|
||||
next();
|
||||
},
|
||||
], function (err) {
|
||||
if (err) {
|
||||
winston.error(err);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -24,11 +24,19 @@ describe('Notifications', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail to create notification without a nid', function (done) {
|
||||
notifications.create({}, function (err) {
|
||||
assert.equal(err.message, '[[error:no-notification-id]]');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should create a notification', function (done) {
|
||||
notifications.create({
|
||||
bodyShort: 'bodyShort',
|
||||
nid: 'notification_id',
|
||||
path: '/notification/path',
|
||||
pid: 1,
|
||||
}, function (err, _notification) {
|
||||
notification = _notification;
|
||||
assert.ifError(err);
|
||||
@@ -45,6 +53,29 @@ describe('Notifications', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('should return null if pid is same and importance is lower', function (done) {
|
||||
notifications.create({
|
||||
bodyShort: 'bodyShort',
|
||||
nid: 'notification_id',
|
||||
path: '/notification/path',
|
||||
pid: 1,
|
||||
importance: 1,
|
||||
}, function (err, notification) {
|
||||
assert.ifError(err);
|
||||
assert.strictEqual(notification, null);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should get empty array', function (done) {
|
||||
notifications.getMultiple(null, function (err, data) {
|
||||
assert.ifError(err);
|
||||
assert(Array.isArray(data));
|
||||
assert.equal(data.length, 0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should get notifications', function (done) {
|
||||
notifications.getMultiple([notification.nid], function (err, notificationsData) {
|
||||
assert.ifError(err);
|
||||
@@ -55,6 +86,19 @@ describe('Notifications', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('should do nothing', function (done) {
|
||||
notifications.push(null, [], function (err) {
|
||||
assert.ifError(err);
|
||||
notifications.push({ nid: null }, [], function (err) {
|
||||
assert.ifError(err);
|
||||
notifications.push(notification, [], function (err) {
|
||||
assert.ifError(err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should push a notification to uid', function (done) {
|
||||
notifications.push(notification, [uid], function (err) {
|
||||
assert.ifError(err);
|
||||
@@ -94,6 +138,16 @@ describe('Notifications', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('should not mark anything with invalid uid or nid', function (done) {
|
||||
socketNotifications.markRead({ uid: null }, null, function (err) {
|
||||
assert.ifError(err);
|
||||
socketNotifications.markRead({ uid: uid }, null, function (err) {
|
||||
assert.ifError(err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should mark a notification read', function (done) {
|
||||
socketNotifications.markRead({ uid: uid }, notification.nid, function (err) {
|
||||
assert.ifError(err);
|
||||
@@ -109,6 +163,23 @@ describe('Notifications', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('should not mark anything with invalid uid or nid', function (done) {
|
||||
socketNotifications.markUnread({ uid: null }, null, function (err) {
|
||||
assert.ifError(err);
|
||||
socketNotifications.markUnread({ uid: uid }, null, function (err) {
|
||||
assert.ifError(err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should error if notification does not exist', function (done) {
|
||||
socketNotifications.markUnread({ uid: uid }, 123123, function (err) {
|
||||
assert.equal(err.message, '[[error:no-notification]]');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should mark a notification unread', function (done) {
|
||||
socketNotifications.markUnread({ uid: uid }, notification.nid, function (err) {
|
||||
assert.ifError(err);
|
||||
@@ -143,6 +214,13 @@ describe('Notifications', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('should not do anything', function (done) {
|
||||
socketNotifications.markAllRead({ uid: 1000 }, null, function (err) {
|
||||
assert.ifError(err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should link to the first unread post in a watched topic', function (done) {
|
||||
var categories = require('../src/categories');
|
||||
var topics = require('../src/topics');
|
||||
@@ -256,9 +334,23 @@ describe('Notifications', function () {
|
||||
bodyShort: 'bodyShort',
|
||||
nid: 'tobedeleted',
|
||||
path: '/notification/path',
|
||||
}, function (err) {
|
||||
}, function (err, notification) {
|
||||
assert.ifError(err);
|
||||
notifications.prune(done);
|
||||
notifications.prune(function (err) {
|
||||
assert.ifError(err);
|
||||
var week = 604800000;
|
||||
db.sortedSetAdd('notifications', Date.now() - (2 * week), notification.nid, function (err) {
|
||||
assert.ifError(err);
|
||||
notifications.prune(function (err) {
|
||||
assert.ifError(err);
|
||||
notifications.get(notification.nid, function (err, data) {
|
||||
assert.ifError(err);
|
||||
assert(!data);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user