topics unread tests

v1.18.x
barisusakli 8 years ago
parent 444da3c9ec
commit 49267d54b7

@ -33,6 +33,9 @@ module.exports = function (Categories) {
}; };
Categories.markAsUnreadForAll = function (cid, callback) { Categories.markAsUnreadForAll = function (cid, callback) {
if (!parseInt(cid, 10)) {
return callback();
}
callback = callback || function () {}; callback = callback || function () {};
db.delete('cid:' + cid + ':read_by_uid', callback); db.delete('cid:' + cid + ':read_by_uid', callback);
}; };

@ -352,7 +352,9 @@ var utils = require('../public/src/utils');
function (next) { function (next) {
db.sortedSetAdd('uid:' + uid + ':notifications:read', datetimes, nids, next); db.sortedSetAdd('uid:' + uid + ':notifications:read', datetimes, nids, next);
} }
], callback); ], function (err) {
callback(err);
});
}); });
}; };

@ -44,7 +44,7 @@ SocketPosts.reply = function (socket, data, callback) {
callback(null, postData); callback(null, postData);
socket.emit('event:new_post', result); websockets.in('uid_' + socket.uid).emit('event:new_post', result);
user.updateOnlineUsers(socket.uid); user.updateOnlineUsers(socket.uid);

@ -11,61 +11,67 @@ module.exports = function (SocketTopics) {
if (!Array.isArray(tids) || !socket.uid) { if (!Array.isArray(tids) || !socket.uid) {
return callback(new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
} }
async.waterfall([
topics.markAsRead(tids, socket.uid, function (err) { function (next) {
if (err) { topics.markAsRead(tids, socket.uid, next);
return callback(err); },
function (hasMarked, next) {
if (hasMarked) {
topics.pushUnreadCount(socket.uid);
topics.markTopicNotificationsRead(tids, socket.uid);
}
next();
} }
], callback);
topics.pushUnreadCount(socket.uid);
topics.markTopicNotificationsRead(tids, socket.uid);
callback();
});
}; };
SocketTopics.markTopicNotificationsRead = function (socket, tids, callback) { SocketTopics.markTopicNotificationsRead = function (socket, tids, callback) {
if (!Array.isArray(tids) || !socket.uid) { if (!Array.isArray(tids) || !socket.uid) {
return callback(new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
} }
topics.markTopicNotificationsRead(tids, socket.uid); topics.markTopicNotificationsRead(tids, socket.uid, callback);
}; };
SocketTopics.markAllRead = function (socket, data, callback) { SocketTopics.markAllRead = function (socket, data, callback) {
topics.markAllRead(socket.uid, function (err) { if (!socket.uid) {
if (err) { return callback(new Error('[[error:invalid-uid]]'));
return callback(err); }
async.waterfall([
function (next) {
topics.markAllRead(socket.uid, next);
},
function (next) {
topics.pushUnreadCount(socket.uid);
next();
} }
], callback);
topics.pushUnreadCount(socket.uid);
callback();
});
}; };
SocketTopics.markCategoryTopicsRead = function (socket, cid, callback) { SocketTopics.markCategoryTopicsRead = function (socket, cid, callback) {
topics.getUnreadTids(cid, socket.uid, '', function (err, tids) { async.waterfall([
if (err) { function (next) {
return callback(err); topics.getUnreadTids(cid, socket.uid, '', next);
},
function (tids, next) {
SocketTopics.markAsRead(socket, tids, next);
} }
], callback);
SocketTopics.markAsRead(socket, tids, callback);
});
}; };
SocketTopics.markUnread = function (socket, tid, callback) { SocketTopics.markUnread = function (socket, tid, callback) {
if (!tid || !socket.uid) { if (!tid || !socket.uid) {
return callback(new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
} }
topics.markUnread(tid, socket.uid, function (err) { async.waterfall([
if (err) { function (next) {
return callback(err); topics.markUnread(tid, socket.uid, next);
},
function (next) {
topics.pushUnreadCount(socket.uid);
next();
} }
], callback);
topics.pushUnreadCount(socket.uid);
callback();
});
}; };
SocketTopics.markAsUnreadForAll = function (socket, tids, callback) { SocketTopics.markAsUnreadForAll = function (socket, tids, callback) {
@ -77,42 +83,41 @@ module.exports = function (SocketTopics) {
return callback(new Error('[[error:no-privileges]]')); return callback(new Error('[[error:no-privileges]]'));
} }
user.isAdministrator(socket.uid, function (err, isAdmin) { async.waterfall([
if (err) { function (next) {
return callback(err); user.isAdministrator(socket.uid, next);
} },
function (isAdmin, next) {
async.each(tids, function (tid, next) { async.each(tids, function (tid, next) {
async.waterfall([ async.waterfall([
function (next) { function (next) {
topics.exists(tid, next); topics.exists(tid, next);
}, },
function (exists, next) { function (exists, next) {
if (!exists) { if (!exists) {
return next(new Error('[[error:invalid-tid]]')); return next(new Error('[[error:no-topic]]'));
} }
topics.getTopicField(tid, 'cid', next); topics.getTopicField(tid, 'cid', next);
}, },
function (cid, next) { function (cid, next) {
user.isModerator(socket.uid, cid, next); user.isModerator(socket.uid, cid, next);
}, },
function (isMod, next) { function (isMod, next) {
if (!isAdmin && !isMod) { if (!isAdmin && !isMod) {
return next(new Error('[[error:no-privileges]]')); return next(new Error('[[error:no-privileges]]'));
}
topics.markAsUnreadForAll(tid, next);
},
function (next) {
topics.updateRecent(tid, Date.now(), next);
} }
topics.markAsUnreadForAll(tid, next); ], next);
}, }, next);
function (next) { },
topics.updateRecent(tid, Date.now(), next); function (next) {
}
], next);
}, function (err) {
if (err) {
return callback(err);
}
topics.pushUnreadCount(socket.uid); topics.pushUnreadCount(socket.uid);
callback(); next();
}); }
}); ], callback);
}; };
}; };

