Display notice for missing permissions.

This commit is contained in:
gnuxie authored and Gnuxie committed 2024-05-02 15:57:44 +01:00
1 parent 0049dfd367
commit aa72badd3e
2 files changed
+89

No files matched your search

@@ -31,6 +31,7 @@ import { DefaultEnabledProtectionsMigration } from "../protections/DefaultEnable
import '../protections/DraupnirProtectionsIndex';
import { IConfig } from "../config";
import { runProtectionConfigHooks } from "../protections/ConfigHooks";
import { makeHandleMissingProtectionPermissions } from "../protections/MissingProtectionPermissions";
const log = new Logger('DraupnirProtectedRoomsSet');
@@ -166,6 +167,10 @@ export async function makeProtectedRoomsSet(
protectedRoomsManager.ok,
protectionsConfig.ok,
userID,
makeHandleMissingProtectionPermissions(
client,
managementRoom.toRoomIDOrAlias()
)
);
return Ok(protectedRoomsSet);
}
@@ -0,0 +1,84 @@
// SPDX-FileCopyrightText: 2024 Gnuxie <Gnuxie@protonmail.com>
//
// SPDX-License-Identifier: AFL-3.0
import { HandleMissingProtectionPermissions, MatrixRoomReference, ProtectionPermissionsChange, StringRoomID, Task } from "matrix-protection-suite";
import { renderMatrixAndSend } from "../commands/interface-manager/DeadDocumentMatrix";
import { MatrixSendClient } from "matrix-protection-suite-for-matrix-bot-sdk";
import { JSXFactory } from "../commands/interface-manager/JSXFactory";
import { DocumentNode } from "../commands/interface-manager/DeadDocument";
import { renderRoomPill } from "../commands/interface-manager/MatrixHelpRenderer";
function renderPermissions(
title: DocumentNode,
permissions: string[]
): DocumentNode {
return permissions.length === 0
? <fragment></fragment>
: <fragment>
{title}
<ul>
{permissions.map(permission => <li><code>{permission}</code></li>)}
</ul>
</fragment>
}
function missingPermissionsTotal(change: ProtectionPermissionsChange): number {
return change.permissionsChange.missingEventPermissions.length
+ change.permissionsChange.missingPermissions.length
+ change.permissionsChange.missingStatePermissions.length
}
function renderMissingProtectionPermissions(
protectionPermissions: ProtectionPermissionsChange
): DocumentNode {
return <details>
<summary>The <code>{protectionPermissions.protection.description.name}</code> is missing the following permissions ({missingPermissionsTotal(protectionPermissions)}):</summary>
{renderPermissions(
<fragment>Missing permissions:</fragment>,
protectionPermissions.permissionsChange.missingPermissions
)}
{renderPermissions(
<fragment>Missing state permissions:</fragment>,
protectionPermissions.permissionsChange.missingStatePermissions
)}
{renderPermissions(
<fragment>Missing event permissions:</fragment>,
protectionPermissions.permissionsChange.missingEventPermissions
)}
</details>
}
function renderMissingProtectionsPermissions(
roomID: StringRoomID,
protectionPermissions: ProtectionPermissionsChange[]
): DocumentNode {
return <fragment>
There are protections with missing permissions within the room {renderRoomPill(MatrixRoomReference.fromRoomID(roomID, []))}.
<ul>
{protectionPermissions.map(details => <li>{renderMissingProtectionPermissions(details)}</li>)}
</ul>
</fragment>
}
export function makeHandleMissingProtectionPermissions(
client: MatrixSendClient,
managementRoomID: StringRoomID
): HandleMissingProtectionPermissions {
return function(
roomID,
protectionPermissions
) {
void Task((async () => {
renderMatrixAndSend(
<root>{renderMissingProtectionsPermissions(
roomID,
protectionPermissions
)}</root>,
managementRoomID,
undefined,
client
)
})())
}
}