Skip to content

Commit 1b85a9e

Browse files
authored
chore: Replace Meteor.userId() for client code (RocketChat#36911)
1 parent 7809014 commit 1b85a9e

85 files changed

Lines changed: 304 additions & 262 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apps/meteor/app/authorization/client/hasPermission.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { IUser, IPermission } from '@rocket.chat/core-typings';
2-
import { Meteor } from 'meteor/meteor';
32

43
import { hasRole } from './hasRole';
54
import { PermissionsCachedStore } from '../../../client/cachedStores';
5+
import { watchUserId } from '../../../client/meteor/user';
66
import { watch } from '../../../client/meteor/watch';
77
import { Permissions, Users } from '../../../client/stores';
88
import { AuthorizationUtils } from '../lib/AuthorizationUtils';
@@ -47,10 +47,10 @@ const validatePermissions = (
4747
userId: IUser['_id'],
4848
scopedRoles?: IPermission['_id'][],
4949
) => boolean,
50-
userId?: IUser['_id'] | null,
50+
userId?: IUser['_id'],
5151
scopedRoles?: IPermission['_id'][],
5252
): boolean => {
53-
userId = userId ?? Meteor.userId();
53+
userId = userId ?? watchUserId() ?? undefined;
5454

5555
if (!userId) {
5656
return false;
@@ -75,7 +75,7 @@ export const hasAtLeastOnePermission = (permissions: IPermission['_id'] | IPermi
7575
export const userHasAllPermission = (
7676
permissions: IPermission['_id'] | IPermission['_id'][],
7777
scope?: string,
78-
userId?: IUser['_id'] | null,
78+
userId?: IUser['_id'],
7979
): boolean => validatePermissions(permissions, scope, all, userId);
8080

8181
export const hasPermission = hasAllPermission;

apps/meteor/app/autotranslate/client/lib/autotranslate.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
import type {
2-
IRoom,
3-
ISubscription,
4-
ISupportedLanguage,
5-
ITranslatedMessage,
6-
IUser,
7-
MessageAttachmentDefault,
8-
} from '@rocket.chat/core-typings';
1+
import type { IRoom, ISubscription, ISupportedLanguage, ITranslatedMessage, MessageAttachmentDefault } from '@rocket.chat/core-typings';
92
import { isTranslatedMessageAttachment } from '@rocket.chat/core-typings';
103
import mem from 'mem';
114
import { Meteor } from 'meteor/meteor';
125
import { Tracker } from 'meteor/tracker';
136

147
import { settings } from '../../../../client/lib/settings';
8+
import { getUserId } from '../../../../client/lib/user';
9+
import { watchUser, watchUserId } from '../../../../client/meteor/user';
1510
import { Messages, Subscriptions } from '../../../../client/stores';
1611
import {
1712
hasTranslationLanguageInAttachments,
@@ -25,10 +20,9 @@ let username = '';
2520

2621
Meteor.startup(() => {
2722
Tracker.autorun(() => {
28-
const user: Pick<IUser, 'language' | 'username'> | null = Meteor.user();
29-
if (!user) {
30-
return;
31-
}
23+
const user = watchUser();
24+
if (!user) return;
25+
3226
userLanguage = user.language || 'en';
3327
username = user.username || '';
3428
});
@@ -102,7 +96,7 @@ export const AutoTranslate = {
10296
}
10397

10498
Tracker.autorun(async (c) => {
105-
const uid = Meteor.userId();
99+
const uid = watchUserId();
106100
if (!settings.watch('AutoTranslate_Enabled') || !uid || !hasPermission('auto-translate')) {
107101
return;
108102
}
@@ -132,7 +126,7 @@ export const createAutoTranslateMessageStreamHandler = (): ((message: ITranslate
132126
AutoTranslate.init();
133127

134128
return (message: ITranslatedMessage): void => {
135-
if (message.u && message.u._id !== Meteor.userId()) {
129+
if (message.u && message.u._id !== getUserId()) {
136130
const subscription = AutoTranslate.findSubscriptionByRid(message.rid);
137131
const language = AutoTranslate.getLanguage(message.rid);
138132
if (

apps/meteor/app/lib/client/methods/sendMessage.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
import type { IMessage, IUser } from '@rocket.chat/core-typings';
1+
import type { IMessage } from '@rocket.chat/core-typings';
22
import type { ServerMethods } from '@rocket.chat/ddp-client';
33
import { Meteor } from 'meteor/meteor';
44

55
import { onClientMessageReceived } from '../../../../client/lib/onClientMessageReceived';
66
import { settings } from '../../../../client/lib/settings';
77
import { dispatchToastMessage } from '../../../../client/lib/toast';
8+
import { getUser, getUserId } from '../../../../client/lib/user';
89
import { Messages, Rooms } from '../../../../client/stores';
910
import { callbacks } from '../../../../lib/callbacks';
1011
import { trim } from '../../../../lib/utils/stringUtils';
1112
import { t } from '../../../utils/lib/i18n';
1213

1314
Meteor.methods<ServerMethods>({
1415
async sendMessage(message) {
15-
const uid = Meteor.userId();
16+
const uid = getUserId();
1617
if (!uid || trim(message.msg) === '') {
1718
return false;
1819
}
1920
const messageAlreadyExists = message._id && Messages.state.get(message._id);
2021
if (messageAlreadyExists) {
2122
return dispatchToastMessage({ type: 'error', message: t('Message_Already_Sent') });
2223
}
23-
const user = Meteor.user() as IUser | null;
24+
const user = getUser();
2425
if (!user?.username) {
2526
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'sendMessage' });
2627
}

apps/meteor/app/otr/client/OTRRoom.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Tracker } from 'meteor/tracker';
99

1010
import { Presence } from '../../../client/lib/presence';
1111
import { dispatchToastMessage } from '../../../client/lib/toast';
12+
import { getUser } from '../../../client/lib/user';
1213
import { getUidDirectMessage } from '../../../client/lib/utils/getUidDirectMessage';
1314
import { goToRoomById } from '../../../client/lib/utils/goToRoomById';
1415
import { Messages } from '../../../client/stores';
@@ -102,10 +103,9 @@ export class OTRRoom implements IOTRRoom {
102103
]);
103104

104105
if (refresh) {
105-
const user = Meteor.user();
106-
if (!user) {
107-
return;
108-
}
106+
const user = getUser();
107+
if (!user) return;
108+
109109
await sdk.rest.post('/v1/chat.otr', {
110110
roomId: this._roomId,
111111
type: otrSystemMessages.USER_REQUESTED_OTR_KEY_REFRESH,

apps/meteor/app/reactions/client/methods/setReaction.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ import type { ServerMethods } from '@rocket.chat/ddp-client';
33
import { Meteor } from 'meteor/meteor';
44

55
import { roomCoordinator } from '../../../../client/lib/rooms/roomCoordinator';
6+
import { getUser, getUserId } from '../../../../client/lib/user';
67
import { Rooms, Subscriptions, Messages } from '../../../../client/stores';
78
import { emoji } from '../../../emoji/client';
89

910
Meteor.methods<ServerMethods>({
1011
async setReaction(reaction, messageId) {
11-
if (!Meteor.userId()) {
12+
if (!getUserId()) {
1213
throw new Meteor.Error(203, 'User_logged_out');
1314
}
1415

15-
const user = Meteor.user();
16+
const user = getUser();
1617

1718
if (!user?.username) {
1819
return false;

apps/meteor/app/ui-utils/client/lib/RoomHistoryManager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { MutableRefObject } from 'react';
77
import { v4 as uuidv4 } from 'uuid';
88

99
import { onClientMessageReceived } from '../../../../client/lib/onClientMessageReceived';
10+
import { getUserId } from '../../../../client/lib/user';
1011
import { callWithErrorHandling } from '../../../../client/lib/utils/callWithErrorHandling';
1112
import { getConfig } from '../../../../client/lib/utils/getConfig';
1213
import { waitForElement } from '../../../../client/lib/utils/waitForElement';
@@ -138,7 +139,7 @@ class RoomHistoryManagerClass extends Emitter {
138139
({ ls } = subscription);
139140
}
140141

141-
const showThreadsInMainChannel = getUserPreference(Meteor.userId(), 'showThreadsInMainChannel', false);
142+
const showThreadsInMainChannel = getUserPreference(getUserId(), 'showThreadsInMainChannel', false);
142143
const result = await callWithErrorHandling(
143144
'loadHistory',
144145
rid,

apps/meteor/app/ui/client/lib/ChatMessages.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type DeepWritable<T> = T extends (...args: any) => any
2525
};
2626

2727
export class ChatMessages implements ChatAPI {
28-
public uid: string | null;
28+
public uid: string | undefined;
2929

3030
public tmid?: IMessage['_id'];
3131

@@ -142,7 +142,7 @@ export class ChatMessages implements ChatAPI {
142142

143143
public flows: DeepWritable<ChatAPI['flows']>;
144144

145-
public constructor(params: { rid: IRoom['_id']; tmid?: IMessage['_id']; uid: IUser['_id'] | null; actionManager: IActionManager }) {
145+
public constructor(params: { rid: IRoom['_id']; tmid?: IMessage['_id']; uid: IUser['_id'] | undefined; actionManager: IActionManager }) {
146146
const { rid, tmid } = params;
147147
this.tmid = tmid;
148148
this.uid = params.uid;

apps/meteor/app/ui/client/lib/UserAction.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { IExtras, IRoomActivity, IUser } from '@rocket.chat/core-typings';
22
import { Emitter } from '@rocket.chat/emitter';
33
import { debounce } from 'lodash';
4-
import { Meteor } from 'meteor/meteor';
54

65
import { settings } from '../../../../client/lib/settings';
6+
import { getUser, getUserId } from '../../../../client/lib/user';
77
import { Users } from '../../../../client/stores';
88
import { sdk } from '../../../utils/client/lib/SDKClient';
99

@@ -40,7 +40,7 @@ const shownName = function (user: IUser | null | undefined): string | undefined
4040

4141
const emitActivities = debounce(async (rid: string, extras: IExtras): Promise<void> => {
4242
const activities = roomActivities.get(extras?.tmid || rid) || new Set();
43-
sdk.publish('notify-room', [`${rid}/${USER_ACTIVITY}`, shownName(Meteor.user() as unknown as IUser), [...activities], extras]);
43+
sdk.publish('notify-room', [`${rid}/${USER_ACTIVITY}`, shownName(getUser()), [...activities], extras]);
4444
}, 500);
4545

4646
function handleStreamAction(rid: string, username: string, activityTypes: string[], extras?: IExtras): void {
@@ -74,7 +74,7 @@ export const UserAction = new (class {
7474
}
7575

7676
const handler = function (username: string, activityType: string[], extras?: object): void {
77-
const uid = Meteor.userId();
77+
const uid = getUserId();
7878
const user = uid ? Users.state.get(uid) : undefined;
7979

8080
if (username === shownName(user)) {

apps/meteor/app/utils/client/lib/getUserPreference.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Users } from '../../../../client/stores';
99
* @param key The preference name
1010
* @returns The preference value
1111
*/
12-
export function getUserPreference<TValue>(user: IUser['_id'] | null | undefined, key: string): TValue | undefined;
12+
export function getUserPreference<TValue>(user: IUser['_id'] | undefined, key: string): TValue | undefined;
1313
/**
1414
* Get a user preference
1515
* @param user The user
@@ -24,7 +24,7 @@ export function getUserPreference<TValue>(user: Pick<IUser, '_id' | 'settings'>
2424
* @param defaultValue The default value
2525
* @returns The preference value or the default value
2626
*/
27-
export function getUserPreference<TValue>(user: IUser['_id'] | null | undefined, key: string, defaultValue: TValue): TValue;
27+
export function getUserPreference<TValue>(user: IUser['_id'] | undefined, key: string, defaultValue: TValue): TValue;
2828
/**
2929
* Get a user preference
3030
* @param user The user

apps/meteor/app/webrtc/client/WebRTCClass.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import type { IRoom } from '@rocket.chat/core-typings';
22
import type { StreamKeys, StreamNames, StreamerCallbackArgs } from '@rocket.chat/ddp-client';
33
import { Emitter } from '@rocket.chat/emitter';
44
import { GenericModal, imperativeModal } from '@rocket.chat/ui-client';
5-
import { Meteor } from 'meteor/meteor';
65
import { ReactiveVar } from 'meteor/reactive-var';
76

87
import { ChromeScreenShare } from './screenShare';
98
import { settings } from '../../../client/lib/settings';
9+
import { getUserId } from '../../../client/lib/user';
1010
import { goToRoomById } from '../../../client/lib/utils/goToRoomById';
1111
import { Subscriptions, Users } from '../../../client/stores';
1212
import { sdk } from '../../utils/client/lib/SDKClient';
@@ -1035,31 +1035,28 @@ const WebRTC = new (class {
10351035
}
10361036
switch (subscription.t) {
10371037
case 'd':
1038-
enabled = settings.watch('WebRTC_Enable_Direct') ?? false;
1038+
enabled = settings.peek('WebRTC_Enable_Direct') ?? false;
10391039
break;
10401040
case 'p':
1041-
enabled = settings.watch('WebRTC_Enable_Private') ?? false;
1041+
enabled = settings.peek('WebRTC_Enable_Private') ?? false;
10421042
break;
10431043
case 'c':
1044-
enabled = settings.watch('WebRTC_Enable_Channel') ?? false;
1044+
enabled = settings.peek('WebRTC_Enable_Channel') ?? false;
10451045
break;
10461046
case 'l':
1047-
enabled = settings.watch<string>('Omnichannel_call_provider') === 'WebRTC';
1047+
enabled = settings.peek<string>('Omnichannel_call_provider') === 'WebRTC';
10481048
}
10491049
} else {
1050-
enabled = settings.watch<string>('Omnichannel_call_provider') === 'WebRTC';
1050+
enabled = settings.peek<string>('Omnichannel_call_provider') === 'WebRTC';
10511051
}
1052-
enabled = enabled && (settings.watch('WebRTC_Enabled') ?? false);
1052+
enabled = enabled && (settings.peek('WebRTC_Enabled') ?? false);
10531053
if (enabled === false) {
10541054
return;
10551055
}
10561056
if (this.instancesByRoomId[rid] == null) {
1057-
let uid = Meteor.userId()!;
1058-
let autoAccept = false;
1059-
if (visitorId) {
1060-
uid = visitorId;
1061-
autoAccept = true;
1062-
}
1057+
const uid = visitorId ?? getUserId();
1058+
if (!uid) return undefined;
1059+
const autoAccept = !!visitorId;
10631060
this.instancesByRoomId[rid] = new WebRTCClass(uid, rid, autoAccept);
10641061
}
10651062
return this.instancesByRoomId[rid];

0 commit comments

Comments
 (0)