From 862ffa13c64a998cd488ef3623b35887c06f4553 Mon Sep 17 00:00:00 2001 From: Djorkaeff Alexandre Date: Tue, 28 Jan 2020 10:19:17 -0300 Subject: [PATCH] [FIX] Channels doesn't load (#1586) * [FIX] Channels doesn't load * [FIX] Update roomsUpdatedAt when subscriptions.length is 0 * [FIX] Remove unnecessary changes * [FIX] Improve the code Co-authored-by: Diego Mello --- app/sagas/rooms.js | 63 ++++++++++++++-------------------------------- 1 file changed, 19 insertions(+), 44 deletions(-) diff --git a/app/sagas/rooms.js b/app/sagas/rooms.js index 17da0b07..7476724b 100644 --- a/app/sagas/rooms.js +++ b/app/sagas/rooms.js @@ -11,7 +11,18 @@ import database from '../lib/database'; import log from '../utils/log'; import mergeSubscriptionsRooms from '../lib/methods/helpers/mergeSubscriptionsRooms'; import RocketChat from '../lib/rocketchat'; -// import buildMessage from '../lib/methods/helpers/buildMessage'; + +const updateRooms = function* updateRooms({ server, newRoomsUpdatedAt }) { + const serversDB = database.servers; + const serversCollection = serversDB.collections.get('servers'); + const serverRecord = yield serversCollection.find(server); + + return serversDB.action(async() => { + await serverRecord.update((record) => { + record.roomsUpdatedAt = newRoomsUpdatedAt; + }); + }); +}; const handleRoomsRequest = function* handleRoomsRequest() { try { @@ -26,28 +37,15 @@ const handleRoomsRequest = function* handleRoomsRequest() { const { subscriptions } = mergeSubscriptionsRooms(subscriptionsResult, roomsResult); const db = database.active; - yield db.action(async() => { - if (!subscriptions.length) { - return; - } - - const subCollection = db.collections.get('subscriptions'); - // const messagesCollection = db.collections.get('messages'); + const subCollection = db.collections.get('subscriptions'); + if (subscriptions.length) { const subsIds = subscriptions.map(sub => sub.rid); - const existingSubs = await subCollection.query(Q.where('id', Q.oneOf(subsIds))).fetch(); + const existingSubs = yield subCollection.query(Q.where('id', Q.oneOf(subsIds))).fetch(); const subsToUpdate = existingSubs.filter(i1 => subscriptions.find(i2 => i1._id === i2._id)); const subsToCreate = subscriptions.filter(i1 => !existingSubs.find(i2 => i1._id === i2._id)); // TODO: subsToDelete? - // const lastMessages = subscriptions - // .map(sub => sub.lastMessage && buildMessage(sub.lastMessage)) - // .filter(lm => lm); - // const lastMessagesIds = lastMessages.map(lm => lm._id); - // const existingMessages = await messagesCollection.query(Q.where('id', Q.oneOf(lastMessagesIds))).fetch(); - // const messagesToUpdate = existingMessages.filter(i1 => lastMessages.find(i2 => i1.id === i2._id)); - // const messagesToCreate = lastMessages.filter(i1 => !existingMessages.find(i2 => i1._id === i2.id)); - const allRecords = [ ...subsToCreate.map(subscription => subCollection.prepareCreate((s) => { s._raw = sanitizedRaw({ id: subscription.rid }, subCollection.schema); @@ -59,37 +57,14 @@ const handleRoomsRequest = function* handleRoomsRequest() { Object.assign(subscription, newSub); }); }) - // ...messagesToCreate.map(message => messagesCollection.prepareCreate((m) => { - // m._raw = sanitizedRaw({ id: message._id }, messagesCollection.schema); - // m.subscription.id = message.rid; - // return Object.assign(m, message); - // })), - // ...messagesToUpdate.map((message) => { - // const newMessage = lastMessages.find(m => m._id === message.id); - // return message.prepareUpdate(() => { - // Object.assign(message, newMessage); - // }); - // }) ]; - try { + yield db.action(async() => { await db.batch(...allRecords); - } catch (e) { - log(e); - } - return allRecords.length; - }); - - yield serversDB.action(async() => { - try { - await serverRecord.update((record) => { - record.roomsUpdatedAt = newRoomsUpdatedAt; - }); - } catch (e) { - log(e); - } - }); + }); + } + yield updateRooms({ server, newRoomsUpdatedAt }); yield put(roomsSuccess()); } catch (e) { yield put(roomsFailure(e));