codeclimate made me make my code ugly 😢

v1.18.x
Julian Lam 7 years ago
parent 9197879291
commit a1338221bf

@ -8,16 +8,15 @@ var db = require('../database');
var plugins = require('../plugins'); var plugins = require('../plugins');
var translator = require('../translator'); var translator = require('../translator');
module.exports = function (Posts) { var Diffs = {};
Posts.diffs = {};
Posts.diffs.exists = function (pid, callback) { Diffs.exists = function (pid, callback) {
db.listLength('post:' + pid + ':diffs', function (err, numDiffs) { db.listLength('post:' + pid + ':diffs', function (err, numDiffs) {
return callback(err, !!numDiffs); return callback(err, !!numDiffs);
}); });
}; };
Posts.diffs.get = function (pid, since, callback) { Diffs.get = function (pid, since, callback) {
async.waterfall([ async.waterfall([
async.apply(db.getListRange.bind(db), 'post:' + pid + ':diffs', 0, -1), async.apply(db.getListRange.bind(db), 'post:' + pid + ':diffs', 0, -1),
function (timestamps, next) { function (timestamps, next) {
@ -33,11 +32,11 @@ module.exports = function (Posts) {
], callback); ], callback);
}; };
Posts.diffs.list = function (pid, callback) { Diffs.list = function (pid, callback) {
db.getListRange('post:' + pid + ':diffs', 0, -1, callback); db.getListRange('post:' + pid + ':diffs', 0, -1, callback);
}; };
Posts.diffs.save = function (pid, oldContent, newContent, callback) { Diffs.save = function (pid, oldContent, newContent, callback) {
const now = Date.now(); const now = Date.now();
const patch = diff.createPatch('', newContent, oldContent); const patch = diff.createPatch('', newContent, oldContent);
async.parallel([ async.parallel([
@ -52,7 +51,9 @@ module.exports = function (Posts) {
}); });
}; };
Posts.diffs.load = function (pid, since, uid, callback) { Diffs.load = function (pid, since, uid, callback) {
var Posts = require('../posts');
// Retrieves all diffs made since `since` and replays them to reconstruct what the post looked like at `since` // Retrieves all diffs made since `since` and replays them to reconstruct what the post looked like at `since`
since = parseInt(since, 10); since = parseInt(since, 10);
@ -70,6 +71,21 @@ module.exports = function (Posts) {
return callback(err); return callback(err);
} }
postDiffLoad(data);
async.waterfall([
function (next) {
plugins.fireHook('filter:parse.post', { postData: data.post }, next);
},
function (data, next) {
data.postData.content = translator.escape(data.postData.content);
next(null, data.postData);
},
], callback);
});
};
function postDiffLoad(data) {
data.post = data.post[0]; data.post = data.post[0];
data.post.content = validator.unescape(data.post.content); data.post.content = validator.unescape(data.post.content);
@ -85,16 +101,12 @@ module.exports = function (Posts) {
data.post.editor = null; data.post.editor = null;
data.post.content = String(data.post.content || ''); data.post.content = String(data.post.content || '');
}
async.waterfall([ module.exports = function (Posts) {
function (next) { Posts.diffs = {};
plugins.fireHook('filter:parse.post', { postData: data.post }, next);
}, Object.keys(Diffs).forEach(function (property) {
function (data, next) { Posts.diffs[property] = Diffs[property];
data.postData.content = translator.escape(data.postData.content);
next(null, data.postData);
},
], callback);
}); });
}; };
};

Loading…
Cancel
Save