chore: some optimizations for codeclimate

v1.18.x
Julian Lam 4 years ago
parent ebcb664b41
commit 4a63c20a72

@ -19,10 +19,6 @@ const socketTopics = require('../../socket.io/topics'); // eehhh...
const Posts = module.exports;
Posts.edit = async (req, res) => {
if (meta.config.minimumPostLength !== 0 && !req.body.content) {
throw new Error('[[error:invalid-data]]');
}
// Trim and remove HTML (latter for composers that send in HTML, like redactor)
var contentLen = utils.stripHTMLTags(req.body.content).trim().length;
@ -146,11 +142,17 @@ Posts.delete = async (req, res) => {
helpers.formatApiResponse(200, res);
};
Posts.vote = async (req, res) => {
async function mock(req) {
const tid = await posts.getPostField(req.params.pid, 'tid');
const data = { pid: req.params.pid, room_id: `topic_${tid}` };
const socketMock = { uid: req.user.uid };
return { data, socketMock };
}
Posts.vote = async (req, res) => {
const { data, socketMock } = await mock();
if (req.body.delta > 0) {
await socketPostHelpers.postCommand(socketMock, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data);
} else if (req.body.delta < 0) {
@ -163,27 +165,21 @@ Posts.vote = async (req, res) => {
};
Posts.unvote = async (req, res) => {
const tid = await posts.getPostField(req.params.pid, 'tid');
const data = { pid: req.params.pid, room_id: `topic_${tid}` };
const socketMock = { uid: req.user.uid };
const { data, socketMock } = await mock();
await socketPostHelpers.postCommand(socketMock, 'unvote', 'voted', '', data);
helpers.formatApiResponse(200, res);
};
Posts.bookmark = async (req, res) => {
const tid = await posts.getPostField(req.params.pid, 'tid');
const data = { pid: req.params.pid, room_id: `topic_${tid}` };
const socketMock = { uid: req.user.uid };
const { data, socketMock } = await mock();
await socketPostHelpers.postCommand(socketMock, 'bookmark', 'bookmarked', '', data);
helpers.formatApiResponse(200, res);
};
Posts.unbookmark = async (req, res) => {
const tid = await posts.getPostField(req.params.pid, 'tid');
const data = { pid: req.params.pid, room_id: `topic_${tid}` };
const socketMock = { uid: req.user.uid };
const { data, socketMock } = await mock();
await socketPostHelpers.postCommand(socketMock, 'unbookmark', 'bookmarked', '', data);
helpers.formatApiResponse(200, res);

@ -8,6 +8,8 @@ const util = require('util');
const db = require('../database');
const pubsub = require('../pubsub');
const plugins = require('../plugins');
const utils = require('../utils');
const Meta = require('./index');
const cacheBuster = require('./cacheBuster');
const defaults = require('../../install/data/defaults');
@ -147,6 +149,46 @@ Configs.remove = async function (field) {
await db.deleteObjectField('config', field);
};
Configs.registerHooks = () => {
plugins.registerHook('core', {
hook: 'filter:settings.set',
method: async ({ plugin, settings, quiet }) => {
if (plugin === 'core.api' && Array.isArray(settings.tokens)) {
// Generate tokens if not present already
settings.tokens.forEach((set) => {
if (set.token === '') {
set.token = utils.generateUUID();
}
if (isNaN(parseInt(set.uid, 10))) {
set.uid = 0;
}
});
}
return { plugin, settings, quiet };
},
});
plugins.registerHook('core', {
hook: 'filter:settings.get',
method: async ({ plugin, values }) => {
if (plugin === 'core.api' && Array.isArray(values.tokens)) {
values.tokens = values.tokens.map((tokenObj) => {
tokenObj.uid = parseInt(tokenObj.uid, 10);
if (tokenObj.timestamp) {
tokenObj.timestampISO = new Date(parseInt(tokenObj.timestamp, 10)).toISOString();
}
return tokenObj;
});
}
return { plugin, values };
},
});
};
Configs.cookie = {
get: () => {
const cookie = {};

@ -10,7 +10,7 @@ const request = require('request-promise-native');
const user = require('../user');
const posts = require('../posts');
const utils = require('../utils');
const meta = require('../meta');
const { pluginNamePattern, themeNamePattern, paths } = require('../constants');
@ -123,69 +123,9 @@ Plugins.reload = async function () {
console.log('');
}
// Possibly put these in a different file...
Plugins.registerHook('core', {
hook: 'filter:parse.post',
method: async (data) => {
data.postData.content = posts.sanitize(data.postData.content);
return data;
},
});
Plugins.registerHook('core', {
hook: 'filter:parse.raw',
method: async content => posts.sanitize(content),
});
Plugins.registerHook('core', {
hook: 'filter:parse.aboutme',
method: async content => posts.sanitize(content),
});
Plugins.registerHook('core', {
hook: 'filter:parse.signature',
method: async (data) => {
data.userData.signature = posts.sanitize(data.userData.signature);
return data;
},
});
Plugins.registerHook('core', {
hook: 'filter:settings.set',
method: async ({ plugin, settings, quiet }) => {
if (plugin === 'core.api' && Array.isArray(settings.tokens)) {
// Generate tokens if not present already
settings.tokens.forEach((set) => {
if (set.token === '') {
set.token = utils.generateUUID();
}
if (isNaN(parseInt(set.uid, 10))) {
set.uid = 0;
}
});
}
return { plugin, settings, quiet };
},
});
Plugins.registerHook('core', {
hook: 'filter:settings.get',
method: async ({ plugin, values }) => {
if (plugin === 'core.api' && Array.isArray(values.tokens)) {
values.tokens = values.tokens.map((tokenObj) => {
tokenObj.uid = parseInt(tokenObj.uid, 10);
if (tokenObj.timestamp) {
tokenObj.timestampISO = new Date(parseInt(tokenObj.timestamp, 10)).toISOString();
}
return tokenObj;
});
}
return { plugin, values };
},
});
// Core hooks
posts.registerHooks();
meta.configs.registerHooks();
// Lower priority runs earlier
Object.keys(Plugins.loadedHooks).forEach(function (hook) {

@ -125,6 +125,34 @@ module.exports = function (Posts) {
sanitizeConfig = await plugins.fireHook('filter:sanitize.config', sanitizeConfig);
};
Posts.registerHooks = () => {
plugins.registerHook('core', {
hook: 'filter:parse.post',
method: async (data) => {
data.postData.content = Posts.sanitize(data.postData.content);
return data;
},
});
plugins.registerHook('core', {
hook: 'filter:parse.raw',
method: async content => Posts.sanitize(content),
});
plugins.registerHook('core', {
hook: 'filter:parse.aboutme',
method: async content => Posts.sanitize(content),
});
plugins.registerHook('core', {
hook: 'filter:parse.signature',
method: async (data) => {
data.userData.signature = Posts.sanitize(data.userData.signature);
return data;
},
});
};
function sanitizeSignature(signature) {
signature = translator.escape(signature);
var tagsToStrip = [];

Loading…
Cancel
Save