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] New event interfaces for pre/post user leaving a room #20917

Merged
merged 6 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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: 9 additions & 0 deletions app/apps/server/bridges/listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export class AppListenerBridge {
case AppInterface.IPostRoomDeleted:
case AppInterface.IPreRoomUserJoined:
case AppInterface.IPostRoomUserJoined:
case AppInterface.IPreRoomUserLeave:
case AppInterface.IPostRoomUserLeave:
return 'roomEvent';
/**
* @deprecated please prefer the AppInterface.IPostLivechatRoomClosed event
Expand Down Expand Up @@ -76,6 +78,13 @@ export class AppListenerBridge {
joiningUser: this.orch.getConverters().get('users').convertToApp(joiningUser),
invitingUser: this.orch.getConverters().get('users').convertToApp(invitingUser),
};
case AppInterface.IPreRoomUserLeave:
case AppInterface.IPostRoomUserLeave:
const [leavingUser] = payload;
return {
room: rm,
leavingUser: this.orch.getConverters().get('users').convertToApp(leavingUser),
};
default:
return rm;
}
Expand Down
14 changes: 14 additions & 0 deletions app/lib/server/functions/removeUserFromRoom.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { AppsEngineException } from '@rocket.chat/apps-engine/definition/exceptions';
import { Meteor } from 'meteor/meteor';

import { Rooms, Messages, Subscriptions } from '../../../models';
import { AppEvents, Apps } from '../../../apps/server';
import { callbacks } from '../../../callbacks';

export const removeUserFromRoom = function(rid, user, options = {}) {
const room = Rooms.findOneById(rid);

if (room) {
try {
Promise.await(Apps.triggerEvent(AppEvents.IPreRoomUserLeave, room, user));
} catch (error) {
if (error instanceof AppsEngineException) {
throw new Meteor.Error('error-app-prevented', error.message);
}

throw error;
}

callbacks.run('beforeLeaveRoom', user, room);

const subscription = Subscriptions.findOneByRoomIdAndUserId(rid, user._id, { fields: { _id: 1 } });
Expand All @@ -31,6 +43,8 @@ export const removeUserFromRoom = function(rid, user, options = {}) {
Meteor.defer(function() {
// TODO: CACHE: maybe a queue?
callbacks.run('afterLeaveRoom', user, room);

Promise.await(Apps.triggerEvent(AppEvents.IPostRoomUserLeave, room, user));
});
}
};
6 changes: 3 additions & 3 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 @@ -134,7 +134,7 @@
"@nivo/heatmap": "^0.61.0",
"@nivo/line": "^0.61.1",
"@nivo/pie": "^0.61.1",
"@rocket.chat/apps-engine": "1.23.0",
"@rocket.chat/apps-engine": "1.24.0-alpha.4718",
"@rocket.chat/css-in-js": "^0.6.3-dev.180",
"@rocket.chat/emitter": "^0.6.3-dev.185",
"@rocket.chat/fuselage": "^0.6.3-dev.188",
Expand Down