2021-05-26 17:24:54 +00:00
|
|
|
import moment from 'moment';
|
|
|
|
|
2022-04-07 14:10:03 +00:00
|
|
|
import { MessageTypeLoad } from '../constants';
|
2022-03-03 20:25:03 +00:00
|
|
|
import { IMessage, TMessageModel } from '../../definitions';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2021-05-26 17:24:54 +00:00
|
|
|
import { getMessageById } from '../database/services/Message';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { RoomTypes, roomTypeToApiType } from './roomTypeToApiType';
|
2022-04-07 16:53:07 +00:00
|
|
|
import sdk from '../services/sdk';
|
2021-09-13 20:41:05 +00:00
|
|
|
import updateMessages from './updateMessages';
|
2022-04-07 13:13:19 +00:00
|
|
|
import { generateLoadMoreId } from './helpers/generateLoadMoreId';
|
2021-05-26 17:24:54 +00:00
|
|
|
|
|
|
|
const COUNT = 50;
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2022-03-02 14:18:01 +00:00
|
|
|
async function load({ rid: roomId, latest, t }: { rid: string; latest?: Date; t: RoomTypes }) {
|
2022-02-21 14:40:57 +00:00
|
|
|
let params = { roomId, count: COUNT } as { roomId: string; count: number; latest?: string };
|
2018-10-15 20:22:42 +00:00
|
|
|
if (latest) {
|
2018-12-05 20:52:08 +00:00
|
|
|
params = { ...params, latest: new Date(latest).toISOString() };
|
2018-10-15 20:22:42 +00:00
|
|
|
}
|
2020-06-15 14:00:46 +00:00
|
|
|
|
2022-02-21 14:40:57 +00:00
|
|
|
const apiType = roomTypeToApiType(t);
|
2020-06-15 14:00:46 +00:00
|
|
|
if (!apiType) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
// RC 0.48.0
|
2022-02-25 21:32:03 +00:00
|
|
|
const data = await sdk.get(`${apiType}.history`, params);
|
|
|
|
if (!data.success) {
|
2018-09-26 13:56:36 +00:00
|
|
|
return [];
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
return data.messages;
|
|
|
|
}
|
|
|
|
|
2022-04-28 20:37:25 +00:00
|
|
|
export function loadMessagesForRoom(args: {
|
2022-02-21 14:40:57 +00:00
|
|
|
rid: string;
|
|
|
|
t: RoomTypes;
|
2022-03-02 14:18:01 +00:00
|
|
|
latest?: Date;
|
|
|
|
loaderItem?: TMessageModel;
|
|
|
|
}): Promise<void> {
|
2021-09-13 20:41:05 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2022-02-25 21:32:03 +00:00
|
|
|
const data: Partial<IMessage>[] = await load(args);
|
2021-05-26 17:24:54 +00:00
|
|
|
if (data?.length) {
|
|
|
|
const lastMessage = data[data.length - 1];
|
2022-02-25 21:32:03 +00:00
|
|
|
const lastMessageRecord = await getMessageById(lastMessage._id as string);
|
2021-05-26 17:24:54 +00:00
|
|
|
if (!lastMessageRecord && data.length === COUNT) {
|
2022-02-25 21:32:03 +00:00
|
|
|
const loadMoreMessage = {
|
|
|
|
_id: generateLoadMoreId(lastMessage._id as string),
|
2021-05-26 17:24:54 +00:00
|
|
|
rid: lastMessage.rid,
|
2022-02-25 21:32:03 +00:00
|
|
|
ts: moment(lastMessage.ts).subtract(1, 'millisecond').toString(),
|
2022-03-03 20:25:03 +00:00
|
|
|
t: MessageTypeLoad.MORE,
|
2021-05-26 17:24:54 +00:00
|
|
|
msg: lastMessage.msg
|
|
|
|
};
|
2022-02-25 21:32:03 +00:00
|
|
|
data.push(loadMoreMessage);
|
2021-05-26 17:24:54 +00:00
|
|
|
}
|
|
|
|
await updateMessages({ rid: args.rid, update: data, loaderItem: args.loaderItem });
|
2022-03-02 14:18:01 +00:00
|
|
|
return resolve();
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
2022-03-02 14:18:01 +00:00
|
|
|
return resolve();
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-05-18 17:55:08 +00:00
|
|
|
reject(e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|