From 45c8e3a793cef9b207bc7e834ad45f2073ae632e Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 8 Feb 2022 14:34:34 +0100 Subject: [PATCH] mark room as DM based on synced state events,rather than just inviteData as that does not work for rooms we create ourselves --- src/matrix/Sync.js | 13 +++++----- src/matrix/room/Invite.js | 8 ++---- src/matrix/room/Room.js | 7 ++---- src/matrix/room/RoomSummary.js | 46 ++++++++++++++++++---------------- 4 files changed, 34 insertions(+), 40 deletions(-) diff --git a/src/matrix/Sync.js b/src/matrix/Sync.js index 5eb50b5c..3574213e 100644 --- a/src/matrix/Sync.js +++ b/src/matrix/Sync.js @@ -249,7 +249,7 @@ export class Sync { if (!isRoomInResponse) { let room = this._session.rooms.get(roomId); if (room) { - roomStates.push(new RoomSyncProcessState(room, false, null, {}, room.membership)); + roomStates.push(new RoomSyncProcessState(room, false, {}, room.membership)); } } } @@ -264,7 +264,7 @@ export class Sync { await rs.room.load(null, prepareTxn, log); } return rs.room.prepareSync( - rs.roomResponse, rs.membership, rs.invite, newKeys, prepareTxn, log) + rs.roomResponse, rs.membership, newKeys, prepareTxn, log) }, log.level.Detail); })); @@ -366,7 +366,7 @@ export class Sync { if (invite) { inviteStates.push(new InviteSyncProcessState(invite, false, null, membership)); } - const roomState = this._createRoomSyncState(roomId, invite, roomResponse, membership, isInitialSync); + const roomState = this._createRoomSyncState(roomId, roomResponse, membership, isInitialSync); if (roomState) { roomStates.push(roomState); } @@ -381,7 +381,7 @@ export class Sync { return {roomStates, archivedRoomStates}; } - _createRoomSyncState(roomId, invite, roomResponse, membership, isInitialSync) { + _createRoomSyncState(roomId, roomResponse, membership, isInitialSync) { let isNewRoom = false; let room = this._session.rooms.get(roomId); // create room only either on new join, @@ -397,7 +397,7 @@ export class Sync { } if (room) { return new RoomSyncProcessState( - room, isNewRoom, invite, roomResponse, membership); + room, isNewRoom, roomResponse, membership); } } @@ -468,10 +468,9 @@ class SessionSyncProcessState { } class RoomSyncProcessState { - constructor(room, isNewRoom, invite, roomResponse, membership) { + constructor(room, isNewRoom, roomResponse, membership) { this.room = room; this.isNewRoom = isNewRoom; - this.invite = invite; this.roomResponse = roomResponse; this.membership = membership; this.preparation = null; diff --git a/src/matrix/room/Invite.js b/src/matrix/room/Invite.js index 18bb7b8d..6c5ef121 100644 --- a/src/matrix/room/Invite.js +++ b/src/matrix/room/Invite.js @@ -188,7 +188,7 @@ export class Invite extends EventEmitter { return { roomId: this.id, isEncrypted: !!summaryData.encryption, - isDirectMessage: this._isDirectMessage(myInvite), + isDirectMessage: summaryData.isDirectMessage, // type: name, avatarUrl, @@ -200,12 +200,8 @@ export class Invite extends EventEmitter { }; } - _isDirectMessage(myInvite) { - return !!(myInvite?.content?.is_direct); - } - _createSummaryData(inviteState) { - return inviteState.reduce(processStateEvent, new SummaryData(null, this.id)); + return inviteState.reduce((data, event) => processStateEvent(data, event, this._user.id), new SummaryData(null, this.id)); } async _createHeroes(inviteState, log) { diff --git a/src/matrix/room/Room.js b/src/matrix/room/Room.js index 46c06af0..65cfe693 100644 --- a/src/matrix/room/Room.js +++ b/src/matrix/room/Room.js @@ -54,15 +54,12 @@ export class Room extends BaseRoom { return false; } - async prepareSync(roomResponse, membership, invite, newKeys, txn, log) { + async prepareSync(roomResponse, membership, newKeys, txn, log) { log.set("id", this.id); if (newKeys) { log.set("newKeys", newKeys.length); } - let summaryChanges = this._summary.data.applySyncResponse(roomResponse, membership); - if (membership === "join" && invite) { - summaryChanges = summaryChanges.applyInvite(invite); - } + let summaryChanges = this._summary.data.applySyncResponse(roomResponse, membership, this._user.id); let roomEncryption = this._roomEncryption; // encryption is enabled in this sync if (!roomEncryption && summaryChanges.encryption) { diff --git a/src/matrix/room/RoomSummary.js b/src/matrix/room/RoomSummary.js index 82087200..410352ca 100644 --- a/src/matrix/room/RoomSummary.js +++ b/src/matrix/room/RoomSummary.js @@ -46,7 +46,7 @@ export function reduceStateEvents(roomResponse, callback, value) { return value; } -function applySyncResponse(data, roomResponse, membership) { +function applySyncResponse(data, roomResponse, membership, ownUserId) { if (roomResponse.summary) { data = updateSummary(data, roomResponse.summary); } @@ -60,7 +60,7 @@ function applySyncResponse(data, roomResponse, membership) { // process state events in state and in timeline. // non-state events are handled by applyTimelineEntries // so decryption is handled properly - data = reduceStateEvents(roomResponse, processStateEvent, data); + data = reduceStateEvents(roomResponse, (data, event) => processStateEvent(data, event, ownUserId), data); const unreadNotifications = roomResponse.unread_notifications; if (unreadNotifications) { data = processNotificationCounts(data, unreadNotifications); @@ -95,7 +95,7 @@ function processRoomAccountData(data, event) { return data; } -export function processStateEvent(data, event) { +export function processStateEvent(data, event, ownUserId) { if (event.type === "m.room.create") { data = data.cloneIfNeeded(); data.lastMessageTimestamp = event.origin_server_ts; @@ -121,6 +121,25 @@ export function processStateEvent(data, event) { const content = event.content; data = data.cloneIfNeeded(); data.canonicalAlias = content.alias; + } else if (event.type === "m.room.member") { + const content = event.content; + if (content.is_direct === true && content.membership === "invite" && !data.isDirectMessage) { + let other; + if (event.sender === ownUserId) { + other = event.state_key; + } else if (event.state_key === ownUserId) { + other = event.sender; + } + if (other) { + data = data.cloneIfNeeded(); + data.isDirectMessage = true; + data.dmUserId = other; + } + } else if (content.membership === "leave" && data.isDirectMessage && data.dmUserId === event.state_key) { + data = data.cloneIfNeeded(); + data.isDirectMessage = false; + data.dmUserId = null; + } } return data; } @@ -161,19 +180,6 @@ function updateSummary(data, summary) { return data; } -function applyInvite(data, invite) { - if (data.isDirectMessage !== invite.isDirectMessage) { - data = data.cloneIfNeeded(); - data.isDirectMessage = invite.isDirectMessage; - if (invite.isDirectMessage) { - data.dmUserId = invite.inviter?.userId; - } else { - data.dmUserId = null; - } - } - return data; -} - export class SummaryData { constructor(copy, roomId) { this.roomId = copy ? copy.roomId : roomId; @@ -230,12 +236,8 @@ export class SummaryData { return applyTimelineEntries(this, timelineEntries, isInitialSync, canMarkUnread, ownUserId); } - applySyncResponse(roomResponse, membership) { - return applySyncResponse(this, roomResponse, membership); - } - - applyInvite(invite) { - return applyInvite(this, invite); + applySyncResponse(roomResponse, membership, ownUserId) { + return applySyncResponse(this, roomResponse, membership, ownUserId); } get needsHeroes() {