[FIX] Fetch rooms date (#662)

This commit is contained in:
Diego Mello 2019-02-27 17:29:37 -03:00 committed by GitHub
parent 8c1b57eb26
commit 6106c40557
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 18 deletions

View File

@ -1,17 +1,8 @@
import database from '../realm';
const lastMessage = () => {
const message = database
.objects('subscriptions')
.sorted('roomUpdatedAt', true)[0];
return message && new Date(message.roomUpdatedAt).toISOString();
};
export default function() {
const updatedSince = lastMessage();
export default function(updatedSince) {
// subscriptions.get: Since RC 0.60.0
// rooms.get: Since RC 0.62.0
if (updatedSince) {
updatedSince = updatedSince.toISOString();
return Promise.all([this.sdk.get('subscriptions.get', { updatedSince }), this.sdk.get('rooms.get', { updatedSince })]);
}
return Promise.all([this.sdk.get('subscriptions.get'), this.sdk.get('rooms.get')]);

View File

@ -10,7 +10,8 @@ const serversSchema = {
properties: {
id: 'string',
name: { type: 'string', optional: true },
iconURL: { type: 'string', optional: true }
iconURL: { type: 'string', optional: true },
roomsUpdatedAt: { type: 'date', optional: true }
}
};
@ -22,7 +23,6 @@ const settingsSchema = {
valueAsString: { type: 'string', optional: true },
valueAsBoolean: { type: 'bool', optional: true },
valueAsNumber: { type: 'int', optional: true },
_updatedAt: { type: 'date', optional: true }
}
};
@ -308,7 +308,7 @@ class DB {
schema: [
serversSchema
],
deleteRealmIfMigrationNeeded: true
schemaVersion: 1
})
}
@ -341,7 +341,7 @@ class DB {
return this.databases.activeDB = new Realm({
path: `${ path }.realm`,
schema,
deleteRealmIfMigrationNeeded: true
schemaVersion: 1
});
}
}

View File

@ -1,20 +1,34 @@
import { put, takeLatest } from 'redux-saga/effects';
import {
put, takeLatest, select
} from 'redux-saga/effects';
import * as types from '../actions/actionsTypes';
import { roomsSuccess, roomsFailure } from '../actions/rooms';
import RocketChat from '../lib/rocketchat';
import database from '../lib/realm';
import log from '../utils/log';
import mergeSubscriptionsRooms from '../lib/methods/helpers/mergeSubscriptionsRooms';
import RocketChat from '../lib/rocketchat';
const handleRoomsRequest = function* handleRoomsRequest() {
try {
const [subscriptionsResult, roomsResult] = yield RocketChat.getRooms();
const newRoomsUpdatedAt = new Date();
const server = yield select(state => state.server.server);
const [serverRecord] = database.databases.serversDB.objects('servers').filtered('id = $0', server);
const { roomsUpdatedAt } = serverRecord;
const [subscriptionsResult, roomsResult] = yield RocketChat.getRooms(roomsUpdatedAt);
const { subscriptions } = mergeSubscriptionsRooms(subscriptionsResult, roomsResult);
database.write(() => {
subscriptions.forEach(subscription => database.create('subscriptions', subscription, true));
});
database.databases.serversDB.write(() => {
try {
database.databases.serversDB.create('servers', { id: server, roomsUpdatedAt: newRoomsUpdatedAt }, true);
} catch (e) {
log('handleRoomsRequest update roomsUpdatedAt', e);
}
});
yield put(roomsSuccess());
} catch (e) {
yield put(roomsFailure(e));