Skip to content

Commit d277737

Browse files
ggazzojuliajforestitassoevan
authored
chore: Rooms store (RocketChat#36439)
Co-authored-by: juliajforesti <juliajforesti@gmail.com> Co-authored-by: Tasso <tasso.evangelista@rocket.chat>
1 parent 7ab18f5 commit d277737

60 files changed

Lines changed: 956 additions & 416 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/e2e/client/rocketchat.e2e.room.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,8 @@ export class E2ERoom extends Emitter {
335335
}
336336

337337
try {
338-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
339-
const room = Rooms.findOne({ _id: this.roomId })!;
340-
if (!room.e2eKeyId) {
338+
const room = Rooms.state.get(this.roomId);
339+
if (!room?.e2eKeyId) {
341340
this.setState(E2ERoomState.CREATING_KEYS);
342341
await this.createGroupKey();
343342
this.setState(E2ERoomState.READY);

apps/meteor/app/e2e/client/rocketchat.e2e.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import * as banners from '../../../client/lib/banners';
3232
import type { LegacyBannerPayload } from '../../../client/lib/banners';
3333
import { dispatchToastMessage } from '../../../client/lib/toast';
3434
import { mapMessageFromApi } from '../../../client/lib/utils/mapMessageFromApi';
35-
import { waitUntilFind } from '../../../client/lib/utils/waitUntilFind';
3635
import EnterE2EPasswordModal from '../../../client/views/e2e/EnterE2EPasswordModal';
3736
import SaveE2EPasswordModal from '../../../client/views/e2e/SaveE2EPasswordModal';
3837
import { createQuoteAttachment } from '../../../lib/createQuoteAttachment';
@@ -254,8 +253,24 @@ class E2E extends Emitter {
254253
);
255254
}
256255

256+
private waitForRoom(rid: IRoom['_id']): Promise<IRoom> {
257+
return new Promise((resolve) => {
258+
const room = Rooms.state.get(rid);
259+
260+
if (room) resolve(room);
261+
262+
const unsubscribe = Rooms.use.subscribe((state) => {
263+
const room = state.get(rid);
264+
if (room) {
265+
unsubscribe();
266+
resolve(room);
267+
}
268+
});
269+
});
270+
}
271+
257272
async getInstanceByRoomId(rid: IRoom['_id']): Promise<E2ERoom | null> {
258-
const room = await waitUntilFind(() => Rooms.findOne({ _id: rid }));
273+
const room = await this.waitForRoom(rid);
259274

260275
if (room.t !== 'd' && room.t !== 'p') {
261276
return null;
@@ -846,11 +861,12 @@ class E2E extends Emitter {
846861
return;
847862
}
848863

864+
const predicate = (record: IRoom) =>
865+
Boolean('usersWaitingForE2EKeys' in record && record.usersWaitingForE2EKeys?.every((user) => user.userId !== Meteor.userId()));
866+
849867
const keyDistribution = async () => {
850-
const roomIds = Rooms.find({
851-
'usersWaitingForE2EKeys': { $exists: true },
852-
'usersWaitingForE2EKeys.userId': { $ne: Meteor.userId() },
853-
}).map((room) => room._id);
868+
const roomIds = Rooms.state.filter(predicate).map((room) => room._id);
869+
854870
if (!roomIds.length) {
855871
return;
856872
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Meteor.methods<ServerMethods>({
3636
}
3737

3838
// If the room is federated, send the message to matrix only
39-
const room = Rooms.findOne({ _id: message.rid }, { fields: { federated: 1, name: 1 } });
39+
const room = Rooms.state.get(message.rid);
4040
if (room?.federated) {
4141
return;
4242
}

apps/meteor/app/livechat/client/lib/stream/queueManager.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ const processInquiryEvent = async (args: unknown): Promise<void> => {
4545
};
4646

4747
const invalidateRoomQueries = async (rid: string) => {
48-
await queryClient.invalidateQueries({ queryKey: ['rooms', { reference: rid, type: 'l' }] });
49-
queryClient.removeQueries({ queryKey: roomsQueryKeys.room(rid) });
50-
queryClient.removeQueries({ queryKey: roomsQueryKeys.info(rid) });
48+
await Promise.all([
49+
queryClient.invalidateQueries({ queryKey: ['rooms', { reference: rid, type: 'l' }] }),
50+
queryClient.invalidateQueries({ queryKey: roomsQueryKeys.room(rid) }),
51+
queryClient.invalidateQueries({ queryKey: roomsQueryKeys.info(rid) }),
52+
]);
5153
};
5254

5355
const removeInquiry = async (inquiry: ILivechatInquiryRecord) => {

apps/meteor/app/models/client/models/CachedChatRoom.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import { DEFAULT_SLA_CONFIG, LivechatPriorityWeight } from '@rocket.chat/core-ty
33
import type { SubscriptionWithRoom } from '@rocket.chat/ui-contexts';
44

55
import { CachedChatSubscription } from './CachedChatSubscription';
6-
import { PrivateCachedCollection } from '../../../../client/lib/cachedCollections/CachedCollection';
6+
import { PrivateCachedStore } from '../../../../client/lib/cachedCollections/CachedCollection';
7+
import { createDocumentMapStore } from '../../../../client/lib/cachedCollections/DocumentMapStore';
78

8-
class CachedChatRoom extends PrivateCachedCollection<IRoom> {
9+
class CachedChatRoom extends PrivateCachedStore<IRoom> {
910
constructor() {
1011
super({
1112
name: 'rooms',
1213
eventType: 'notify-user',
14+
store: createDocumentMapStore(),
1315
});
1416
}
1517

apps/meteor/app/models/client/models/CachedChatSubscription.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CachedChatSubscription extends PrivateCachedCollection<SubscriptionWithRoo
2121
}
2222

2323
protected override mapRecord(subscription: ISubscription): SubscriptionWithRoom {
24-
const room = CachedChatRoom.collection.state.find((r) => r._id === subscription.rid);
24+
const room = CachedChatRoom.store.getState().find((r) => r._id === subscription.rid);
2525

2626
const lastRoomUpdate = room?.lm || subscription.ts || room?.ts;
2727

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import { CachedChatRoom } from './CachedChatRoom';
22

33
/** @deprecated new code refer to Minimongo collections like this one; prefer fetching data from the REST API, listening to changes via streamer events, and storing the state in a Tanstack Query */
4-
export const Rooms = CachedChatRoom.collection;
4+
export const Rooms = {
5+
use: CachedChatRoom.store,
6+
get state() {
7+
return this.use.getState();
8+
},
9+
} as const;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { IMessage, IRoom } 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

@@ -23,7 +23,7 @@ Meteor.methods<ServerMethods>({
2323
return false;
2424
}
2525

26-
const room: IRoom | undefined = Rooms.findOne({ _id: message.rid });
26+
const room = Rooms.state.get(message.rid);
2727
if (!room) {
2828
return false;
2929
}
@@ -36,7 +36,7 @@ Meteor.methods<ServerMethods>({
3636
return false;
3737
}
3838

39-
if (roomCoordinator.readOnly(room._id, user)) {
39+
if (roomCoordinator.readOnly(room, user)) {
4040
return false;
4141
}
4242

apps/meteor/app/slashcommands-topic/client/topic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ slashCommands.add({
1313
if (hasPermission('edit-room', message.rid)) {
1414
try {
1515
await sdk.call('saveRoomSettings', message.rid, 'roomTopic', params);
16-
await callbacks.run('roomTopicChanged', Rooms.findOne(message.rid));
16+
await callbacks.run('roomTopicChanged', Rooms.state.get(message.rid));
1717
} catch (error: unknown) {
1818
dispatchToastMessage({ type: 'error', message: error });
1919
throw error;

apps/meteor/app/webrtc/client/actionLink.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { sdk } from '../../utils/client/lib/SDKClient';
77
import { t } from '../../utils/lib/i18n';
88

99
actionLinks.register('joinLivechatWebRTCCall', (message: IMessage) => {
10-
const room = Rooms.findOne({ _id: message.rid });
10+
const room = Rooms.state.get(message.rid);
1111
if (!room) {
1212
throw new Error('Room not found');
1313
}
@@ -20,7 +20,7 @@ actionLinks.register('joinLivechatWebRTCCall', (message: IMessage) => {
2020
});
2121

2222
actionLinks.register('endLivechatWebRTCCall', async (message: IMessage) => {
23-
const room = Rooms.findOne({ _id: message.rid });
23+
const room = Rooms.state.get(message.rid);
2424
if (!room) {
2525
throw new Error('Room not found');
2626
}

0 commit comments

Comments
 (0)