Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
type MutedUser = {
username?: string;
};

type MutedRoom = {
ro?: boolean;
unmuted?: string[];
muted?: string[];
};

export const getUserIsMuted = (user: MutedUser, room: MutedRoom | undefined, userCanPostReadonly: boolean): boolean | undefined => {
const username = user.username ?? '';

if (room?.ro) {
if (Array.isArray(room.unmuted) && room.unmuted.indexOf(username) !== -1) {
return false;
}

if (userCanPostReadonly) {
const roomHasNoMutedList = !Array.isArray(room.muted) || room.muted.length === 0;

return roomHasNoMutedList || room.muted.indexOf(username) !== -1;
}

return true;
}

return room && Array.isArray(room.muted) && room.muted.indexOf(username) > -1;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getUserIsMuted } from './getUserIsMuted';

describe('getUserIsMuted', () => {
it('should consider users muted by default in readonly rooms', () => {
const user = { _id: 'uid', username: 'john' };
const room = { ro: true, unmuted: [], muted: [] };

expect(getUserIsMuted(user, room as any, true)).toBe(true);
});

it('should consider users unmuted when they are listed in readonly room unmuted list', () => {
const user = { _id: 'uid', username: 'john' };
const room = { ro: true, unmuted: ['john'], muted: [] };

expect(getUserIsMuted(user, room as any, true)).toBe(false);
});

it('should consider users with post-readonly permission unmuted when they are not in readonly room muted list', () => {
const user = { _id: 'uid', username: 'john' };
const room = { ro: true, unmuted: [], muted: ['mary'] };

expect(getUserIsMuted(user, room as any, true)).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,11 @@ import {
} from '@rocket.chat/ui-contexts';
import { useMemo } from 'react';

import { getUserIsMuted } from './getUserIsMuted';
import { roomCoordinator } from '../../../../../lib/rooms/roomCoordinator';
import { getRoomDirectives } from '../../../lib/getRoomDirectives';
import type { UserInfoAction, UserInfoActionType } from '../useUserInfoActions';

const getUserIsMuted = (
user: Pick<IUser, '_id' | 'username'>,
room: IRoom | undefined,
userCanPostReadonly: boolean,
): boolean | undefined => {
if (room?.ro) {
if (Array.isArray(room.unmuted) && room.unmuted.indexOf(user.username ?? '') !== -1) {
return false;
}

if (userCanPostReadonly) {
return Array.isArray(room.muted) && room.muted.indexOf(user.username ?? '') !== -1;
}

return true;
}

return room && Array.isArray(room.muted) && room.muted.indexOf(user.username ?? '') > -1;
};

export const useMuteUserAction = (user: Pick<IUser, '_id' | 'username'>, rid: IRoom['_id']): UserInfoAction | undefined => {
const t = useTranslation();
const room = useUserRoom(rid);
Expand Down