Skip to content

Commit

Permalink
Regression: App event listeners broke Slackbridge integration and imp…
Browse files Browse the repository at this point in the history
…orters (#25689)
  • Loading branch information
d-gubert committed May 31, 2022
1 parent bf3483d commit 88a8e8e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
3 changes: 2 additions & 1 deletion apps/meteor/app/authentication/server/startup/index.js
Expand Up @@ -18,6 +18,7 @@ import './settings';
import { getClientAddress } from '../../../../server/lib/getClientAddress';
import { getNewUserRoles } from '../../../../server/services/user/lib/getNewUserRoles';
import { AppEvents, Apps } from '../../../apps/server/orchestrator';
import { safeGetMeteorUser } from '../../../utils/server/functions/safeGetMeteorUser';

Accounts.config({
forbidClientAccountCreation: true,
Expand Down Expand Up @@ -213,7 +214,7 @@ Accounts.onCreateUser(function (options, user = {}) {
callbacks.run('onCreateUser', options, user);

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

return user;
});
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/app/lib/server/functions/saveUser.js
Expand Up @@ -15,6 +15,7 @@ import { checkEmailAvailability, checkUsernameAvailability, setUserAvatar, setEm
import { Users } from '../../../models/server';
import { callbacks } from '../../../../lib/callbacks';
import { AppEvents, Apps } from '../../../apps/server/orchestrator';
import { safeGetMeteorUser } from '../../../utils/server/functions/safeGetMeteorUser';

const MAX_BIO_LENGTH = 260;
const MAX_NICKNAME_LENGTH = 120;
Expand Down Expand Up @@ -420,7 +421,7 @@ export const saveUser = function (userId, userData) {
Apps.triggerEvent(AppEvents.IPostUserUpdated, {
user: userUpdated,
previousUser: oldUserData,
performedBy: Meteor.user(),
performedBy: safeGetMeteorUser(),
}),
);

Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/reactions/server/setReaction.js
Expand Up @@ -94,7 +94,7 @@ async function setReaction(room, user, message, reaction, shouldReact) {
isReacted = true;
}

Promise.await(Apps.triggerEvent(AppEvents.IPostMessageReacted, message, Meteor.user(), reaction, isReacted));
Promise.await(Apps.triggerEvent(AppEvents.IPostMessageReacted, message, user, reaction, isReacted));

msgStream.emit(message.rid, message);
}
Expand Down
29 changes: 29 additions & 0 deletions apps/meteor/app/utils/server/functions/safeGetMeteorUser.ts
@@ -0,0 +1,29 @@
import { Meteor } from 'meteor/meteor';

const invalidEnvironmentErrorMessage = 'Meteor.userId can only be invoked in method calls or publications.';

/**
* Helper that executes the `Meteor.user()`, but
* supresses errors thrown if the code isn't
* executed inside Meteor's environment
*
* Use this function only if it the code path is
* expected to run out of Meteor's environment and
* is prepared to handle those cases. Otherwise, it
* is advisable to call `Meteor.user()` directly
*
* @returns The current user in the Meteor session, or null if not available
*/
export function safeGetMeteorUser(): Meteor.User | null {
try {
return Meteor.user();
} catch (error: any) {
// This is the only type of error we want to capture and supress,
// so if the error thrown is different from what we expect, we let it go
if (error?.message !== invalidEnvironmentErrorMessage) {
throw error;
}

return null;
}
}
7 changes: 1 addition & 6 deletions apps/meteor/server/methods/saveUserProfile.js
Expand Up @@ -121,12 +121,7 @@ function saveUserProfile(settings, customFields) {

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

return true;
}
Expand Down

0 comments on commit 88a8e8e

Please sign in to comment.