Skip to content

Commit

Permalink
[NEW] Add user events for apps (#25165)
Browse files Browse the repository at this point in the history
* Add user events for apps

* Remove trycatch validation

* Get the user entity after the update

* Trigger events when the user updates his profile or deletes his account

* Update Apps-Engine

* Make update return doc after update

* Find user after the update to trigger event with correct data

* Change where clause to use userData instead of userId

* Update apps-engine
  • Loading branch information
tapiarafael committed May 20, 2022
1 parent 6aa09ec commit 603ade2
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 3 deletions.
17 changes: 17 additions & 0 deletions apps/meteor/app/apps/server/bridges/listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export class AppListenerBridge {
case AppInterface.IPostLivechatGuestSaved:
case AppInterface.IPostLivechatRoomSaved:
return 'livechatEvent';
case AppInterface.IPostUserCreated:
case AppInterface.IPostUserUpdated:
case AppInterface.IPostUserDeleted:
return 'userEvent';
default:
return 'defaultEvent';
}
Expand Down Expand Up @@ -134,4 +138,17 @@ export class AppListenerBridge {
return this.orch.getManager().getListenerManager().executeListener(inte, room);
}
}

async userEvent(inte, data) {
const context = {
user: this.orch.getConverters().get('users').convertToApp(data.user),
performedBy: this.orch.getConverters().get('users').convertToApp(data.performedBy),
};

if (inte === AppInterface.IPostUserUpdated) {
context.previousData = this.orch.getConverters().get('users').convertToApp(data.previousUser);
}

return this.orch.getManager().getListenerManager().executeListener(inte, context);
}
}
5 changes: 5 additions & 0 deletions apps/meteor/app/authentication/server/startup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { isValidAttemptByUser, isValidLoginAttemptByIp } from '../lib/restrictLo
import './settings';
import { getClientAddress } from '../../../../server/lib/getClientAddress';
import { getNewUserRoles } from '../../../../server/services/user/lib/getNewUserRoles';
import { AppEvents, Apps } from '../../../apps/server/orchestrator';

Accounts.config({
forbidClientAccountCreation: true,
Expand Down Expand Up @@ -210,6 +211,10 @@ Accounts.onCreateUser(function (options, user = {}) {
}

callbacks.run('onCreateUser', options, user);

// App IPostUserCreated event hook
Promise.await(Apps.triggerEvent(AppEvents.IPostUserCreated, { user, performedBy: Meteor.user() }));

return user;
});

Expand Down
13 changes: 13 additions & 0 deletions apps/meteor/app/lib/server/functions/saveUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { saveUserIdentity } from './saveUserIdentity';
import { checkEmailAvailability, checkUsernameAvailability, setUserAvatar, setEmail, setStatusText } from '.';
import { Users } from '../../../models/server';
import { callbacks } from '../../../../lib/callbacks';
import { AppEvents, Apps } from '../../../apps/server/orchestrator';

const MAX_BIO_LENGTH = 260;
const MAX_NICKNAME_LENGTH = 120;
Expand Down Expand Up @@ -346,6 +347,8 @@ export const saveUser = function (userId, userData) {

validateUserEditing(userId, userData);

const oldUserData = Users.findOneById(userId);

// update user
if (userData.hasOwnProperty('username') || userData.hasOwnProperty('name')) {
if (
Expand Down Expand Up @@ -411,6 +414,16 @@ export const saveUser = function (userId, userData) {

callbacks.run('afterSaveUser', userData);

// App IPostUserUpdated event hook
const userUpdated = Users.findOneById(userId);
Promise.await(
Apps.triggerEvent(AppEvents.IPostUserUpdated, {
user: userUpdated,
previousUser: oldUserData,
performedBy: Meteor.user(),
}),
);

if (sendPassword) {
_sendUserEmail(settings.get('Password_Changed_Email_Subject'), passwordChangedHtml, userData);
}
Expand Down
4 changes: 4 additions & 0 deletions apps/meteor/app/lib/server/methods/deleteUserOwnAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import s from 'underscore.string';
import { settings } from '../../../settings/server';
import { Users } from '../../../models/server';
import { deleteUser } from '../functions';
import { AppEvents, Apps } from '../../../apps/server/orchestrator';

Meteor.methods({
async deleteUserOwnAccount(password, confirmRelinquish) {
Expand Down Expand Up @@ -51,6 +52,9 @@ Meteor.methods({

await deleteUser(uid, confirmRelinquish);

// App IPostUserDeleted event hook
Promise.await(Apps.triggerEvent(AppEvents.IPostUserDeleted, { user }));

return true;
},
});
4 changes: 4 additions & 0 deletions apps/meteor/server/methods/deleteUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Users } from '../../app/models';
import { hasPermission } from '../../app/authorization';
import { callbacks } from '../../lib/callbacks';
import { deleteUser } from '../../app/lib/server';
import { AppEvents, Apps } from '../../app/apps/server/orchestrator';

Meteor.methods({
async deleteUser(userId, confirmRelinquish = false) {
Expand Down Expand Up @@ -50,6 +51,9 @@ Meteor.methods({

callbacks.run('afterDeleteUser', user);

// App IPostUserDeleted event hook
Promise.await(Apps.triggerEvent(AppEvents.IPostUserDeleted, { user, performedBy: Meteor.user() }));

return true;
},
});
10 changes: 10 additions & 0 deletions apps/meteor/server/methods/saveUserProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { twoFactorRequired } from '../../app/2fa/server/twoFactorRequired';
import { saveUserIdentity } from '../../app/lib/server/functions/saveUserIdentity';
import { compareUserPassword } from '../lib/compareUserPassword';
import { compareUserPasswordHistory } from '../lib/compareUserPasswordHistory';
import { AppEvents, Apps } from '../../app/apps/server/orchestrator';

function saveUserProfile(settings, customFields) {
if (!rcSettings.get('Accounts_AllowUserProfileChange')) {
Expand Down Expand Up @@ -118,6 +119,15 @@ function saveUserProfile(settings, customFields) {
saveCustomFields(this.userId, customFields);
}

// App IPostUserUpdated event hook
const updatedUser = Users.findOneById(this.userId);
Promise.await(
Apps.triggerEvent(AppEvents.IPostUserUpdated, {
user: updatedUser,
previousUser: user,
}),
);

return true;
}

Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3601,16 +3601,16 @@ __metadata:
linkType: hard

"@rocket.chat/apps-engine@npm:alpha":
version: 1.31.0-alpha.6070
resolution: "@rocket.chat/apps-engine@npm:1.31.0-alpha.6070"
version: 1.32.0-alpha.6099
resolution: "@rocket.chat/apps-engine@npm:1.32.0-alpha.6099"
dependencies:
adm-zip: ^0.4.9
cryptiles: ^4.1.3
lodash.clonedeep: ^4.5.0
semver: ^5.5.0
stack-trace: 0.0.10
uuid: ^3.2.1
checksum: 98aad03f4b6758cc54923f59dc77df4aca2d70aae510b33d6a75d0db660329d4c9d926bcbc8768ce705479a797bf395d62789533f4289a1753c71bd393c8eb74
checksum: 68ec386fc1a5daf80408c6a67f28bb19b7e5e4c6387b2e6c33aec9fc6dcc7f2412e738687f23b25af6f7864e648421568f372f76df7d3a06abb1b24d0c747c4b
languageName: node
linkType: hard

Expand Down

0 comments on commit 603ade2

Please sign in to comment.