vn-verdnaturachat/app/lib/methods/canOpenRoom.js

37 lines
709 B
JavaScript
Raw Normal View History

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 }) {
try {
// RC 0.61.0
2019-02-07 15:48:10 +00:00
await this.sdk.post(`${ restTypes[type] }.open`, { roomId: rid });
return true;
2018-12-05 20:52:08 +00:00
} catch (e) {
if (e.data && /is already open/.test(e.data.error)) {
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') {
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);
if (room.length) {
return true;
}
try {
2018-12-05 20:52:08 +00:00
return await open.call(this, { type, rid });
} catch (e) {
return false;
}
}