remove more parseInts

v1.18.x
Barış Soner Uşaklı 6 years ago
parent 9aa39c91a3
commit ac39112271

@ -158,7 +158,7 @@ apiController.getTopicData = function (tid, uid, callback) {
return callback(err); return callback(err);
} }
if (!results.privileges.read || !results.privileges['topics:read'] || (parseInt(results.topic.deleted, 10) && !results.privileges.view_deleted)) { if (!results.privileges.read || !results.privileges['topics:read'] || (results.topic.deleted && !results.privileges.view_deleted)) {
return callback(); return callback();
} }
callback(null, results.topic); callback(null, results.topic);

@ -54,7 +54,7 @@ topicsController.get = function (req, res, callback) {
userPrivileges = results.privileges; userPrivileges = results.privileges;
rssToken = results.rssToken; rssToken = results.rssToken;
if (!userPrivileges['topics:read'] || (parseInt(results.topic.deleted, 10) && !userPrivileges.view_deleted)) { if (!userPrivileges['topics:read'] || (results.topic.deleted && !userPrivileges.view_deleted)) {
return helpers.notAllowed(req, res); return helpers.notAllowed(req, res);
} }
@ -386,7 +386,7 @@ topicsController.pagination = function (req, res, callback) {
return callback(err); return callback(err);
} }
if (!results.privileges.read || (parseInt(results.topic.deleted, 10) && !results.privileges.view_deleted)) { if (!results.privileges.read || (results.topic.deleted && !results.privileges.view_deleted)) {
return helpers.notAllowed(req, res); return helpers.notAllowed(req, res);
} }

