Skip to content

Commit

Permalink
[NEW] [Apps-Engine] New Livechat event handlers (#17033)
Browse files Browse the repository at this point in the history
* Add livechat missing event handlers

* Rely on removeAgentFromSubscription

Co-authored-by: Renato Becker <renato.augusto.becker@gmail.com>
Co-authored-by: Douglas Gubert <douglas.gubert@gmail.com>
  • Loading branch information
3 people committed May 1, 2020
1 parent 9559d3c commit 629cbfb
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
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
5 changes: 5 additions & 0 deletions app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,12 @@ export const Livechat = {
Messages.createCommandWithRoomIdAndUser('promptTranscript', rid, closeData.closedBy);

Meteor.defer(() => {
/**
* @deprecated the `AppInterface.ILivechatRoomClosedHandler` event will be removed
* in the next major version of the Apps-Engine
*/
Apps.getBridges().getListenerBridge().livechatEvent(AppInterface.ILivechatRoomClosedHandler, room);
Apps.getBridges().getListenerBridge().livechatEvent(AppInterface.IPostLivechatRoomClosed, room);
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
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
"@nivo/heatmap": "^0.61.0",
"@nivo/line": "^0.61.1",
"@nivo/pie": "^0.61.1",
"@rocket.chat/apps-engine": "^1.14.0-beta.3119",
"@rocket.chat/apps-engine": "^1.15.0-alpha.3202",
"@rocket.chat/fuselage": "^0.7.1",
"@rocket.chat/fuselage-hooks": "^0.7.1",
"@rocket.chat/fuselage-polyfills": "^0.7.1",
Expand Down

0 comments on commit 629cbfb

Please sign in to comment.