2018-04-24 19:34:03 +00:00
|
|
|
import { InteractionManager } from 'react-native';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2019-02-07 15:48:10 +00:00
|
|
|
import mergeSubscriptionsRooms from './helpers/mergeSubscriptionsRooms';
|
2018-04-24 19:34:03 +00:00
|
|
|
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
|
|
|
|
|
|
|
const lastMessage = () => {
|
|
|
|
const message = database
|
|
|
|
.objects('subscriptions')
|
|
|
|
.sorted('roomUpdatedAt', true)[0];
|
2018-10-15 20:22:42 +00:00
|
|
|
return message && new Date(message.roomUpdatedAt).toISOString();
|
2018-04-24 19:34:03 +00:00
|
|
|
};
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
export default function() {
|
2018-05-18 17:55:08 +00:00
|
|
|
return new Promise(async(resolve, reject) => {
|
|
|
|
try {
|
2018-12-05 20:52:08 +00:00
|
|
|
const updatedSince = lastMessage();
|
2018-12-21 10:55:35 +00:00
|
|
|
// subscriptions.get: Since RC 0.60.0
|
|
|
|
// rooms.get: Since RC 0.62.0
|
2018-12-05 20:52:08 +00:00
|
|
|
const [subscriptionsResult, roomsResult] = await (updatedSince
|
2019-02-07 15:48:10 +00:00
|
|
|
? Promise.all([this.sdk.get('subscriptions.get', { updatedSince }), this.sdk.get('rooms.get', { updatedSince })])
|
|
|
|
: Promise.all([this.sdk.get('subscriptions.get'), this.sdk.get('rooms.get')])
|
2018-12-05 20:52:08 +00:00
|
|
|
);
|
2019-02-07 15:48:10 +00:00
|
|
|
const { subscriptions } = mergeSubscriptionsRooms(subscriptionsResult, roomsResult);
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
InteractionManager.runAfterInteractions(() => {
|
2019-02-07 15:48:10 +00:00
|
|
|
database.write(() => {
|
|
|
|
subscriptions.forEach(subscription => database.create('subscriptions', subscription, true));
|
2018-05-18 17:55:08 +00:00
|
|
|
});
|
2019-02-07 15:48:10 +00:00
|
|
|
resolve(subscriptions);
|
2018-04-24 19:34:03 +00:00
|
|
|
});
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
log('getRooms', e);
|
|
|
|
reject(e);
|
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
});
|
|
|
|
}
|