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', {
|
2019-05-03 13:33:38 +00:00
|
|
|
tmid, count: 50, offset, sort: { ts: -1 }, query: { _hidden: { $ne: true } }
|
2019-04-24 18:36:29 +00:00
|
|
|
});
|
|
|
|
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) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-04-17 17:01:03 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
return resolve(data);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return resolve([]);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-04-17 17:01:03 +00:00
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|