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 <diegolmello@gmail.com>
Co-authored-by: Gleidson Daniel Silva <gleidson10daniel@hotmail.com>
This commit is contained in:
Alex Junior 2022-02-21 11:40:57 -03:00 committed by GitHub
parent 113cfb043e
commit a3a4b66810
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 9 deletions

View File

@ -37,6 +37,7 @@ export interface IRoom {
tags?: string[]; tags?: string[];
e2eKeyId?: string; e2eKeyId?: string;
avatarETag?: string; avatarETag?: string;
latest?: string;
default?: true; default?: true;
featured?: true; featured?: true;
} }

View File

@ -5,32 +5,41 @@ import log from '../../utils/log';
import { getMessageById } from '../database/services/Message'; import { getMessageById } from '../database/services/Message';
import { generateLoadMoreId } from '../utils'; import { generateLoadMoreId } from '../utils';
import updateMessages from './updateMessages'; 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; const COUNT = 50;
async function load({ rid: roomId, latest, t }) { async function load({ rid: roomId, latest, t }: { rid: string; latest?: string; t: RoomTypes }) {
let params = { roomId, count: COUNT }; let params = { roomId, count: COUNT } as { roomId: string; count: number; latest?: string };
if (latest) { if (latest) {
params = { ...params, latest: new Date(latest).toISOString() }; params = { ...params, latest: new Date(latest).toISOString() };
} }
const apiType = this.roomTypeToApiType(t); const apiType = roomTypeToApiType(t);
if (!apiType) { if (!apiType) {
return []; return [];
} }
// RC 0.48.0 // 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') { if (!data || data.status === 'error') {
return []; return [];
} }
return data.messages; return data.messages;
} }
export default function loadMessagesForRoom(args) { export default function loadMessagesForRoom(args: {
rid: string;
t: RoomTypes;
latest: string;
loaderItem: TMessageModel;
}): Promise<IMessage[] | []> {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
try { try {
const data = await load.call(this, args); const data = await load(args);
if (data?.length) { if (data?.length) {
const lastMessage = data[data.length - 1]; const lastMessage = data[data.length - 1];
const lastMessageRecord = await getMessageById(lastMessage._id); 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 }); await updateMessages({ rid: args.rid, update: data, loaderItem: args.loaderItem });
return resolve(data); return resolve(data);
} else {
return resolve([]);
} }
return resolve([]);
} catch (e) { } catch (e) {
log(e); log(e);
reject(e); reject(e);

View File

@ -13,7 +13,7 @@ import { getSubscriptionByRoomId } from '../database/services/Subscription';
interface IUpdateMessages { interface IUpdateMessages {
rid: string; rid: string;
update: IMessage[]; update: IMessage[];
remove: IMessage[]; remove?: IMessage[];
loaderItem?: TMessageModel; loaderItem?: TMessageModel;
} }