fix: #9395, pass all data from client to Topics.reply (#9396)

* fix: #9395, pass all data from client to Topics.reply

so plugins can set custom fields
refactor and use setDefaultPostData

* fix: circular json error

* refactor: change params
v1.18.x
Barış Soner Uşaklı 4 years ago committed by GitHub
parent 33fbfdfe40
commit a8f7b24452
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -10,6 +10,13 @@ const socketHelpers = require('../socket.io/helpers');
const websockets = require('../socket.io');
const events = require('../events');
exports.setDefaultPostData = function (reqOrSocket, data) {
data.uid = reqOrSocket.uid;
data.req = exports.buildReqObject(reqOrSocket, { ...data });
data.timestamp = Date.now();
data.fromQueue = false;
};
// creates a slimmed down version of the request object
exports.buildReqObject = (req, payload) => {
req = req || {};

@ -34,10 +34,7 @@ topicsAPI.create = async function (caller, data) {
const payload = { ...data };
payload.tags = payload.tags || [];
payload.uid = caller.uid;
payload.req = apiHelpers.buildReqObject(caller);
payload.timestamp = Date.now();
payload.fromQueue = false;
apiHelpers.setDefaultPostData(caller, payload);
// Blacklist & Post Queue
await meta.blacklist.test(caller.ip);
@ -58,19 +55,8 @@ topicsAPI.create = async function (caller, data) {
};
topicsAPI.reply = async function (caller, data) {
const payload = {
tid: data.tid,
uid: caller.uid,
req: apiHelpers.buildReqObject(caller), // For IP recording
content: data.content,
timestamp: Date.now(),
fromQueue: false,
};
if (data.toPid) { payload.toPid = data.toPid; }
if (data.handle && !parseInt(caller.uid, 10)) {
payload.handle = data.handle;
}
const payload = { ...data };
apiHelpers.setDefaultPostData(caller, payload);
// Blacklist & Post Queue
await meta.blacklist.test(caller.ip);

@ -24,7 +24,7 @@ module.exports = function (Posts) {
throw new Error('[[error:invalid-uid]]');
}
if (data.toPid && !utils.isNumber(data.toPid)) {
if (data.hasOwnProperty('toPid') && !utils.isNumber(data.toPid)) {
throw new Error('[[error:invalid-pid]]');
}

@ -18,10 +18,8 @@ const apiHelpers = require('../api/helpers');
const SocketHelpers = module.exports;
SocketHelpers.setDefaultPostData = function (data, socket) {
data.uid = socket.uid;
data.req = apiHelpers.buildReqObject(socket);
data.timestamp = Date.now();
data.fromQueue = false;
websockets.warnDeprecated(socket, 'apiHelpers.setDefaultPostData');
apiHelpers.setDefaultPostData(socket, data);
};
SocketHelpers.notifyNew = async function (uid, type, result) {

@ -10,6 +10,7 @@ const user = require('../user');
const socketHelpers = require('./helpers');
const utils = require('../utils');
const api = require('../api');
const apiHelpers = require('../api/helpers');
const sockets = require('.');
@ -29,7 +30,7 @@ SocketPosts.reply = async function (socket, data) {
throw new Error('[[error:invalid-data]]');
}
socketHelpers.setDefaultPostData(data, socket);
apiHelpers.setDefaultPostData(socket, data);
await meta.blacklist.test(data.req.ip);
const shouldQueue = await posts.shouldQueue(socket.uid, data);
if (shouldQueue) {

Loading…
Cancel
Save