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:
parent
113cfb043e
commit
a3a4b66810
|
@ -37,6 +37,7 @@ export interface IRoom {
|
|||
tags?: string[];
|
||||
e2eKeyId?: string;
|
||||
avatarETag?: string;
|
||||
latest?: string;
|
||||
default?: true;
|
||||
featured?: true;
|
||||
}
|
||||
|
|
|
@ -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<IMessage[] | []> {
|
||||
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);
|
|
@ -13,7 +13,7 @@ import { getSubscriptionByRoomId } from '../database/services/Subscription';
|
|||
interface IUpdateMessages {
|
||||
rid: string;
|
||||
update: IMessage[];
|
||||
remove: IMessage[];
|
||||
remove?: IMessage[];
|
||||
loaderItem?: TMessageModel;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue