(drunk) not even done yet
This commit is contained in:
barisusakli
2014-08-16 21:33:42 -04:00
parent 3011491863
commit ca90afd544
14 changed files with 178 additions and 108 deletions

View File

@@ -20,11 +20,11 @@ module.exports = function(Categories) {
return callback(err, []);
}
posts.getPostSummaryByPids(pids, {stripTags: true}, callback);
posts.getPostSummaryByPids(pids, uid, {stripTags: true}, callback);
});
};
Categories.getRecentTopicReplies = function(categoryData, callback) {
Categories.getRecentTopicReplies = function(categoryData, uid, callback) {
async.map(categoryData, getRecentTopicPids, function(err, results) {
var pids = _.flatten(results);
@@ -32,7 +32,7 @@ module.exports = function(Categories) {
return !!pid && array.indexOf(pid) === index;
});
posts.getPostSummaryByPids(pids, {stripTags: true}, function(err, posts) {
posts.getPostSummaryByPids(pids, uid, {stripTags: true}, function(err, posts) {
if (err) {
return callback(err);
}

View File

@@ -17,6 +17,7 @@ groupsController.list = function(req, res) {
};
groupsController.details = function(req, res) {
var uid = req.user ? parseInt(req.user.uid, 10) : 0;
async.parallel({
group: function(next) {
groups.get(req.params.name, {
@@ -24,7 +25,7 @@ groupsController.details = function(req, res) {
}, next);
},
posts: function(next) {
groups.getLatestMemberPosts(req.params.name, 10, next);
groups.getLatestMemberPosts(req.params.name, 10, uid, next);
}
}, function(err, results) {
if (!err) {

View File

@@ -69,7 +69,7 @@ Controllers.home = function(req, res, next) {
return next(err);
}
categories.getRecentTopicReplies(categoryData, function(err) {
categories.getRecentTopicReplies(categoryData, uid, function(err) {
next(err, categoryData);
});
});

View File

@@ -30,20 +30,18 @@ topicsController.get = function(req, res, next) {
userPrivileges = privileges;
user.getSettings(uid, next);
async.parallel({
postCount: function(next) {
topics.getPostCount(tid, next);
},
settings: function(next) {
user.getSettings(uid, next);
}
}, next);
},
function (settings, next) {
var postIndex = 0;
if (!settings.usePagination) {
postIndex = Math.max((req.params.post_index || 1) - (settings.postsPerPage - 1), 0);
} else if (!req.query.page) {
var index = Math.max(parseInt((req.params.post_index || 0), 10), 0);
page = Math.ceil((index + 1) / settings.postsPerPage);
}
var start = (page - 1) * settings.postsPerPage + postIndex,
end = start + settings.postsPerPage - 1;
function (results, next) {
var settings = results.settings;
var postCount = parseInt(results.postCount, 10) + 1;
var set = 'tid:' + tid + ':posts',
reverse = false;
@@ -54,6 +52,24 @@ topicsController.get = function(req, res, next) {
set = 'tid:' + tid + ':posts:votes';
}
var postIndex = 0;
if (!settings.usePagination) {
if (reverse) {
if (!req.params.post_index || parseInt(req.params.post_index, 10) === 1) {
req.params.post_index = 0;
}
postIndex = Math.max(postCount - (req.params.post_index || postCount) - (settings.postsPerPage - 1), 0);
} else {
postIndex = Math.max((req.params.post_index || 1) - (settings.postsPerPage + 1), 0);
}
} else if (!req.query.page) {
var index = Math.max(parseInt(req.params.post_index, 10), 0);
page = Math.ceil((index + 1) / settings.postsPerPage);
}
var start = (page - 1) * settings.postsPerPage + postIndex,
end = start + settings.postsPerPage - 1;
topics.getTopicWithPosts(tid, set, uid, start, end, reverse, function (err, topicData) {
if (topicData) {
if (topicData.deleted && !userPrivileges.view_deleted) {
@@ -191,6 +207,7 @@ topicsController.get = function(req, res, next) {
topicsController.teaser = function(req, res, next) {
var tid = req.params.topic_id;
var uid = req.user ? parseInt(req.user.uid, 10) : 0;
topics.getLatestUndeletedPid(tid, function(err, pid) {
if (err) {
return next(err);
@@ -200,7 +217,7 @@ topicsController.teaser = function(req, res, next) {
return res.json(404, 'not-found');
}
posts.getPostSummaryByPids([pid], {stripTags: false}, function(err, posts) {
posts.getPostSummaryByPids([pid], uid, {stripTags: false}, function(err, posts) {
if (err) {
return next(err);
}

View File

@@ -418,7 +418,7 @@
});
};
Groups.getLatestMemberPosts = function(groupName, max, callback) {
Groups.getLatestMemberPosts = function(groupName, max, uid, callback) {
Groups.get(groupName, {}, function(err, groupObj) {
if (err || parseInt(groupObj.memberCount, 10) === 0) {
return callback(null, []);
@@ -433,7 +433,7 @@
return callback(err);
}
posts.getPostSummaryByPids(pids, {stripTags: false}, callback);
posts.getPostSummaryByPids(pids, uid, {stripTags: false}, callback);
});
});
};

View File

@@ -186,7 +186,7 @@ var async = require('async'),
if (err) {
return callback(err);
}
Posts.getPostSummaryByPids(pids, {stripTags: true}, callback);
Posts.getPostSummaryByPids(pids, uid, {stripTags: true}, callback);
});
});
};
@@ -270,7 +270,7 @@ var async = require('async'),
});
};
Posts.getPostSummaryByPids = function(pids, options, callback) {
Posts.getPostSummaryByPids = function(pids, uid, options, callback) {
options.stripTags = options.hasOwnProperty('stripTags') ? options.stripTags : false;
options.parse = options.hasOwnProperty('parse') ? options.parse : true;
@@ -319,13 +319,7 @@ var async = require('async'),
});
},
indices: function(next) {
var pids = [], keys = [];
for (var i=0; i<posts.length; ++i) {
pids.push(posts[i].pid);
keys.push('tid:' + posts[i].tid + ':posts');
}
db.sortedSetsRanks(keys, pids, next);
Posts.getPostIndices(posts, uid, next);
}
}, function(err, results) {
function toObject(key, data) {
@@ -345,7 +339,7 @@ var async = require('async'),
results.categories = toObject('cid', results.topicsAndCategories.categories);
for (var i=0; i<posts.length; ++i) {
posts[i].index = utils.isNumber(results.indices[i]) ? parseInt(results.indices[i], 10) + 2 : 1;
posts[i].index = utils.isNumber(results.indices[i]) ? parseInt(results.indices[i], 10) + 1 : 1;
}
posts = posts.filter(function(post) {
@@ -486,7 +480,7 @@ var async = require('async'),
if (err) {
return callback(err);
}
getPostsFromSet('uid:' + uid + ':posts', pids, callback);
getPostsFromSet('uid:' + uid + ':posts', pids, callerUid, callback);
});
});
};
@@ -497,16 +491,16 @@ var async = require('async'),
return callback(err);
}
getPostsFromSet('uid:' + uid + ':favourites', pids, callback);
getPostsFromSet('uid:' + uid + ':favourites', pids, uid, callback);
});
};
function getPostsFromSet(set, pids, callback) {
function getPostsFromSet(set, pids, uid, callback) {
if (!Array.isArray(pids) || !pids.length) {
return callback(null, {posts: [], nextStart: 0});
}
Posts.getPostSummaryByPids(pids, {stripTags: false}, function(err, posts) {
Posts.getPostSummaryByPids(pids, uid, {stripTags: false}, function(err, posts) {
if (err) {
return callback(err);
}
@@ -566,6 +560,38 @@ var async = require('async'),
});
};
Posts.getPostIndices = function(posts, uid, callback) {
user.getSettings(uid, function(err, settings) {
if (err) {
return callback(err);
}
var byVotes = settings.topicPostSort === 'most_votes';
var sets = posts.map(function(post) {
if (byVotes) {
return 'tid:' + post.tid + ':posts:votes';
} else {
return 'tid:' + post.tid + ':posts';
}
});
var pids = posts.map(function(post) {
return post.pid;
});
db.sortedSetsRanks(sets, pids, function(err, indices) {
if (err) {
return callback(err);
}
for (var i=0; i<indices.length; ++i) {
indices[i] = utils.isNumber(indices[i]) ? parseInt(indices[i], 10) + 1 : 0;
}
callback(null, indices);
});
});
};
Posts.isOwner = function(pid, uid, callback) {
uid = parseInt(uid, 10);
if (Array.isArray(pid)) {

View File

@@ -50,7 +50,7 @@ search.search = function(term, uid, callback) {
return callback(err);
}
posts.getPostSummaryByPids(pids, {stripTags: true, parse: false}, function(err, posts) {
posts.getPostSummaryByPids(pids, uid, {stripTags: true, parse: false}, function(err, posts) {
if (err) {
return callback(err);
}

View File

@@ -70,10 +70,6 @@ SocketTopics.postcount = function(socket, tid, callback) {
topics.getTopicField(tid, 'postcount', callback);
};
SocketTopics.lastPostIndex = function(socket, tid, callback) {
db.sortedSetCard('tid:' + tid + ':posts', callback);
};
SocketTopics.increaseViewCount = function(socket, tid) {
topics.increaseViewCount(tid);
};
@@ -421,6 +417,9 @@ SocketTopics.loadMore = function(socket, data, callback) {
},
privileges: function(next) {
privileges.topics.get(data.tid, socket.uid, next);
},
postCount: function(next) {
topics.getPostCount(data.tid, next);
}
}, function(err, results) {
if (err) {
@@ -431,19 +430,23 @@ SocketTopics.loadMore = function(socket, data, callback) {
return callback(new Error('[[error:no-privileges]]'));
}
var start = Math.max(parseInt(data.after, 10) - 1, 0),
end = start + results.settings.postsPerPage - 1;
var set = 'tid:' + data.tid + ':posts',
reverse = false;
reverse = false,
start = Math.max(parseInt(data.after, 10) - 1, 0);
if (results.settings.topicPostSort === 'newest_to_oldest') {
reverse = true;
data.after = results.postCount - data.after;
start = Math.max(parseInt(data.after, 10), 0);
} else if (results.settings.topicPostSort === 'most_votes') {
reverse = true;
data.after = results.postCount - data.after;
start = Math.max(parseInt(data.after, 10), 0);
set = 'tid:' + data.tid + ':posts:votes';
}
var end = start + results.settings.postsPerPage - 1;
async.parallel({
posts: function(next) {
topics.getTopicPosts(data.tid, set, start, end, socket.uid, reverse, next);

View File

@@ -199,7 +199,7 @@ var async = require('async'),
privileges.categories.isAdminOrMod(cids, uid, next);
},
teasers: function(next) {
Topics.getTeasers(tids, next);
Topics.getTeasers(tids, uid, next);
},
tags: function(next) {
Topics.getTopicsTagsObjects(tids, next);
@@ -256,7 +256,7 @@ var async = require('async'),
}
async.parallel({
posts: function(next) {
posts: function(next) {
posts.getPidsFromSet(set, start, end, reverse, function(err, pids) {
if (err) {
return next(err);
@@ -270,11 +270,7 @@ var async = require('async'),
if (err) {
return next(err);
}
start = parseInt(start, 10);
for(var i=0; i<posts.length; ++i) {
posts[i].index = start + i;
}
posts[0].index = 0;
Topics.addPostData(posts, uid, next);
});
});
@@ -332,7 +328,7 @@ var async = require('async'),
});
};
Topics.getTeasers = function(tids, callback) {
Topics.getTeasers = function(tids, uid, callback) {
if(!Array.isArray(tids)) {
return callback(null, []);
}
@@ -350,51 +346,41 @@ var async = require('async'),
return 'post:' + pid;
});
async.parallel({
indices: function(next) {
var sets = tids.map(function(tid) {
return 'tid:' + tid + ':posts';
});
db.sortedSetsRanks(sets, pids, next);
},
posts: function(next) {
db.getObjectsFields(postKeys, ['pid', 'uid', 'timestamp'], next);
}
}, function(err, results) {
db.getObjectsFields(postKeys, ['pid', 'uid', 'timestamp', 'tid'], function(err, postData) {
if (err) {
return callback(err);
}
var indices = results.indices.map(function(index) {
if (!utils.isNumber(index)) {
return 1;
}
return parseInt(index, 10) + 2;
});
var uids = results.posts.map(function(post) {
var uids = postData.map(function(post) {
return post.uid;
}).filter(function(uid, index, array) {
return array.indexOf(uid) === index;
});
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], function(err, userData) {
async.parallel({
users: function(next) {
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);
},
indices: function(next) {
posts.getPostIndices(postData, uid, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
var users = {};
userData.forEach(function(user) {
results.users.forEach(function(user) {
users[user.uid] = user;
});
results.posts.forEach(function(post, index) {
postData.forEach(function(post, index) {
post.user = users[post.uid];
post.index = indices[index];
post.index = results.indices[index] + 1;
post.timestamp = utils.toISOString(post.timestamp);
});
callback(err, results.posts);
callback(null, postData);
});
});
});

View File

@@ -32,11 +32,6 @@ module.exports = function(Topics) {
return callback(null, []);
}
start = parseInt(start, 10);
for(var i=0; i<postData.length; ++i) {
postData[i].index = start + i + 1;
}
Topics.addPostData(postData, uid, callback);
});
};
@@ -96,6 +91,9 @@ module.exports = function(Topics) {
},
privileges: function(next) {
privileges.posts.get(pids, uid, next);
},
indices: function(next) {
posts.getPostIndices(postData, uid, next);
}
}, function(err, results) {
if(err) {
@@ -103,6 +101,7 @@ module.exports = function(Topics) {
}
for (var i = 0; i < postData.length; ++i) {
postData[i].index = results.indices[i];
postData[i].deleted = parseInt(postData[i].deleted, 10) === 1;
postData[i].user = results.userData[postData[i].uid];
postData[i].editor = postData[i].editor ? results.editors[postData[i].editor] : null;