2018-04-24 19:34:03 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2019-10-07 20:56:30 +00:00
|
|
|
import updateMessages from './updateMessages';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
async function load({ rid: roomId, latest, t }) {
|
|
|
|
let params = { roomId, count: 50 };
|
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
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
// RC 0.48.0
|
2019-02-07 15:48:10 +00:00
|
|
|
const data = await this.sdk.get(`${ this.roomTypeToApiType(t) }.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-10-07 20:56:30 +00:00
|
|
|
export default function loadMessagesForRoom(args) {
|
2018-05-18 17:55:08 +00:00
|
|
|
return new Promise(async(resolve, reject) => {
|
|
|
|
try {
|
2019-10-07 20:56:30 +00:00
|
|
|
const data = await load.call(this, args);
|
2018-07-18 20:34:59 +00:00
|
|
|
|
2018-06-01 17:56:59 +00:00
|
|
|
if (data && data.length) {
|
2019-10-07 20:56:30 +00:00
|
|
|
InteractionManager.runAfterInteractions(async() => {
|
|
|
|
await updateMessages({ rid: args.rid, update: data });
|
2018-05-18 17:55:08 +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-09-03 19:27:57 +00:00
|
|
|
log(e);
|
2018-05-18 17:55:08 +00:00
|
|
|
reject(e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|