From a3a4b66810b531c185900a8047da66c2aeebf701 Mon Sep 17 00:00:00 2001 From: Alex Junior Date: Mon, 21 Feb 2022 11:40:57 -0300 Subject: [PATCH] Chore: Migrate methods/loadMessagesForRoom to Typescript (#3701) * chore: change loadMessagesForRoom to typescript * minor tweak * chore: minor tweaks after merge with developer * chore: minor tweaks after merge with developer * chore: minor tweak * chore: minor tweaks * Fix return Co-authored-by: Diego Mello Co-authored-by: Gleidson Daniel Silva --- app/definitions/IRoom.ts | 1 + ...sagesForRoom.js => loadMessagesForRoom.ts} | 24 ++++++++++++------- app/lib/methods/updateMessages.ts | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) rename app/lib/methods/{loadMessagesForRoom.js => loadMessagesForRoom.ts} (62%) diff --git a/app/definitions/IRoom.ts b/app/definitions/IRoom.ts index 11130215b5..2ea2f18240 100644 --- a/app/definitions/IRoom.ts +++ b/app/definitions/IRoom.ts @@ -37,6 +37,7 @@ export interface IRoom { tags?: string[]; e2eKeyId?: string; avatarETag?: string; + latest?: string; default?: true; featured?: true; } diff --git a/app/lib/methods/loadMessagesForRoom.js b/app/lib/methods/loadMessagesForRoom.ts similarity index 62% rename from app/lib/methods/loadMessagesForRoom.js rename to app/lib/methods/loadMessagesForRoom.ts index 4a11bcc8ce..db64a05caa 100644 --- a/app/lib/methods/loadMessagesForRoom.js +++ b/app/lib/methods/loadMessagesForRoom.ts @@ -5,32 +5,41 @@ import log from '../../utils/log'; import { getMessageById } from '../database/services/Message'; import { generateLoadMoreId } from '../utils'; import updateMessages from './updateMessages'; +import { IMessage, TMessageModel } from '../../definitions'; +import sdk from '../rocketchat/services/sdk'; +import roomTypeToApiType, { RoomTypes } from '../rocketchat/methods/roomTypeToApiType'; const COUNT = 50; -async function load({ rid: roomId, latest, t }) { - let params = { roomId, count: COUNT }; +async function load({ rid: roomId, latest, t }: { rid: string; latest?: string; t: RoomTypes }) { + let params = { roomId, count: COUNT } as { roomId: string; count: number; latest?: string }; if (latest) { params = { ...params, latest: new Date(latest).toISOString() }; } - const apiType = this.roomTypeToApiType(t); + const apiType = roomTypeToApiType(t); if (!apiType) { return []; } // RC 0.48.0 - const data = await this.sdk.get(`${apiType}.history`, params); + // @ts-ignore + const data: any = await sdk.get(`${apiType}.history`, params); if (!data || data.status === 'error') { return []; } return data.messages; } -export default function loadMessagesForRoom(args) { +export default function loadMessagesForRoom(args: { + rid: string; + t: RoomTypes; + latest: string; + loaderItem: TMessageModel; +}): Promise { return new Promise(async (resolve, reject) => { try { - const data = await load.call(this, args); + const data = await load(args); if (data?.length) { const lastMessage = data[data.length - 1]; const lastMessageRecord = await getMessageById(lastMessage._id); @@ -46,9 +55,8 @@ export default function loadMessagesForRoom(args) { } await updateMessages({ rid: args.rid, update: data, loaderItem: args.loaderItem }); return resolve(data); - } else { - return resolve([]); } + return resolve([]); } catch (e) { log(e); reject(e); diff --git a/app/lib/methods/updateMessages.ts b/app/lib/methods/updateMessages.ts index ddfd872218..dd1f535cbd 100644 --- a/app/lib/methods/updateMessages.ts +++ b/app/lib/methods/updateMessages.ts @@ -13,7 +13,7 @@ import { getSubscriptionByRoomId } from '../database/services/Subscription'; interface IUpdateMessages { rid: string; update: IMessage[]; - remove: IMessage[]; + remove?: IMessage[]; loaderItem?: TMessageModel; }