Skip to content

Commit 6611cfa

Browse files
authored
add-privileged-users-in-room (#2892)
1 parent 25296bb commit 6611cfa

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

spec/integ/matrix-client-methods.spec.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,27 +1337,38 @@ describe("MatrixClient", function() {
13371337
});
13381338
});
13391339

1340-
describe("registerWithIdentityServer", () => {
1341-
it("should pass data to POST request", async () => {
1342-
const token = {
1343-
access_token: "access_token",
1344-
token_type: "Bearer",
1345-
matrix_server_name: "server_name",
1346-
expires_in: 12345,
1347-
};
1340+
describe("setPowerLevel", () => {
1341+
it.each([
1342+
{
1343+
userId: "alice@localhost",
1344+
expectation: {
1345+
"alice@localhost": 100,
1346+
},
1347+
},
1348+
{
1349+
userId: ["alice@localhost", "bob@localhost"],
1350+
expectation: {
1351+
"alice@localhost": 100,
1352+
"bob@localhost": 100,
1353+
},
1354+
},
1355+
])("should modify power levels of $userId correctly", async ({ userId, expectation }) => {
1356+
const event = {
1357+
getType: () => "m.room.power_levels",
1358+
getContent: () => ({
1359+
users: {
1360+
"alice@localhost": 50,
1361+
},
1362+
}),
1363+
} as MatrixEvent;
13481364

1349-
httpBackend!.when("POST", "/account/register").check(req => {
1350-
expect(req.data).toStrictEqual(token);
1351-
}).respond(200, {
1352-
access_token: "at",
1353-
token: "tt",
1354-
});
1365+
httpBackend!.when("PUT", "/state/m.room.power_levels").check(req => {
1366+
expect(req.data.users).toStrictEqual(expectation);
1367+
}).respond(200, {});
13551368

1356-
const prom = client!.registerWithIdentityServer(token);
1369+
const prom = client!.setPowerLevel("!room_id:server", userId, 100, event);
13571370
await httpBackend!.flushAllExpected();
1358-
const resp = await prom;
1359-
expect(resp.access_token).toBe("at");
1360-
expect(resp.token).toBe("tt");
1371+
await prom;
13611372
});
13621373
});
13631374
});

src/client.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3795,29 +3795,35 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
37953795
}
37963796

37973797
/**
3798-
* Set a user's power level.
3798+
* Set a power level to one or multiple users.
37993799
* @param {string} roomId
3800-
* @param {string} userId
3800+
* @param {string|string[]} userId
38013801
* @param {Number} powerLevel
38023802
* @param {MatrixEvent} event
38033803
* @return {Promise} Resolves: to an ISendEventResponse object
38043804
* @return {module:http-api.MatrixError} Rejects: with an error response.
38053805
*/
38063806
public setPowerLevel(
38073807
roomId: string,
3808-
userId: string,
3808+
userId: string | string[],
38093809
powerLevel: number,
38103810
event: MatrixEvent,
38113811
): Promise<ISendEventResponse> {
38123812
let content = {
3813-
users: {},
3813+
users: {} as Record<string, number>,
38143814
};
3815-
if (event?.getType() === EventType.RoomPowerLevels) {
3815+
if (event.getType() === EventType.RoomPowerLevels) {
38163816
// take a copy of the content to ensure we don't corrupt
38173817
// existing client state with a failed power level change
38183818
content = utils.deepCopy(event.getContent());
38193819
}
3820-
content.users[userId] = powerLevel;
3820+
if (Array.isArray(userId)) {
3821+
for (const user of userId) {
3822+
content.users[user] = powerLevel;
3823+
}
3824+
} else {
3825+
content.users[userId] = powerLevel;
3826+
}
38213827
const path = utils.encodeUri("/rooms/$roomId/state/m.room.power_levels", {
38223828
$roomId: roomId,
38233829
});

0 commit comments

Comments
 (0)