Skip to content

Commit

Permalink
add-privileged-users-in-room (#2892)
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodGuyMarco authored Nov 29, 2022
1 parent 25296bb commit 6611cfa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
47 changes: 29 additions & 18 deletions spec/integ/matrix-client-methods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});
});
Expand Down
18 changes: 12 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3795,29 +3795,35 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
}

/**
* Set a user's power level.
* Set a power level to one or multiple users.
* @param {string} roomId
* @param {string} userId
* @param {string|string[]} userId
* @param {Number} powerLevel
* @param {MatrixEvent} event
* @return {Promise} Resolves: to an ISendEventResponse object
* @return {module:http-api.MatrixError} Rejects: with an error response.
*/
public setPowerLevel(
roomId: string,
userId: string,
userId: string | string[],
powerLevel: number,
event: MatrixEvent,
): Promise<ISendEventResponse> {
let content = {
users: {},
users: {} as Record<string, number>,
};
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,
});
Expand Down

0 comments on commit 6611cfa

Please sign in to comment.