Skip to content

Commit

Permalink
refactor: remove sockets.reqFromSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Oct 17, 2020
1 parent 8fd3c04 commit bc880ee
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 67 deletions.
30 changes: 30 additions & 0 deletions src/api/helpers.js
@@ -0,0 +1,30 @@
'use strict';

const url = require('url');

// creates a slimmed down version of the request object
exports.buildReqObject = (req, payload) => {
req = req || {};
const headers = req.headers || {};
const encrypted = req.connection ? !!req.connection.encrypted : false;
let host = headers.host;
const referer = headers.referer || '';

if (!host) {
host = url.parse(referer).host || '';
}

return {
uid: req.uid,
params: req.params,
method: req.method,
body: payload || req.body,
ip: req.ip,
host: host,
protocol: encrypted ? 'https' : 'http',
secure: encrypted,
url: referer,
path: referer.substr(referer.indexOf(host) + host.length),
headers: headers,
};
};
6 changes: 3 additions & 3 deletions src/api/topics.js
Expand Up @@ -7,7 +7,7 @@ const meta = require('../meta');
const events = require('../events');
const privileges = require('../privileges');

const controllerHelpers = require('../controllers/helpers');
const apiHelpers = require('./helpers');
const socketHelpers = require('../socket.io/helpers');

const topicsAPI = module.exports;
Expand All @@ -21,7 +21,7 @@ topicsAPI.create = async function (caller, data) {
payload.tags = payload.tags || [];
payload.uid = caller.uid;
payload.uid = caller.uid;
payload.req = controllerHelpers.buildReqObject(caller);
payload.req = apiHelpers.buildReqObject(caller);
payload.timestamp = Date.now();
payload.fromQueue = false;

Expand All @@ -46,7 +46,7 @@ topicsAPI.reply = async function (caller, data) {
var payload = {
tid: data.tid,
uid: caller.uid,
req: controllerHelpers.buildReqObject(caller), // For IP recording
req: apiHelpers.buildReqObject(caller), // For IP recording
content: data.content,
timestamp: data.timestamp,
fromQueue: false,
Expand Down
32 changes: 0 additions & 32 deletions src/controllers/helpers.js
Expand Up @@ -3,7 +3,6 @@
const nconf = require('nconf');
const validator = require('validator');
const querystring = require('querystring');
const url = require('url');
const _ = require('lodash');

const user = require('../user');
Expand All @@ -14,8 +13,6 @@ const meta = require('../meta');
const middleware = require('../middleware');
const translator = require('../translator');

const websockets = require('../socket.io');

const isLanguageKey = /^\[\[[\w.\-_:]+]]$/;
const helpers = module.exports;

Expand Down Expand Up @@ -427,33 +424,4 @@ helpers.generateError = (statusCode, message) => {
return payload;
};

helpers.buildReqObject = (req) => {
// If a socket object is received instead, handle accordingly
if (req.id) {
return websockets.reqFromSocket(req);
}

var headers = req.headers;
var encrypted = !!req.connection.encrypted;
var host = headers.host;
var referer = headers.referer || '';
if (!host) {
host = url.parse(referer).host || '';
}

return {
uid: req.uid,
params: req.params,
method: req.method,
body: req.body,
ip: req.ip,
host: host,
protocol: encrypted ? 'https' : 'http',
secure: encrypted,
url: referer,
path: referer.substr(referer.indexOf(host) + host.length),
headers: headers,
};
};

require('../promisify')(helpers);
3 changes: 2 additions & 1 deletion src/socket.io/helpers.js
Expand Up @@ -13,12 +13,13 @@ const notifications = require('../notifications');
const plugins = require('../plugins');
const utils = require('../utils');
const batch = require('../batch');
const apiHelpers = require('../api/helpers');

const SocketHelpers = module.exports;

SocketHelpers.setDefaultPostData = function (data, socket) {
data.uid = socket.uid;
data.req = websockets.reqFromSocket(socket);
data.req = apiHelpers.buildReqObject(socket);
data.timestamp = Date.now();
data.fromQueue = false;
};
Expand Down
31 changes: 2 additions & 29 deletions src/socket.io/index.js
Expand Up @@ -69,7 +69,7 @@ Sockets.init = function (server) {

function onConnection(socket) {
socket.ip = (socket.request.headers['x-forwarded-for'] || socket.request.connection.remoteAddress || '').split(',')[0];

socket.request.ip = socket.ip;
logger.io_one(socket, socket.uid);

onConnect(socket);
Expand Down Expand Up @@ -214,7 +214,7 @@ async function authorize(socket, callback) {
} else {
socket.uid = 0;
}

request.uid = socket.uid;
callback();
}

Expand All @@ -231,33 +231,6 @@ Sockets.getUserSocketCount = function (uid) {
return room ? room.length : 0;
};


Sockets.reqFromSocket = function (socket, payload, event) {
var headers = socket.request ? socket.request.headers : {};
var encrypted = socket.request ? !!socket.request.connection.encrypted : false;
var host = headers.host;
var referer = headers.referer || '';
var data = ((payload || {}).data || []);

if (!host) {
host = url.parse(referer).host || '';
}

return {
uid: socket.uid,
params: data[1],
method: event || data[0],
body: payload,
ip: socket.ip,
host: host,
protocol: encrypted ? 'https' : 'http',
secure: encrypted,
url: referer,
path: referer.substr(referer.indexOf(host) + host.length),
headers: headers,
};
};

Sockets.warnDeprecated = (socket, replacement) => {
if (socket.previousEvents) {
socket.emit('event:deprecated_call', {
Expand Down
3 changes: 2 additions & 1 deletion src/socket.io/posts/diffs.js
Expand Up @@ -3,6 +3,7 @@
const posts = require('../../posts');
const user = require('../../user');
const privileges = require('../../privileges');
const apiHelpers = require('../../api/helpers');
const websockets = require('..');

module.exports = function (SocketPosts) {
Expand Down Expand Up @@ -55,7 +56,7 @@ module.exports = function (SocketPosts) {
throw new Error('[[error:no-privileges]]');
}

const edit = await posts.diffs.restore(data.pid, data.since, socket.uid, websockets.reqFromSocket(socket));
const edit = await posts.diffs.restore(data.pid, data.since, socket.uid, apiHelpers.buildReqObject(socket));
websockets.in('topic_' + edit.topic.tid).emit('event:post_edited', edit);
};
};
3 changes: 2 additions & 1 deletion src/socket.io/posts/edit.js
Expand Up @@ -8,6 +8,7 @@ const groups = require('../../groups');
const events = require('../../events');
const meta = require('../../meta');
const utils = require('../../utils');
const apiHelpers = require('../../api/helpers');
const websockets = require('../index');

module.exports = function (SocketPosts) {
Expand All @@ -34,7 +35,7 @@ module.exports = function (SocketPosts) {
}

data.uid = socket.uid;
data.req = websockets.reqFromSocket(socket);
data.req = apiHelpers.buildReqObject(socket);

const editResult = await posts.edit(data);
if (editResult.topic.renamed) {
Expand Down

1 comment on commit bc880ee

@julianlam
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.