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';
|
|
|
|
|
|
|
|
const restTypes = {
|
|
|
|
channel: 'channels', direct: 'im', group: 'groups'
|
|
|
|
};
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
async function open({ type, rid }) {
|
2018-05-07 20:43:26 +00:00
|
|
|
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;
|
2018-12-05 20:52:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
if (e.data && /is already open/.test(e.data.error)) {
|
2018-05-07 20:43:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
export default async function canOpenRoom({ rid, path }) {
|
|
|
|
const [type] = path.split('/');
|
|
|
|
if (type === 'channel') {
|
2018-05-07 20:43:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-23 13:39:18 +00:00
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
const room = database.objects('subscriptions').filtered('rid == $0', rid);
|
2018-05-07 20:43:26 +00:00
|
|
|
if (room.length) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2018-12-05 20:52:08 +00:00
|
|
|
return await open.call(this, { type, rid });
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (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
|
|
|
}
|