Skip to content

Commit

Permalink
check power levels without relying on membership
Browse files Browse the repository at this point in the history
as this might not be known for the syncing user.
instead, add a method to room which always knows the syncing user's membership
  • Loading branch information
bwindels committed Sep 5, 2018
1 parent 3d98e32 commit 8b00083
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 38 deletions.
27 changes: 14 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
Changes in [0.11.0](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v0.11.0) (TDB)
==================================================================================================
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v0.10.9...v0.11.0)

* Support for lazy loading members. This should improve performance for
users who joined big rooms a lot. Pass to `lazyLoadMembers = true` option when calling `startClient`.

BREAKING CHANGES
----------------

* `MatrixClient::startClient` now returns a Promise. No method should be called on the client before that promise resolves. Before this method didn't return anything.
* A new `CATCHUP` sync state, emitted by `MatrixClient#"sync"` and returned by `MatrixClient::getSyncState()`, when doing initial sync after the `ERROR` state. See `MatrixClient` documentation for details.
* `RoomState::maySendEvent('m.room.message', userId)` & `RoomState::maySendMessage(userId)` do not check the membership of the user anymore, only the power level. To check if the syncing user is allowed to write in a room, use `Room::maySendMessage()` as `RoomState` is not always aware of the syncing user's membership anymore, in case lazy loading of members is enabled.

Changes in [0.10.9](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v0.10.9) (2018-09-03)
==================================================================================================
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v0.10.9-rc.2...v0.10.9)
Expand Down Expand Up @@ -36,19 +50,6 @@ Changes in [0.10.9-rc.1](https://github.com/matrix-org/matrix-js-sdk/releases/ta
* Lazy loading of room members
[\#691](https://github.com/matrix-org/matrix-js-sdk/pull/691)

BREAKING CHANGE
---------------

* `MatrixClient::startClient` now returns a Promise. No method should be called on the client before that promise resolves. Before this method didn't return anything.
* A new `CATCHUP` sync state, emitted by `MatrixClient#"sync"` and returned by `MatrixClient::getSyncState()`, when doing initial sync after the `ERROR` state. See `MatrixClient` documentation for details.

Changes in [0.11.0](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v0.11.0) (TDB)
==================================================================================================
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v0.10.6...v0.11.0)

* Support for lazy loading members. This should improve performance for
users who joined big rooms a lot. Pass to `lazyLoadMembers = true` option when calling `startClient`.

Changes in [0.10.8](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v0.10.8) (2018-08-20)
==================================================================================================
[Full Changelog](https://github.com/matrix-org/matrix-js-sdk/compare/v0.10.8-rc.1...v0.10.8)
Expand Down
15 changes: 0 additions & 15 deletions spec/unit/room-state.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,13 +447,6 @@ describe("RoomState", function() {
});

describe("maySendStateEvent", function() {
it("should say non-joined members may not send state",
function() {
expect(state.maySendStateEvent(
'm.room.name', "@nobody:nowhere",
)).toEqual(false);
});

it("should say any member may send state with no power level event",
function() {
expect(state.maySendStateEvent('m.room.name', userA)).toEqual(true);
Expand Down Expand Up @@ -640,14 +633,6 @@ describe("RoomState", function() {
});

describe("maySendEvent", function() {
it("should say non-joined members may not send events",
function() {
expect(state.maySendEvent(
'm.room.message', "@nobody:nowhere",
)).toEqual(false);
expect(state.maySendMessage("@nobody:nowhere")).toEqual(false);
});

it("should say any member may send events with no power level event",
function() {
expect(state.maySendEvent('m.room.message', userA)).toEqual(true);
Expand Down
15 changes: 14 additions & 1 deletion spec/unit/room.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,7 @@ describe("Room", function() {

describe("getMyMembership", function() {
it("should return synced membership if membership isn't available yet",
async function() {
function() {
const room = new Room(roomId, null, userA);
room.setSyncedMembership("invite");
expect(room.getMyMembership()).toEqual("invite");
Expand Down Expand Up @@ -1434,4 +1434,17 @@ describe("Room", function() {
expect(room.guessDMUserId()).toEqual(userA);
});
});

describe("maySendMessage", function() {
it("should return false if synced membership not join",
function() {
const room = new Room(roomId, null, userA);
room.setSyncedMembership("invite");
expect(room.maySendMessage()).toEqual(false);
room.setSyncedMembership("leave");
expect(room.maySendMessage()).toEqual(false);
room.setSyncedMembership("join");
expect(room.maySendMessage()).toEqual(true);
});
});
});
22 changes: 13 additions & 9 deletions src/models/room-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,37 +667,41 @@ RoomState.prototype.maySendStateEvent = function(stateEventType, userId) {
* according to the room's state.
*/
RoomState.prototype._maySendEventOfType = function(eventType, userId, state) {
const member = this.getMember(userId);
if (!member || member.membership == 'leave') {
return false;
}

const power_levels_event = this.getStateEvents('m.room.power_levels', '');

let power_levels;
let events_levels = {};

let state_default = 0;
let events_default = 0;
let powerLevel = 0;
if (power_levels_event) {
power_levels = power_levels_event.getContent();
events_levels = power_levels.events || {};

if (utils.isNumber(power_levels.state_default)) {
if (Number.isFinite(power_levels.state_default)) {
state_default = power_levels.state_default;
} else {
state_default = 50;
}
if (utils.isNumber(power_levels.events_default)) {

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

if (Number.isFinite(power_levels.events_default)) {
events_default = power_levels.events_default;
}
}

let required_level = state ? state_default : events_default;
if (utils.isNumber(events_levels[eventType])) {
if (Number.isFinite(events_levels[eventType])) {
required_level = events_levels[eventType];
}
return member.powerLevel >= required_level;
return powerLevel >= required_level;
};

/**
Expand Down
11 changes: 11 additions & 0 deletions src/models/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,17 @@ Room.prototype.getAccountData = function(type) {
return this.accountData[type];
};


/**
* Returns wheter the syncing user has permission to send a message in the room
* @return {boolean} true if the user should be permitted to send
* message events into the room.
*/
Room.prototype.maySendMessage = function() {
return this.getMyMembership() === 'join' &&
this.currentState.maySendEvent('m.room.message', this.myUserId);
};

/**
* This is an internal method. Calculates the name of the room from the current
* room state.
Expand Down

0 comments on commit 8b00083

Please sign in to comment.