|
|
@ -8,52 +8,14 @@ var categories = require('../categories');
|
|
|
|
var utils = require('../utils');
|
|
|
|
var utils = require('../utils');
|
|
|
|
var translator = require('../translator');
|
|
|
|
var translator = require('../translator');
|
|
|
|
|
|
|
|
|
|
|
|
function escapeTitle(topicData) {
|
|
|
|
const intFields = ['tid', 'cid', 'uid', 'mainPid', 'deleted', 'locked', 'pinned'];
|
|
|
|
if (!topicData) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (topicData.title) {
|
|
|
|
|
|
|
|
topicData.title = translator.escape(validator.escape(topicData.title.toString()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (topicData.titleRaw) {
|
|
|
|
|
|
|
|
topicData.titleRaw = translator.escape(topicData.titleRaw);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = function (Topics) {
|
|
|
|
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) {
|
|
|
|
Topics.getTopicsFields = function (tids, fields, callback) {
|
|
|
|
if (!Array.isArray(tids) || !tids.length) {
|
|
|
|
if (!Array.isArray(tids) || !tids.length) {
|
|
|
|
return callback(null, []);
|
|
|
|
return callback(null, []);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var keys = tids.map(function (tid) {
|
|
|
|
var keys = tids.map(tid => 'topic:' + tid);
|
|
|
|
return 'topic:' + tid;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
async.waterfall([
|
|
|
|
function (next) {
|
|
|
|
function (next) {
|
|
|
|
if (fields.length) {
|
|
|
|
if (fields.length) {
|
|
|
@ -69,35 +31,82 @@ module.exports = function (Topics) {
|
|
|
|
], callback);
|
|
|
|
], 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) {
|
|
|
|
Topics.getTopicData = function (tid, callback) {
|
|
|
|
|
|
|
|
Topics.getTopicsFields([tid], [], function (err, topics) {
|
|
|
|
|
|
|
|
callback(err, topics && topics.length ? topics[0] : null);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Topics.getTopicsData = function (tids, callback) {
|
|
|
|
|
|
|
|
Topics.getTopicsFields(tids, [], callback);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Topics.getCategoryData = function (tid, callback) {
|
|
|
|
async.waterfall([
|
|
|
|
async.waterfall([
|
|
|
|
function (next) {
|
|
|
|
function (next) {
|
|
|
|
db.getObject('topic:' + tid, next);
|
|
|
|
Topics.getTopicField(tid, 'cid', next);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
function (topic, next) {
|
|
|
|
function (cid, next) {
|
|
|
|
if (!topic) {
|
|
|
|
categories.getCategoryData(cid, next);
|
|
|
|
return next(null, null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
modifyTopic(topic);
|
|
|
|
|
|
|
|
next(null, topic);
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
], callback);
|
|
|
|
], callback);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Topics.getTopicsData = function (tids, callback) {
|
|
|
|
Topics.setTopicField = function (tid, field, value, callback) {
|
|
|
|
Topics.getTopicsFields(tids, [], callback);
|
|
|
|
db.setObjectField('topic:' + tid, field, value, callback);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function modifyTopic(topic) {
|
|
|
|
Topics.setTopicFields = function (tid, data, callback) {
|
|
|
|
|
|
|
|
callback = callback || function () {};
|
|
|
|
|
|
|
|
db.setObject('topic:' + tid, data, callback);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Topics.deleteTopicField = function (tid, field, callback) {
|
|
|
|
|
|
|
|
db.deleteObjectField('topic:' + tid, field, callback);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Topics.deleteTopicFields = function (tid, fields, callback) {
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (!topic) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
intFields.forEach(field => parseIntField(topic, field));
|
|
|
|
|
|
|
|
|
|
|
|
if (topic.hasOwnProperty('title')) {
|
|
|
|
if (topic.hasOwnProperty('title')) {
|
|
|
|
topic.titleRaw = topic.title;
|
|
|
|
topic.titleRaw = topic.title;
|
|
|
|
topic.title = String(topic.title);
|
|
|
|
topic.title = String(topic.title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
escapeTitle(topic);
|
|
|
|
escapeTitle(topic);
|
|
|
|
|
|
|
|
|
|
|
|
if (topic.hasOwnProperty('timestamp')) {
|
|
|
|
if (topic.hasOwnProperty('timestamp')) {
|
|
|
|
topic.timestampISO = utils.toISOString(topic.timestamp);
|
|
|
|
topic.timestampISO = utils.toISOString(topic.timestamp);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -114,33 +123,10 @@ module.exports = function (Topics) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Topics.getCategoryData = function (tid, callback) {
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
|
|
|
function (next) {
|
|
|
|
|
|
|
|
Topics.getTopicField(tid, 'cid', next);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
function (cid, next) {
|
|
|
|
|
|
|
|
categories.getCategoryData(cid, next);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
], callback);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Topics.setTopicField = function (tid, field, value, callback) {
|
|
|
|
|
|
|
|
db.setObjectField('topic:' + tid, field, value, callback);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Topics.setTopicFields = function (tid, data, callback) {
|
|
|
|
|
|
|
|
callback = callback || function () {};
|
|
|
|
|
|
|
|
db.setObject('topic:' + tid, data, callback);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Topics.deleteTopicField = function (tid, field, callback) {
|
|
|
|
|
|
|
|
db.deleteObjectField('topic:' + tid, field, callback);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Topics.deleteTopicFields = function (tid, fields, callback) {
|
|
|
|
function parseIntField(topic, field) {
|
|
|
|
db.deleteObjectFields('topic:' + tid, fields, callback);
|
|
|
|
if (topic.hasOwnProperty(field)) {
|
|
|
|
};
|
|
|
|
topic[field] = parseInt(topic[field], 10);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|