2022-02-22 20:00:33 +00:00
|
|
|
import { ERoomTypes } from '../../definitions';
|
2022-04-07 14:19:54 +00:00
|
|
|
import { store } from '../store/auxStore';
|
2022-02-22 20:00:33 +00:00
|
|
|
import database from '../database';
|
|
|
|
import RocketChat from '../rocketchat';
|
2022-04-07 16:53:07 +00:00
|
|
|
import sdk from '../services/sdk';
|
2018-05-07 20:43:26 +00:00
|
|
|
|
|
|
|
const restTypes = {
|
2021-09-13 20:41:05 +00:00
|
|
|
channel: 'channels',
|
|
|
|
direct: 'im',
|
|
|
|
group: 'groups'
|
2018-05-07 20:43:26 +00:00
|
|
|
};
|
|
|
|
|
2022-02-22 20:00:33 +00:00
|
|
|
async function open({ type, rid, name }: { type: ERoomTypes; rid: string; name: string }) {
|
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
|
2022-02-22 20:00:33 +00:00
|
|
|
if (type === ERoomTypes.DIRECT && !rid) {
|
|
|
|
const result = await RocketChat.createDirectMessage(name);
|
2020-04-01 19:39:30 +00:00
|
|
|
if (result.success) {
|
|
|
|
const { room } = result;
|
2022-03-08 16:25:27 +00:00
|
|
|
return {
|
|
|
|
...room,
|
|
|
|
rid: room._id
|
|
|
|
};
|
2020-04-01 19:39:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's a group we need to check if you can open
|
2022-02-22 20:00:33 +00:00
|
|
|
if (type === ERoomTypes.GROUP) {
|
2020-04-01 19:39:30 +00:00
|
|
|
try {
|
|
|
|
// RC 0.61.0
|
2022-02-22 20:00:33 +00:00
|
|
|
// @ts-ignore
|
|
|
|
await sdk.post(`${restTypes[type]}.open`, params);
|
|
|
|
} catch (e: any) {
|
2020-04-01 19:39:30 +00:00
|
|
|
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
|
2022-02-22 20:00:33 +00:00
|
|
|
if ((type === ERoomTypes.CHANNEL || type === ERoomTypes.GROUP) && !rid) {
|
2020-04-01 19:39:30 +00:00
|
|
|
// RC 0.72.0
|
2022-02-22 20:00:33 +00:00
|
|
|
// @ts-ignore
|
2022-03-02 14:18:01 +00:00
|
|
|
const result: any = await sdk.get(`${restTypes[type]}.info`, params);
|
2020-04-01 19:39:30 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 20:00:33 +00:00
|
|
|
export default async function canOpenRoom({ rid, path, isCall }: { rid: string; isCall: boolean; path: string }): Promise<any> {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const subsCollection = db.get('subscriptions');
|
2020-07-30 17:25:52 +00:00
|
|
|
|
|
|
|
if (isCall && !rid) {
|
|
|
|
// Extract rid from a Jitsi URL
|
|
|
|
// Eg.: [Jitsi_URL_Room_Prefix][uniqueID][rid][?jwt]
|
|
|
|
const { Jitsi_URL_Room_Prefix, uniqueID } = store.getState().settings;
|
2021-09-13 20:41:05 +00:00
|
|
|
rid = path.replace(`${Jitsi_URL_Room_Prefix}${uniqueID}`, '').replace(/\?(.*)/g, '');
|
2020-07-30 17:25:52 +00:00
|
|
|
}
|
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,
|
2021-02-22 21:37:13 +00:00
|
|
|
uids: room.uids,
|
|
|
|
usernames: room.usernames
|
2020-04-09 05:20:57 +00:00
|
|
|
};
|
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
|
|
|
|
2020-07-30 17:25:52 +00:00
|
|
|
const [type, name] = path.split('/');
|
2022-02-22 20:00:33 +00:00
|
|
|
const t = type as ERoomTypes;
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
2022-02-22 20:00:33 +00:00
|
|
|
const result = await open({ type: t, rid, name });
|
2020-07-30 17:25:52 +00:00
|
|
|
return result;
|
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
|
|
|
}
|