From b75760c43741c332f23553aadbf9656506d61147 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 4 Aug 2021 13:34:35 +0530 Subject: [PATCH 1/8] Add event type string Signed-off-by: RMidhunSuresh --- src/matrix/room/timeline/PowerLevels.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/matrix/room/timeline/PowerLevels.js b/src/matrix/room/timeline/PowerLevels.js index c19f992d..bb723c34 100644 --- a/src/matrix/room/timeline/PowerLevels.js +++ b/src/matrix/room/timeline/PowerLevels.js @@ -14,6 +14,8 @@ See the License for the specific language governing permissions and limitations under the License. */ +export const EVENT_TYPE = "m.room.power_levels"; + export class PowerLevels { constructor({powerLevelEvent, createEvent, ownUserId, membership}) { this._plEvent = powerLevelEvent; From 40128c7ff6436e0106e92b501ae758e9c851db53 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Wed, 4 Aug 2021 13:35:33 +0530 Subject: [PATCH 2/8] Set pl from room response Signed-off-by: RMidhunSuresh --- src/matrix/room/Room.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/matrix/room/Room.js b/src/matrix/room/Room.js index c49d6a4e..7b75d361 100644 --- a/src/matrix/room/Room.js +++ b/src/matrix/room/Room.js @@ -23,6 +23,7 @@ import {WrappedError} from "../error.js" import {Heroes} from "./members/Heroes.js"; import {AttachmentUpload} from "./AttachmentUpload.js"; import {DecryptionSource} from "../e2ee/common.js"; +import {PowerLevels, EVENT_TYPE as POWERLEVELS_EVENT_TYPE } from "./timeline/PowerLevels.js"; const EVENT_ENCRYPTED_TYPE = "m.room.encrypted"; @@ -173,6 +174,7 @@ export class Room extends BaseRoom { if (Array.isArray(roomResponse.timeline?.events)) { removedPendingEvents = await this._sendQueue.removeRemoteEchos(roomResponse.timeline.events, txn, log); } + this._updatePowerLevels(roomResponse); return { summaryChanges, roomEncryption, @@ -262,6 +264,18 @@ export class Room extends BaseRoom { } } + _updatePowerLevels(roomResponse) { + const powerLevelEvent = roomResponse.timeline.events.find(event => event.type === POWERLEVELS_EVENT_TYPE); + if (powerLevelEvent && this._powerLevels) { + const newPowerLevels = new PowerLevels({ + powerLevelEvent, + ownUserId: this._user.id, + membership: this.membership, + }); + this._powerLevels.set(newPowerLevels); + } + } + needsAfterSyncCompleted({shouldFlushKeyShares}) { return shouldFlushKeyShares; } From 4ddc7c06837118f7e8f63bda4e5222e869040f13 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Thu, 5 Aug 2021 12:41:54 +0530 Subject: [PATCH 3/8] Update powerlevels in afterSync Signed-off-by: RMidhunSuresh --- src/matrix/room/Room.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/matrix/room/Room.js b/src/matrix/room/Room.js index 7b75d361..b56e5933 100644 --- a/src/matrix/room/Room.js +++ b/src/matrix/room/Room.js @@ -174,7 +174,7 @@ export class Room extends BaseRoom { if (Array.isArray(roomResponse.timeline?.events)) { removedPendingEvents = await this._sendQueue.removeRemoteEchos(roomResponse.timeline.events, txn, log); } - this._updatePowerLevels(roomResponse); + const powerLevelsEvent = this._getPowerLevelsEvent(roomResponse); return { summaryChanges, roomEncryption, @@ -184,6 +184,7 @@ export class Room extends BaseRoom { removedPendingEvents, memberChanges, heroChanges, + powerLevelsEvent, shouldFlushKeyShares, }; } @@ -196,7 +197,7 @@ export class Room extends BaseRoom { afterSync(changes, log) { const { summaryChanges, newEntries, updatedEntries, newLiveKey, - removedPendingEvents, memberChanges, + removedPendingEvents, memberChanges, powerLevelsEvent, heroChanges, roomEncryption } = changes; log.set("id", this.id); @@ -238,6 +239,9 @@ export class Room extends BaseRoom { emitChange = true; } } + if (powerLevelsEvent) { + this._updatePowerLevels(powerLevelsEvent); + } if (emitChange) { this._emitUpdate(); } @@ -264,9 +268,14 @@ export class Room extends BaseRoom { } } - _updatePowerLevels(roomResponse) { - const powerLevelEvent = roomResponse.timeline.events.find(event => event.type === POWERLEVELS_EVENT_TYPE); - if (powerLevelEvent && this._powerLevels) { + _getPowerLevelsEvent(roomResponse) { + const isPowerlevelEvent = event => event.type === POWERLEVELS_EVENT_TYPE; + const powerLevelEvent = roomResponse.state?.events.find(isPowerlevelEvent) ?? roomResponse.timeline?.events.find(isPowerlevelEvent); + return powerLevelEvent; + } + + _updatePowerLevels(powerLevelEvent) { + if (this._powerLevels) { const newPowerLevels = new PowerLevels({ powerLevelEvent, ownUserId: this._user.id, From 2cae7c41d3a1c32be8110916cb42465402cc968c Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Thu, 5 Aug 2021 16:19:02 +0530 Subject: [PATCH 4/8] Check timeline before state Signed-off-by: RMidhunSuresh --- src/matrix/room/Room.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/matrix/room/Room.js b/src/matrix/room/Room.js index b56e5933..03b2ed4b 100644 --- a/src/matrix/room/Room.js +++ b/src/matrix/room/Room.js @@ -270,7 +270,7 @@ export class Room extends BaseRoom { _getPowerLevelsEvent(roomResponse) { const isPowerlevelEvent = event => event.type === POWERLEVELS_EVENT_TYPE; - const powerLevelEvent = roomResponse.state?.events.find(isPowerlevelEvent) ?? roomResponse.timeline?.events.find(isPowerlevelEvent); + const powerLevelEvent = roomResponse.timeline.events.find(isPowerlevelEvent) ?? roomResponse.state.events.find(isPowerlevelEvent); return powerLevelEvent; } From 187cf5cd2852026ca79d9db471c3d453a44e1dc1 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 6 Aug 2021 17:23:01 +0530 Subject: [PATCH 5/8] Use optional chaining Signed-off-by: RMidhunSuresh --- src/matrix/room/Room.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/matrix/room/Room.js b/src/matrix/room/Room.js index 03b2ed4b..0427799c 100644 --- a/src/matrix/room/Room.js +++ b/src/matrix/room/Room.js @@ -270,7 +270,7 @@ export class Room extends BaseRoom { _getPowerLevelsEvent(roomResponse) { const isPowerlevelEvent = event => event.type === POWERLEVELS_EVENT_TYPE; - const powerLevelEvent = roomResponse.timeline.events.find(isPowerlevelEvent) ?? roomResponse.state.events.find(isPowerlevelEvent); + const powerLevelEvent = roomResponse.timeline?.events.find(isPowerlevelEvent) ?? roomResponse.state?.events.find(isPowerlevelEvent); return powerLevelEvent; } From d85cf982cce263f15404db2a711eefa452b5c068 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 6 Aug 2021 17:34:21 +0530 Subject: [PATCH 6/8] Check if state_key is present Signed-off-by: RMidhunSuresh --- src/matrix/room/Room.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/matrix/room/Room.js b/src/matrix/room/Room.js index 0427799c..44ba536f 100644 --- a/src/matrix/room/Room.js +++ b/src/matrix/room/Room.js @@ -269,7 +269,7 @@ export class Room extends BaseRoom { } _getPowerLevelsEvent(roomResponse) { - const isPowerlevelEvent = event => event.type === POWERLEVELS_EVENT_TYPE; + const isPowerlevelEvent = event => event.state_key !== undefined && event.type === POWERLEVELS_EVENT_TYPE; const powerLevelEvent = roomResponse.timeline?.events.find(isPowerlevelEvent) ?? roomResponse.state?.events.find(isPowerlevelEvent); return powerLevelEvent; } From 1a062f69fd7fc76f66b4f22a6ea0bc34b800f1b7 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 6 Aug 2021 19:02:50 +0530 Subject: [PATCH 7/8] Check state_key is zero-length string Signed-off-by: RMidhunSuresh --- src/matrix/room/Room.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/matrix/room/Room.js b/src/matrix/room/Room.js index 44ba536f..4ba5fefc 100644 --- a/src/matrix/room/Room.js +++ b/src/matrix/room/Room.js @@ -269,7 +269,7 @@ export class Room extends BaseRoom { } _getPowerLevelsEvent(roomResponse) { - const isPowerlevelEvent = event => event.state_key !== undefined && event.type === POWERLEVELS_EVENT_TYPE; + const isPowerlevelEvent = event => event.state_key === "" && event.type === POWERLEVELS_EVENT_TYPE; const powerLevelEvent = roomResponse.timeline?.events.find(isPowerlevelEvent) ?? roomResponse.state?.events.find(isPowerlevelEvent); return powerLevelEvent; } From be57352f71d4b5dec21e7dc89b3f6be04182c5d8 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Fri, 6 Aug 2021 19:49:48 +0530 Subject: [PATCH 8/8] Move PowerLevels.js into /room Signed-off-by: RMidhunSuresh --- src/domain/session/rightpanel/members/comparator.js | 2 +- src/domain/session/room/timeline/ReactionsViewModel.js | 2 +- src/matrix/room/BaseRoom.js | 2 +- src/matrix/room/{timeline => }/PowerLevels.js | 0 src/matrix/room/Room.js | 2 +- 5 files changed, 4 insertions(+), 4 deletions(-) rename src/matrix/room/{timeline => }/PowerLevels.js (100%) diff --git a/src/domain/session/rightpanel/members/comparator.js b/src/domain/session/rightpanel/members/comparator.js index 50ddb9ba..642f8512 100644 --- a/src/domain/session/rightpanel/members/comparator.js +++ b/src/domain/session/rightpanel/members/comparator.js @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {PowerLevels} from "../../../../matrix/room/timeline/PowerLevels.js"; +import {PowerLevels} from "../../../../matrix/room/PowerLevels.js"; export function createMemberComparator(powerLevels) { const collator = new Intl.Collator(); diff --git a/src/domain/session/room/timeline/ReactionsViewModel.js b/src/domain/session/room/timeline/ReactionsViewModel.js index 51226775..051e4de9 100644 --- a/src/domain/session/room/timeline/ReactionsViewModel.js +++ b/src/domain/session/room/timeline/ReactionsViewModel.js @@ -190,7 +190,7 @@ import {HomeServer as MockHomeServer} from "../../../../mocks/HomeServer.js"; import {BaseMessageTile} from "./tiles/BaseMessageTile.js"; import {MappedList} from "../../../../observable/list/MappedList.js"; import {ObservableValue} from "../../../../observable/ObservableValue.js"; -import {PowerLevels} from "../../../../matrix/room/timeline/PowerLevels.js"; +import {PowerLevels} from "../../../../matrix/room/PowerLevels.js"; export function tests() { const fragmentIdComparer = new FragmentIdComparer([]); diff --git a/src/matrix/room/BaseRoom.js b/src/matrix/room/BaseRoom.js index b24ef706..4a8f50b4 100644 --- a/src/matrix/room/BaseRoom.js +++ b/src/matrix/room/BaseRoom.js @@ -28,7 +28,7 @@ import {EventEntry} from "./timeline/entries/EventEntry.js"; import {ObservedEventMap} from "./ObservedEventMap.js"; import {DecryptionSource} from "../e2ee/common.js"; import {ensureLogItem} from "../../logging/utils.js"; -import {PowerLevels} from "./timeline/PowerLevels.js"; +import {PowerLevels} from "./PowerLevels.js"; import {RetainedObservableValue} from "../../observable/ObservableValue.js"; const EVENT_ENCRYPTED_TYPE = "m.room.encrypted"; diff --git a/src/matrix/room/timeline/PowerLevels.js b/src/matrix/room/PowerLevels.js similarity index 100% rename from src/matrix/room/timeline/PowerLevels.js rename to src/matrix/room/PowerLevels.js diff --git a/src/matrix/room/Room.js b/src/matrix/room/Room.js index 4ba5fefc..f5c42097 100644 --- a/src/matrix/room/Room.js +++ b/src/matrix/room/Room.js @@ -23,7 +23,7 @@ import {WrappedError} from "../error.js" import {Heroes} from "./members/Heroes.js"; import {AttachmentUpload} from "./AttachmentUpload.js"; import {DecryptionSource} from "../e2ee/common.js"; -import {PowerLevels, EVENT_TYPE as POWERLEVELS_EVENT_TYPE } from "./timeline/PowerLevels.js"; +import {PowerLevels, EVENT_TYPE as POWERLEVELS_EVENT_TYPE } from "./PowerLevels.js"; const EVENT_ENCRYPTED_TYPE = "m.room.encrypted";