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

[NEW] [Apps-Engine] New Livechat event handlers #17033

Merged
merged 9 commits into from
Apr 30, 2020
22 changes: 16 additions & 6 deletions app/apps/server/bridges/listeners.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AppInterface } from '@rocket.chat/apps-engine/server/compiler';

export class AppListenerBridge {
constructor(orch) {
this.orch = orch;
Expand Down Expand Up @@ -54,13 +56,21 @@ export class AppListenerBridge {
// }
}

async livechatEvent(inte, room) {
const rm = this.orch.getConverters().get('rooms').convertRoom(room);
const result = await this.orch.getManager().getListenerManager().executeListener(inte, rm);
async livechatEvent(inte, data) {
switch (inte) {
case AppInterface.IPostLivechatRoomStarted:
case AppInterface.IPostLivechatRoomClosed:
const room = this.orch.getConverters().get('rooms').convertRoom(data);

if (typeof result === 'boolean') {
return result;
return this.orch.getManager().getListenerManager().executeListener(inte, room);
case AppInterface.IPostLivechatAgentAssigned:
case AppInterface.IPostLivechatAgentUnassigned:
return this.orch.getManager().getListenerManager().executeListener(inte, {
room: this.orch.getConverters().get('rooms').convertRoom(data.room),
agent: this.orch.getConverters().get('users').convertToApp(data.user),
});
default:
break;
}
return this.orch.getConverters().get('rooms').convertAppRoom(result);
}
}
9 changes: 9 additions & 0 deletions app/livechat/server/lib/Helper.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AppInterface } from '@rocket.chat/apps-engine/server/compiler';
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';
import { MongoInternals } from 'meteor/mongo';
Expand All @@ -7,6 +8,7 @@ import { Livechat } from './Livechat';
import { RoutingManager } from './RoutingManager';
import { callbacks } from '../../../callbacks/server';
import { settings } from '../../../settings';
import { Apps } from '../../../apps/server';

export const createLivechatRoom = (rid, name, guest, roomInfo = {}, extraData = {}) => {
check(rid, String);
Expand Down Expand Up @@ -42,6 +44,8 @@ export const createLivechatRoom = (rid, name, guest, roomInfo = {}, extraData =
}, extraRoomInfo);

const roomId = Rooms.insert(room);

Apps.getBridges().getListenerBridge().livechatEvent(AppInterface.IPostLivechatRoomStarted, room);
callbacks.run('livechat.newRoom', room);
return roomId;
};
Expand Down Expand Up @@ -157,8 +161,13 @@ export const createLivechatQueueView = () => {
};

export const removeAgentFromSubscription = (rid, { _id, username }) => {
const room = LivechatRooms.findOneById(rid);
const user = Users.findOneById(_id);

Subscriptions.removeByRoomIdAndUserId(rid, _id);
Messages.createUserLeaveWithRoomIdAndUser(rid, { _id, username });

Apps.getBridges().getListenerBridge().livechatEvent(AppInterface.IPostLivechatAgentUnassigned, { room, user });
};

export const normalizeAgent = (agentId) => {
Expand Down
2 changes: 1 addition & 1 deletion app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ export const Livechat = {
Messages.createCommandWithRoomIdAndUser('promptTranscript', rid, closeData.closedBy);

Meteor.defer(() => {
Apps.getBridges().getListenerBridge().livechatEvent(AppInterface.ILivechatRoomClosedHandler, room);
Apps.getBridges().getListenerBridge().livechatEvent(AppInterface.IPostLivechatRoomClosed, room);
shiqimei marked this conversation as resolved.
Show resolved Hide resolved
callbacks.run('livechat.closeRoom', room);
});

Expand Down
7 changes: 7 additions & 0 deletions app/livechat/server/lib/RoutingManager.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AppInterface } from '@rocket.chat/apps-engine/server/compiler';
import { Meteor } from 'meteor/meteor';
import { Match, check } from 'meteor/check';

Expand All @@ -11,6 +12,7 @@ import { createLivechatSubscription,
} from './Helper';
import { callbacks } from '../../../callbacks/server';
import { LivechatRooms, Rooms, Messages, Users, LivechatInquiry } from '../../../models/server';
import { Apps } from '../../../apps/server';

export const RoutingManager = {
methodName: null,
Expand Down Expand Up @@ -74,8 +76,12 @@ export const RoutingManager = {
Rooms.incUsersCountById(rid);

const user = Users.findOneById(agent.agentId);
const room = LivechatRooms.findOneById(rid);

Messages.createCommandWithRoomIdAndUser('connected', rid, user);
dispatchAgentDelegated(rid, agent.agentId);

Apps.getBridges().getListenerBridge().livechatEvent(AppInterface.IPostLivechatAgentAssigned, { room, user });
return inquiry;
},

Expand All @@ -98,6 +104,7 @@ export const RoutingManager = {
}

const { servedBy } = room;

if (servedBy) {
removeAgentFromSubscription(rid, servedBy);
LivechatRooms.removeAgentByRoomId(rid);
Expand Down