-
-
Notifications
You must be signed in to change notification settings - Fork 585
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; } | ||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this (It is also odd that the spec doesn't clarify this) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
*/ | ||
|
There was a problem hiding this comment.
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 aRoomMember
withmembership = leave
if they leave the room.There was a problem hiding this comment.
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 accountusers_default
and explicitusers
entries.