2021-05-26 17:24:54 +00:00
|
|
|
import moment from 'moment';
|
|
|
|
|
|
|
|
import { MESSAGE_TYPE_LOAD_MORE } from '../../constants/messageTypeLoad';
|
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';
|
|
|
|
import { generateLoadMoreId } from '../utils';
|
2021-09-13 20:41:05 +00:00
|
|
|
import updateMessages from './updateMessages';
|
2021-05-26 17:24:54 +00:00
|
|
|
|
|
|
|
const COUNT = 50;
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
async function load({ rid: roomId, latest, t }) {
|
2021-05-26 17:24:54 +00:00
|
|
|
let params = { roomId, count: COUNT };
|
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
|
|
|
|
|
|
|
const apiType = this.roomTypeToApiType(t);
|
|
|
|
if (!apiType) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-12-21 10:55:35 +00:00
|
|
|
// RC 0.48.0
|
2021-09-13 20:41:05 +00:00
|
|
|
const data = await this.sdk.get(`${apiType}.history`, params);
|
2018-09-26 13:56:36 +00:00
|
|
|
if (!data || data.status === 'error') {
|
|
|
|
return [];
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
return data.messages;
|
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
export default function loadMessagesForRoom(args) {
|
2021-09-13 20:41:05 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2019-09-16 20:26:32 +00:00
|
|
|
const data = await load.call(this, args);
|
2021-05-26 17:24:54 +00:00
|
|
|
if (data?.length) {
|
|
|
|
const lastMessage = data[data.length - 1];
|
|
|
|
const lastMessageRecord = await getMessageById(lastMessage._id);
|
|
|
|
if (!lastMessageRecord && data.length === COUNT) {
|
|
|
|
const loadMoreItem = {
|
|
|
|
_id: generateLoadMoreId(lastMessage._id),
|
|
|
|
rid: lastMessage.rid,
|
|
|
|
ts: moment(lastMessage.ts).subtract(1, 'millisecond'),
|
|
|
|
t: MESSAGE_TYPE_LOAD_MORE,
|
|
|
|
msg: lastMessage.msg
|
|
|
|
};
|
|
|
|
data.push(loadMoreItem);
|
|
|
|
}
|
|
|
|
await updateMessages({ rid: args.rid, update: data, loaderItem: args.loaderItem });
|
2020-03-30 20:10:19 +00:00
|
|
|
return resolve(data);
|
2018-06-01 17:56:59 +00:00
|
|
|
} else {
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|