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
|
|
|
|
2019-02-07 15:48:10 +00:00
|
|
|
const getLastUpdate = (rid) => {
|
|
|
|
const sub = database
|
|
|
|
.objects('subscriptions')
|
|
|
|
.filtered('rid == $0', rid)[0];
|
|
|
|
return sub && new Date(sub.lastOpen).toISOString();
|
|
|
|
};
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
async function load({ rid: roomId, lastOpen }) {
|
2018-10-15 20:22:42 +00:00
|
|
|
let lastUpdate;
|
|
|
|
if (lastOpen) {
|
|
|
|
lastUpdate = new Date(lastOpen).toISOString();
|
2018-11-16 11:06:29 +00:00
|
|
|
} else {
|
2019-02-07 15:48:10 +00:00
|
|
|
lastUpdate = getLastUpdate(roomId);
|
2018-10-15 20:22:42 +00:00
|
|
|
}
|
2018-12-21 10:55:35 +00:00
|
|
|
// RC 0.60.0
|
2019-04-08 12:35:28 +00:00
|
|
|
const { result } = await this.sdk.get('chat.syncMessages', { roomId, lastUpdate });
|
2018-09-28 18:57:29 +00:00
|
|
|
return result;
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
export default function loadMissedMessages(...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-05-18 17:55:08 +00:00
|
|
|
|
|
|
|
if (data) {
|
2018-09-28 18:57:29 +00:00
|
|
|
if (data.updated && data.updated.length) {
|
|
|
|
const { updated } = data;
|
|
|
|
InteractionManager.runAfterInteractions(() => {
|
2019-02-25 16:23:17 +00:00
|
|
|
database.write(() => updated.forEach((message) => {
|
|
|
|
try {
|
2019-04-17 17:01:03 +00:00
|
|
|
message = buildMessage(message);
|
2019-02-25 16:23:17 +00:00
|
|
|
database.create('messages', message, true);
|
2019-04-17 17:01:03 +00:00
|
|
|
// 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('loadMissedMessages -> create messages', e);
|
|
|
|
}
|
|
|
|
}));
|
2018-09-28 18:57:29 +00:00
|
|
|
resolve(updated);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (data.deleted && data.deleted.length) {
|
|
|
|
const { deleted } = data;
|
|
|
|
InteractionManager.runAfterInteractions(() => {
|
2019-02-25 16:23:17 +00:00
|
|
|
try {
|
|
|
|
database.write(() => {
|
|
|
|
deleted.forEach((m) => {
|
|
|
|
const message = database.objects('messages').filtered('_id = $0', m._id);
|
|
|
|
database.delete(message);
|
|
|
|
});
|
2018-09-28 18:57:29 +00:00
|
|
|
});
|
2019-02-25 16:23:17 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('loadMissedMessages -> delete message', e);
|
|
|
|
}
|
2018-09-28 18:57:29 +00:00
|
|
|
});
|
|
|
|
}
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
|
|
|
resolve([]);
|
|
|
|
} catch (e) {
|
|
|
|
log('loadMissedMessages', e);
|
|
|
|
reject(e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|