2019-04-17 17:01:03 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
|
|
|
import EJSON from 'ejson';
|
|
|
|
|
|
|
|
import buildMessage from './helpers/buildMessage';
|
|
|
|
import database from '../realm';
|
|
|
|
import log from '../../utils/log';
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
async function load({ tmid, offset }) {
|
2019-04-17 17:01:03 +00:00
|
|
|
try {
|
|
|
|
// RC 1.0
|
2019-04-24 18:36:29 +00:00
|
|
|
const result = await this.sdk.get('chat.getThreadMessages', {
|
|
|
|
tmid, count: 50, offset, sort: { ts: -1 }
|
|
|
|
});
|
|
|
|
if (!result || !result.success) {
|
2019-04-17 17:01:03 +00:00
|
|
|
return [];
|
|
|
|
}
|
2019-04-24 18:36:29 +00:00
|
|
|
return result.messages;
|
2019-04-17 17:01:03 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 18:36:29 +00:00
|
|
|
export default function loadThreadMessages({ tmid, offset = 0 }) {
|
2019-04-17 17:01:03 +00:00
|
|
|
return new Promise(async(resolve, reject) => {
|
|
|
|
try {
|
2019-04-24 18:36:29 +00:00
|
|
|
const data = await load.call(this, { tmid, offset });
|
2019-04-17 17:01:03 +00:00
|
|
|
|
|
|
|
if (data && data.length) {
|
|
|
|
InteractionManager.runAfterInteractions(() => {
|
|
|
|
database.write(() => data.forEach((m) => {
|
|
|
|
try {
|
|
|
|
const message = buildMessage(EJSON.fromJSONValue(m));
|
|
|
|
message.rid = tmid;
|
|
|
|
database.create('threadMessages', message, true);
|
|
|
|
} catch (e) {
|
|
|
|
log('loadThreadMessages -> create messages', e);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
return resolve(data);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return resolve([]);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log('loadThreadMessages', e);
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|