2022-04-07 14:19:54 +00:00
|
|
|
import { store as reduxStore } from '../lib/store/auxStore';
|
2022-01-12 12:54:04 +00:00
|
|
|
import { ISubscription } from '../definitions/ISubscription';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { hasPermission } from '../lib/methods';
|
2020-04-13 13:56:30 +00:00
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
const canPostReadOnly = async ({ rid }: { rid: string }) => {
|
2021-02-25 16:41:44 +00:00
|
|
|
// TODO: this is not reactive. If this permission changes, the component won't be updated
|
|
|
|
const postReadOnlyPermission = reduxStore.getState().permissions['post-readonly'];
|
2022-04-28 20:37:25 +00:00
|
|
|
const permission = await hasPermission([postReadOnlyPermission], rid);
|
2021-02-25 16:41:44 +00:00
|
|
|
return permission[0];
|
2020-04-13 13:56:30 +00:00
|
|
|
};
|
|
|
|
|
2022-03-02 14:18:01 +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
|
|
|
|
2022-03-02 14:18:01 +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;
|
|
|
|
}
|
2022-03-02 14:18:01 +00:00
|
|
|
if (isMuted(room, username)) {
|
2020-06-26 20:22:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (room?.ro) {
|
2022-03-02 14:18:01 +00:00
|
|
|
const allowPost = await canPostReadOnly({ rid: room.rid as string });
|
2020-06-26 20:22:56 +00:00
|
|
|
if (allowPost) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-04-13 13:56:30 +00:00
|
|
|
}
|
2020-06-26 20:22:56 +00:00
|
|
|
return false;
|
2020-04-13 13:56:30 +00:00
|
|
|
};
|