From a2c6d2c901fd736bb3eb6384d32273fbb31366d7 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Tue, 25 Jan 2022 09:38:56 -0300 Subject: [PATCH 1/3] chore: migrate canOpenRoom to ts --- .../{canOpenRoom.js => canOpenRoom.ts} | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) rename app/lib/methods/{canOpenRoom.js => canOpenRoom.ts} (77%) diff --git a/app/lib/methods/canOpenRoom.js b/app/lib/methods/canOpenRoom.ts similarity index 77% rename from app/lib/methods/canOpenRoom.js rename to app/lib/methods/canOpenRoom.ts index 183b32a369..e719cd7f52 100644 --- a/app/lib/methods/canOpenRoom.js +++ b/app/lib/methods/canOpenRoom.ts @@ -7,13 +7,19 @@ const restTypes = { group: 'groups' }; -async function open({ type, rid, name }) { +enum ETypes { + DIRECT = 'direct', + GROUP = 'group', + CHANNEL = 'channel' +} + +async function open(this: any, { type, rid, name }: { type: ETypes; 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) { + if (type === ETypes.DIRECT && !rid) { const result = await this.createDirectMessage(name); if (result.success) { const { room } = result; @@ -23,11 +29,11 @@ async function open({ type, rid, name }) { } // if it's a group we need to check if you can open - if (type === 'group') { + if (type === ETypes.GROUP) { try { // RC 0.61.0 await this.sdk.post(`${restTypes[type]}.open`, params); - } catch (e) { + } catch (e: any) { if (!(e.data && /is already open/.test(e.data.error))) { return false; } @@ -36,7 +42,7 @@ 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 === ETypes.CHANNEL || type === ETypes.GROUP) && !rid) { // RC 0.72.0 const result = await this.sdk.get(`${restTypes[type]}.info`, params); if (result.success) { @@ -56,7 +62,10 @@ async function open({ type, rid, name }) { } } -export default async function canOpenRoom({ rid, path, isCall }) { +export default async function canOpenRoom( + this: any, + { rid, path, isCall }: { rid: string; isCall: boolean; path: string } +): Promise { try { const db = database.active; const subsCollection = db.get('subscriptions'); @@ -86,8 +95,9 @@ export default async function canOpenRoom({ rid, path, isCall }) { } const [type, name] = path.split('/'); + const t = type as ETypes; try { - const result = await open.call(this, { type, rid, name }); + const result = await open.call(this, { type: t, rid, name }); return result; } catch (e) { return false; From bdb9d82eab65131a171ad0a877273ea1f6593818 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Tue, 25 Jan 2022 09:59:27 -0300 Subject: [PATCH 2/3] chore: update rocketchat types --- app/lib/methods/canOpenRoom.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/app/lib/methods/canOpenRoom.ts b/app/lib/methods/canOpenRoom.ts index e719cd7f52..3212f896e9 100644 --- a/app/lib/methods/canOpenRoom.ts +++ b/app/lib/methods/canOpenRoom.ts @@ -1,5 +1,7 @@ -import database from '../database'; import { store } from '../auxStore'; +import database from '../database'; +import RocketChat from '../rocketchat'; +import sdk from '../rocketchat/services/sdk'; const restTypes = { channel: 'channels', @@ -13,17 +15,17 @@ enum ETypes { CHANNEL = 'channel' } -async function open(this: any, { type, rid, name }: { type: ETypes; rid: string; name: string }) { +async function open({ type, rid, name }: { type: ETypes; 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 === ETypes.DIRECT && !rid) { - const result = await this.createDirectMessage(name); + const result = await RocketChat.createDirectMessage(name); if (result.success) { const { room } = result; - room.rid = room._id; + room.rid = room._id as string; return room; } } @@ -32,7 +34,8 @@ async function open(this: any, { type, rid, name }: { type: ETypes; rid: string; if (type === ETypes.GROUP) { try { // RC 0.61.0 - await this.sdk.post(`${restTypes[type]}.open`, params); + // @ts-ignore + await sdk.post(`${restTypes[type]}.open`, params); } catch (e: any) { if (!(e.data && /is already open/.test(e.data.error))) { return false; @@ -44,7 +47,8 @@ async function open(this: any, { type, rid, name }: { type: ETypes; rid: string; // we'll get info from the room if ((type === ETypes.CHANNEL || type === ETypes.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; @@ -62,10 +66,7 @@ async function open(this: any, { type, rid, name }: { type: ETypes; rid: string; } } -export default async function canOpenRoom( - this: any, - { rid, path, isCall }: { rid: string; isCall: boolean; path: string } -): Promise { +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'); @@ -97,7 +98,7 @@ export default async function canOpenRoom( const [type, name] = path.split('/'); const t = type as ETypes; try { - const result = await open.call(this, { type: t, rid, name }); + const result = await open({ type: t, rid, name }); return result; } catch (e) { return false; From 4ee978a2290806aadddd17957ee12b7cbf1228b5 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Fri, 4 Feb 2022 17:33:56 -0300 Subject: [PATCH 3/3] change types to Isubscription types --- app/definitions/ISubscription.ts | 6 ++++++ app/lib/methods/canOpenRoom.ts | 17 ++++++----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/app/definitions/ISubscription.ts b/app/definitions/ISubscription.ts index 5f561edfb5..1484b9ebed 100644 --- a/app/definitions/ISubscription.ts +++ b/app/definitions/ISubscription.ts @@ -23,6 +23,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.ts b/app/lib/methods/canOpenRoom.ts index 3212f896e9..7d3e72687d 100644 --- a/app/lib/methods/canOpenRoom.ts +++ b/app/lib/methods/canOpenRoom.ts @@ -1,3 +1,4 @@ +import { ERoomTypes } from '../../definitions'; import { store } from '../auxStore'; import database from '../database'; import RocketChat from '../rocketchat'; @@ -9,19 +10,13 @@ const restTypes = { group: 'groups' }; -enum ETypes { - DIRECT = 'direct', - GROUP = 'group', - CHANNEL = 'channel' -} - -async function open({ type, rid, name }: { type: ETypes; rid: string; name: string }) { +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 === ETypes.DIRECT && !rid) { + if (type === ERoomTypes.DIRECT && !rid) { const result = await RocketChat.createDirectMessage(name); if (result.success) { const { room } = result; @@ -31,7 +26,7 @@ async function open({ type, rid, name }: { type: ETypes; rid: string; name: stri } // if it's a group we need to check if you can open - if (type === ETypes.GROUP) { + if (type === ERoomTypes.GROUP) { try { // RC 0.61.0 // @ts-ignore @@ -45,7 +40,7 @@ async function open({ type, rid, name }: { type: ETypes; rid: string; name: stri // if it's a channel or group and the link don't have rid // we'll get info from the room - if ((type === ETypes.CHANNEL || type === ETypes.GROUP) && !rid) { + if ((type === ERoomTypes.CHANNEL || type === ERoomTypes.GROUP) && !rid) { // RC 0.72.0 // @ts-ignore const result: any = await sdk.get(`channel.info`, params); @@ -96,7 +91,7 @@ export default async function canOpenRoom({ rid, path, isCall }: { rid: string; } const [type, name] = path.split('/'); - const t = type as ETypes; + const t = type as ERoomTypes; try { const result = await open({ type: t, rid, name }); return result;