Skip to content

Commit

Permalink
fix(meteor): room-opened event not dispatching when navigating cach…
Browse files Browse the repository at this point in the history
…ed rooms (#29718)
  • Loading branch information
gabriellsh authored and sampaiodiego committed Jul 5, 2023
1 parent e6b4e34 commit 2f0f67f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-hornets-fail.md
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

fixed `room-opened` event not dispatching when navigating cached rooms
8 changes: 6 additions & 2 deletions apps/meteor/client/views/room/hooks/useOpenRoom.ts
@@ -1,6 +1,7 @@
import type { IRoom, RoomType } from '@rocket.chat/core-typings';
import { useMethod, useRoute, useSetting, useUser } from '@rocket.chat/ui-contexts';
import { useQuery } from '@tanstack/react-query';
import { useRef } from 'react';

import { ChatRoom, ChatSubscription } from '../../../../app/models/client';
import { LegacyRoomManager } from '../../../../app/ui-utils/client';
Expand All @@ -20,6 +21,8 @@ export function useOpenRoom({ type, reference }: { type: RoomType; reference: st
const openRoom = useMethod('openRoom');
const directRoute = useRoute('direct');

const unsubscribeFromRoomOpenedEvent = useRef<() => void>(() => undefined);

return useQuery(
// we need to add uid and username here because `user` is not loaded all at once (see UserProvider -> Meteor.user())
['rooms', { type, reference }, { uid: user?._id, username: user?.username }] as const,
Expand Down Expand Up @@ -63,14 +66,15 @@ export function useOpenRoom({ type, reference }: { type: RoomType; reference: st
throw new RoomNotFoundError(undefined, { rid: room._id });
}

unsubscribeFromRoomOpenedEvent.current();
unsubscribeFromRoomOpenedEvent.current = RoomManager.once('opened', () => fireGlobalEvent('room-opened', omit(room, 'usernames')));

LegacyRoomManager.open({ typeName: type + reference, rid: room._id });

if (room._id === RoomManager.opened) {
return { rid: room._id };
}

fireGlobalEvent('room-opened', omit(room, 'usernames'));

// update user's room subscription
const sub = ChatSubscription.findOne({ rid: room._id });
if (sub && !sub.open) {
Expand Down

0 comments on commit 2f0f67f

Please sign in to comment.