Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add maySendStateEvent method, ported from react-sdk (but fixed). #94

Merged
merged 4 commits into from
Mar 16, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions lib/models/room-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,52 @@ RoomState.prototype.getUserIdsWithDisplayName = function(displayName) {
return this._displayNameToUserIds[displayName] || [];
};

/**
* Returns true if the given user ID has permission to send a state
* event of type `stateEventType` into this room.
* @param {string} type The type of state events to test
* @param {string} userId The user ID of the user to test permission for
* @return {boolean} true if the given user ID should be permitted to send
* the given type of state event into this room,
* according to the room's state.
*/
RoomState.prototype.maySendStateEvent = function(stateEventType, userId) {
if (!this.getMember(userId)) { return false; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only tests for presence of m.room.member events. This will return a RoomMember with membership = leave if they leave the room.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also help yourself by using RoomMember.powerLevel to get the actual power level of that member, which already takes into account users_default and explicit users entries.


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

var power_levels;
var events_levels = {};

var default_user_level = 0;
var user_levels = [];
var current_user_level = 0;

var state_default = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this 0, but 50 at line 262? it is odd that the required power level is 50 if there is an m.room.power_levels with no state_default, but 0 if there is no m.room.power_levels at all

(It is also odd that the spec doesn't clarify this)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, so in this case I'd like your opinion on matrix-org/matrix-spec-proposals#286 which is where I'm fixing the spec to document the default levels. I think it's a bug in synapse that the default is 0 with no power levels event, but apparently this is now baked into room history so we have to make the spec fit the bug.

if (power_levels_event) {
power_levels = power_levels_event.getContent();
events_levels = power_levels.events || {};

default_user_level = parseInt(power_levels.users_default || 0);
user_levels = power_levels.users || {};
current_user_level = user_levels[userId];

if (current_user_level === undefined) { current_user_level = default_user_level; }

if (power_levels.state_default !== undefined) {
state_default = power_levels.state_default;
} else {
state_default = 50;
}
}

var state_event_level = state_default;
if (events_levels[stateEventType] !== undefined) {
state_event_level = events_levels[stateEventType];
}
return current_user_level >= state_event_level;
};

/**
* The RoomState class.
*/
Expand Down
83 changes: 83 additions & 0 deletions spec/unit/room-state.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,87 @@ 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);
});

it("should say members with power >=50 may send state with power level event " +
"but no state default",
function() {
var powerLevelEvent = {
type: "m.room.power_levels", room: roomId, user: userA, event: true,
content: {
users_default: 10,
// state_default: 50, "intentionally left blank"
events_default: 25,
users: {
}
}
};
powerLevelEvent.content.users[userA] = 50;

state.setStateEvents([utils.mkEvent(powerLevelEvent)]);

expect(state.maySendStateEvent('m.room.name', userA)).toEqual(true);
expect(state.maySendStateEvent('m.room.name', userB)).toEqual(false);
});

it("should obey state_default",
function() {
var powerLevelEvent = {
type: "m.room.power_levels", room: roomId, user: userA, event: true,
content: {
users_default: 10,
state_default: 30,
events_default: 25,
users: {
}
}
};
powerLevelEvent.content.users[userA] = 30;
powerLevelEvent.content.users[userB] = 29;

state.setStateEvents([utils.mkEvent(powerLevelEvent)]);

expect(state.maySendStateEvent('m.room.name', userA)).toEqual(true);
expect(state.maySendStateEvent('m.room.name', userB)).toEqual(false);
});

it("should honour explicit event power levels in the power_levels event",
function() {
var powerLevelEvent = {
type: "m.room.power_levels", room: roomId, user: userA, event: true,
content: {
events: {
"m.room.other_thing": 76
},
users_default: 10,
state_default: 50,
events_default: 25,
users: {
}
}
};
powerLevelEvent.content.users[userA] = 80;
powerLevelEvent.content.users[userB] = 50;

state.setStateEvents([utils.mkEvent(powerLevelEvent)]);

expect(state.maySendStateEvent('m.room.name', userA)).toEqual(true);
expect(state.maySendStateEvent('m.room.name', userB)).toEqual(true);

expect(state.maySendStateEvent('m.room.other_thing', userA)).toEqual(true);
expect(state.maySendStateEvent('m.room.other_thing', userB)).toEqual(false);
});
});
});