Chore: Migrate method canOpenRoom to Typescript (#3650)

* chore: migrate canOpenRoom to ts

* chore: update rocketchat types

* change types to Isubscription types
This commit is contained in:
Gleidson Daniel Silva 2022-02-22 17:00:33 -03:00 committed by GitHub
parent cbfca132c5
commit b5d964095e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 12 deletions

View File

@ -24,6 +24,12 @@ export interface IVisitor {
lastMessageTs: Date; lastMessageTs: Date;
} }
export enum ERoomTypes {
DIRECT = 'direct',
GROUP = 'group',
CHANNEL = 'channel'
}
export interface ISubscription { export interface ISubscription {
_id: string; // _id belongs watermelonDB _id: string; // _id belongs watermelonDB
id: string; // id from server id: string; // id from server

View File

@ -1,5 +1,8 @@
import database from '../database'; import { ERoomTypes } from '../../definitions';
import { store } from '../auxStore'; import { store } from '../auxStore';
import database from '../database';
import RocketChat from '../rocketchat';
import sdk from '../rocketchat/services/sdk';
const restTypes = { const restTypes = {
channel: 'channels', channel: 'channels',
@ -7,27 +10,28 @@ const restTypes = {
group: 'groups' group: 'groups'
}; };
async function open({ type, rid, name }) { async function open({ type, rid, name }: { type: ERoomTypes; rid: string; name: string }) {
try { try {
const params = rid ? { roomId: rid } : { roomName: name }; const params = rid ? { roomId: rid } : { roomName: name };
// if it's a direct link without rid we'll create a new dm // 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 the dm already exists it'll return the existent
if (type === 'direct' && !rid) { if (type === ERoomTypes.DIRECT && !rid) {
const result = await this.createDirectMessage(name); const result = await RocketChat.createDirectMessage(name);
if (result.success) { if (result.success) {
const { room } = result; const { room } = result;
room.rid = room._id; room.rid = room._id as string;
return room; return room;
} }
} }
// if it's a group we need to check if you can open // if it's a group we need to check if you can open
if (type === 'group') { if (type === ERoomTypes.GROUP) {
try { try {
// RC 0.61.0 // RC 0.61.0
await this.sdk.post(`${restTypes[type]}.open`, params); // @ts-ignore
} catch (e) { await sdk.post(`${restTypes[type]}.open`, params);
} catch (e: any) {
if (!(e.data && /is already open/.test(e.data.error))) { if (!(e.data && /is already open/.test(e.data.error))) {
return false; return false;
} }
@ -36,9 +40,10 @@ async function open({ type, rid, name }) {
// if it's a channel or group and the link don't have rid // if it's a channel or group and the link don't have rid
// we'll get info from the room // we'll get info from the room
if ((type === 'channel' || type === 'group') && !rid) { if ((type === ERoomTypes.CHANNEL || type === ERoomTypes.GROUP) && !rid) {
// RC 0.72.0 // RC 0.72.0
const result = await this.sdk.get(`${restTypes[type]}.info`, params); // @ts-ignore
const result: any = await sdk.get(`channel.info`, params);
if (result.success) { if (result.success) {
const room = result[type]; const room = result[type];
room.rid = room._id; room.rid = room._id;
@ -56,7 +61,7 @@ async function open({ type, rid, name }) {
} }
} }
export default async function canOpenRoom({ rid, path, isCall }) { export default async function canOpenRoom({ rid, path, isCall }: { rid: string; isCall: boolean; path: string }): Promise<any> {
try { try {
const db = database.active; const db = database.active;
const subsCollection = db.get('subscriptions'); const subsCollection = db.get('subscriptions');
@ -86,8 +91,9 @@ export default async function canOpenRoom({ rid, path, isCall }) {
} }
const [type, name] = path.split('/'); const [type, name] = path.split('/');
const t = type as ERoomTypes;
try { try {
const result = await open.call(this, { type, rid, name }); const result = await open({ type: t, rid, name });
return result; return result;
} catch (e) { } catch (e) {
return false; return false;