Skip to content

Commit

Permalink
chore: some optimizations for codeclimate
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Oct 8, 2020
1 parent ebcb664 commit 4a63c20
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 78 deletions.
24 changes: 10 additions & 14 deletions src/controllers/write/posts.js
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
42 changes: 42 additions & 0 deletions src/meta/configs.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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 = {};
Expand Down
68 changes: 4 additions & 64 deletions src/plugins/index.js
Expand Up @@ -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');

Expand Down Expand Up @@ -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) {
Expand Down
28 changes: 28 additions & 0 deletions src/posts/parse.js
Expand Up @@ -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 = [];
Expand Down

0 comments on commit 4a63c20

Please sign in to comment.