From 4fb3010676ae639dd6fc132bea0df5116dc5447b Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Fri, 21 Aug 2020 13:45:38 +0200 Subject: [PATCH] only set unread for incremental syncs --- src/matrix/Sync.js | 2 +- src/matrix/room/Room.js | 4 ++-- src/matrix/room/RoomSummary.js | 20 +++++++++++++------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/matrix/Sync.js b/src/matrix/Sync.js index 4b4acd9d..e6de7146 100644 --- a/src/matrix/Sync.js +++ b/src/matrix/Sync.js @@ -142,7 +142,7 @@ export class Sync { room = this._session.createRoom(roomId); } console.log(` * applying sync response to room ${roomId} ...`); - const changes = await room.writeSync(roomResponse, membership, syncTxn); + const changes = await room.writeSync(roomResponse, membership, isInitialSync, syncTxn); roomChanges.push({room, changes}); }); await Promise.all(promises); diff --git a/src/matrix/room/Room.js b/src/matrix/room/Room.js index 9d543e7d..d1da35e4 100644 --- a/src/matrix/room/Room.js +++ b/src/matrix/room/Room.js @@ -42,8 +42,8 @@ export class Room extends EventEmitter { } /** @package */ - async writeSync(roomResponse, membership, txn) { - const summaryChanges = this._summary.writeSync(roomResponse, membership, txn); + async writeSync(roomResponse, membership, isInitialSync, txn) { + const summaryChanges = this._summary.writeSync(roomResponse, membership, isInitialSync, txn); const {entries, newLiveKey, changedMembers} = await this._syncWriter.writeSync(roomResponse, txn); let removedPendingEvents; if (roomResponse.timeline && roomResponse.timeline.events) { diff --git a/src/matrix/room/RoomSummary.js b/src/matrix/room/RoomSummary.js index 399d869b..d149e1cc 100644 --- a/src/matrix/room/RoomSummary.js +++ b/src/matrix/room/RoomSummary.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -function applySyncResponse(data, roomResponse, membership) { +function applySyncResponse(data, roomResponse, membership, isInitialSync) { if (roomResponse.summary) { data = updateSummary(data, roomResponse.summary); } @@ -24,7 +24,9 @@ function applySyncResponse(data, roomResponse, membership) { } // state comes before timeline if (roomResponse.state) { - data = roomResponse.state.events.reduce(processEvent, data); + data = roomResponse.state.events.reduce((data, event) => { + return processEvent(data, event, isInitialSync); + }, data); } if (roomResponse.timeline) { const {timeline} = roomResponse; @@ -32,7 +34,9 @@ function applySyncResponse(data, roomResponse, membership) { data = data.cloneIfNeeded(); data.lastPaginationToken = timeline.prev_batch; } - data = timeline.events.reduce(processEvent, data); + data = timeline.events.reduce((data, event) => { + return processEvent(data, event, isInitialSync); + }, data); } const unreadNotifications = roomResponse.unread_notifications; if (unreadNotifications) { @@ -44,7 +48,7 @@ function applySyncResponse(data, roomResponse, membership) { return data; } -function processEvent(data, event) { +function processEvent(data, event, isInitialSync) { if (event.type === "m.room.encryption") { if (!data.isEncrypted) { data = data.cloneIfNeeded(); @@ -65,7 +69,9 @@ function processEvent(data, event) { } else if (event.type === "m.room.message") { data = data.cloneIfNeeded(); data.lastMessageTimestamp = event.origin_server_ts; - data.isUnread = true; + if (!isInitialSync) { + data.isUnread = true; + } const {content} = event; const body = content?.body; const msgtype = content?.msgtype; @@ -207,11 +213,11 @@ export class RoomSummary { return data; } - writeSync(roomResponse, membership, txn) { + writeSync(roomResponse, membership, isInitialSync, txn) { // clear cloned flag, so cloneIfNeeded makes a copy and // this._data is not modified if any field is changed. this._data.cloned = false; - const data = applySyncResponse(this._data, roomResponse, membership); + const data = applySyncResponse(this._data, roomResponse, membership, isInitialSync); if (data !== this._data) { // need to think here how we want to persist // things like unread status (as read marker, or unread count)?