@ -74,7 +74,10 @@ module.exports = function (Topics) {
function (next) { function (next) {
method2(tid, uid, next); method2(tid, uid, next);
}, },
async.apply(plugins.fireHook, hook, {uid: uid, tid: tid}) function (next) {
plugins.fireHook(hook, {uid: uid, tid: tid});
next();
}
], callback); ], callback);
} }

@ -2,7 +2,6 @@
'use strict'; 'use strict';
var async = require('async'); var async = require('async');
var winston = require('winston');
var db = require('../database'); var db = require('../database');
var user = require('../user'); var user = require('../user');
@ -277,9 +276,10 @@ module.exports = function (Topics) {
], callback); ], callback);
}; };
Topics.markTopicNotificationsRead = function (tids, uid) { Topics.markTopicNotificationsRead = function (tids, uid, callback) {
callback = callback || function () {};
if (!Array.isArray(tids) || !tids.length) { if (!Array.isArray(tids) || !tids.length) {
return; return callback();
} }
async.waterfall([ async.waterfall([
@ -288,23 +288,23 @@ module.exports = function (Topics) {
}, },
function (nids, next) { function (nids, next) {
notifications.markReadMultiple(nids, uid, next); notifications.markReadMultiple(nids, uid, next);
},
function (next) {
user.notifications.pushCount(uid);
next();
} }
], function (err) { ], callback);
if (err) {
return winston.error(err);
}
user.notifications.pushCount(uid);
});
}; };
Topics.markCategoryUnreadForAll = function (tid, callback) { Topics.markCategoryUnreadForAll = function (tid, callback) {
Topics.getTopicField(tid, 'cid', function (err, cid) { async.waterfall([
if(err) { function (next) {
return callback(err); Topics.getTopicField(tid, 'cid', next);
},
function (cid, next) {
categories.markAsUnreadForAll(cid, next);
} }
], callback);
categories.markAsUnreadForAll(cid, callback);
});
}; };
Topics.hasReadTopics = function (tids, uid, callback) { Topics.hasReadTopics = function (tids, uid, callback) {

@ -110,6 +110,12 @@
enableDefaultPlugins(next); enableDefaultPlugins(next);
}, },
function (next) {
meta.themes.set({
type: 'local',
id: 'nodebb-theme-persona'
}, next);
},
function (next) { function (next) {
// nconf defaults, if not set in config // nconf defaults, if not set in config
if (!nconf.get('upload_path')) { if (!nconf.get('upload_path')) {

@ -683,6 +683,200 @@ describe('Topic\'s', function () {
}); });
}); });
describe('unread', function () {
var socketTopics = require('../src/socket.io/topics');
var tid;
var mainPid;
var uid;
before(function (done) {
async.parallel({
topic: function (next) {
topics.post({uid: topic.userId, title: 'unread topic', content: 'unread topic content', cid: topic.categoryId}, next);
},
user: function (next) {
User.create({username: 'regularJoe'}, next);
}
}, function (err, results) {
assert.ifError(err);
tid = results.topic.topicData.tid;
mainPid = results.topic.postData.pid;
uid = results.user;
done();
});
});
it('should fail with invalid data', function (done) {
socketTopics.markUnread({uid: adminUid}, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
it('should fail if topic does not exist', function (done) {
socketTopics.markUnread({uid: adminUid}, 1231082, function (err) {
assert.equal(err.message, '[[error:no-topic]]');
done();
});
});
it('should mark topic unread', function (done) {
socketTopics.markUnread({uid: adminUid}, tid, function (err) {
assert.ifError(err);
topics.hasReadTopic(tid, adminUid, function (err, hasRead) {
assert.ifError(err);
assert.equal(hasRead, false);
done();
});
});
});
it('should fail with invalid data', function (done) {
socketTopics.markAsRead({uid: 0}, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
it('should mark topic read', function (done) {
socketTopics.markAsRead({uid: adminUid}, [tid], function (err) {
assert.ifError(err);
topics.hasReadTopic(tid, adminUid, function (err, hasRead) {
assert.ifError(err);
assert(hasRead);
done();
});
});
});
it('should fail with invalid data', function (done) {
socketTopics.markTopicNotificationsRead({uid: 0}, null, function (err) {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
it('should mark topic notifications read', function (done) {
var socketPosts = require('../src/socket.io/posts');
async.waterfall([
function (next) {
socketTopics.follow({uid: adminUid}, tid, next);
},
function (next) {
socketPosts.reply({uid: uid}, {content: 'some content', tid: tid}, next);
},
function (data, next) {
setTimeout(next, 2500);
},
function (next) {
User.notifications.getUnreadCount(adminUid, next);
},
function (count, next) {
assert.equal(count, 1);
socketTopics.markTopicNotificationsRead({uid: adminUid}, [tid], next);
},
function (next) {
User.notifications.getUnreadCount(adminUid, next);
},
function (count, next) {
assert.equal(count, 0);
next();
}
], function (err) {
assert.ifError(err);
done();
});
});
it('should fail with invalid data', function (done) {
socketTopics.markAllRead({uid: 0}, null, function (err) {
assert.equal(err.message, '[[error:invalid-uid]]');
done();
});
});
it('should mark all read', function (done) {
socketTopics.markUnread({uid: adminUid}, tid, function (err) {
assert.ifError(err);
socketTopics.markAllRead({uid: adminUid}, {}, function (err) {
assert.ifError(err);
topics.hasReadTopic(tid, adminUid, function (err, hasRead) {
assert.ifError(err);
assert(hasRead);
done();
});
});
});
});
it('should mark all read', function (done) {
socketTopics.markUnread({uid: adminUid}, tid, function (err) {
assert.ifError(err);
socketTopics.markCategoryTopicsRead({uid: adminUid}, topic.categoryId, function (err) {
assert.ifError(err);
topics.hasReadTopic(tid, adminUid, function (err, hasRead) {
assert.ifError(err);
assert(hasRead);
done();
});
});
});
});
it('should fail with invalid data', function (done) {
socketTopics.markAsUnreadForAll({uid: adminUid}, null, function (err) {
assert.equal(err.message, '[[error:invalid-tid]]');
done();
});
});
it('should fail with invalid data', function (done) {
socketTopics.markAsUnreadForAll({uid: 0}, [tid], function (err) {
assert.equal(err.message, '[[error:no-privileges]]');
done();
});
});
it('should fail if user is not admin', function (done) {
socketTopics.markAsUnreadForAll({uid: uid}, [tid], function (err) {
assert.equal(err.message, '[[error:no-privileges]]');
done();
});
});
it('should fail if topic does not exist', function (done) {
socketTopics.markAsUnreadForAll({uid: uid}, [12312313], function (err) {
assert.equal(err.message, '[[error:no-topic]]');
done();
});
});
it('should mark topic unread for everyone', function (done) {
socketTopics.markAsUnreadForAll({uid: adminUid}, [tid], function (err) {
assert.ifError(err);
async.parallel({
adminRead: function (next) {
topics.hasReadTopic(tid, adminUid, next);
},
regularRead: function (next) {
topics.hasReadTopic(tid, uid, next);
}
}, function (err, results) {
assert.ifError(err);
assert.equal(results.adminRead, false);
assert.equal(results.regularRead, false);
done();
});
});
});
});
after(function (done) { after(function (done) {

Loading…
Cancel
Save