Skip to content

Node.js based forum software built for the modern web

topics data refactor

Barış Soner Uşaklıb2b33ffa57d7
diff --git a/src/database/mongo/hash.js b/src/database/mongo/hash.js
index 1298e6e292..05e2407f24 100644
--- a/src/database/mongo/hash.js
+++ b/src/database/mongo/hash.js
@@ -108,7 +108,11 @@ module.exports = function (db, module) {
return getFromCache();
}
- db.collection('objects').find({ _key: { $in: nonCachedKeys } }, { projection: { _id: 0 } }).toArray(function (err, data) {
+ var query = { _key: { $in: nonCachedKeys } };
+ if (nonCachedKeys.length === 1) {
+ query._key = nonCachedKeys[0];
+ }
+ db.collection('objects').find(query, { projection: { _id: 0 } }).toArray(function (err, data) {
if (err) {
return callback(err);
}
diff --git a/src/notifications.js b/src/notifications.js
index ac79722384..13a9376713 100644
--- a/src/notifications.js
+++ b/src/notifications.js
@@ -133,8 +133,8 @@ Notifications.findRelated = function (mergeIds, set, callback) {
db.getObjectsFields(keys, ['mergeId'], next);
},
function (sets, next) {
- sets = sets.map(set => set.mergeId);
- var mergeSet = new Set(mergeIds);
+ sets = sets.map(set => String(set.mergeId));
+ var mergeSet = new Set(mergeIds.map(id => String(id)));
next(null, nids.filter((nid, idx) => mergeSet.has(sets[idx])));
},
], callback);
diff --git a/src/topics/data.js b/src/topics/data.js
index 80f4969103..f66f2725ab 100644
--- a/src/topics/data.js
+++ b/src/topics/data.js
@@ -8,52 +8,14 @@ var categories = require('../categories');
var utils = require('../utils');
var translator = require('../translator');
-function escapeTitle(topicData) {
- if (!topicData) {
- return;
- }
- if (topicData.title) {
- topicData.title = translator.escape(validator.escape(topicData.title.toString()));
- }
- if (topicData.titleRaw) {
- topicData.titleRaw = translator.escape(topicData.titleRaw);
- }
-}
+const intFields = ['tid', 'cid', 'uid', 'mainPid', 'deleted', 'locked', 'pinned'];
module.exports = function (Topics) {
- Topics.getTopicField = function (tid, field, callback) {
- async.waterfall([
- function (next) {
- db.getObjectField('topic:' + tid, field, next);
- },
- function (value, next) {
- if (field === 'title') {
- value = translator.escape(validator.escape(String(value)));
- }
- next(null, value);
- },
- ], callback);
- };
-
- Topics.getTopicFields = function (tid, fields, callback) {
- async.waterfall([
- function (next) {
- db.getObjectFields('topic:' + tid, fields, next);
- },
- function (topic, next) {
- escapeTitle(topic);
- next(null, topic);
- },
- ], callback);
- };
-
Topics.getTopicsFields = function (tids, fields, callback) {
if (!Array.isArray(tids) || !tids.length) {
return callback(null, []);
}
- var keys = tids.map(function (tid) {
- return 'topic:' + tid;
- });
+ var keys = tids.map(tid => 'topic:' + tid);
async.waterfall([
function (next) {
if (fields.length) {
@@ -69,53 +31,28 @@ module.exports = function (Topics) {
], callback);
};
+ Topics.getTopicField = function (tid, field, callback) {
+ Topics.getTopicFields(tid, [field], function (err, topic) {
+ callback(err, topic ? topic[field] : null);
+ });
+ };
+
+ Topics.getTopicFields = function (tid, fields, callback) {
+ Topics.getTopicsFields([tid], fields, function (err, topics) {
+ callback(err, topics ? topics[0] : null);
+ });
+ };
+
Topics.getTopicData = function (tid, callback) {
- async.waterfall([
- function (next) {
- db.getObject('topic:' + tid, next);
- },
- function (topic, next) {
- if (!topic) {
- return next(null, null);
- }
- modifyTopic(topic);
- next(null, topic);
- },
- ], callback);
+ Topics.getTopicsFields([tid], [], function (err, topics) {
+ callback(err, topics && topics.length ? topics[0] : null);
+ });
};
Topics.getTopicsData = function (tids, callback) {
Topics.getTopicsFields(tids, [], callback);
};
- function modifyTopic(topic) {
- if (!topic) {
- return;
- }
- if (topic.hasOwnProperty('title')) {
- topic.titleRaw = topic.title;
- topic.title = String(topic.title);
- }
-
- escapeTitle(topic);
- if (topic.hasOwnProperty('timestamp')) {
- topic.timestampISO = utils.toISOString(topic.timestamp);
- }
- if (topic.hasOwnProperty('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')) {
- topic.votes = topic.upvotes - topic.downvotes;
- }
- }
-
Topics.getCategoryData = function (tid, callback) {
async.waterfall([
function (next) {
@@ -144,3 +81,52 @@ module.exports = function (Topics) {
db.deleteObjectFields('topic:' + tid, fields, callback);
};
};
+
+function escapeTitle(topicData) {
+ if (topicData) {
+ if (topicData.title) {
+ topicData.title = translator.escape(validator.escape(topicData.title.toString()));
+ }
+ if (topicData.titleRaw) {
+ topicData.titleRaw = translator.escape(topicData.titleRaw);
+ }
+ }
+}
+
+function modifyTopic(topic) {
+ if (!topic) {
+ return;
+ }
+
+ intFields.forEach(field => parseIntField(topic, field));
+
+ if (topic.hasOwnProperty('title')) {
+ topic.titleRaw = topic.title;
+ topic.title = String(topic.title);
+ }
+
+ escapeTitle(topic);
+
+ if (topic.hasOwnProperty('timestamp')) {
+ topic.timestampISO = utils.toISOString(topic.timestamp);
+ }
+ if (topic.hasOwnProperty('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')) {
+ topic.votes = topic.upvotes - topic.downvotes;
+ }
+}
+
+function parseIntField(topic, field) {
+ if (topic.hasOwnProperty(field)) {
+ topic[field] = parseInt(topic[field], 10);
+ }
+}
diff --git a/src/user/categories.js b/src/user/categories.js
index c88eac802d..e5417849ab 100644
--- a/src/user/categories.js
+++ b/src/user/categories.js
@@ -26,7 +26,7 @@ module.exports = function (User) {
const ignored = new Set(results.ignored);
var watched = results.all.filter(function (cid) {
- return cid && !ignored.has(cid);
+ return cid && !ignored.has(String(cid));
});
next(null, watched);
},
diff --git a/src/user/data.js b/src/user/data.js
index 25739eda03..f157b06ad4 100644
--- a/src/user/data.js
+++ b/src/user/data.js
@@ -113,25 +113,23 @@ module.exports = function (User) {
};
function uidsToUsers(uids, uniqueUids, usersData) {
- var ref = uniqueUids.reduce(function (memo, cur, idx) {
- memo[cur] = idx;
+ var uidToUser = uniqueUids.reduce(function (memo, cur, idx) {
+ memo[cur] = usersData[idx];
return memo;
}, {});
var users = uids.map(function (uid) {
- const returnPayload = usersData[ref[uid]];
+ const returnPayload = uidToUser[uid];
if (uid > 0 && !returnPayload.uid) {
returnPayload.oldUid = parseInt(uid, 10);
}
- return usersData[ref[uid]];
+ return returnPayload;
});
return users;
}
function uidsToUserKeys(uids) {
- return uids.map(function (uid) {
- return 'user:' + uid;
- });
+ return uids.map(uid => 'user:' + uid);
}
function modifyUserData(users, fieldsToRemove, callback) {