diff --git a/app/apps/server/bridges/listeners.js b/app/apps/server/bridges/listeners.js index 693c55271c31..259b937b1686 100644 --- a/app/apps/server/bridges/listeners.js +++ b/app/apps/server/bridges/listeners.js @@ -1,3 +1,5 @@ +import { AppInterface } from '@rocket.chat/apps-engine/server/compiler'; + export class AppListenerBridge { constructor(orch) { this.orch = orch; @@ -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); } } diff --git a/app/livechat/server/lib/Helper.js b/app/livechat/server/lib/Helper.js index bfa83dc0d742..5a9e78e63464 100644 --- a/app/livechat/server/lib/Helper.js +++ b/app/livechat/server/lib/Helper.js @@ -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'; @@ -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); @@ -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; }; @@ -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) => { diff --git a/app/livechat/server/lib/Livechat.js b/app/livechat/server/lib/Livechat.js index 501e53406591..4050d17bfeaa 100644 --- a/app/livechat/server/lib/Livechat.js +++ b/app/livechat/server/lib/Livechat.js @@ -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); }); diff --git a/app/livechat/server/lib/RoutingManager.js b/app/livechat/server/lib/RoutingManager.js index 3b65ba14ceaa..2f4579f29b7b 100644 --- a/app/livechat/server/lib/RoutingManager.js +++ b/app/livechat/server/lib/RoutingManager.js @@ -1,3 +1,4 @@ +import { AppInterface } from '@rocket.chat/apps-engine/server/compiler'; import { Meteor } from 'meteor/meteor'; import { Match, check } from 'meteor/check'; @@ -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, @@ -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; }, @@ -98,6 +104,7 @@ export const RoutingManager = { } const { servedBy } = room; + if (servedBy) { removeAgentFromSubscription(rid, servedBy); LivechatRooms.removeAgentByRoomId(rid); diff --git a/package-lock.json b/package-lock.json index 1afa69f2f29b..4732a19cc78b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2928,9 +2928,9 @@ } }, "@rocket.chat/apps-engine": { - "version": "1.14.0-beta.3119", - "resolved": "https://registry.npmjs.org/@rocket.chat/apps-engine/-/apps-engine-1.14.0-beta.3119.tgz", - "integrity": "sha512-SoQicHOGkQD6wwcnMzc1qETbNoMwQ2ei5j5krzh0/dachCbHG+C1rBO8yYbK2TQwuSjxR0wLM20ZbM57iHaYKw==", + "version": "1.15.0-alpha.3202", + "resolved": "https://registry.npmjs.org/@rocket.chat/apps-engine/-/apps-engine-1.15.0-alpha.3202.tgz", + "integrity": "sha512-cNPPjTJE6V6OHp/6vY86LFOKEaaMELWocDxncMp2JetyYinYsU8rlHu8s/KHz7Nhptka5YMkXsZ4ozseld/wuw==", "requires": { "adm-zip": "^0.4.9", "cryptiles": "^4.1.3", @@ -6895,8 +6895,8 @@ "integrity": "sha1-WYc/Nej89sc2HBAjkmHXbhU0i7I=" }, "adm-zip": { - "version": "0.4.12", - "resolved": "github:RocketChat/adm-zip#34ac787ce7f45c6cea99df049deb17d0c476a3b5" + "version": "github:RocketChat/adm-zip#34ac787ce7f45c6cea99df049deb17d0c476a3b5", + "from": "github:RocketChat/adm-zip" }, "aggregate-error": { "version": "3.0.1", diff --git a/package.json b/package.json index 29f7fd599db5..3082257f2656 100644 --- a/package.json +++ b/package.json @@ -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",