From 3f6f2bcbb33df749d673a2023fcdbb165aaef595 Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Wed, 6 Oct 2021 16:48:17 +0100 Subject: [PATCH] no-op a setBotPower request from the integration manager when the PL is equal to or greater to the requested PL When configured --- src/ScalarMessaging.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/ScalarMessaging.ts b/src/ScalarMessaging.ts index 888b9ce9ed..3fc362c0eb 100644 --- a/src/ScalarMessaging.ts +++ b/src/ScalarMessaging.ts @@ -452,7 +452,7 @@ function setBotOptions(event: MessageEvent, roomId: string, userId: string) }); } -function setBotPower(event: MessageEvent, roomId: string, userId: string, level: number): void { +function setBotPower(event: MessageEvent, roomId: string, userId: string, level: number, ignoreIfGreater?: boolean): void { if (!(Number.isInteger(level) && level >= 0)) { sendError(event, _t('Power level must be positive integer.')); return; @@ -473,6 +473,18 @@ function setBotPower(event: MessageEvent, roomId: string, userId: string, l }, ); + // If the PL is equal to or greater than the requested PL, ignore. + if (ignoreIfGreater) { + // 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) { + sendResponse(event, { + success: true, + }); + } + } + client.setPowerLevel(roomId, userId, level, powerEvent).then(() => { sendResponse(event, { success: true, @@ -678,7 +690,7 @@ const onMessage = function(event: MessageEvent): void { setBotOptions(event, roomId, userId); break; case Action.SetBotPower: - setBotPower(event, roomId, userId, event.data.level); + setBotPower(event, roomId, userId, event.data.level, event.data.ignoreIfGreater); break; default: console.warn("Unhandled postMessage event with action '" + event.data.action +"'");