From f6b679217ff464388aa8f762fe38a69d41cf103d Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Sat, 25 Mar 2023 10:59:13 -0600 Subject: [PATCH] refactor: Ditch `Meteor.user` in favor of `Meteor.userAsync` - 1x (#28601) Co-authored-by: Rodrigo Nascimento <234261+rodrigok@users.noreply.github.com> --- apps/meteor/app/2fa/server/methods/checkCodesRemaining.ts | 4 ++-- apps/meteor/app/2fa/server/methods/disable.ts | 4 ++-- apps/meteor/app/2fa/server/methods/enable.ts | 4 ++-- apps/meteor/app/2fa/server/methods/regenerateCodes.ts | 4 ++-- apps/meteor/app/2fa/server/methods/validateTempToken.ts | 4 ++-- .../app/channel-settings/server/methods/saveRoomSettings.ts | 2 +- apps/meteor/app/crowd/server/methods.ts | 4 ++-- .../app/discussion/server/methods/createDiscussion.ts | 2 +- .../app/emoji-custom/server/methods/listEmojiCustom.ts | 2 +- .../app/file-upload/server/methods/sendFileMessage.ts | 2 +- apps/meteor/app/lib/server/functions/archiveRoom.ts | 6 +++--- apps/meteor/app/lib/server/functions/unarchiveRoom.ts | 4 ++-- apps/meteor/app/lib/server/methods/addUsersToRoom.ts | 2 +- apps/meteor/app/lib/server/methods/archiveRoom.ts | 2 +- .../app/lib/server/methods/checkUsernameAvailability.ts | 2 +- apps/meteor/app/lib/server/methods/createChannel.ts | 2 +- apps/meteor/app/lib/server/methods/unarchiveRoom.ts | 2 +- apps/meteor/app/slackbridge/server/SlackAdapter.js | 4 ++-- 18 files changed, 28 insertions(+), 28 deletions(-) diff --git a/apps/meteor/app/2fa/server/methods/checkCodesRemaining.ts b/apps/meteor/app/2fa/server/methods/checkCodesRemaining.ts index 5a74aea74dd..3ea3ef25fc7 100644 --- a/apps/meteor/app/2fa/server/methods/checkCodesRemaining.ts +++ b/apps/meteor/app/2fa/server/methods/checkCodesRemaining.ts @@ -9,12 +9,12 @@ declare module '@rocket.chat/ui-contexts' { } Meteor.methods({ - '2fa:checkCodesRemaining'() { + async '2fa:checkCodesRemaining'() { if (!Meteor.userId()) { throw new Meteor.Error('not-authorized'); } - const user = Meteor.user(); + const user = await Meteor.userAsync(); if (!user) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { diff --git a/apps/meteor/app/2fa/server/methods/disable.ts b/apps/meteor/app/2fa/server/methods/disable.ts index 32b1cb4f981..94e9e26b6e8 100644 --- a/apps/meteor/app/2fa/server/methods/disable.ts +++ b/apps/meteor/app/2fa/server/methods/disable.ts @@ -12,13 +12,13 @@ declare module '@rocket.chat/ui-contexts' { } Meteor.methods({ - '2fa:disable'(code) { + async '2fa:disable'(code) { const userId = Meteor.userId(); if (!userId) { throw new Meteor.Error('not-authorized'); } - const user = Meteor.user(); + const user = await Meteor.userAsync(); if (!user) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { diff --git a/apps/meteor/app/2fa/server/methods/enable.ts b/apps/meteor/app/2fa/server/methods/enable.ts index 2fc24d27d3a..41d46f62733 100644 --- a/apps/meteor/app/2fa/server/methods/enable.ts +++ b/apps/meteor/app/2fa/server/methods/enable.ts @@ -12,13 +12,13 @@ declare module '@rocket.chat/ui-contexts' { } Meteor.methods({ - '2fa:enable'() { + async '2fa:enable'() { const userId = Meteor.userId(); if (!userId) { throw new Meteor.Error('not-authorized'); } - const user = Meteor.user(); + const user = await Meteor.userAsync(); if (!user?.username) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { diff --git a/apps/meteor/app/2fa/server/methods/regenerateCodes.ts b/apps/meteor/app/2fa/server/methods/regenerateCodes.ts index 9744e4a6407..2b4af8c6e63 100644 --- a/apps/meteor/app/2fa/server/methods/regenerateCodes.ts +++ b/apps/meteor/app/2fa/server/methods/regenerateCodes.ts @@ -12,13 +12,13 @@ declare module '@rocket.chat/ui-contexts' { } Meteor.methods({ - '2fa:regenerateCodes'(userToken) { + async '2fa:regenerateCodes'(userToken) { const userId = Meteor.userId(); if (!userId) { throw new Meteor.Error('not-authorized'); } - const user = Meteor.user(); + const user = await Meteor.userAsync(); if (!user) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: '2fa:regenerateCodes', diff --git a/apps/meteor/app/2fa/server/methods/validateTempToken.ts b/apps/meteor/app/2fa/server/methods/validateTempToken.ts index ff213dde3d0..2bcbe2980a8 100644 --- a/apps/meteor/app/2fa/server/methods/validateTempToken.ts +++ b/apps/meteor/app/2fa/server/methods/validateTempToken.ts @@ -12,13 +12,13 @@ declare module '@rocket.chat/ui-contexts' { } Meteor.methods({ - '2fa:validateTempToken'(userToken) { + async '2fa:validateTempToken'(userToken) { const userId = Meteor.userId(); if (!userId) { throw new Meteor.Error('not-authorized'); } - const user = Meteor.user(); + const user = await Meteor.userAsync(); if (!user) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: '2fa:validateTempToken', diff --git a/apps/meteor/app/channel-settings/server/methods/saveRoomSettings.ts b/apps/meteor/app/channel-settings/server/methods/saveRoomSettings.ts index 043387cc412..a401123c227 100644 --- a/apps/meteor/app/channel-settings/server/methods/saveRoomSettings.ts +++ b/apps/meteor/app/channel-settings/server/methods/saveRoomSettings.ts @@ -448,7 +448,7 @@ async function saveRoomSettings( }); } - const user = Meteor.user() as (IUser & Required>) | null; + const user = (await Meteor.userAsync()) as (IUser & Required>) | null; if (!user) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'saveRoomSettings', diff --git a/apps/meteor/app/crowd/server/methods.ts b/apps/meteor/app/crowd/server/methods.ts index c2cfbcfe54c..542ab7d043f 100644 --- a/apps/meteor/app/crowd/server/methods.ts +++ b/apps/meteor/app/crowd/server/methods.ts @@ -16,7 +16,7 @@ declare module '@rocket.chat/ui-contexts' { Meteor.methods({ async crowd_test_connection() { - const user = Meteor.user(); + const user = await Meteor.userAsync(); if (!user) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'crowd_test_connection', @@ -50,7 +50,7 @@ Meteor.methods({ } }, async crowd_sync_users() { - const user = Meteor.user(); + const user = await Meteor.userAsync(); if (settings.get('CROWD_Enable') !== true) { throw new Meteor.Error('crowd_disabled'); } diff --git a/apps/meteor/app/discussion/server/methods/createDiscussion.ts b/apps/meteor/app/discussion/server/methods/createDiscussion.ts index 0149cc4e074..186eb4a53e2 100644 --- a/apps/meteor/app/discussion/server/methods/createDiscussion.ts +++ b/apps/meteor/app/discussion/server/methods/createDiscussion.ts @@ -212,6 +212,6 @@ Meteor.methods({ throw new Meteor.Error('error-action-not-allowed', 'You are not allowed to create a discussion', { method: 'createDiscussion' }); } - return create({ prid, pmid, t_name: discussionName, reply, users, user: Meteor.user() as IUser, encrypted }); + return create({ prid, pmid, t_name: discussionName, reply, users, user: (await Meteor.userAsync()) as IUser, encrypted }); }, }); diff --git a/apps/meteor/app/emoji-custom/server/methods/listEmojiCustom.ts b/apps/meteor/app/emoji-custom/server/methods/listEmojiCustom.ts index da114d8511a..215817a4996 100644 --- a/apps/meteor/app/emoji-custom/server/methods/listEmojiCustom.ts +++ b/apps/meteor/app/emoji-custom/server/methods/listEmojiCustom.ts @@ -17,7 +17,7 @@ Meteor.methods({ async listEmojiCustom(options = {}) { methodDeprecationLogger.warn('listEmojiCustom will be removed in future versions of Rocket.Chat'); - const user = Meteor.user(); + const user = await Meteor.userAsync(); if (!user) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { diff --git a/apps/meteor/app/file-upload/server/methods/sendFileMessage.ts b/apps/meteor/app/file-upload/server/methods/sendFileMessage.ts index 882b2239559..d624c6d2495 100644 --- a/apps/meteor/app/file-upload/server/methods/sendFileMessage.ts +++ b/apps/meteor/app/file-upload/server/methods/sendFileMessage.ts @@ -128,7 +128,7 @@ declare module '@rocket.chat/ui-contexts' { Meteor.methods({ async sendFileMessage(roomId, _store, file, msgData = {}) { - const user = Meteor.user() as IUser | undefined; + const user = (await Meteor.userAsync()) as IUser | undefined; if (!user) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'sendFileMessage', diff --git a/apps/meteor/app/lib/server/functions/archiveRoom.ts b/apps/meteor/app/lib/server/functions/archiveRoom.ts index 335a73b85aa..5d86cda4d26 100644 --- a/apps/meteor/app/lib/server/functions/archiveRoom.ts +++ b/apps/meteor/app/lib/server/functions/archiveRoom.ts @@ -3,10 +3,10 @@ import { Meteor } from 'meteor/meteor'; import { Rooms, Messages, Subscriptions } from '../../../models/server'; import { callbacks } from '../../../../lib/callbacks'; -export const archiveRoom = function (rid: string): void { +export const archiveRoom = async function (rid: string): Promise { Rooms.archiveById(rid); Subscriptions.archiveByRoomId(rid); - Messages.createRoomArchivedByRoomIdAndUser(rid, Meteor.user()); + Messages.createRoomArchivedByRoomIdAndUser(rid, await Meteor.userAsync()); - callbacks.run('afterRoomArchived', Rooms.findOneById(rid), Meteor.user()); + callbacks.run('afterRoomArchived', Rooms.findOneById(rid), await Meteor.userAsync()); }; diff --git a/apps/meteor/app/lib/server/functions/unarchiveRoom.ts b/apps/meteor/app/lib/server/functions/unarchiveRoom.ts index 5f1ab1415f7..19d2b4b98c6 100644 --- a/apps/meteor/app/lib/server/functions/unarchiveRoom.ts +++ b/apps/meteor/app/lib/server/functions/unarchiveRoom.ts @@ -2,8 +2,8 @@ import { Meteor } from 'meteor/meteor'; import { Rooms, Messages, Subscriptions } from '../../../models/server'; -export const unarchiveRoom = function (rid: string): void { +export const unarchiveRoom = async function (rid: string): Promise { Rooms.unarchiveById(rid); Subscriptions.unarchiveByRoomId(rid); - Messages.createRoomUnarchivedByRoomIdAndUser(rid, Meteor.user()); + Messages.createRoomUnarchivedByRoomIdAndUser(rid, await Meteor.userAsync()); }; diff --git a/apps/meteor/app/lib/server/methods/addUsersToRoom.ts b/apps/meteor/app/lib/server/methods/addUsersToRoom.ts index a79a1fd88b2..c6f6a34f0c9 100644 --- a/apps/meteor/app/lib/server/methods/addUsersToRoom.ts +++ b/apps/meteor/app/lib/server/methods/addUsersToRoom.ts @@ -75,7 +75,7 @@ Meteor.methods({ } // Validate each user, then add to room - const user = (Meteor.user() as IUser | null) ?? undefined; + const user = ((await Meteor.userAsync()) as IUser | null) ?? undefined; if (isRoomFederated(room)) { callbacks.run('federation.onAddUsersToARoom', { invitees: data.users, inviter: user }, room); return true; diff --git a/apps/meteor/app/lib/server/methods/archiveRoom.ts b/apps/meteor/app/lib/server/methods/archiveRoom.ts index 9fadb1e9924..1e297f81ce4 100644 --- a/apps/meteor/app/lib/server/methods/archiveRoom.ts +++ b/apps/meteor/app/lib/server/methods/archiveRoom.ts @@ -11,7 +11,7 @@ import { RoomMemberActions } from '../../../../definition/IRoomTypeConfig'; declare module '@rocket.chat/ui-contexts' { // eslint-disable-next-line @typescript-eslint/naming-convention interface ServerMethods { - archiveRoom(rid: string): void; + archiveRoom(rid: string): Promise; } } diff --git a/apps/meteor/app/lib/server/methods/checkUsernameAvailability.ts b/apps/meteor/app/lib/server/methods/checkUsernameAvailability.ts index 10b2ef968fa..f15205eb9db 100644 --- a/apps/meteor/app/lib/server/methods/checkUsernameAvailability.ts +++ b/apps/meteor/app/lib/server/methods/checkUsernameAvailability.ts @@ -20,7 +20,7 @@ Meteor.methods({ check(username, String); - const user = Meteor.user(); + const user = await Meteor.userAsync(); if (!user) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'setUsername' }); diff --git a/apps/meteor/app/lib/server/methods/createChannel.ts b/apps/meteor/app/lib/server/methods/createChannel.ts index e1216884b74..70535607945 100644 --- a/apps/meteor/app/lib/server/methods/createChannel.ts +++ b/apps/meteor/app/lib/server/methods/createChannel.ts @@ -26,7 +26,7 @@ Meteor.methods({ const uid = Meteor.userId(); - const user = Meteor.user(); + const user = await Meteor.userAsync(); if (!uid || !user?.username) { throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'createChannel' }); diff --git a/apps/meteor/app/lib/server/methods/unarchiveRoom.ts b/apps/meteor/app/lib/server/methods/unarchiveRoom.ts index ba8c4e84c96..54145900d53 100644 --- a/apps/meteor/app/lib/server/methods/unarchiveRoom.ts +++ b/apps/meteor/app/lib/server/methods/unarchiveRoom.ts @@ -9,7 +9,7 @@ import { unarchiveRoom } from '../functions'; declare module '@rocket.chat/ui-contexts' { // eslint-disable-next-line @typescript-eslint/naming-convention interface ServerMethods { - unarchiveRoom(rid: string): void; + unarchiveRoom(rid: string): Promise; } } diff --git a/apps/meteor/app/slackbridge/server/SlackAdapter.js b/apps/meteor/app/slackbridge/server/SlackAdapter.js index 0b739b6dce7..d6f92b27021 100644 --- a/apps/meteor/app/slackbridge/server/SlackAdapter.js +++ b/apps/meteor/app/slackbridge/server/SlackAdapter.js @@ -1099,13 +1099,13 @@ export default class SlackAdapter { case 'channel_archive': case 'group_archive': if (!isImporting) { - archiveRoom(rocketChannel); + await archiveRoom(rocketChannel); } return; case 'channel_unarchive': case 'group_unarchive': if (!isImporting) { - unarchiveRoom(rocketChannel); + await unarchiveRoom(rocketChannel); } return; case 'file_share':