-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display notice for missing permissions.
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
})()) | ||
} | ||
} |