2018-04-24 19:34:03 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
|
|
|
|
|
|
|
import buildMessage from './helpers/buildMessage';
|
|
|
|
import database from '../realm';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../../utils/log';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
async function load({ rid: roomId, latest, t }) {
|
2018-12-12 15:15:10 +00:00
|
|
|
if (t === 'l') {
|
|
|
|
try {
|
2018-12-21 10:55:35 +00:00
|
|
|
// RC 0.51.0
|
2019-02-07 15:48:10 +00:00
|
|
|
const data = await this.sdk.methodCall('loadHistory', roomId, null, 50, latest);
|
2018-12-12 15:15:10 +00:00
|
|
|
if (!data || data.status === 'error') {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return data.messages;
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
export default function loadMessagesForRoom(...args) {
|
2018-05-18 17:55:08 +00:00
|
|
|
return new Promise(async(resolve, reject) => {
|
|
|
|
try {
|
2018-12-05 20:52:08 +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) {
|
2018-05-18 17:55:08 +00:00
|
|
|
InteractionManager.runAfterInteractions(() => {
|
2019-02-07 15:48:10 +00:00
|
|
|
database.write(() => data.forEach((message) => {
|
2019-04-17 17:01:03 +00:00
|
|
|
message = buildMessage(message);
|
2019-02-25 16:23:17 +00:00
|
|
|
try {
|
2019-04-17 17:01:03 +00:00
|
|
|
database.create('messages', message, true);
|
|
|
|
// if it's a thread "header"
|
|
|
|
if (message.tlm) {
|
|
|
|
database.create('threads', message, true);
|
|
|
|
}
|
2019-02-25 16:23:17 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('loadMessagesForRoom -> create messages', e);
|
|
|
|
}
|
2018-10-15 20:22:42 +00:00
|
|
|
}));
|
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) {
|
|
|
|
log('loadMessagesForRoom', e);
|
|
|
|
reject(e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|