|
|
|
|
@ -1,3 +1,5 @@ |
|
|
|
|
import { IRoom, IUser } from '@rocket.chat/core-typings'; |
|
|
|
|
|
|
|
|
|
import { canAccessRoomAsync } from './canAccessRoom'; |
|
|
|
|
import { hasPermissionAsync } from './hasPermission'; |
|
|
|
|
import { Subscriptions, Rooms } from '../../../models/server/raw'; |
|
|
|
|
@ -11,12 +13,16 @@ const subscriptionOptions = { |
|
|
|
|
}, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export const validateRoomMessagePermissionsAsync = async (room, { uid, username, type }, extraData) => { |
|
|
|
|
async function validateRoomMessagePermissionsAsync( |
|
|
|
|
room: IRoom, |
|
|
|
|
{ uid, username, type }: { uid: IUser['_id']; username: IUser['username']; type: IUser['type'] }, |
|
|
|
|
extraData: Record<string, any>, |
|
|
|
|
): Promise<void> { |
|
|
|
|
if (!room) { |
|
|
|
|
throw new Error('error-invalid-room'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (type !== 'app' && !(await canAccessRoomAsync(room, { _id: uid, username }, extraData))) { |
|
|
|
|
if (type !== 'app' && !(await canAccessRoomAsync(room, { _id: uid }, extraData))) { |
|
|
|
|
throw new Error('error-not-allowed'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -29,23 +35,37 @@ export const validateRoomMessagePermissionsAsync = async (room, { uid, username, |
|
|
|
|
|
|
|
|
|
if (room.ro === true && !(await hasPermissionAsync(uid, 'post-readonly', room._id))) { |
|
|
|
|
// Unless the user was manually unmuted
|
|
|
|
|
if (!(room.unmuted || []).includes(username)) { |
|
|
|
|
if (username && !(room.unmuted || []).includes(username)) { |
|
|
|
|
throw new Error("You can't send messages because the room is readonly."); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (room?.muted?.includes(username)) { |
|
|
|
|
if (username && room?.muted?.includes(username)) { |
|
|
|
|
throw new Error('You_have_been_muted'); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const canSendMessageAsync = async (rid, { uid, username, type }, extraData) => { |
|
|
|
|
export async function canSendMessageAsync( |
|
|
|
|
rid: IRoom['_id'], |
|
|
|
|
{ uid, username, type }: { uid: IUser['_id']; username: IUser['username']; type: IUser['type'] }, |
|
|
|
|
extraData: Record<string, any>, |
|
|
|
|
): Promise<IRoom> { |
|
|
|
|
const room = await Rooms.findOneById(rid); |
|
|
|
|
await validateRoomMessagePermissionsAsync(room, { uid, username, type }, extraData); |
|
|
|
|
return room; |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export const canSendMessage = (rid, { uid, username, type }, extraData) => |
|
|
|
|
Promise.await(canSendMessageAsync(rid, { uid, username, type }, extraData)); |
|
|
|
|
export const validateRoomMessagePermissions = (room, { uid, username, type }, extraData) => |
|
|
|
|
Promise.await(validateRoomMessagePermissionsAsync(room, { uid, username, type }, extraData)); |
|
|
|
|
export function canSendMessage( |
|
|
|
|
rid: IRoom['_id'], |
|
|
|
|
{ uid, username, type }: { uid: IUser['_id']; username: IUser['username']; type: IUser['type'] }, |
|
|
|
|
extraData: Record<string, any>, |
|
|
|
|
): IRoom { |
|
|
|
|
return Promise.await(canSendMessageAsync(rid, { uid, username, type }, extraData)); |
|
|
|
|
} |
|
|
|
|
export function validateRoomMessagePermissions( |
|
|
|
|
room: IRoom, |
|
|
|
|
{ uid, username, type }: { uid: IUser['_id']; username: IUser['username']; type: IUser['type'] }, |
|
|
|
|
extraData: Record<string, any>, |
|
|
|
|
): void { |
|
|
|
|
return Promise.await(validateRoomMessagePermissionsAsync(room, { uid, username, type }, extraData)); |
|
|
|
|
} |