Skip to content

Commit

Permalink
Merge pull request #1754 from aaronraimist/string-pl
Browse files Browse the repository at this point in the history
Do not honor string power levels
  • Loading branch information
jryans authored Jun 30, 2021
2 parents 12deaa8 + 55fb3d4 commit e3a00c2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
28 changes: 28 additions & 0 deletions spec/unit/room-member.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,34 @@ describe("RoomMember", function() {
expect(member.powerLevel).toEqual(0);
expect(emitCount).toEqual(1);
});

it("should not honor string power levels.",
function() {
const event = utils.mkEvent({
type: "m.room.power_levels",
room: roomId,
user: userA,
content: {
users_default: 20,
users: {
"@alice:bar": "5",
},
},
event: true,
});
let emitCount = 0;

member.on("RoomMember.powerLevel", function(emitEvent, emitMember) {
emitCount += 1;
expect(emitMember.userId).toEqual('@alice:bar');
expect(emitMember.powerLevel).toEqual(20);
expect(emitEvent).toEqual(event);
});

member.setPowerLevelEvent(event);
expect(member.powerLevel).toEqual(20);
expect(emitCount).toEqual(1);
});
});

describe("setTypingEvent", function() {
Expand Down
2 changes: 1 addition & 1 deletion src/models/room-member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class RoomMember extends EventEmitter {
const oldPowerLevel = this.powerLevel;
const oldPowerLevelNorm = this.powerLevelNorm;

if (users[this.userId] !== undefined) {
if (users[this.userId] !== undefined && Number.isInteger(users[this.userId])) {
this.powerLevel = users[this.userId];
} else if (evContent.users_default !== undefined) {
this.powerLevel = evContent.users_default;
Expand Down
10 changes: 5 additions & 5 deletions src/models/room-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,26 +670,26 @@ export class RoomState extends EventEmitter {
powerLevels = powerLevelsEvent.getContent();
eventsLevels = powerLevels.events || {};

if (Number.isFinite(powerLevels.state_default)) {
if (Number.isSafeInteger(powerLevels.state_default)) {
stateDefault = powerLevels.state_default;
} else {
stateDefault = 50;
}

const userPowerLevel = powerLevels.users && powerLevels.users[userId];
if (Number.isFinite(userPowerLevel)) {
if (Number.isSafeInteger(userPowerLevel)) {
powerLevel = userPowerLevel;
} else if (Number.isFinite(powerLevels.users_default)) {
} else if (Number.isSafeInteger(powerLevels.users_default)) {
powerLevel = powerLevels.users_default;
}

if (Number.isFinite(powerLevels.events_default)) {
if (Number.isSafeInteger(powerLevels.events_default)) {
eventsDefault = powerLevels.events_default;
}
}

let requiredLevel = state ? stateDefault : eventsDefault;
if (Number.isFinite(eventsLevels[eventType])) {
if (Number.isSafeInteger(eventsLevels[eventType])) {
requiredLevel = eventsLevels[eventType];
}
return powerLevel >= requiredLevel;
Expand Down

0 comments on commit e3a00c2

Please sign in to comment.