Skip to content

Commit

Permalink
Display notice for missing permissions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnuxie committed May 2, 2024
1 parent 0049dfd commit aa72bad
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/draupnirfactory/DraupnirProtectedRoomsSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -166,6 +167,10 @@ export async function makeProtectedRoomsSet(
protectedRoomsManager.ok,
protectionsConfig.ok,
userID,
makeHandleMissingProtectionPermissions(
client,
managementRoom.toRoomIDOrAlias()
)
);
return Ok(protectedRoomsSet);
}
84 changes: 84 additions & 0 deletions src/protections/MissingProtectionPermissions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SPDX-FileCopyrightText: 2024 Gnuxie <[email protected]>
//
// 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
)
})())
}
}

0 comments on commit aa72bad

Please sign in to comment.