@ -47,25 +47,14 @@ Posts.getPostsByPids = function (pids, uid, callback) {
async.waterfall([ async.waterfall([
function (next) { function (next) {
var keys = pids.map(function (pid) { Posts.getPostsData(pids, next);
return 'post:' + pid;
});
db.getObjects(keys, next);
}, },
function (posts, next) { function (posts, next) {
async.map(posts, function (post, next) { async.map(posts, Posts.parsePost, next);
if (!post) { },
return next(); function (posts, next) {
} user.blocks.filter(uid, posts, next);
post.upvotes = parseInt(post.upvotes, 10) || 0;
post.downvotes = parseInt(post.downvotes, 10) || 0;
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);
}, },
async.apply(user.blocks.filter, uid),
function (posts, next) { function (posts, next) {
plugins.fireHook('filter:post.getPosts', { posts: posts, uid: uid }, next); plugins.fireHook('filter:post.getPosts', { posts: posts, uid: uid }, next);
}, },

@ -4,8 +4,12 @@ var async = require('async');
var db = require('../database'); var db = require('../database');
var plugins = require('../plugins'); var plugins = require('../plugins');
var utils = require('../utils');
const intFields = ['uid', 'pid', 'tid', 'deleted', 'timestamp']; const intFields = [
'uid', 'pid', 'tid', 'deleted', 'timestamp',
'upvotes', 'downvotes', 'deleterUid', 'edited',
];
module.exports = function (Posts) { module.exports = function (Posts) {
Posts.getPostsFields = function (pids, fields, callback) { Posts.getPostsFields = function (pids, fields, callback) {
@ -13,10 +17,9 @@ module.exports = function (Posts) {
return callback(null, []); return callback(null, []);
} }
var keys = pids.map(pid => 'post:' + pid);
async.waterfall([ async.waterfall([
function (next) { function (next) {
const keys = pids.map(pid => 'post:' + pid);
if (fields.length) { if (fields.length) {
db.getObjectsFields(keys, fields, next); db.getObjectsFields(keys, fields, next);
} else { } else {
@ -76,5 +79,15 @@ module.exports = function (Posts) {
function modifyPost(post) { function modifyPost(post) {
if (post) { if (post) {
intFields.forEach(field => db.parseIntField(post, field)); intFields.forEach(field => db.parseIntField(post, field));
if (post.hasOwnProperty('upvotes') && post.hasOwnProperty('downvotes')) {
post.votes = post.upvotes - post.downvotes;
}
if (post.hasOwnProperty('timestamp')) {
post.timestampISO = utils.toISOString(post.timestamp);
}
if (post.hasOwnProperty('edited')) {
post.editedISO = post.edited !== 0 ? utils.toISOString(post.edited) : '';
}
} }
} }

@ -22,6 +22,9 @@ module.exports = function (Posts) {
}; };
Posts.parsePost = function (postData, callback) { Posts.parsePost = function (postData, callback) {
if (!postData) {
return setImmediate(callback, null, postData);
}
postData.content = String(postData.content || ''); postData.content = String(postData.content || '');
var cache = require('./cache'); var cache = require('./cache');
if (postData.pid && cache.has(String(postData.pid))) { if (postData.pid && cache.has(String(postData.pid))) {

@ -63,11 +63,8 @@ module.exports = function (Posts) {
post.user = results.users[post.uid]; post.user = results.users[post.uid];
post.topic = results.topics[post.tid]; post.topic = results.topics[post.tid];
post.category = post.topic && results.categories[post.topic.cid]; post.category = post.topic && results.categories[post.topic.cid];
post.isMainPost = post.topic && parseInt(post.pid, 10) === parseInt(post.topic.mainPid, 10); post.isMainPost = post.topic && post.pid === post.topic.mainPid;
post.deleted = parseInt(post.deleted, 10) === 1; post.deleted = post.deleted === 1;
post.upvotes = parseInt(post.upvotes, 10) || 0;
post.downvotes = parseInt(post.downvotes, 10) || 0;
post.votes = post.upvotes - post.downvotes;
post.timestampISO = utils.toISOString(post.timestamp); post.timestampISO = utils.toISOString(post.timestamp);
}); });
@ -114,16 +111,7 @@ module.exports = function (Posts) {
}, },
function (_topicsData, next) { function (_topicsData, next) {
topicsData = _topicsData; topicsData = _topicsData;
var cids = topicsData.map(function (topic) { var cids = _.uniq(topicsData.map(topic => topic && topic.cid));
if (topic) {
topic.title = String(topic.title);
topic.deleted = parseInt(topic.deleted, 10) === 1;
}
return topic && parseInt(topic.cid, 10);
});
cids = _.uniq(cids);
categories.getCategoriesFields(cids, ['cid', 'name', 'icon', 'slug', 'parentCid', 'bgColor', 'color'], next); categories.getCategoriesFields(cids, ['cid', 'name', 'icon', 'slug', 'parentCid', 'bgColor', 'color'], next);
}, },
function (categoriesData, next) { function (categoriesData, next) {

@ -123,7 +123,7 @@ module.exports = function (privileges) {
pids = postData.filter(function (post) { pids = postData.filter(function (post) {
return post.topic && cidsSet.has(post.topic.cid) && return post.topic && cidsSet.has(post.topic.cid) &&
((parseInt(post.topic.deleted, 10) !== 1 && parseInt(post.deleted, 10) !== 1) || results.isAdmin || isModOf[post.cid]); ((!post.topic.deleted && !post.deleted) || results.isAdmin || isModOf[post.cid]);
}).map(post => post.pid); }).map(post => post.pid);
plugins.fireHook('filter:privileges.posts.filter', { plugins.fireHook('filter:privileges.posts.filter', {
@ -184,11 +184,11 @@ module.exports = function (privileges) {
} }
var postDeleteDuration = meta.config.postDeleteDuration; var postDeleteDuration = meta.config.postDeleteDuration;
if (postDeleteDuration && (Date.now() - parseInt(postData.timestamp, 10) > postDeleteDuration * 1000)) { if (postDeleteDuration && (Date.now() - postData.timestamp > postDeleteDuration * 1000)) {
return next(null, { flag: false, message: '[[error:post-delete-duration-expired, ' + meta.config.postDeleteDuration + ']]' }); return next(null, { flag: false, message: '[[error:post-delete-duration-expired, ' + meta.config.postDeleteDuration + ']]' });
} }
var deleterUid = parseInt(postData.deleterUid, 10) || 0; var deleterUid = postData.deleterUid;
var flag = results.isOwner && (deleterUid === 0 || deleterUid === parseInt(postData.uid, 10)); var flag = results.isOwner && (deleterUid === 0 || deleterUid === postData.uid);
next(null, { flag: flag, message: '[[error:no-privileges]]' }); next(null, { flag: flag, message: '[[error:no-privileges]]' });
}, },
], callback); ], callback);

@ -100,7 +100,7 @@ module.exports = function (privileges) {
tids = topicsData.filter(function (topic) { tids = topicsData.filter(function (topic) {
return cidsSet.has(topic.cid) && return cidsSet.has(topic.cid) &&
(parseInt(topic.deleted, 10) !== 1 || results.isAdmin || isModOf[topic.cid]); (!topic.deleted || results.isAdmin || isModOf[topic.cid]);
}).map(topic => topic.tid); }).map(topic => topic.tid);
plugins.fireHook('filter:privileges.topics.filter', { plugins.fireHook('filter:privileges.topics.filter', {

@ -97,7 +97,7 @@ function generateForTopic(req, res, callback) {
}, next); }, next);
}, },
function (results, next) { function (results, next) {
if (!results.topic || (parseInt(results.topic.deleted, 10) && !results.privileges.view_deleted)) { if (!results.topic || (results.topic.deleted && !results.privileges.view_deleted)) {
return controllers404.send404(req, res); return controllers404.send404(req, res);
} }
userPrivileges = results.privileges; userPrivileges = results.privileges;

@ -87,9 +87,7 @@ function searchInContent(data, callback) {
topics.getMainPids(results.tids, next); topics.getMainPids(results.tids, next);
}, },
function (mainPids, next) { function (mainPids, next) {
pids = mainPids.concat(pids).map(function (pid) { pids = mainPids.concat(pids).filter(Boolean);
return pid && pid.toString();
}).filter(Boolean);
privileges.posts.filter('read', pids, data.uid, next); privileges.posts.filter('read', pids, data.uid, next);
}, },
@ -144,9 +142,7 @@ function filterAndSort(pids, data, callback) {
plugins.fireHook('filter:search.filterAndSort', { pids: pids, posts: posts, data: data }, next); plugins.fireHook('filter:search.filterAndSort', { pids: pids, posts: posts, data: data }, next);
}, },
function (result, next) { function (result, next) {
pids = result.posts.map(function (post) { pids = result.posts.map(post => post && post.pid);
return post && post.pid;
});
next(null, pids); next(null, pids);
}, },
@ -163,25 +159,18 @@ function getMatchedPosts(pids, data, callback) {
} }
} }
var posts; var postsData;
async.waterfall([ async.waterfall([
function (next) { function (next) {
var keys = pids.map(function (pid) { posts.getPostsFields(pids, postFields, next);
return 'post:' + pid;
});
db.getObjectsFields(keys, postFields, next);
}, },
function (_posts, next) { function (_postsData, next) {
posts = _posts.filter(function (post) { postsData = _postsData.filter(post => post && !post.deleted);
return post && parseInt(post.deleted, 10) !== 1;
});
async.parallel({ async.parallel({
users: function (next) { users: function (next) {
if (data.sortBy && data.sortBy.startsWith('user')) { if (data.sortBy && data.sortBy.startsWith('user')) {
var uids = posts.map(function (post) { var uids = postsData.map(post => post.uid);
return post.uid;
});
user.getUsersFields(uids, ['username'], next); user.getUsersFields(uids, ['username'], next);
} else { } else {
next(); next();
@ -189,22 +178,17 @@ function getMatchedPosts(pids, data, callback) {
}, },
topics: function (next) { topics: function (next) {
var topicsData; var topicsData;
const tids = postsData.map(post => post.tid);
async.waterfall([ async.waterfall([
function (next) { function (next) {
var topicKeys = posts.map(function (post) { topics.getTopicsData(tids, next);
return 'topic:' + post.tid;
});
db.getObjects(topicKeys, next);
}, },
function (_topics, next) { function (_topics, next) {
topicsData = _topics; topicsData = _topics;
async.parallel({ async.parallel({
teasers: function (next) { teasers: function (next) {
if (data.sortBy && data.sortBy.startsWith('teaser')) { if (data.sortBy && data.sortBy.startsWith('teaser')) {
var teaserKeys = topicsData.map(function (topic) { var teaserKeys = topicsData.map(topic => 'post:' + topic.teaserPid);
return 'post:' + topic.teaserPid;
});
db.getObjectsFields(teaserKeys, ['timestamp'], next); db.getObjectsFields(teaserKeys, ['timestamp'], next);
} else { } else {
next(); next();
@ -214,16 +198,11 @@ function getMatchedPosts(pids, data, callback) {
if (!categoryFields.length) { if (!categoryFields.length) {
return next(); return next();
} }
var cids = topicsData.map(function (topic) { var cids = topicsData.map(topic => 'category:' + topic.cid);
return 'category:' + topic.cid;
});
db.getObjectsFields(cids, categoryFields, next); db.getObjectsFields(cids, categoryFields, next);
}, },
tags: function (next) { tags: function (next) {
if (Array.isArray(data.hasTags) && data.hasTags.length) { if (Array.isArray(data.hasTags) && data.hasTags.length) {
var tids = posts.map(function (post) {
return post && post.tid;
});
topics.getTopicsTags(tids, next); topics.getTopicsTags(tids, next);
} else { } else {
setImmediate(next); setImmediate(next);
@ -251,7 +230,7 @@ function getMatchedPosts(pids, data, callback) {
}, next); }, next);
}, },
function (results, next) { function (results, next) {
posts.forEach(function (post, index) { postsData.forEach(function (post, index) {
if (results.topics && results.topics[index]) { if (results.topics && results.topics[index]) {
post.topic = results.topics[index]; post.topic = results.topics[index];
if (results.topics[index].category) { if (results.topics[index].category) {
@ -267,11 +246,8 @@ function getMatchedPosts(pids, data, callback) {
} }
}); });
posts = posts.filter(function (post) { postsData = postsData.filter(post => post && post.topic && !post.topic.deleted);
return post && post.topic && parseInt(post.topic.deleted, 10) !== 1; next(null, postsData);
});
next(null, posts);
}, },
], callback); ], callback);
} }
@ -412,9 +388,7 @@ function getChildrenCids(cids, uid, callback) {
childrenCategories.forEach(function (childrens) { childrenCategories.forEach(function (childrens) {
categories.flattenCategories(allCategories, childrens); categories.flattenCategories(allCategories, childrens);
childrenCids = childrenCids.concat(allCategories.map(function (category) { childrenCids = childrenCids.concat(allCategories.map(category => category && category.cid));
return category && category.cid;
}));
}); });
next(null, childrenCids); next(null, childrenCids);

@ -10,7 +10,7 @@ var translator = require('../translator');
const intFields = [ const intFields = [
'tid', 'cid', 'uid', 'mainPid', 'deleted', 'locked', 'pinned', 'tid', 'cid', 'uid', 'mainPid', 'deleted', 'locked', 'pinned',
'timestamp', 'timestamp', 'upvotes', 'downvotes',
]; ];
module.exports = function (Topics) { module.exports = function (Topics) {
@ -88,7 +88,7 @@ module.exports = function (Topics) {
function escapeTitle(topicData) { function escapeTitle(topicData) {
if (topicData) { if (topicData) {
if (topicData.title) { if (topicData.title) {
topicData.title = translator.escape(validator.escape(topicData.title.toString())); topicData.title = translator.escape(validator.escape(String(topicData.title)));
} }
if (topicData.titleRaw) { if (topicData.titleRaw) {
topicData.titleRaw = translator.escape(topicData.titleRaw); topicData.titleRaw = translator.escape(topicData.titleRaw);
@ -117,12 +117,6 @@ function modifyTopic(topic) {
topic.lastposttimeISO = utils.toISOString(topic.lastposttime); topic.lastposttimeISO = utils.toISOString(topic.lastposttime);
} }
if (topic.hasOwnProperty('upvotes')) {
topic.upvotes = parseInt(topic.upvotes, 10) || 0;
}
if (topic.hasOwnProperty('upvotes')) {
topic.downvotes = parseInt(topic.downvotes, 10) || 0;
}
if (topic.hasOwnProperty('upvotes') && topic.hasOwnProperty('downvotes')) { if (topic.hasOwnProperty('upvotes') && topic.hasOwnProperty('downvotes')) {
topic.votes = topic.upvotes - topic.downvotes; topic.votes = topic.upvotes - topic.downvotes;
} }

@ -251,7 +251,7 @@ describe('Post\'s', function () {
assert.ifError(err); assert.ifError(err);
posts.getPostField(replyPid, 'deleted', function (err, isDeleted) { posts.getPostField(replyPid, 'deleted', function (err, isDeleted) {
assert.ifError(err); assert.ifError(err);
assert.equal(parseInt(isDeleted, 10), 1); assert.strictEqual(isDeleted, 1);
done(); done();
}); });
}); });
@ -262,7 +262,7 @@ describe('Post\'s', function () {
assert.ifError(err); assert.ifError(err);
posts.getPostField(replyPid, 'deleted', function (err, isDeleted) { posts.getPostField(replyPid, 'deleted', function (err, isDeleted) {
assert.ifError(err); assert.ifError(err);
assert.equal(parseInt(isDeleted, 10), 0); assert.strictEqual(isDeleted, 0);
done(); done();
}); });
}); });
@ -591,6 +591,14 @@ describe('Post\'s', function () {
}); });
describe('parse', function () { describe('parse', function () {
it('should not crash and return falsy if post data is falsy', function (done) {
posts.parsePost(null, function (err, postData) {
assert.ifError(err);
assert.strictEqual(postData, null);
done();
});
});
it('should store post content in cache', function (done) { it('should store post content in cache', function (done) {
var oldValue = global.env; var oldValue = global.env;
global.env = 'production'; global.env = 'production';

@ -218,7 +218,18 @@ describe('Topic\'s', function () {
it('should not receive errors', function (done) { it('should not receive errors', function (done) {
topics.getTopicData(newTopic.tid, done); topics.getTopicData(newTopic.tid, function (err, topicData) {
assert.ifError(err);
assert(typeof topicData.tid === 'number');
assert(typeof topicData.uid === 'number');
assert(typeof topicData.cid === 'number');
assert(typeof topicData.mainPid === 'number');
assert(typeof topicData.deleted === 'number');
assert(typeof topicData.locked === 'number');
assert(typeof topicData.pinned === 'number');
assert(typeof topicData.timestamp === 'number');
done();
});
}); });
it('should get topic title by pid', function (done) { it('should get topic title by pid', function (done) {

Loading…
Cancel
Save