mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-06 15:42:52 +01:00
inf scroll tests for topics
This commit is contained in:
@@ -14,78 +14,80 @@ module.exports = function (SocketTopics) {
|
||||
if (!data || !data.tid || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
var userPrivileges;
|
||||
|
||||
async.parallel({
|
||||
privileges: function (next) {
|
||||
privileges.topics.get(data.tid, socket.uid, next);
|
||||
},
|
||||
topic: function (next) {
|
||||
topics.getTopicFields(data.tid, ['postcount', 'deleted'], next);
|
||||
}
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (!results.privileges.read || (parseInt(results.topic.deleted, 10) && !results.privileges.view_deleted)) {
|
||||
return callback(new Error('[[error:no-privileges]]'));
|
||||
}
|
||||
|
||||
var set = 'tid:' + data.tid + ':posts';
|
||||
if (data.topicPostSort === 'most_votes') {
|
||||
set = 'tid:' + data.tid + ':posts:votes';
|
||||
}
|
||||
var reverse = data.topicPostSort === 'newest_to_oldest' || data.topicPostSort === 'most_votes';
|
||||
var start = Math.max(0, parseInt(data.after, 10));
|
||||
|
||||
var infScrollPostsPerPage = 10;
|
||||
|
||||
if (data.direction > 0) {
|
||||
if (reverse) {
|
||||
start = results.topic.postcount - start;
|
||||
}
|
||||
} else {
|
||||
if (reverse) {
|
||||
start = results.topic.postcount - start - infScrollPostsPerPage - 1;
|
||||
} else {
|
||||
start = start - infScrollPostsPerPage - 1;
|
||||
}
|
||||
}
|
||||
|
||||
var stop = start + (infScrollPostsPerPage - 1);
|
||||
|
||||
start = Math.max(0, start);
|
||||
stop = Math.max(0, stop);
|
||||
|
||||
async.parallel({
|
||||
mainPost: function (next) {
|
||||
if (start > 0) {
|
||||
return next();
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
privileges: function (next) {
|
||||
privileges.topics.get(data.tid, socket.uid, next);
|
||||
},
|
||||
topic: function (next) {
|
||||
topics.getTopicFields(data.tid, ['postcount', 'deleted'], next);
|
||||
}
|
||||
topics.getMainPost(data.tid, socket.uid, next);
|
||||
},
|
||||
posts: function (next) {
|
||||
topics.getTopicPosts(data.tid, set, start, stop, socket.uid, reverse, next);
|
||||
},
|
||||
postSharing: function (next) {
|
||||
social.getActivePostSharing(next);
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
if (!results.privileges.read || (parseInt(results.topic.deleted, 10) && !results.privileges.view_deleted)) {
|
||||
return callback(new Error('[[error:no-privileges]]'));
|
||||
}
|
||||
}, function (err, topicData) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
|
||||
userPrivileges = results.privileges;
|
||||
|
||||
var set = 'tid:' + data.tid + ':posts';
|
||||
if (data.topicPostSort === 'most_votes') {
|
||||
set = 'tid:' + data.tid + ':posts:votes';
|
||||
}
|
||||
var reverse = data.topicPostSort === 'newest_to_oldest' || data.topicPostSort === 'most_votes';
|
||||
var start = Math.max(0, parseInt(data.after, 10));
|
||||
|
||||
var infScrollPostsPerPage = 10;
|
||||
|
||||
if (data.direction > 0) {
|
||||
if (reverse) {
|
||||
start = results.topic.postcount - start;
|
||||
}
|
||||
} else {
|
||||
if (reverse) {
|
||||
start = results.topic.postcount - start - infScrollPostsPerPage - 1;
|
||||
} else {
|
||||
start = start - infScrollPostsPerPage - 1;
|
||||
}
|
||||
}
|
||||
|
||||
var stop = start + (infScrollPostsPerPage - 1);
|
||||
|
||||
start = Math.max(0, start);
|
||||
stop = Math.max(0, stop);
|
||||
|
||||
async.parallel({
|
||||
mainPost: function (next) {
|
||||
if (start > 0) {
|
||||
return next();
|
||||
}
|
||||
topics.getMainPost(data.tid, socket.uid, next);
|
||||
},
|
||||
posts: function (next) {
|
||||
topics.getTopicPosts(data.tid, set, start, stop, socket.uid, reverse, next);
|
||||
},
|
||||
postSharing: function (next) {
|
||||
social.getActivePostSharing(next);
|
||||
}
|
||||
}, next);
|
||||
},
|
||||
function (topicData, next) {
|
||||
if (topicData.mainPost) {
|
||||
topicData.posts = [topicData.mainPost].concat(topicData.posts);
|
||||
}
|
||||
|
||||
topicData.privileges = results.privileges;
|
||||
topicData.privileges = userPrivileges;
|
||||
topicData['reputation:disabled'] = parseInt(meta.config['reputation:disabled'], 10) === 1;
|
||||
topicData['downvote:disabled'] = parseInt(meta.config['downvote:disabled'], 10) === 1;
|
||||
|
||||
topics.modifyPostsByPrivilege(topicData, results.privileges);
|
||||
callback(null, topicData);
|
||||
});
|
||||
});
|
||||
topics.modifyPostsByPrivilege(topicData, userPrivileges);
|
||||
next(null, topicData);
|
||||
}
|
||||
], callback);
|
||||
};
|
||||
|
||||
SocketTopics.loadMoreUnreadTopics = function (socket, data, callback) {
|
||||
@@ -110,7 +112,6 @@ module.exports = function (SocketTopics) {
|
||||
topics.getRecentTopics(data.cid, socket.uid, start, stop, data.filter, callback);
|
||||
};
|
||||
|
||||
|
||||
SocketTopics.loadMoreFromSet = function (socket, data, callback) {
|
||||
if (!data || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0 || !data.set) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
|
||||
@@ -460,27 +460,23 @@ describe('Topic\'s', function () {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
describe('.fork', function () {
|
||||
var newTopic;
|
||||
var replies = [];
|
||||
var topicPids;
|
||||
var originalBookmark = 5;
|
||||
function postReply( next ) {
|
||||
topics.reply({uid: topic.userId, content: 'test post ' + replies.length, tid: newTopic.tid},
|
||||
function (err, result) {
|
||||
function postReply(next) {
|
||||
topics.reply({uid: topic.userId, content: 'test post ' + replies.length, tid: newTopic.tid}, function (err, result) {
|
||||
assert.equal(err, null, 'was created with error');
|
||||
assert.ok(result);
|
||||
replies.push( result );
|
||||
replies.push(result);
|
||||
next();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
before( function (done) {
|
||||
async.waterfall(
|
||||
[
|
||||
before(function (done) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
groups.join('administrators', topic.userId, next);
|
||||
},
|
||||
@@ -576,6 +572,90 @@ describe('Topic\'s', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('infinitescroll', function () {
|
||||
var socketTopics = require('../src/socket.io/topics');
|
||||
var tid;
|
||||
before(function (done) {
|
||||
topics.post({uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId}, function (err, result) {
|
||||
assert.ifError(err);
|
||||
tid = result.topicData.tid;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should error with invalid data', function (done) {
|
||||
socketTopics.loadMore({uid: adminUid}, {}, function (err) {
|
||||
assert.equal(err.message, '[[error:invalid-data]]');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should infinite load topic posts', function (done) {
|
||||
socketTopics.loadMore({uid: adminUid}, {tid: tid, after: 0}, function (err, data) {
|
||||
assert.ifError(err);
|
||||
assert(data.mainPost);
|
||||
assert(data.posts);
|
||||
assert(data.privileges);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should error with invalid data', function (done) {
|
||||
socketTopics.loadMoreUnreadTopics({uid: adminUid}, {after: 'invalid'}, function (err, data) {
|
||||
assert.equal(err.message, '[[error:invalid-data]]');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should load more unread topics', function (done) {
|
||||
socketTopics.markUnread({uid: adminUid}, tid, function (err) {
|
||||
assert.ifError(err);
|
||||
socketTopics.loadMoreUnreadTopics({uid: adminUid}, {cid: topic.categoryId, after: 0}, function (err, data) {
|
||||
assert.ifError(err);
|
||||
assert(data);
|
||||
assert(Array.isArray(data.topics));
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should error with invalid data', function (done) {
|
||||
socketTopics.loadMoreRecentTopics({uid: adminUid}, {after: 'invalid'}, function (err, data) {
|
||||
assert.equal(err.message, '[[error:invalid-data]]');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should load more recent topics', function (done) {
|
||||
socketTopics.loadMoreRecentTopics({uid: adminUid}, {cid: topic.categoryId, after: 0}, function (err, data) {
|
||||
assert.ifError(err);
|
||||
assert(data);
|
||||
assert(Array.isArray(data.topics));
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should error with invalid data', function (done) {
|
||||
socketTopics.loadMoreFromSet({uid: adminUid}, {after: 'invalid'}, function (err, data) {
|
||||
assert.equal(err.message, '[[error:invalid-data]]');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should load more from custom set', function (done) {
|
||||
socketTopics.loadMoreRecentTopics({uid: adminUid}, {set: 'uid:' + adminUid + ':topics', after: 0}, function (err, data) {
|
||||
assert.ifError(err);
|
||||
assert(data);
|
||||
assert(Array.isArray(data.topics));
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
after(function (done) {
|
||||
db.emptydb(done);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user