Skip to content

Commit e1682d2

Browse files
authored
fix: Leave room action available for users without subscription (RocketChat#37477)
1 parent 0691514 commit e1682d2

4 files changed

Lines changed: 54 additions & 7 deletions

File tree

.changeset/modern-onions-repair.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@rocket.chat/mock-providers': patch
3+
'@rocket.chat/meteor': patch
4+
---
5+
6+
Fixes an issue where leave room action is available for users without subscription
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { mockAppRoot } from '@rocket.chat/mock-providers';
2+
import type { SubscriptionWithRoom } from '@rocket.chat/ui-contexts';
3+
import { renderHook } from '@testing-library/react';
4+
5+
import { useRoomLeave } from './useRoomLeave';
6+
import { createFakeRoom, createFakeSubscription } from '../../../../../../../tests/mocks/data';
7+
8+
const mockRoom = createFakeRoom({ _id: 'room1', t: 'c', name: 'room1', fname: 'Room 1' });
9+
const mockSubscription = createFakeSubscription({ name: 'room1', t: 'c', disableNotifications: false, rid: 'room1' });
10+
11+
jest.mock('../../../../../../../app/ui-utils/client', () => ({
12+
LegacyRoomManager: {
13+
close: jest.fn(),
14+
},
15+
}));
16+
17+
jest.mock('../../../../../../../client/lib/rooms/roomCoordinator', () => ({
18+
roomCoordinator: {
19+
getRoomDirectives: () => ({
20+
getUiText: () => 'leaveWarning',
21+
}),
22+
},
23+
}));
24+
25+
it('should return leave function if user has subscription', () => {
26+
const wrapper = mockAppRoot()
27+
.withPermission('leave-c')
28+
.withSubscriptions([{ ...mockSubscription, rid: 'room1' }] as unknown as SubscriptionWithRoom[])
29+
.build();
30+
31+
const { result } = renderHook(() => useRoomLeave(mockRoom), { wrapper });
32+
expect(typeof result.current).toBe('function');
33+
});
34+
35+
it('should return null if user does not have subscription', () => {
36+
const wrapper = mockAppRoot().withPermission('leave-c').build();
37+
38+
const { result } = renderHook(() => useRoomLeave(mockRoom), { wrapper });
39+
expect(result.current).toBeNull();
40+
});

apps/meteor/client/views/room/contextualBar/Info/hooks/actions/useRoomLeave.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import type { IRoom } from '@rocket.chat/core-typings';
22
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';
33
import type { TranslationKey } from '@rocket.chat/ui-contexts';
4-
import { useRouter, useSetModal, useToastMessageDispatch, useMethod, useTranslation, usePermission } from '@rocket.chat/ui-contexts';
4+
import { useRouter, useSetModal, useToastMessageDispatch, useMethod, usePermission, useUserSubscription } from '@rocket.chat/ui-contexts';
5+
import { useTranslation } from 'react-i18next';
56

67
import { LegacyRoomManager } from '../../../../../../../app/ui-utils/client';
78
import { UiTextContext } from '../../../../../../../definition/IRoomTypeConfig';
89
import WarningModal from '../../../../../../components/WarningModal';
910
import { roomCoordinator } from '../../../../../../lib/rooms/roomCoordinator';
1011

11-
// TODO implement joined
12-
export const useRoomLeave = (room: IRoom, joined = true) => {
13-
const t = useTranslation();
12+
export const useRoomLeave = (room: IRoom) => {
13+
const { t } = useTranslation();
14+
const subscription = useUserSubscription(room._id);
1415
const setModal = useSetModal();
1516
const dispatchToastMessage = useToastMessageDispatch();
1617
const leaveRoom = useMethod('leaveRoom');
1718
const router = useRouter();
1819

19-
const canLeave = usePermission(room.t === 'c' ? 'leave-c' : 'leave-p') && room.cl !== false && joined;
20+
const canLeave = usePermission(room.t === 'c' ? 'leave-c' : 'leave-p') && room.cl !== false && Boolean(subscription);
2021

2122
const handleLeave = useEffectEvent(() => {
2223
const leaveAction = async () => {

packages/mock-providers/src/MockedAppRootBuilder.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export class MockedAppRootBuilder {
150150
queryPreference: () => [() => () => undefined, () => undefined],
151151
queryRoom: () => [() => () => undefined, () => this.room],
152152
querySubscription: () => [() => () => undefined, () => this.subscriptions as unknown as ISubscription],
153-
querySubscriptions: () => [() => () => undefined, () => this.subscriptions], // apply query and option
153+
querySubscriptions: () => [() => () => undefined, () => this.subscriptions ?? []], // apply query and option
154154
user: null,
155155
userId: undefined,
156156
};
@@ -203,7 +203,7 @@ export class MockedAppRootBuilder {
203203

204204
private room: IRoom | undefined = undefined;
205205

206-
private subscriptions: SubscriptionWithRoom[] = [];
206+
private subscriptions: SubscriptionWithRoom[] | undefined = undefined;
207207

208208
private modal: ModalContextValue = {
209209
currentModal: { component: null },

0 commit comments

Comments
 (0)