Rocket.Chat.ReactNative/app/lib/methods/helpers/isReadOnly.ts

33 lines
1.1 KiB
TypeScript
Raw Normal View History

import { store as reduxStore } from '../../store/auxStore';
import { ISubscription } from '../../../definitions';
import { hasPermission } from './helpers';
2020-04-13 13:56:30 +00:00
const canPostReadOnly = async (room: Partial<ISubscription>, username: string) => {
// RC 6.4.0
const isUnmuted = !!room?.unmuted?.find(m => m === username);
// TODO: this is not reactive. If this permission changes, the component won't be updated
const postReadOnlyPermission = reduxStore.getState().permissions['post-readonly'];
const permission = await hasPermission([postReadOnlyPermission], room.rid);
return permission[0] || isUnmuted;
2020-04-13 13:56:30 +00:00
};
const isMuted = (room: Partial<ISubscription>, username: string) =>
room && room.muted && room.muted.find && !!room.muted.find(m => m === username);
2020-04-13 13:56:30 +00:00
export const isReadOnly = async (room: Partial<ISubscription>, username: string): Promise<boolean> => {
2020-04-13 13:56:30 +00:00
if (room.archived) {
return true;
}
if (isMuted(room, username)) {
return true;
}
if (room?.ro) {
const allowPost = await canPostReadOnly(room, username);
if (allowPost) {
return false;
}
return true;
2020-04-13 13:56:30 +00:00
}
return false;
2020-04-13 13:56:30 +00:00
};