1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-07-31 15:24:23 +03:00

Simplify MatrixClient::setPowerLevel API (#3570)

* Simplify `MatrixClient::setPowerLevel` API

While making it more resilient to causing issues like nuking room state

* Handle edge case

* Fix tests

* Add test coverage
This commit is contained in:
Michael Telatynski
2023-07-11 08:26:30 +01:00
committed by GitHub
parent 5df4ebaada
commit d2b782a2f5
2 changed files with 110 additions and 23 deletions

View File

@ -1386,14 +1386,11 @@ describe("MatrixClient", function () {
expectation: {},
},
])("should modify power levels of $userId correctly", async ({ userId, powerLevel, expectation }) => {
const event = {
getType: () => "m.room.power_levels",
getContent: () => ({
users: {
"alice@localhost": 50,
},
}),
} as MatrixEvent;
httpBackend!.when("GET", "/state/m.room.power_levels/").respond(200, {
users: {
"alice@localhost": 50,
},
});
httpBackend!
.when("PUT", "/state/m.room.power_levels")
@ -1402,7 +1399,76 @@ describe("MatrixClient", function () {
})
.respond(200, {});
const prom = client!.setPowerLevel("!room_id:server", userId, powerLevel, event);
const prom = client!.setPowerLevel("!room_id:server", userId, powerLevel);
await httpBackend!.flushAllExpected();
await prom;
});
it("should use power level from room state if available", async () => {
client!.clientRunning = true;
client!.isInitialSyncComplete = () => true;
const room = new Room("!room_id:server", client!, client!.getUserId()!);
room.currentState.events.set("m.room.power_levels", new Map());
room.currentState.events.get("m.room.power_levels")!.set(
"",
new MatrixEvent({
type: "m.room.power_levels",
state_key: "",
content: {
users: {
"@bob:localhost": 50,
},
},
}),
);
client!.getRoom = () => room;
httpBackend!
.when("PUT", "/state/m.room.power_levels")
.check((req) => {
expect(req.data).toStrictEqual({
users: {
"@bob:localhost": 50,
[userId]: 42,
},
});
})
.respond(200, {});
const prom = client!.setPowerLevel("!room_id:server", userId, 42);
await httpBackend!.flushAllExpected();
await prom;
});
it("should throw error if state API errors", async () => {
httpBackend!.when("GET", "/state/m.room.power_levels/").respond(500, {
errcode: "ERR_DERP",
});
const prom = client!.setPowerLevel("!room_id:server", userId, 42);
await Promise.all([
expect(prom).rejects.toMatchInlineSnapshot(`[ERR_DERP: MatrixError: [500] Unknown message]`),
httpBackend!.flushAllExpected(),
]);
});
it("should not throw error if /state/ API returns M_NOT_FOUND", async () => {
httpBackend!.when("GET", "/state/m.room.power_levels/").respond(404, {
errcode: "M_NOT_FOUND",
});
httpBackend!
.when("PUT", "/state/m.room.power_levels")
.check((req) => {
expect(req.data).toStrictEqual({
users: {
[userId]: 42,
},
});
})
.respond(200, {});
const prom = client!.setPowerLevel("!room_id:server", userId, 42);
await httpBackend!.flushAllExpected();
await prom;
});