From 6106c4055780eb1bb983f295076630364ab1cb75 Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Wed, 27 Feb 2019 17:29:37 -0300 Subject: [PATCH] [FIX] Fetch rooms date (#662) --- app/lib/methods/getRooms.js | 13 ++----------- app/lib/realm.js | 8 ++++---- app/sagas/rooms.js | 20 +++++++++++++++++--- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/app/lib/methods/getRooms.js b/app/lib/methods/getRooms.js index 8e4fb7e3..1278b8b1 100644 --- a/app/lib/methods/getRooms.js +++ b/app/lib/methods/getRooms.js @@ -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')]); diff --git a/app/lib/realm.js b/app/lib/realm.js index 3365acce..947f10ba 100644 --- a/app/lib/realm.js +++ b/app/lib/realm.js @@ -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 }); } } diff --git a/app/sagas/rooms.js b/app/sagas/rooms.js index e8d5cb52..8ecc54d2 100644 --- a/app/sagas/rooms.js +++ b/app/sagas/rooms.js @@ -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));