Skip to content

Commit

Permalink
[FIX] Missing connection headers on Livechat REST API (#14130)
Browse files Browse the repository at this point in the history
  • Loading branch information
renatobecker authored and sampaiodiego committed Apr 12, 2019
1 parent 4987ca2 commit d5c14f7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
4 changes: 4 additions & 0 deletions app/livechat/server/api/lib/livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export function findAgent(agentId) {
return Users.getAgentInfo(agentId);
}

export function normalizeHttpHeaderData(headers = {}) {
const httpHeaders = Object.assign({}, headers);
return { httpHeaders };
}
export function settings() {
const initSettings = Livechat.getInitSettings();
const triggers = findTriggers();
Expand Down
8 changes: 6 additions & 2 deletions app/livechat/server/api/v1/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Messages, Rooms, LivechatVisitors } from '../../../../models';
import { hasPermission } from '../../../../authorization';
import { API } from '../../../../api';
import { loadMessageHistory } from '../../../../lib';
import { findGuest, findRoom } from '../lib/livechat';
import { findGuest, findRoom, normalizeHttpHeaderData } from '../lib/livechat';
import { Livechat } from '../../lib/Livechat';

API.v1.addRoute('livechat/message', {
Expand Down Expand Up @@ -238,7 +238,11 @@ API.v1.addRoute('livechat/messages', { authRequired: true }, {
}
} else {
rid = Random.id();
const visitorId = Livechat.registerGuest(this.bodyParams.visitor);

const guest = this.bodyParams.visitor;
guest.connectionData = normalizeHttpHeaderData(this.request.headers);

const visitorId = Livechat.registerGuest(guest);
visitor = LivechatVisitors.findOneById(visitorId);
}

Expand Down
3 changes: 2 additions & 1 deletion app/livechat/server/api/v1/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Match, check } from 'meteor/check';
import { Rooms, LivechatVisitors, LivechatCustomField } from '../../../../models';
import { hasPermission } from '../../../../authorization';
import { API } from '../../../../api';
import { findGuest } from '../lib/livechat';
import { findGuest, normalizeHttpHeaderData } from '../lib/livechat';
import { Livechat } from '../../lib/Livechat';

API.v1.addRoute('livechat/visitor', {
Expand Down Expand Up @@ -34,6 +34,7 @@ API.v1.addRoute('livechat/visitor', {
guest.phone = { number: this.bodyParams.visitor.phone };
}

guest.connectionData = normalizeHttpHeaderData(this.request.headers);
const visitorId = Livechat.registerGuest(guest);

let visitor = LivechatVisitors.getVisitorByToken(token);
Expand Down
14 changes: 8 additions & 6 deletions app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export const Livechat = {
return true;
},

registerGuest({ token, name, email, department, phone, username } = {}) {
registerGuest({ token, name, email, department, phone, username, connectionData } = {}) {
check(token, String);

let userId;
Expand Down Expand Up @@ -204,12 +204,14 @@ export const Livechat = {
username,
};

const storeHttpHeaderData = settings.get('Livechat_Allow_collect_and_store_HTTP_header_informations');
if (settings.get('Livechat_Allow_collect_and_store_HTTP_header_informations')) {

if (this.connection && storeHttpHeaderData) {
userData.userAgent = this.connection.httpHeaders['user-agent'];
userData.ip = this.connection.httpHeaders['x-real-ip'] || this.connection.httpHeaders['x-forwarded-for'] || this.connection.clientAddress;
userData.host = this.connection.httpHeaders.host;
const connection = this.connection || connectionData;
if (connection && connection.httpHeaders) {
userData.userAgent = connection.httpHeaders['user-agent'];
userData.ip = connection.httpHeaders['x-real-ip'] || connection.httpHeaders['x-forwarded-for'] || connection.clientAddress;
userData.host = connection.httpHeaders.host;
}
}

userId = LivechatVisitors.insert(userData);
Expand Down

0 comments on commit d5c14f7

Please sign in to comment.