From 78441bf3454ac54bce0952241f39c52c1f6fdb4c Mon Sep 17 00:00:00 2001 From: Djorkaeff Alexandre Date: Mon, 6 Apr 2020 17:23:13 -0300 Subject: [PATCH] [FIX] Loading all updated rooms after app resume (#1998) * [FIX] Loading all updated rooms after app resume * Fix room date on RoomItem Co-authored-by: Diego Mello --- .../methods/helpers/findSubscriptionsRooms.js | 78 +++++++++++++++++++ .../helpers/mergeSubscriptionsRooms.js | 6 +- app/presentation/RoomItem/index.js | 2 +- app/sagas/rooms.js | 2 +- 4 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 app/lib/methods/helpers/findSubscriptionsRooms.js diff --git a/app/lib/methods/helpers/findSubscriptionsRooms.js b/app/lib/methods/helpers/findSubscriptionsRooms.js new file mode 100644 index 000000000..9773c5f7d --- /dev/null +++ b/app/lib/methods/helpers/findSubscriptionsRooms.js @@ -0,0 +1,78 @@ +import { Q } from '@nozbe/watermelondb'; + +import database from '../../database'; + +export default async(subscriptions = [], rooms = []) => { + try { + const db = database.active; + const subCollection = db.collections.get('subscriptions'); + + const roomIds = rooms.filter(r => !subscriptions.find(s => s.rid === r._id)).map(r => r._id); + let existingSubs = await subCollection.query(Q.where('rid', Q.oneOf(roomIds))).fetch(); + existingSubs = existingSubs.map(s => ({ + _id: s._id, + f: s.f, + t: s.t, + ts: s.ts, + ls: s.ls, + name: s.name, + fname: s.fname, + rid: s.rid, + open: s.open, + alert: s.alert, + unread: s.unread, + userMentions: s.userMentions, + roomUpdatedAt: s.roomUpdatedAt, + ro: s.ro, + lastOpen: s.lastOpen, + description: s.description, + announcement: s.announcement, + topic: s.topic, + blocked: s.blocked, + blocker: s.blocker, + reactWhenReadOnly: s.reactWhenReadOnly, + archived: s.archived, + joinCodeRequired: s.joinCodeRequired, + muted: s.muted, + broadcast: s.broadcast, + prid: s.prid, + draftMessage: s.draftMessage, + lastThreadSync: s.lastThreadSync, + jitsiTimeout: s.jitsiTimeout, + autoTranslate: s.autoTranslate, + autoTranslateLanguage: s.autoTranslateLanguage, + lastMessage: s.lastMessage, + usernames: s.usernames, + uids: s.uids + })); + subscriptions = subscriptions.concat(existingSubs); + + const subsIds = subscriptions.filter(s => !rooms.find(r => s.rid === r._id)).map(s => s._id); + let existingRooms = await subCollection.query(Q.where('id', Q.oneOf(subsIds))).fetch(); + existingRooms = existingRooms.map(r => ({ + _updatedAt: r._updatedAt, + lastMessage: r.lastMessage, + description: r.description, + topic: r.topic, + announcement: r.announcement, + reactWhenReadOnly: r.reactWhenReadOnly, + archived: r.archived, + joinCodeRequired: r.joinCodeRequired, + jitsiTimeout: r.jitsiTimeout, + usernames: r.usernames, + uids: r.uids, + ro: r.ro, + broadcast: r.broadcast, + muted: r.muted, + sysMes: r.sysMes + })); + rooms = rooms.concat(existingRooms); + } catch { + // do nothing + } + + return { + subscriptions, + rooms + }; +}; diff --git a/app/lib/methods/helpers/mergeSubscriptionsRooms.js b/app/lib/methods/helpers/mergeSubscriptionsRooms.js index ba26294ce..2ab12f652 100644 --- a/app/lib/methods/helpers/mergeSubscriptionsRooms.js +++ b/app/lib/methods/helpers/mergeSubscriptionsRooms.js @@ -1,6 +1,7 @@ import EJSON from 'ejson'; import normalizeMessage from './normalizeMessage'; +import findSubscriptionsRooms from './findSubscriptionsRooms'; // TODO: delete and update export const merge = (subscription, room) => { @@ -46,11 +47,14 @@ export const merge = (subscription, room) => { return subscription; }; -export default (subscriptions = [], rooms = []) => { +export default async(subscriptions = [], rooms = []) => { if (subscriptions.update) { subscriptions = subscriptions.update; rooms = rooms.update; } + + ({ subscriptions, rooms } = await findSubscriptionsRooms(subscriptions, rooms)); + return { subscriptions: subscriptions.map((s) => { const index = rooms.findIndex(({ _id }) => _id === s.rid); diff --git a/app/presentation/RoomItem/index.js b/app/presentation/RoomItem/index.js index a28bacdcb..dc39fe303 100644 --- a/app/presentation/RoomItem/index.js +++ b/app/presentation/RoomItem/index.js @@ -49,7 +49,7 @@ const RoomItem = React.memo(({ } }, [connected]); - const date = formatDate(_updatedAt); + const date = lastMessage && formatDate(lastMessage.ts); let accessibilityLabel = name; if (unread === 1) { diff --git a/app/sagas/rooms.js b/app/sagas/rooms.js index 02f989d54..1b5940d1a 100644 --- a/app/sagas/rooms.js +++ b/app/sagas/rooms.js @@ -41,7 +41,7 @@ const handleRoomsRequest = function* handleRoomsRequest({ params }) { ({ roomsUpdatedAt } = serverRecord); } const [subscriptionsResult, roomsResult] = yield RocketChat.getRooms(roomsUpdatedAt); - const { subscriptions } = mergeSubscriptionsRooms(subscriptionsResult, roomsResult); + const { subscriptions } = yield mergeSubscriptionsRooms(subscriptionsResult, roomsResult); const db = database.active; const subCollection = db.collections.get('subscriptions');