style changes

v1.18.x
barisusakli
parent e5f7eed8ec
commit 0d1d8f3874

@ -10,265 +10,277 @@ var topics = require('./topics');
var privileges = require('./privileges'); var privileges = require('./privileges');
var plugins = require('./plugins'); var plugins = require('./plugins');
(function (Posts) { var Posts = module.exports;
require('./posts/create')(Posts);
require('./posts/delete')(Posts);
require('./posts/edit')(Posts);
require('./posts/parse')(Posts);
require('./posts/user')(Posts);
require('./posts/topics')(Posts);
require('./posts/category')(Posts);
require('./posts/summary')(Posts);
require('./posts/recent')(Posts);
require('./posts/tools')(Posts);
require('./posts/votes')(Posts);
require('./posts/bookmarks')(Posts);
Posts.exists = function (pid, callback) { require('./posts/create')(Posts);
db.isSortedSetMember('posts:pid', pid, callback); require('./posts/delete')(Posts);
}; require('./posts/edit')(Posts);
require('./posts/parse')(Posts);
require('./posts/user')(Posts);
require('./posts/topics')(Posts);
require('./posts/category')(Posts);
require('./posts/summary')(Posts);
require('./posts/recent')(Posts);
require('./posts/tools')(Posts);
require('./posts/votes')(Posts);
require('./posts/bookmarks')(Posts);
Posts.getPidsFromSet = function (set, start, stop, reverse, callback) { Posts.exists = function (pid, callback) {
if (isNaN(start) || isNaN(stop)) { db.isSortedSetMember('posts:pid', pid, callback);
return callback(null, []); };
}
db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop, callback);
};
Posts.getPostsByPids = function (pids, uid, callback) {
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
var keys = []; Posts.getPidsFromSet = function (set, start, stop, reverse, callback) {
if (isNaN(start) || isNaN(stop)) {
return callback(null, []);
}
db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange'](set, start, stop, callback);
};
for (var x = 0, numPids = pids.length; x < numPids; x += 1) { Posts.getPostsByPids = function (pids, uid, callback) {
keys.push('post:' + pids[x]); if (!Array.isArray(pids) || !pids.length) {
} return callback(null, []);
}
async.waterfall([ async.waterfall([
function (next) { function (next) {
db.getObjects(keys, next); var keys = pids.map(function (pid) {
}, return 'post:' + pid;
function (posts, next) { });
async.map(posts, function (post, next) { db.getObjects(keys, next);
if (!post) { },
return next(); function (posts, next) {
} async.map(posts, function (post, next) {
post.upvotes = parseInt(post.upvotes, 10) || 0; if (!post) {
post.downvotes = parseInt(post.downvotes, 10) || 0; return next();
post.votes = post.upvotes - post.downvotes;
post.timestampISO = utils.toISOString(post.timestamp);
post.editedISO = parseInt(post.edited, 10) !== 0 ? utils.toISOString(post.edited) : '';
Posts.parsePost(post, next);
}, next);
},
function (posts, next) {
plugins.fireHook('filter:post.getPosts', { posts: posts, uid: uid }, next);
},
function (data, next) {
if (!data || !Array.isArray(data.posts)) {
return next(null, []);
} }
data.posts = data.posts.filter(Boolean); post.upvotes = parseInt(post.upvotes, 10) || 0;
next(null, data.posts); post.downvotes = parseInt(post.downvotes, 10) || 0;
}, post.votes = post.upvotes - post.downvotes;
], callback); post.timestampISO = utils.toISOString(post.timestamp);
}; post.editedISO = parseInt(post.edited, 10) !== 0 ? utils.toISOString(post.edited) : '';
Posts.parsePost(post, next);
Posts.getPostSummariesFromSet = function (set, uid, start, stop, callback) { }, next);
async.waterfall([ },
function (next) { function (posts, next) {
db.getSortedSetRevRange(set, start, stop, next); plugins.fireHook('filter:post.getPosts', { posts: posts, uid: uid }, next);
}, },
function (pids, next) { function (data, next) {
privileges.posts.filter('read', pids, uid, next); if (!data || !Array.isArray(data.posts)) {
}, return next(null, []);
function (pids, next) {
Posts.getPostSummaryByPids(pids, uid, { stripTags: false }, next);
},
function (posts, next) {
next(null, { posts: posts, nextStart: stop + 1 });
},
], callback);
};
Posts.getPostData = function (pid, callback) {
async.waterfall([
function (next) {
db.getObject('post:' + pid, next);
},
function (data, next) {
plugins.fireHook('filter:post.getPostData', { post: data }, next);
},
function (data, next) {
next(null, data.post);
} }
], callback); data.posts = data.posts.filter(Boolean);
}; next(null, data.posts);
},
], callback);
};
Posts.getPostField = function (pid, field, callback) { Posts.getPostSummariesFromSet = function (set, uid, start, stop, callback) {
Posts.getPostFields(pid, [field], function (err, data) { async.waterfall([
if (err) { function (next) {
return callback(err); db.getSortedSetRevRange(set, start, stop, next);
} },
function (pids, next) {
privileges.posts.filter('read', pids, uid, next);
},
function (pids, next) {
Posts.getPostSummaryByPids(pids, uid, { stripTags: false }, next);
},
function (posts, next) {
next(null, { posts: posts, nextStart: stop + 1 });
},
], callback);
};
callback(null, data[field]); Posts.getPostData = function (pid, callback) {
}); async.waterfall([
}; function (next) {
db.getObject('post:' + pid, next);
},
function (data, next) {
plugins.fireHook('filter:post.getPostData', { post: data }, next);
},
function (data, next) {
next(null, data.post);
},
], callback);
};
Posts.getPostFields = function (pid, fields, callback) { Posts.getPostField = function (pid, field, callback) {
db.getObjectFields('post:' + pid, fields, function (err, data) { async.waterfall([
if (err) { function (next) {
return callback(err); Posts.getPostFields(pid, [field], next);
} },
function (data, next) {
next(null, data[field]);
},
], callback);
};
Posts.getPostFields = function (pid, fields, callback) {
async.waterfall([
function (next) {
db.getObjectFields('post:' + pid, fields, next);
},
function (data, next) {
data.pid = pid; data.pid = pid;
plugins.fireHook('filter:post.getFields', { posts: [data], fields: fields }, function (err, data) { plugins.fireHook('filter:post.getFields', { posts: [data], fields: fields }, next);
callback(err, (data && Array.isArray(data.posts) && data.posts.length) ? data.posts[0] : null); },
}); function (data, next) {
}); next(null, (data && Array.isArray(data.posts) && data.posts.length) ? data.posts[0] : null);
}; },
], callback);
};
Posts.getPostsFields = function (pids, fields, callback) { Posts.getPostsFields = function (pids, fields, callback) {
if (!Array.isArray(pids) || !pids.length) { if (!Array.isArray(pids) || !pids.length) {
return callback(null, []); return callback(null, []);
} }
var keys = pids.map(function (pid) { var keys = pids.map(function (pid) {
return 'post:' + pid; return 'post:' + pid;
}); });
db.getObjectsFields(keys, fields, function (err, posts) { async.waterfall([
if (err) { function (next) {
return callback(err); db.getObjectsFields(keys, fields, next);
} },
plugins.fireHook('filter:post.getFields', { posts: posts, fields: fields }, function (err, data) { function (posts, next) {
callback(err, (data && Array.isArray(data.posts)) ? data.posts : null); plugins.fireHook('filter:post.getFields', { posts: posts, fields: fields }, next);
}); },
}); function (data, next) {
}; next(null, (data && Array.isArray(data.posts)) ? data.posts : null);
},
], callback);
};
Posts.setPostField = function (pid, field, value, callback) { Posts.setPostField = function (pid, field, value, callback) {
db.setObjectField('post:' + pid, field, value, function (err) { async.waterfall([
if (err) { function (next) {
return callback(err); db.setObjectField('post:' + pid, field, value, next);
} },
function (next) {
var data = { var data = {
pid: pid, pid: pid,
}; };
data[field] = value; data[field] = value;
plugins.fireHook('action:post.setFields', { data: data }); plugins.fireHook('action:post.setFields', { data: data });
callback(); next();
}); },
}; ], callback);
};
Posts.setPostFields = function (pid, data, callback) { Posts.setPostFields = function (pid, data, callback) {
db.setObject('post:' + pid, data, function (err) { async.waterfall([
if (err) { function (next) {
return callback(err); db.setObject('post:' + pid, data, next);
} },
function (next) {
data.pid = pid; data.pid = pid;
plugins.fireHook('action:post.setFields', { data: data }); plugins.fireHook('action:post.setFields', { data: data });
callback(); next();
}); },
}; ], callback);
};
Posts.getPidIndex = function (pid, tid, topicPostSort, callback) { Posts.getPidIndex = function (pid, tid, topicPostSort, callback) {
var set = topicPostSort === 'most_votes' ? 'tid:' + tid + ':posts:votes' : 'tid:' + tid + ':posts'; async.waterfall([
db.sortedSetRank(set, pid, function (err, index) { function (next) {
var set = topicPostSort === 'most_votes' ? 'tid:' + tid + ':posts:votes' : 'tid:' + tid + ':posts';
db.sortedSetRank(set, pid, next);
},
function (index, next) {
if (!utils.isNumber(index)) { if (!utils.isNumber(index)) {
return callback(err, 0); return next(null, 0);
} }
callback(err, parseInt(index, 10) + 1); next(null, parseInt(index, 10) + 1);
}); },
}; ], callback);
};
Posts.getPostIndices = function (posts, uid, callback) { Posts.getPostIndices = function (posts, uid, callback) {
if (!Array.isArray(posts) || !posts.length) { if (!Array.isArray(posts) || !posts.length) {
return callback(null, []); return callback(null, []);
} }
async.waterfall([ async.waterfall([
function (next) { function (next) {
user.getSettings(uid, next); user.getSettings(uid, next);
}, },
function (settings, next) { function (settings, next) {
var byVotes = settings.topicPostSort === 'most_votes'; var byVotes = settings.topicPostSort === 'most_votes';
var sets = posts.map(function (post) { var sets = posts.map(function (post) {
return byVotes ? 'tid:' + post.tid + ':posts:votes' : 'tid:' + post.tid + ':posts'; return byVotes ? 'tid:' + post.tid + ':posts:votes' : 'tid:' + post.tid + ':posts';
}); });
var uniqueSets = _.uniq(sets); var uniqueSets = _.uniq(sets);
var method = 'sortedSetsRanks'; var method = 'sortedSetsRanks';
if (uniqueSets.length === 1) { if (uniqueSets.length === 1) {
method = 'sortedSetRanks'; method = 'sortedSetRanks';
sets = uniqueSets[0]; sets = uniqueSets[0];
} }
var pids = posts.map(function (post) { var pids = posts.map(function (post) {
return post.pid; return post.pid;
}); });
db[method](sets, pids, next); db[method](sets, pids, next);
}, },
function (indices, next) { function (indices, next) {
for (var i = 0; i < indices.length; i += 1) { for (var i = 0; i < indices.length; i += 1) {
indices[i] = utils.isNumber(indices[i]) ? parseInt(indices[i], 10) + 1 : 0; indices[i] = utils.isNumber(indices[i]) ? parseInt(indices[i], 10) + 1 : 0;
} }
next(null, indices); next(null, indices);
}, },
], callback); ], callback);
}; };
Posts.updatePostVoteCount = function (postData, callback) { Posts.updatePostVoteCount = function (postData, callback) {
if (!postData || !postData.pid || !postData.tid) { if (!postData || !postData.pid || !postData.tid) {
return callback(); return callback();
} }
async.parallel([ async.parallel([
function (next) { function (next) {
if (postData.uid) { if (postData.uid) {
if (postData.votes > 0) { if (postData.votes > 0) {
db.sortedSetAdd('uid:' + postData.uid + ':posts:votes', postData.votes, postData.pid, next); db.sortedSetAdd('uid:' + postData.uid + ':posts:votes', postData.votes, postData.pid, next);
} else {
db.sortedSetRemove('uid:' + postData.uid + ':posts:votes', postData.pid, next);
}
} else { } else {
next(); db.sortedSetRemove('uid:' + postData.uid + ':posts:votes', postData.pid, next);
} }
}, } else {
function (next) { next();
async.waterfall([
function (next) {
topics.getTopicField(postData.tid, 'mainPid', next);
},
function (mainPid, next) {
if (parseInt(mainPid, 10) === parseInt(postData.pid, 10)) {
return next();
}
db.sortedSetAdd('tid:' + postData.tid + ':posts:votes', postData.votes, postData.pid, next);
},
], next);
},
function (next) {
db.sortedSetAdd('posts:votes', postData.votes, postData.pid, next);
},
function (next) {
Posts.setPostFields(postData.pid, { upvotes: postData.upvotes, downvotes: postData.downvotes }, next);
},
], function (err) {
callback(err);
});
};
Posts.modifyPostByPrivilege = function (post, isAdminOrMod) {
if (post.deleted && !(isAdminOrMod || post.selfPost)) {
post.content = '[[topic:post_is_deleted]]';
if (post.user) {
post.user.signature = '';
} }
},
function (next) {
async.waterfall([
function (next) {
topics.getTopicField(postData.tid, 'mainPid', next);
},
function (mainPid, next) {
if (parseInt(mainPid, 10) === parseInt(postData.pid, 10)) {
return next();
}
db.sortedSetAdd('tid:' + postData.tid + ':posts:votes', postData.votes, postData.pid, next);
},
], next);
},
function (next) {
db.sortedSetAdd('posts:votes', postData.votes, postData.pid, next);
},
function (next) {
Posts.setPostFields(postData.pid, { upvotes: postData.upvotes, downvotes: postData.downvotes }, next);
},
], function (err) {
callback(err);
});
};
Posts.modifyPostByPrivilege = function (post, isAdminOrMod) {
if (post.deleted && !(isAdminOrMod || post.selfPost)) {
post.content = '[[topic:post_is_deleted]]';
if (post.user) {
post.user.signature = '';
} }
}; }
}(exports)); };

Loading…
Cancel
Save