Skip to content

Commit

Permalink
Chore: Migrate methods/loadMessagesForRoom to Typescript (#3701)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Feb 21, 2022
1 parent 113cfb0 commit a3a4b66
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/definitions/IRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface IRoom {
tags?: string[];
e2eKeyId?: string;
avatarETag?: string;
latest?: string;
default?: true;
featured?: true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion app/lib/methods/updateMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getSubscriptionByRoomId } from '../database/services/Subscription';
interface IUpdateMessages {
rid: string;
update: IMessage[];
remove: IMessage[];
remove?: IMessage[];
loaderItem?: TMessageModel;
}

Expand Down

0 comments on commit a3a4b66

Please sign in to comment.