From 4db5370495a63500c078844039441c978dc281da Mon Sep 17 00:00:00 2001 From: Marco Bartelt Date: Fri, 18 Nov 2022 11:18:40 +0100 Subject: [PATCH 1/5] modifies setPowerLevel() to be able to receive an array of user ids --- src/client.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/client.ts b/src/client.ts index b091a31ec45..2c61e460b51 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3795,9 +3795,9 @@ export class MatrixClient extends TypedEventEmitter { let content = { users: {}, }; - if (event?.getType() === EventType.RoomPowerLevels) { + if (event.getType() === EventType.RoomPowerLevels) { // take a copy of the content to ensure we don't corrupt // existing client state with a failed power level change content = utils.deepCopy(event.getContent()); } - content.users[userId] = powerLevel; + if (Array.isArray(userId)) { + for (const user of userId) { + content.users[user] = powerLevel; + } + } else { + content.users[userId] = powerLevel; + } const path = utils.encodeUri("/rooms/$roomId/state/m.room.power_levels", { $roomId: roomId, }); From da0f4beea774d46403cf50ab10f48188a12de1cf Mon Sep 17 00:00:00 2001 From: Marco Bartelt Date: Tue, 29 Nov 2022 08:23:51 +0100 Subject: [PATCH 2/5] adds unit tests that checks against event content modification inside setPowerLevel() --- spec/integ/matrix-client-methods.spec.ts | 47 +++++++++++++++--------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/spec/integ/matrix-client-methods.spec.ts b/spec/integ/matrix-client-methods.spec.ts index 5508ceaf031..f2ff9e6df77 100644 --- a/spec/integ/matrix-client-methods.spec.ts +++ b/spec/integ/matrix-client-methods.spec.ts @@ -1337,27 +1337,38 @@ describe("MatrixClient", function() { }); }); - describe("registerWithIdentityServer", () => { - it("should pass data to POST request", async () => { - const token = { - access_token: "access_token", - token_type: "Bearer", - matrix_server_name: "server_name", - expires_in: 12345, - }; + describe("setPowerLevel", () => { + it.each([ + { + userId: "alice@localhost", + expectation: { + "alice@localhost": 100, + }, + }, + { + userId: ["alice@localhost", "bob@localhost"], + expectation: { + "alice@localhost": 100, + "bob@localhost": 100, + }, + }, + ])("should modify power levels of $userId correctly", async ({ userId, expectation }) => { + const event = { + getType: () => "m.room.power_levels", + getContent: () => ({ + users: { + "alice@localhost": 50, + }, + }), + } as MatrixEvent; - httpBackend!.when("POST", "/account/register").check(req => { - expect(req.data).toStrictEqual(token); - }).respond(200, { - access_token: "at", - token: "tt", - }); + httpBackend!.when("PUT", "/state/m.room.power_levels").check(req => { + expect(req.data.users).toStrictEqual(expectation); + }).respond(200, {}); - const prom = client!.registerWithIdentityServer(token); + const prom = client!.setPowerLevel("!room_id:server", userId, 100, event); await httpBackend!.flushAllExpected(); - const resp = await prom; - expect(resp.access_token).toBe("at"); - expect(resp.token).toBe("tt"); + await prom; }); }); }); From 2a82d324cf5e610b3e070523d15f7b26fee09d86 Mon Sep 17 00:00:00 2001 From: Marco Bartelt Date: Tue, 29 Nov 2022 09:13:33 +0100 Subject: [PATCH 3/5] tries to satisfy strict error checking by typing the event content more explicitly --- src/client.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index 2c61e460b51..3accfca7e90 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3809,7 +3809,10 @@ export class MatrixClient extends TypedEventEmitter { - let content = { + type EventContent = { + users: Record; + }; + let content: EventContent = { users: {}, }; if (event.getType() === EventType.RoomPowerLevels) { From 763532f84a5774b90ce62208e8908545ec16ed7c Mon Sep 17 00:00:00 2001 From: Marco Bartelt Date: Tue, 29 Nov 2022 10:14:29 +0100 Subject: [PATCH 4/5] only type the users property stronger to bypass conflicts with deepCopy typing --- src/client.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/client.ts b/src/client.ts index 3accfca7e90..d18a69f984c 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3809,10 +3809,8 @@ export class MatrixClient extends TypedEventEmitter { - type EventContent = { - users: Record; - }; - let content: EventContent = { + type UsersWithPowerLevel = Record; + let content = { users: {}, }; if (event.getType() === EventType.RoomPowerLevels) { @@ -3822,10 +3820,10 @@ export class MatrixClient extends TypedEventEmitter Date: Tue, 29 Nov 2022 12:36:08 +0100 Subject: [PATCH 5/5] makes typing less verbose as suggested --- src/client.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/client.ts b/src/client.ts index d18a69f984c..d8bb110630f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3809,9 +3809,8 @@ export class MatrixClient extends TypedEventEmitter { - type UsersWithPowerLevel = Record; let content = { - users: {}, + users: {} as Record, }; if (event.getType() === EventType.RoomPowerLevels) { // take a copy of the content to ensure we don't corrupt @@ -3820,10 +3819,10 @@ export class MatrixClient extends TypedEventEmitter