Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/modern-onions-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rocket.chat/mock-providers': patch
'@rocket.chat/meteor': patch
---

Fixes an issue where leave room action is available for users without subscription
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { mockAppRoot } from '@rocket.chat/mock-providers';
import type { SubscriptionWithRoom } from '@rocket.chat/ui-contexts';
import { renderHook } from '@testing-library/react';

import { useRoomLeave } from './useRoomLeave';
import { createFakeRoom, createFakeSubscription } from '../../../../../../../tests/mocks/data';

const mockRoom = createFakeRoom({ _id: 'room1', t: 'c', name: 'room1', fname: 'Room 1' });
const mockSubscription = createFakeSubscription({ name: 'room1', t: 'c', disableNotifications: false, rid: 'room1' });

jest.mock('../../../../../../../app/ui-utils/client', () => ({
LegacyRoomManager: {
close: jest.fn(),
},
}));

jest.mock('../../../../../../../client/lib/rooms/roomCoordinator', () => ({
roomCoordinator: {
getRoomDirectives: () => ({
getUiText: () => 'leaveWarning',
}),
},
}));

it('should return leave function if user has subscription', () => {
const wrapper = mockAppRoot()
.withPermission('leave-c')
.withSubscriptions([{ ...mockSubscription, rid: 'room1' }] as unknown as SubscriptionWithRoom[])
.build();

const { result } = renderHook(() => useRoomLeave(mockRoom), { wrapper });
expect(typeof result.current).toBe('function');
});

it('should return null if user does not have subscription', () => {
const wrapper = mockAppRoot().withPermission('leave-c').build();

const { result } = renderHook(() => useRoomLeave(mockRoom), { wrapper });
expect(result.current).toBeNull();
});
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import type { IRoom } from '@rocket.chat/core-typings';
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';
import type { TranslationKey } from '@rocket.chat/ui-contexts';
import { useRouter, useSetModal, useToastMessageDispatch, useMethod, useTranslation, usePermission } from '@rocket.chat/ui-contexts';
import { useRouter, useSetModal, useToastMessageDispatch, useMethod, usePermission, useUserSubscription } from '@rocket.chat/ui-contexts';
import { useTranslation } from 'react-i18next';

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

// TODO implement joined
export const useRoomLeave = (room: IRoom, joined = true) => {
const t = useTranslation();
export const useRoomLeave = (room: IRoom) => {
const { t } = useTranslation();
const subscription = useUserSubscription(room._id);
const setModal = useSetModal();
const dispatchToastMessage = useToastMessageDispatch();
const leaveRoom = useMethod('leaveRoom');
const router = useRouter();

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

const handleLeave = useEffectEvent(() => {
const leaveAction = async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/mock-providers/src/MockedAppRootBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class MockedAppRootBuilder {
queryPreference: () => [() => () => undefined, () => undefined],
queryRoom: () => [() => () => undefined, () => this.room],
querySubscription: () => [() => () => undefined, () => this.subscriptions as unknown as ISubscription],
querySubscriptions: () => [() => () => undefined, () => this.subscriptions], // apply query and option
querySubscriptions: () => [() => () => undefined, () => this.subscriptions ?? []], // apply query and option
user: null,
userId: undefined,
};
Expand Down Expand Up @@ -203,7 +203,7 @@ export class MockedAppRootBuilder {

private room: IRoom | undefined = undefined;

private subscriptions: SubscriptionWithRoom[] = [];
private subscriptions: SubscriptionWithRoom[] | undefined = undefined;

private modal: ModalContextValue = {
currentModal: { component: null },
Expand Down
Loading