1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

Function for working out notif trigger permission

And make pushprocessor use this function
This commit is contained in:
David Baker
2017-11-02 12:04:55 +00:00
parent ff5f95227a
commit 978db89deb
2 changed files with 35 additions and 22 deletions

View File

@@ -393,6 +393,37 @@ RoomState.prototype._maySendEventOfType = function(eventType, userId, state) {
return member.powerLevel >= required_level;
};
/**
* Returns true if the given user ID has permission to trigger notification
* of type `notifLevelKey`
* @param {string} notifLevelKey The level of notification to test (eg. 'room')
* @param {string} userId The user ID of the user to test permission for
* @return {boolean} true if the given user ID has permission to trigger a
* notification of this type.
*/
RoomState.prototype.mayTriggerNotifOfType = function(notifLevelKey, userId) {
const member = this.getMember(userId);
if (!member || member.membership == 'leave') {
return false;
}
const powerLevelsEvent = this.getStateEvents('m.room.power_levels', '');
if (!powerLevelsEvent || !powerLevelsEvent.getContent()) {
return false;
}
let notifLevel = 50;
if (
powerLevelsEvent.getContent().notifications &&
powerLevelsEvent.getContent().notifications[notifLevelKey]
) {
notifLevel = powerLevelsEvent.getContent().notifications[notifLevelKey];
}
return member.powerLevel >= notifLevel;
};
/**
* The RoomState class.
*/

View File

@@ -145,28 +145,10 @@ function PushProcessor(client) {
return false;
}
const powerLevels = room.currentState.getStateEvents('m.room.power_levels', '');
if (!powerLevels || !powerLevels.getContent()) {
return false;
}
let notifLevel = 50;
if (
powerLevels.getContent().notifications &&
powerLevels.getContent().notifications[notifLevelKey]
) {
notifLevel = powerLevels.getContent().notifications[notifLevelKey];
}
// This cannot be assumed to always be set for state events
// (in particular it is never set for the room creation event
// because it preceeds the join event of the sender).
// In these cases, this condition cannot match.
if (ev.sender === null) {
return false;
}
return ev.sender.powerLevel >= notifLevel;
// Note that this should not be the current state of the room but the state at
// the point the event is in the DAG. Unfortunately the js-sdk does not store
// this.
return room.currentState.mayTriggerNotifOfType(notifLevelKey, ev.getSender());
};
const eventFulfillsRoomMemberCountCondition = function(cond, ev) {