Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 33 additions & 40 deletions app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { normalizeTransferredByData, parseAgentCustomFields, updateDepartmentAge
import { Apps, AppEvents } from '../../../apps/server';
import { businessHourManager } from '../business-hour';
import notifications from '../../../notifications/server/lib/Notifications';
import { validateEmailDomain } from '../../../lib/server';

export const Livechat = {
Analytics,
Expand Down Expand Up @@ -227,62 +228,54 @@ export const Livechat = {
const updateUser = {
$set: {
token,
...phone?.number ? { phone: [{ phoneNumber: phone.number }] } : {},
...name ? { name } : {},
},
};

if (email) {
email = email.trim();
validateEmailDomain(email);
updateUser.$set.visitorEmails = [
{ address: email },
];
}

if (department) {
const dep = LivechatDepartment.findOneByIdOrName(department);
if (!dep) {
throw new Meteor.Error('error-invalid-department', 'The provided department is invalid', { method: 'registerGuest' });
}
updateUser.$set.department = dep._id;
}

const user = LivechatVisitors.getVisitorByToken(token, { fields: { _id: 1 } });
let existingUser = null;

if (user) {
userId = user._id;
} else if (email && (existingUser = LivechatVisitors.findOneGuestByEmailAddress(email))) {
userId = existingUser._id;
} else {
if (!username) {
username = LivechatVisitors.getNextVisitorUsername();
}

let existingUser = null;
const userData = {
username,
ts: new Date(),
};

if (s.trim(email) !== '' && (existingUser = LivechatVisitors.findOneGuestByEmailAddress(email))) {
userId = existingUser._id;
} else {
const userData = {
username,
ts: new Date(),
};

if (settings.get('Livechat_Allow_collect_and_store_HTTP_header_informations')) {
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;
}
if (settings.get('Livechat_Allow_collect_and_store_HTTP_header_informations')) {
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);
}
}

if (phone?.number) {
updateUser.$set.phone = [
{ phoneNumber: phone.number },
];
}

if (email && email.trim() !== '') {
updateUser.$set.visitorEmails = [
{ address: email },
];
}

if (name) {
updateUser.$set.name = name;
}

if (!department) {
Object.assign(updateUser, { $unset: { department: 1 } });
} else {
const dep = LivechatDepartment.findOneByIdOrName(department);
updateUser.$set.department = dep && dep._id;
userId = LivechatVisitors.insert(userData);
}

LivechatVisitors.updateById(userId, updateUser);
Expand Down