2021-05-26 17:24:54 +00:00
|
|
|
import EJSON from 'ejson';
|
|
|
|
import moment from 'moment';
|
|
|
|
import orderBy from 'lodash/orderBy';
|
|
|
|
|
|
|
|
import log from '../../utils/log';
|
|
|
|
import { getMessageById } from '../database/services/Message';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { MessageTypeLoad } from '../constants';
|
2022-03-03 20:25:03 +00:00
|
|
|
import sdk from '../rocketchat/services/sdk';
|
|
|
|
import { IMessage } from '../../definitions';
|
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;
|
|
|
|
|
2022-03-03 20:25:03 +00:00
|
|
|
export default function loadSurroundingMessages({ messageId, rid }: { messageId: string; rid: string }) {
|
2021-09-13 20:41:05 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2021-05-26 17:24:54 +00:00
|
|
|
try {
|
2022-03-03 20:25:03 +00:00
|
|
|
const data = await sdk.methodCallWrapper('loadSurroundingMessages', { _id: messageId, rid }, COUNT);
|
|
|
|
let messages: IMessage[] = EJSON.fromJSONValue(data?.messages);
|
2021-05-26 17:24:54 +00:00
|
|
|
messages = orderBy(messages, 'ts');
|
|
|
|
|
|
|
|
const message = messages.find(m => m._id === messageId);
|
2022-03-03 20:25:03 +00:00
|
|
|
const tmid = message?.tmid;
|
2021-05-26 17:24:54 +00:00
|
|
|
|
|
|
|
if (messages?.length) {
|
|
|
|
if (data?.moreBefore) {
|
|
|
|
const firstMessage = messages[0];
|
|
|
|
const firstMessageRecord = await getMessageById(firstMessage._id);
|
|
|
|
if (!firstMessageRecord) {
|
|
|
|
const loadMoreItem = {
|
|
|
|
_id: generateLoadMoreId(firstMessage._id),
|
|
|
|
rid: firstMessage.rid,
|
|
|
|
tmid,
|
2022-03-03 20:25:03 +00:00
|
|
|
ts: moment(firstMessage.ts).subtract(1, 'millisecond').toDate(),
|
|
|
|
t: MessageTypeLoad.PREVIOUS_CHUNK,
|
2021-05-26 17:24:54 +00:00
|
|
|
msg: firstMessage.msg
|
2022-03-03 20:25:03 +00:00
|
|
|
} as IMessage;
|
2021-05-26 17:24:54 +00:00
|
|
|
messages.unshift(loadMoreItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data?.moreAfter) {
|
|
|
|
const lastMessage = messages[messages.length - 1];
|
|
|
|
const lastMessageRecord = await getMessageById(lastMessage._id);
|
|
|
|
if (!lastMessageRecord) {
|
|
|
|
const loadMoreItem = {
|
|
|
|
_id: generateLoadMoreId(lastMessage._id),
|
|
|
|
rid: lastMessage.rid,
|
|
|
|
tmid,
|
2022-03-03 20:25:03 +00:00
|
|
|
ts: moment(lastMessage.ts).add(1, 'millisecond').toDate(),
|
|
|
|
t: MessageTypeLoad.NEXT_CHUNK,
|
2021-05-26 17:24:54 +00:00
|
|
|
msg: lastMessage.msg
|
2022-03-03 20:25:03 +00:00
|
|
|
} as IMessage;
|
2021-05-26 17:24:54 +00:00
|
|
|
messages.push(loadMoreItem);
|
|
|
|
}
|
|
|
|
}
|
2022-03-03 20:25:03 +00:00
|
|
|
|
2021-05-26 17:24:54 +00:00
|
|
|
await updateMessages({ rid, update: messages });
|
|
|
|
return resolve(messages);
|
|
|
|
}
|
2022-03-03 20:25:03 +00:00
|
|
|
return resolve([]);
|
2021-05-26 17:24:54 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|