Merge pull request #6912 from matrix-org/hs/ignore-greater-pl-scalar

Scalar messaging: No-op setBotPower if the current PL is greater than the requested PL.
pull/21833/head
Travis Ralston 2021-10-06 10:19:14 -06:00 committed by GitHub
commit 0f212b2607
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 13 deletions

View File

@ -452,7 +452,9 @@ function setBotOptions(event: MessageEvent<any>, roomId: string, userId: string)
}); });
} }
function setBotPower(event: MessageEvent<any>, roomId: string, userId: string, level: number): void { async function setBotPower(
event: MessageEvent<any>, roomId: string, userId: string, level: number, ignoreIfGreater?: boolean,
): Promise<void> {
if (!(Number.isInteger(level) && level >= 0)) { if (!(Number.isInteger(level) && level >= 0)) {
sendError(event, _t('Power level must be positive integer.')); sendError(event, _t('Power level must be positive integer.'));
return; return;
@ -465,22 +467,34 @@ function setBotPower(event: MessageEvent<any>, roomId: string, userId: string, l
return; return;
} }
client.getStateEvent(roomId, "m.room.power_levels", "").then((powerLevels) => { try {
const powerEvent = new MatrixEvent( const powerLevels = await client.getStateEvent(roomId, "m.room.power_levels", "");
// If the PL is equal to or greater than the requested PL, ignore.
if (ignoreIfGreater === true) {
// As per https://matrix.org/docs/spec/client_server/r0.6.0#m-room-power-levels
const currentPl = (
powerLevels.content.users && powerLevels.content.users[userId]
) || powerLevels.content.users_default || 0;
if (currentPl >= level) {
return sendResponse(event, {
success: true,
});
}
}
await client.setPowerLevel(roomId, userId, level, new MatrixEvent(
{ {
type: "m.room.power_levels", type: "m.room.power_levels",
content: powerLevels, content: powerLevels,
}, },
); ));
return sendResponse(event, {
client.setPowerLevel(roomId, userId, level, powerEvent).then(() => {
sendResponse(event, {
success: true, success: true,
}); });
}, (err) => { } catch (err) {
sendError(event, err.message ? err.message : _t('Failed to send request.'), err); sendError(event, err.message ? err.message : _t('Failed to send request.'), err);
}); }
});
} }
function getMembershipState(event: MessageEvent<any>, roomId: string, userId: string): void { function getMembershipState(event: MessageEvent<any>, roomId: string, userId: string): void {
@ -678,7 +692,7 @@ const onMessage = function(event: MessageEvent<any>): void {
setBotOptions(event, roomId, userId); setBotOptions(event, roomId, userId);
break; break;
case Action.SetBotPower: case Action.SetBotPower:
setBotPower(event, roomId, userId, event.data.level); setBotPower(event, roomId, userId, event.data.level, event.data.ignoreIfGreater);
break; break;
default: default:
console.warn("Unhandled postMessage event with action '" + event.data.action +"'"); console.warn("Unhandled postMessage event with action '" + event.data.action +"'");