From b5d964095eee59f324fea6bfd08df3ef6ff6890a Mon Sep 17 00:00:00 2001 From: Gleidson Daniel Silva Date: Tue, 22 Feb 2022 17:00:33 -0300 Subject: [PATCH] Chore: Migrate method canOpenRoom to Typescript (#3650) * chore: migrate canOpenRoom to ts * chore: update rocketchat types * change types to Isubscription types --- app/definitions/ISubscription.ts | 6 ++++ .../{canOpenRoom.js => canOpenRoom.ts} | 30 +++++++++++-------- 2 files changed, 24 insertions(+), 12 deletions(-) rename app/lib/methods/{canOpenRoom.js => canOpenRoom.ts} (66%) diff --git a/app/definitions/ISubscription.ts b/app/definitions/ISubscription.ts index 06c4db9b6..cf10b68e9 100644 --- a/app/definitions/ISubscription.ts +++ b/app/definitions/ISubscription.ts @@ -24,6 +24,12 @@ export interface IVisitor { lastMessageTs: Date; } +export enum ERoomTypes { + DIRECT = 'direct', + GROUP = 'group', + CHANNEL = 'channel' +} + export interface ISubscription { _id: string; // _id belongs watermelonDB id: string; // id from server diff --git a/app/lib/methods/canOpenRoom.js b/app/lib/methods/canOpenRoom.ts similarity index 66% rename from app/lib/methods/canOpenRoom.js rename to app/lib/methods/canOpenRoom.ts index 183b32a36..7d3e72687 100644 --- a/app/lib/methods/canOpenRoom.js +++ b/app/lib/methods/canOpenRoom.ts @@ -1,5 +1,8 @@ -import database from '../database'; +import { ERoomTypes } from '../../definitions'; import { store } from '../auxStore'; +import database from '../database'; +import RocketChat from '../rocketchat'; +import sdk from '../rocketchat/services/sdk'; const restTypes = { channel: 'channels', @@ -7,27 +10,28 @@ const restTypes = { group: 'groups' }; -async function open({ type, rid, name }) { +async function open({ type, rid, name }: { type: ERoomTypes; rid: string; name: string }) { try { 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 (type === ERoomTypes.DIRECT && !rid) { + const result = await RocketChat.createDirectMessage(name); if (result.success) { const { room } = result; - room.rid = room._id; + room.rid = room._id as string; return room; } } // if it's a group we need to check if you can open - if (type === 'group') { + if (type === ERoomTypes.GROUP) { try { // RC 0.61.0 - await this.sdk.post(`${restTypes[type]}.open`, params); - } catch (e) { + // @ts-ignore + await sdk.post(`${restTypes[type]}.open`, params); + } catch (e: any) { if (!(e.data && /is already open/.test(e.data.error))) { 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 // 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 - const result = await this.sdk.get(`${restTypes[type]}.info`, params); + // @ts-ignore + const result: any = await sdk.get(`channel.info`, params); if (result.success) { const room = result[type]; 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 { try { const db = database.active; const subsCollection = db.get('subscriptions'); @@ -86,8 +91,9 @@ export default async function canOpenRoom({ rid, path, isCall }) { } const [type, name] = path.split('/'); + const t = type as ERoomTypes; try { - const result = await open.call(this, { type, rid, name }); + const result = await open({ type: t, rid, name }); return result; } catch (e) { return false;