2019-09-16 20:26:32 +00:00
|
|
|
import database from '../database';
|
2018-05-07 20:43:26 +00:00
|
|
|
|
|
|
|
const restTypes = {
|
|
|
|
channel: 'channels', direct: 'im', group: 'groups'
|
|
|
|
};
|
|
|
|
|
2020-04-01 19:39:30 +00:00
|
|
|
async function open({ type, rid, name }) {
|
2018-05-07 20:43:26 +00:00
|
|
|
try {
|
2020-04-01 19:39:30 +00:00
|
|
|
const params = rid ? { roomId: rid } : { roomName: name };
|
|
|
|
|
|
|
|
// if it's a direct link without rid we'll create a new dm
|
|
|
|
// if the dm already exists it'll return the existent
|
|
|
|
if (type === 'direct' && !rid) {
|
|
|
|
const result = await this.createDirectMessage(name);
|
|
|
|
if (result.success) {
|
|
|
|
const { room } = result;
|
|
|
|
room.rid = room._id;
|
|
|
|
return room;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's a group we need to check if you can open
|
|
|
|
if (type === 'group') {
|
|
|
|
try {
|
|
|
|
// RC 0.61.0
|
|
|
|
await this.sdk.post(`${ restTypes[type] }.open`, params);
|
|
|
|
} catch (e) {
|
|
|
|
if (!(e.data && /is already open/.test(e.data.error))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's a channel or group and the link don't have rid
|
|
|
|
// we'll get info from the room
|
|
|
|
if ((type === 'channel' || type === 'group') && !rid) {
|
|
|
|
// RC 0.72.0
|
|
|
|
const result = await this.sdk.get(`${ restTypes[type] }.info`, params);
|
|
|
|
if (result.success) {
|
|
|
|
const room = result[type];
|
|
|
|
room.rid = room._id;
|
|
|
|
return room;
|
|
|
|
}
|
2018-05-07 20:43:26 +00:00
|
|
|
}
|
2020-04-01 19:39:30 +00:00
|
|
|
|
|
|
|
// if rid was sent by link
|
|
|
|
if (rid) {
|
|
|
|
return { rid };
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
} catch (e) {
|
2018-05-07 20:43:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
export default async function canOpenRoom({ rid, path }) {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const db = database.active;
|
|
|
|
const subsCollection = db.collections.get('subscriptions');
|
2020-04-01 19:39:30 +00:00
|
|
|
const [type, name] = path.split('/');
|
2018-05-23 13:39:18 +00:00
|
|
|
|
2020-04-01 19:39:30 +00:00
|
|
|
if (rid) {
|
|
|
|
try {
|
2020-04-09 05:20:57 +00:00
|
|
|
const room = await subsCollection.find(rid);
|
|
|
|
return {
|
|
|
|
rid,
|
|
|
|
t: room.t,
|
|
|
|
name: room.name,
|
|
|
|
fname: room.fname,
|
|
|
|
prid: room.prid,
|
|
|
|
uids: room.uids
|
|
|
|
};
|
2020-04-01 19:39:30 +00:00
|
|
|
} catch (e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
2018-05-07 20:43:26 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
2020-04-01 19:39:30 +00:00
|
|
|
return await open.call(this, { type, rid, name });
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
}
|