Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regression: Revert changes that restrict livechat visitors to their source type scope #33694

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 0 additions & 9 deletions .changeset/fuzzy-pans-share.md

This file was deleted.

37 changes: 5 additions & 32 deletions apps/meteor/app/apps/server/bridges/livechat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,6 @@ export class AppLivechatBridge extends LivechatBridge {
const appMessage = (await this.orch.getConverters().get('messages').convertAppMessage(message)) as IMessage | undefined;
const livechatMessage = appMessage as ILivechatMessage | undefined;

if (guest) {
const visitorSource = {
type: OmnichannelSourceType.APP,
id: appId,
alias: this.orch.getManager()?.getOneById(appId)?.getNameSlug(),
};
const fullVisitor = await LivechatVisitors.findOneEnabledByIdAndSource({
_id: guest._id,
sourceFilter: { 'source.type': visitorSource.type, 'source.id': visitorSource.id, 'source.alias': visitorSource.alias },
});
if (!fullVisitor?.source) {
await LivechatVisitors.setSourceById(guest._id, visitorSource);
}
}

const msg = await LivechatTyped.sendMessage({
guest: guest as ILivechatVisitor,
message: livechatMessage as ILivechatMessage,
Expand Down Expand Up @@ -301,7 +286,7 @@ export class AppLivechatBridge extends LivechatBridge {
}

return Promise.all(
(await LivechatVisitors.findEnabledBySource({ 'source.type': OmnichannelSourceType.APP, 'source.id': appId }, query).toArray()).map(
(await LivechatVisitors.findEnabled(query).toArray()).map(
async (visitor) => visitor && this.orch.getConverters()?.get('visitors').convertVisitor(visitor),
),
);
Expand All @@ -310,7 +295,7 @@ export class AppLivechatBridge extends LivechatBridge {
protected async findVisitorById(id: string, appId: string): Promise<IVisitor | undefined> {
this.orch.debugLog(`The App ${appId} is looking for livechat visitors.`);

return this.orch.getConverters()?.get('visitors').convertByIdAndSource(id, appId);
return this.orch.getConverters()?.get('visitors').convertById(id);
}

protected async findVisitorByEmail(email: string, appId: string): Promise<IVisitor | undefined> {
Expand All @@ -319,9 +304,7 @@ export class AppLivechatBridge extends LivechatBridge {
return this.orch
.getConverters()
?.get('visitors')
.convertVisitor(
await LivechatVisitors.findOneGuestByEmailAddressAndSource(email, { 'source.type': OmnichannelSourceType.APP, 'source.id': appId }),
);
.convertVisitor(await LivechatVisitors.findOneGuestByEmailAddress(email));
}

protected async findVisitorByToken(token: string, appId: string): Promise<IVisitor | undefined> {
Expand All @@ -330,12 +313,7 @@ export class AppLivechatBridge extends LivechatBridge {
return this.orch
.getConverters()
?.get('visitors')
.convertVisitor(
await LivechatVisitors.getVisitorByTokenAndSource({
token,
sourceFilter: { 'source.type': OmnichannelSourceType.APP, 'source.id': appId },
}),
);
.convertVisitor(await LivechatVisitors.getVisitorByToken(token, {}));
}

protected async findVisitorByPhoneNumber(phoneNumber: string, appId: string): Promise<IVisitor | undefined> {
Expand All @@ -344,12 +322,7 @@ export class AppLivechatBridge extends LivechatBridge {
return this.orch
.getConverters()
?.get('visitors')
.convertVisitor(
await LivechatVisitors.findOneVisitorByPhoneAndSource(phoneNumber, {
'source.type': OmnichannelSourceType.APP,
'source.id': appId,
}),
);
.convertVisitor(await LivechatVisitors.findOneVisitorByPhone(phoneNumber));
}

protected async findDepartmentByIdOrName(value: string, appId: string): Promise<IDepartment | undefined> {
Expand Down
21 changes: 0 additions & 21 deletions apps/meteor/app/apps/server/converters/visitors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { OmnichannelSourceType } from '@rocket.chat/core-typings';
import { LivechatVisitors } from '@rocket.chat/models';

import { transformMappedData } from './transformMappedData';
Expand All @@ -15,30 +14,12 @@ export class AppVisitorsConverter {
return this.convertVisitor(visitor);
}

async convertByIdAndSource(id, appId) {
const visitor = await LivechatVisitors.findOneEnabledByIdAndSource({
_id: id,
sourceFilter: { 'source.type': OmnichannelSourceType.APP, 'source.id': appId },
});

return this.convertVisitor(visitor);
}

async convertByToken(token) {
const visitor = await LivechatVisitors.getVisitorByToken(token);

return this.convertVisitor(visitor);
}

async convertByTokenAndSource(token, appId) {
const visitor = await LivechatVisitors.getVisitorByTokenAndSource({
token,
sourceFilter: { 'source.type': OmnichannelSourceType.APP, 'source.id': appId },
});

return this.convertVisitor(visitor);
}

async convertVisitor(visitor) {
if (!visitor) {
return undefined;
Expand All @@ -56,7 +37,6 @@ export class AppVisitorsConverter {
livechatData: 'livechatData',
status: 'status',
contactId: 'contactId',
source: 'source',
};

return transformMappedData(visitor, map);
Expand All @@ -76,7 +56,6 @@ export class AppVisitorsConverter {
livechatData: visitor.livechatData,
status: visitor.status || 'online',
contactId: visitor.contactId,
source: visitor.source,
...(visitor.visitorEmails && { visitorEmails: visitor.visitorEmails }),
...(visitor.department && { department: visitor.department }),
};
Expand Down
23 changes: 4 additions & 19 deletions apps/meteor/app/livechat/imports/server/rest/sms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
MessageAttachment,
ServiceData,
FileAttachmentProps,
IOmnichannelSource,
} from '@rocket.chat/core-typings';
import { OmnichannelSourceType } from '@rocket.chat/core-typings';
import { Logger } from '@rocket.chat/logger';
Expand Down Expand Up @@ -56,24 +55,10 @@ const defineDepartment = async (idOrName?: string) => {
return department?._id;
};

const defineVisitor = async (smsNumber: string, serviceName: string, destination: string, targetDepartment?: string) => {
const visitorSource: IOmnichannelSource = {
type: OmnichannelSourceType.SMS,
alias: serviceName,
};

const visitor = await LivechatVisitors.findOneVisitorByPhoneAndSource(
smsNumber,
{
'source.type': visitorSource.type,
'source.alias': visitorSource.alias,
},
{ projection: { token: 1 } },
);
visitorSource.destination = destination;
let data: { token: string; source: IOmnichannelSource; department?: string } = {
const defineVisitor = async (smsNumber: string, targetDepartment?: string) => {
const visitor = await LivechatVisitors.findOneVisitorByPhone(smsNumber);
let data: { token: string; department?: string } = {
token: visitor?.token || Random.id(),
source: visitorSource,
};

if (!visitor) {
Expand Down Expand Up @@ -132,7 +117,7 @@ API.v1.addRoute('livechat/sms-incoming/:service', {
targetDepartment = await defineDepartment(smsDepartment);
}

const visitor = await defineVisitor(sms.from, service, sms.to, targetDepartment);
const visitor = await defineVisitor(sms.from, targetDepartment);
if (!visitor) {
return API.v1.success(SMSService.error(new Error('Invalid visitor')));
}
Expand Down
12 changes: 1 addition & 11 deletions apps/meteor/app/livechat/imports/server/rest/upload.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { OmnichannelSourceType } from '@rocket.chat/core-typings';
import { LivechatVisitors, LivechatRooms } from '@rocket.chat/models';
import filesize from 'filesize';

import { API } from '../../../../api/server';
import { isWidget } from '../../../../api/server/helpers/isWidget';
import { getUploadFormData } from '../../../../api/server/lib/getUploadFormData';
import { FileUpload } from '../../../../file-upload/server';
import { settings } from '../../../../settings/server';
Expand All @@ -15,7 +13,6 @@ API.v1.addRoute('livechat/upload/:rid', {
if (!this.request.headers['x-visitor-token']) {
return API.v1.unauthorized();
}
const sourceType = isWidget(this.request.headers) ? OmnichannelSourceType.WIDGET : OmnichannelSourceType.API;

const canUpload = settings.get<boolean>('Livechat_fileupload_enabled') && settings.get<boolean>('FileUpload_Enabled');

Expand All @@ -26,10 +23,7 @@ API.v1.addRoute('livechat/upload/:rid', {
}

const visitorToken = this.request.headers['x-visitor-token'];
const visitor = await LivechatVisitors.getVisitorByTokenAndSource({
token: visitorToken as string,
sourceFilter: { 'source.type': sourceType },
});
const visitor = await LivechatVisitors.getVisitorByToken(visitorToken as string, {});

if (!visitor) {
return API.v1.unauthorized();
Expand Down Expand Up @@ -82,10 +76,6 @@ API.v1.addRoute('livechat/upload/:rid', {
return API.v1.failure('Invalid file');
}

if (!visitor.source) {
await LivechatVisitors.setSourceById(visitor._id, { type: sourceType });
}

uploadedFile.description = fields.description;

delete fields.description;
Expand Down
24 changes: 1 addition & 23 deletions apps/meteor/app/livechat/server/api/lib/livechat.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import type {
ILivechatAgent,
ILivechatDepartment,
ILivechatTrigger,
ILivechatVisitor,
IOmnichannelRoom,
OmnichannelSourceType,
} from '@rocket.chat/core-typings';
import type { ILivechatAgent, ILivechatDepartment, ILivechatTrigger, ILivechatVisitor, IOmnichannelRoom } from '@rocket.chat/core-typings';
import { License } from '@rocket.chat/license';
import { EmojiCustom, LivechatTrigger, LivechatVisitors, LivechatRooms, LivechatDepartment } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';
Expand Down Expand Up @@ -69,21 +62,6 @@ export function findGuest(token: string): Promise<ILivechatVisitor | null> {
});
}

export function findGuestBySource(token: string, sourceType: OmnichannelSourceType): Promise<ILivechatVisitor | null> {
const projection = {
name: 1,
username: 1,
token: 1,
visitorEmails: 1,
department: 1,
activity: 1,
contactId: 1,
source: 1,
};

return LivechatVisitors.getVisitorByTokenAndSource({ token, sourceFilter: { 'source.type': sourceType } }, { projection });
}

export function findGuestWithoutActivity(token: string): Promise<ILivechatVisitor | null> {
return LivechatVisitors.getVisitorByToken(token, { projection: { name: 1, username: 1, token: 1, visitorEmails: 1, department: 1 } });
}
Expand Down
27 changes: 6 additions & 21 deletions apps/meteor/app/livechat/server/api/v1/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { IOmnichannelSource } from '@rocket.chat/core-typings';
import { OmnichannelSourceType } from '@rocket.chat/core-typings';
import { LivechatVisitors, LivechatRooms, Messages } from '@rocket.chat/models';
import { Random } from '@rocket.chat/random';
Expand All @@ -19,17 +18,16 @@ import { loadMessageHistory } from '../../../../lib/server/functions/loadMessage
import { settings } from '../../../../settings/server';
import { normalizeMessageFileUpload } from '../../../../utils/server/functions/normalizeMessageFileUpload';
import { Livechat as LivechatTyped } from '../../lib/LivechatTyped';
import { findGuest, findGuestBySource, findRoom, normalizeHttpHeaderData } from '../lib/livechat';
import { findGuest, findRoom, normalizeHttpHeaderData } from '../lib/livechat';

API.v1.addRoute(
'livechat/message',
{ validateParams: isPOSTLivechatMessageParams },
{
async post() {
const { token, rid, agent, msg } = this.bodyParams;
const sourceType = isWidget(this.request.headers) ? OmnichannelSourceType.WIDGET : OmnichannelSourceType.API;

const guest = await findGuestBySource(token, sourceType);
const guest = await findGuest(token);
if (!guest) {
throw new Error('invalid-token');
}
Expand All @@ -50,10 +48,6 @@ API.v1.addRoute(
throw new Error('message-length-exceeds-character-limit');
}

if (!guest.source) {
await LivechatVisitors.setSourceById(guest._id, { type: sourceType });
}

const _id = this.bodyParams._id || Random.id();

const sendMessage = {
Expand All @@ -67,7 +61,7 @@ API.v1.addRoute(
agent,
roomInfo: {
source: {
type: sourceType,
type: isWidget(this.request.headers) ? OmnichannelSourceType.WIDGET : OmnichannelSourceType.API,
},
},
};
Expand Down Expand Up @@ -256,12 +250,8 @@ API.v1.addRoute(
{
async post() {
const visitorToken = this.bodyParams.visitor.token;
const sourceType = isWidget(this.request.headers) ? OmnichannelSourceType.WIDGET : OmnichannelSourceType.API;

const visitor = await LivechatVisitors.getVisitorByTokenAndSource(
{ token: visitorToken, sourceFilter: { 'source.type': sourceType } },
{},
);
const visitor = await LivechatVisitors.getVisitorByToken(visitorToken, {});
let rid: string;
if (visitor) {
const extraQuery = await callbacks.run('livechat.applyRoomRestrictions', {});
Expand All @@ -271,16 +261,11 @@ API.v1.addRoute(
} else {
rid = Random.id();
}

if (!visitor.source) {
await LivechatVisitors.setSourceById(visitor._id, { type: sourceType });
}
} else {
rid = Random.id();

const guest: typeof this.bodyParams.visitor & { connectionData?: unknown; source?: IOmnichannelSource } = this.bodyParams.visitor;
const guest: typeof this.bodyParams.visitor & { connectionData?: unknown } = this.bodyParams.visitor;
guest.connectionData = normalizeHttpHeaderData(this.request.headers);
guest.source = { type: sourceType };

const visitor = await LivechatTyped.registerGuest(guest);
if (!visitor) {
Expand All @@ -305,7 +290,7 @@ API.v1.addRoute(
},
roomInfo: {
source: {
type: sourceType,
type: isWidget(this.request.headers) ? OmnichannelSourceType.WIDGET : OmnichannelSourceType.API,
},
},
};
Expand Down
5 changes: 1 addition & 4 deletions apps/meteor/app/livechat/server/lib/LivechatTyped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ import { isDepartmentCreationAvailable } from './isDepartmentCreationAvailable';
import type { CloseRoomParams, CloseRoomParamsByUser, CloseRoomParamsByVisitor } from './localTypes';
import { parseTranscriptRequest } from './parseTranscriptRequest';

type RegisterGuestType = Partial<Pick<ILivechatVisitor, 'token' | 'name' | 'department' | 'status' | 'username' | 'source'>> & {
type RegisterGuestType = Partial<Pick<ILivechatVisitor, 'token' | 'name' | 'department' | 'status' | 'username'>> & {
id?: string;
connectionData?: any;
email?: string;
Expand Down Expand Up @@ -654,7 +654,6 @@ class LivechatClass {
username,
connectionData,
status = UserStatus.ONLINE,
source,
}: RegisterGuestType): Promise<ILivechatVisitor | null> {
check(token, String);
check(id, Match.Maybe(String));
Expand All @@ -664,7 +663,6 @@ class LivechatClass {
const visitorDataToUpdate: Partial<ILivechatVisitor> & { userAgent?: string; ip?: string; host?: string } = {
token,
status,
source,
...(phone?.number ? { phone: [{ phoneNumber: phone.number }] } : {}),
...(name ? { name } : {}),
};
Expand Down Expand Up @@ -710,7 +708,6 @@ class LivechatClass {
visitorDataToUpdate.username = username || (await LivechatVisitors.getNextVisitorUsername());
visitorDataToUpdate.status = status;
visitorDataToUpdate.ts = new Date();
visitorDataToUpdate.source = source;

if (settings.get('Livechat_Allow_collect_and_store_HTTP_header_informations') && Livechat.isValidObject(connectionData)) {
Livechat.logger.debug(`Saving connection data for visitor ${token}`);
Expand Down
Loading
Loading