From 4a63c20a72edb92379fc88769cc2e26da5c6abe3 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 8 Oct 2020 15:58:42 -0400 Subject: [PATCH] chore: some optimizations for codeclimate --- src/controllers/write/posts.js | 24 +++++------- src/meta/configs.js | 42 +++++++++++++++++++++ src/plugins/index.js | 68 ++-------------------------------- src/posts/parse.js | 28 ++++++++++++++ 4 files changed, 84 insertions(+), 78 deletions(-) diff --git a/src/controllers/write/posts.js b/src/controllers/write/posts.js index 07d5e9c1db..4d6ac7b182 100644 --- a/src/controllers/write/posts.js +++ b/src/controllers/write/posts.js @@ -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); diff --git a/src/meta/configs.js b/src/meta/configs.js index 55cdfaa42c..a01654f7d3 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -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 = {}; diff --git a/src/plugins/index.js b/src/plugins/index.js index 47aa0c8635..172dceedec 100644 --- a/src/plugins/index.js +++ b/src/plugins/index.js @@ -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) { diff --git a/src/posts/parse.js b/src/posts/parse.js index 87e8e7f44d..b07a39a64f 100644 --- a/src/posts/parse.js +++ b/src/posts/parse.js @@ -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 = [];