vn-verdnaturachat/app/utils/isReadOnly.ts

34 lines
1022 B
TypeScript
Raw Normal View History

2020-04-13 13:56:30 +00:00
import RocketChat from '../lib/rocketchat';
import reduxStore from '../lib/createStore';
import { ISubscription } from '../definitions/ISubscription';
2020-04-13 13:56:30 +00:00
const canPostReadOnly = async ({ rid }: { rid: string }) => {
// 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 RocketChat.hasPermission([postReadOnlyPermission], rid);
return permission[0];
2020-04-13 13:56:30 +00:00
};
const isMuted = (room: ISubscription, user: { username: string }) =>
room && room.muted && room.muted.find && !!room.muted.find(m => m === user.username);
2020-04-13 13:56:30 +00:00
export const isReadOnly = async (
room: ISubscription,
user: { id?: string; username: string; token?: string }
): Promise<boolean> => {
2020-04-13 13:56:30 +00:00
if (room.archived) {
return true;
}
if (isMuted(room, user)) {
return true;
}
if (room?.ro) {
const allowPost = await canPostReadOnly(room);
if (allowPost) {
return false;
}
return true;
2020-04-13 13:56:30 +00:00
}
return false;
2020-04-13 13:56:30 +00:00
};