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

refactor: Livechat functions to Typescript #30631

Merged
merged 11 commits into from
Oct 16, 2023
2 changes: 1 addition & 1 deletion apps/meteor/app/apps/server/bridges/livechat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export class AppLivechatBridge extends LivechatBridge {
throw new Error('Could not get the message converter to process livechat room messages');
}

const livechatMessages = await Livechat.getRoomMessages({ rid: roomId });
const livechatMessages = await LivechatTyped.getRoomMessages({ rid: roomId });

return Promise.all(livechatMessages.map((message) => messageConverter.convertMessage(message) as Promise<IAppsEngineMesage>));
}
Expand Down
10 changes: 4 additions & 6 deletions apps/meteor/app/livechat/imports/server/rest/departments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '../../../server/api/lib/departments';
import { DepartmentHelper } from '../../../server/lib/Departments';
import { Livechat } from '../../../server/lib/Livechat';
import { Livechat as LivechatTs } from '../../../server/lib/LivechatTyped';

API.v1.addRoute(
'livechat/department',
Expand Down Expand Up @@ -192,7 +193,7 @@ API.v1.addRoute(
},
{
async post() {
await Livechat.archiveDepartment(this.urlParams._id);
await LivechatTs.archiveDepartment(this.urlParams._id);

return API.v1.success();
},
Expand All @@ -207,11 +208,8 @@ API.v1.addRoute(
},
{
async post() {
if (await Livechat.unarchiveDepartment(this.urlParams._id)) {
return API.v1.success();
}

return API.v1.failure();
await LivechatTs.unarchiveDepartment(this.urlParams._id);
return API.v1.success();
},
},
);
Expand Down
13 changes: 9 additions & 4 deletions apps/meteor/app/livechat/server/api/v1/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { callbacks } from '../../../../../lib/callbacks';
import { i18n } from '../../../../../server/lib/i18n';
import { API } from '../../../../api/server';
import { isWidget } from '../../../../api/server/helpers/isWidget';
import { canAccessRoomAsync } from '../../../../authorization/server';
import { canAccessRoomAsync, roomAccessAttributes } from '../../../../authorization/server';
import { hasPermissionAsync } from '../../../../authorization/server/functions/hasPermission';
import { addUserToRoom } from '../../../../lib/server/functions/addUserToRoom';
import { settings as rcSettings } from '../../../../settings/server';
Expand Down Expand Up @@ -352,7 +352,12 @@ API.v1.addRoute(

API.v1.addRoute(
'livechat/room.visitor',
{ authRequired: true, permissionsRequired: ['view-l-room'], validateParams: isPUTLivechatRoomVisitorParams, deprecationVersion: '7.0.0' },
{
authRequired: true,
permissionsRequired: ['change-livechat-room-visitor'],
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
validateParams: isPUTLivechatRoomVisitorParams,
deprecationVersion: '7.0.0',
},
{
async put() {
// This endpoint is deprecated and will be removed in future versions.
Expand All @@ -363,7 +368,7 @@ API.v1.addRoute(
throw new Error('invalid-visitor');
}

const room = await LivechatRooms.findOneById(rid, { _id: 1, v: 1 }); // TODO: check _id
const room = await LivechatRooms.findOneById(rid, { projection: { ...roomAccessAttributes, _id: 1, t: 1 } }); // TODO: check _id
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
if (!room) {
throw new Error('invalid-room');
}
Expand All @@ -373,7 +378,7 @@ API.v1.addRoute(
throw new Error('invalid-room-visitor');
}

const roomAfterChange = await Livechat.changeRoomVisitor(this.userId, rid, visitor);
const roomAfterChange = await LivechatTyped.changeRoomVisitor(this.userId, room, visitor);

if (!roomAfterChange) {
return API.v1.failure();
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/livechat/server/api/v1/videoCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { i18n } from '../../../../../server/lib/i18n';
import { API } from '../../../../api/server';
import { canSendMessageAsync } from '../../../../authorization/server/functions/canSendMessage';
import { settings as rcSettings } from '../../../../settings/server';
import { Livechat } from '../../lib/Livechat';
import { Livechat } from '../../lib/LivechatTyped';
import { settings } from '../lib/livechat';

API.v1.addRoute(
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/livechat/server/api/v1/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ API.v1.addRoute('livechat/visitor.callStatus', {
if (!guest) {
throw new Meteor.Error('invalid-token');
}
await Livechat.updateCallStatus(callId, rid, callStatus, guest);
await LivechatTyped.updateCallStatus(callId, rid, callStatus, guest);
return API.v1.success({ token, callStatus });
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isOmnichannelRoom } from '@rocket.chat/core-typings';

import { callbacks } from '../../../../lib/callbacks';
import { Livechat } from '../lib/Livechat';
import { Livechat } from '../lib/LivechatTyped';

callbacks.add(
'livechat.newRoom',
Expand Down
125 changes: 1 addition & 124 deletions apps/meteor/app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import dns from 'dns';
import util from 'util';

import { Message, VideoConf, api } from '@rocket.chat/core-services';
import { Message } from '@rocket.chat/core-services';
import { Logger } from '@rocket.chat/logger';
import {
LivechatVisitors,
Expand All @@ -30,7 +30,6 @@ import { trim } from '../../../../lib/utils/stringUtils';
import { i18n } from '../../../../server/lib/i18n';
import { addUserRolesAsync } from '../../../../server/lib/roles/addUserRoles';
import { removeUserFromRolesAsync } from '../../../../server/lib/roles/removeUserFromRoles';
import { canAccessRoomAsync, roomAccessAttributes } from '../../../authorization/server';
import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission';
import { FileUpload } from '../../../file-upload/server';
import { deleteMessage } from '../../../lib/server/functions/deleteMessage';
Expand Down Expand Up @@ -718,40 +717,6 @@ export const Livechat = {
return ret;
},

async unarchiveDepartment(_id) {
check(_id, String);

const department = await LivechatDepartmentRaw.findOneById(_id, { projection: { _id: 1 } });

if (!department) {
throw new Meteor.Error('department-not-found', 'Department not found', {
method: 'livechat:removeDepartment',
});
}

// TODO: these kind of actions should be on events instead of here
await LivechatDepartmentAgents.enableAgentsByDepartmentId(_id);
return LivechatDepartmentRaw.unarchiveDepartment(_id);
},

async archiveDepartment(_id) {
check(_id, String);

const department = await LivechatDepartmentRaw.findOneById(_id, { projection: { _id: 1 } });

if (!department) {
throw new Meteor.Error('department-not-found', 'Department not found', {
method: 'livechat:removeDepartment',
});
}

await LivechatDepartmentAgents.disableAgentsByDepartmentId(_id);
await LivechatDepartmentRaw.archiveDepartment(_id);

this.logger.debug({ msg: 'Running livechat.afterDepartmentArchived callback for department:', departmentId: _id });
await callbacks.run('livechat.afterDepartmentArchived', department);
},

showConnecting() {
const { showConnecting } = RoutingManager.getConfig();
return showConnecting;
Expand All @@ -767,28 +732,6 @@ export const Livechat = {
});
},

async getRoomMessages({ rid }) {
check(rid, String);

const room = await Rooms.findOneById(rid, { projection: { t: 1 } });
if (room?.t !== 'l') {
throw new Meteor.Error('invalid-room');
}

const ignoredMessageTypes = [
'livechat_navigation_history',
'livechat_transcript_history',
'command',
'livechat-close',
'livechat-started',
'livechat_video_call',
];

return Messages.findVisibleByRoomIdNotContainingTypes(rid, ignoredMessageTypes, {
sort: { ts: 1 },
}).toArray();
},

async requestTranscript({ rid, email, subject, user }) {
check(rid, String);
check(email, String);
Expand Down Expand Up @@ -892,77 +835,11 @@ export const Livechat = {
});
},

async notifyAgentStatusChanged(userId, status) {
callbacks.runAsync('livechat.agentStatusChanged', { userId, status });
if (!settings.get('Livechat_show_agent_info')) {
return;
}

await LivechatRooms.findOpenByAgent(userId).forEach((room) => {
void api.broadcast('omnichannel.room', room._id, {
type: 'agentStatus',
status,
});
});
},

async allowAgentChangeServiceStatus(statusLivechat, agentId) {
if (statusLivechat !== 'available') {
return true;
}

return businessHourManager.allowAgentChangeServiceStatus(agentId);
},

notifyRoomVisitorChange(roomId, visitor) {
void api.broadcast('omnichannel.room', roomId, {
type: 'visitorData',
visitor,
});
},

async changeRoomVisitor(userId, roomId, visitor) {
const user = await Users.findOneById(userId);
if (!user) {
throw new Error('error-user-not-found');
}

if (!(await hasPermissionAsync(userId, 'change-livechat-room-visitor'))) {
throw new Error('error-not-authorized');
}

const room = await LivechatRooms.findOneById(roomId, { ...roomAccessAttributes, _id: 1, t: 1 });

if (!room) {
throw new Meteor.Error('invalid-room');
}

if (!(await canAccessRoomAsync(room, user))) {
throw new Error('error-not-allowed');
}

await LivechatRooms.changeVisitorByRoomId(room._id, visitor);

Livechat.notifyRoomVisitorChange(room._id, visitor);

return LivechatRooms.findOneById(roomId);
},
async updateLastChat(contactId, lastChat) {
const updateUser = {
$set: {
lastChat,
},
};
await LivechatVisitors.updateById(contactId, updateUser);
},
async updateCallStatus(callId, rid, status, user) {
await Rooms.setCallStatus(rid, status);
if (status === 'ended' || status === 'declined') {
if (await VideoConf.declineLivechatCall(callId)) {
return;
}

return updateMessage({ _id: callId, msg: status, actionLinks: [], webRtcCallEndTs: new Date() }, user);
}
},
};
Loading
Loading