mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
chore: some optimizations for codeclimate
This commit is contained in:
@@ -19,10 +19,6 @@ const socketTopics = require('../../socket.io/topics'); // eehhh...
|
|||||||
const Posts = module.exports;
|
const Posts = module.exports;
|
||||||
|
|
||||||
Posts.edit = async (req, res) => {
|
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)
|
// Trim and remove HTML (latter for composers that send in HTML, like redactor)
|
||||||
var contentLen = utils.stripHTMLTags(req.body.content).trim().length;
|
var contentLen = utils.stripHTMLTags(req.body.content).trim().length;
|
||||||
|
|
||||||
@@ -146,11 +142,17 @@ Posts.delete = async (req, res) => {
|
|||||||
helpers.formatApiResponse(200, res);
|
helpers.formatApiResponse(200, res);
|
||||||
};
|
};
|
||||||
|
|
||||||
Posts.vote = async (req, res) => {
|
async function mock(req) {
|
||||||
const tid = await posts.getPostField(req.params.pid, 'tid');
|
const tid = await posts.getPostField(req.params.pid, 'tid');
|
||||||
const data = { pid: req.params.pid, room_id: `topic_${tid}` };
|
const data = { pid: req.params.pid, room_id: `topic_${tid}` };
|
||||||
const socketMock = { uid: req.user.uid };
|
const socketMock = { uid: req.user.uid };
|
||||||
|
|
||||||
|
return { data, socketMock };
|
||||||
|
}
|
||||||
|
|
||||||
|
Posts.vote = async (req, res) => {
|
||||||
|
const { data, socketMock } = await mock();
|
||||||
|
|
||||||
if (req.body.delta > 0) {
|
if (req.body.delta > 0) {
|
||||||
await socketPostHelpers.postCommand(socketMock, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data);
|
await socketPostHelpers.postCommand(socketMock, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data);
|
||||||
} else if (req.body.delta < 0) {
|
} else if (req.body.delta < 0) {
|
||||||
@@ -163,27 +165,21 @@ Posts.vote = async (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Posts.unvote = async (req, res) => {
|
Posts.unvote = async (req, res) => {
|
||||||
const tid = await posts.getPostField(req.params.pid, 'tid');
|
const { data, socketMock } = await mock();
|
||||||
const data = { pid: req.params.pid, room_id: `topic_${tid}` };
|
|
||||||
const socketMock = { uid: req.user.uid };
|
|
||||||
|
|
||||||
await socketPostHelpers.postCommand(socketMock, 'unvote', 'voted', '', data);
|
await socketPostHelpers.postCommand(socketMock, 'unvote', 'voted', '', data);
|
||||||
helpers.formatApiResponse(200, res);
|
helpers.formatApiResponse(200, res);
|
||||||
};
|
};
|
||||||
|
|
||||||
Posts.bookmark = async (req, res) => {
|
Posts.bookmark = async (req, res) => {
|
||||||
const tid = await posts.getPostField(req.params.pid, 'tid');
|
const { data, socketMock } = await mock();
|
||||||
const data = { pid: req.params.pid, room_id: `topic_${tid}` };
|
|
||||||
const socketMock = { uid: req.user.uid };
|
|
||||||
|
|
||||||
await socketPostHelpers.postCommand(socketMock, 'bookmark', 'bookmarked', '', data);
|
await socketPostHelpers.postCommand(socketMock, 'bookmark', 'bookmarked', '', data);
|
||||||
helpers.formatApiResponse(200, res);
|
helpers.formatApiResponse(200, res);
|
||||||
};
|
};
|
||||||
|
|
||||||
Posts.unbookmark = async (req, res) => {
|
Posts.unbookmark = async (req, res) => {
|
||||||
const tid = await posts.getPostField(req.params.pid, 'tid');
|
const { data, socketMock } = await mock();
|
||||||
const data = { pid: req.params.pid, room_id: `topic_${tid}` };
|
|
||||||
const socketMock = { uid: req.user.uid };
|
|
||||||
|
|
||||||
await socketPostHelpers.postCommand(socketMock, 'unbookmark', 'bookmarked', '', data);
|
await socketPostHelpers.postCommand(socketMock, 'unbookmark', 'bookmarked', '', data);
|
||||||
helpers.formatApiResponse(200, res);
|
helpers.formatApiResponse(200, res);
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ const util = require('util');
|
|||||||
|
|
||||||
const db = require('../database');
|
const db = require('../database');
|
||||||
const pubsub = require('../pubsub');
|
const pubsub = require('../pubsub');
|
||||||
|
const plugins = require('../plugins');
|
||||||
|
const utils = require('../utils');
|
||||||
const Meta = require('./index');
|
const Meta = require('./index');
|
||||||
const cacheBuster = require('./cacheBuster');
|
const cacheBuster = require('./cacheBuster');
|
||||||
const defaults = require('../../install/data/defaults');
|
const defaults = require('../../install/data/defaults');
|
||||||
@@ -147,6 +149,46 @@ Configs.remove = async function (field) {
|
|||||||
await db.deleteObjectField('config', 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 = {
|
Configs.cookie = {
|
||||||
get: () => {
|
get: () => {
|
||||||
const cookie = {};
|
const cookie = {};
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ const request = require('request-promise-native');
|
|||||||
|
|
||||||
const user = require('../user');
|
const user = require('../user');
|
||||||
const posts = require('../posts');
|
const posts = require('../posts');
|
||||||
const utils = require('../utils');
|
const meta = require('../meta');
|
||||||
|
|
||||||
const { pluginNamePattern, themeNamePattern, paths } = require('../constants');
|
const { pluginNamePattern, themeNamePattern, paths } = require('../constants');
|
||||||
|
|
||||||
@@ -123,69 +123,9 @@ Plugins.reload = async function () {
|
|||||||
console.log('');
|
console.log('');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Possibly put these in a different file...
|
// Core hooks
|
||||||
Plugins.registerHook('core', {
|
posts.registerHooks();
|
||||||
hook: 'filter:parse.post',
|
meta.configs.registerHooks();
|
||||||
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 };
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// Lower priority runs earlier
|
// Lower priority runs earlier
|
||||||
Object.keys(Plugins.loadedHooks).forEach(function (hook) {
|
Object.keys(Plugins.loadedHooks).forEach(function (hook) {
|
||||||
|
|||||||
@@ -125,6 +125,34 @@ module.exports = function (Posts) {
|
|||||||
sanitizeConfig = await plugins.fireHook('filter:sanitize.config', sanitizeConfig);
|
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) {
|
function sanitizeSignature(signature) {
|
||||||
signature = translator.escape(signature);
|
signature = translator.escape(signature);
|
||||||
var tagsToStrip = [];
|
var tagsToStrip = [];
|
||||||
|
|||||||
Reference in New Issue
Block a user