2018-04-24 19:34:03 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
2018-10-15 20:22:42 +00:00
|
|
|
import * as SDK from '@rocket.chat/sdk';
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
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
|
2018-12-12 15:15:10 +00:00
|
|
|
const data = await SDK.driver.asyncCall('loadHistory', roomId, null, 50, latest);
|
|
|
|
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
|
2018-12-05 20:52:08 +00:00
|
|
|
const data = await SDK.api.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-04-24 19:34:03 +00:00
|
|
|
const { database: db } = database;
|
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(() => {
|
2018-10-15 20:22:42 +00:00
|
|
|
db.write(() => data.forEach((message) => {
|
2018-12-05 20:52:08 +00:00
|
|
|
db.create('messages', buildMessage(message), true);
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|