2018-10-15 20:22:42 +00:00
|
|
|
import * as SDK from '@rocket.chat/sdk';
|
|
|
|
|
2018-05-07 20:43:26 +00:00
|
|
|
import database from '../realm';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2018-05-07 20:43:26 +00:00
|
|
|
|
|
|
|
// TODO: api fix
|
|
|
|
const ddpTypes = {
|
|
|
|
channel: 'c', direct: 'd', group: 'p'
|
|
|
|
};
|
|
|
|
const restTypes = {
|
|
|
|
channel: 'channels', direct: 'im', group: 'groups'
|
|
|
|
};
|
|
|
|
|
|
|
|
async function canOpenRoomREST({ type, rid }) {
|
|
|
|
try {
|
2018-10-15 20:22:42 +00:00
|
|
|
await SDK.api.post(`${ restTypes[type] }.open`, { roomId: rid });
|
2018-05-07 20:43:26 +00:00
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
// TODO: workround for 'already open for the sender' error
|
|
|
|
if (!error.errorType) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function canOpenRoomDDP(...args) {
|
|
|
|
try {
|
|
|
|
const [{ type, name }] = args;
|
2018-10-15 20:22:42 +00:00
|
|
|
await SDK.driver.asyncCall('getRoomByTypeAndName', ddpTypes[type], name);
|
2018-05-07 20:43:26 +00:00
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
if (error.isClientSafe) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return canOpenRoomREST.call(this, ...args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default async function canOpenRoom({ rid, path }) {
|
|
|
|
const { database: db } = database;
|
2018-05-23 13:39:18 +00:00
|
|
|
|
2018-05-07 20:43:26 +00:00
|
|
|
const room = db.objects('subscriptions').filtered('rid == $0', rid);
|
|
|
|
if (room.length) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const [type, name] = path.split('/');
|
2018-05-18 17:55:08 +00:00
|
|
|
|
|
|
|
try {
|
2018-11-16 11:06:29 +00:00
|
|
|
const data = await (this.connected() ? canOpenRoomDDP.call(this, { rid, type, name }) : canOpenRoomREST.call(this, { type, rid }));
|
2018-05-18 17:55:08 +00:00
|
|
|
return data;
|
|
|
|
} catch (e) {
|
|
|
|
log('canOpenRoom', e);
|
2018-07-10 13:40:32 +00:00
|
|
|
return false;
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|