Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Migrate methods/loadMessagesForRoom to Typescript #3701

Merged
merged 13 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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