From 728089365b632da28a0695ff2c6876191fbc4032 Mon Sep 17 00:00:00 2001 From: Gnuxie <50846879+Gnuxie@users.noreply.github.com> Date: Thu, 28 Nov 2024 16:36:12 +0000 Subject: [PATCH] Unban propagation prompt now appears whenever there are room bans. (#629) Before the prompt would only show when there were rules matching the unbanned user. Now we only decide to hide the prompt when there are no rules and no room bans. Fixes the unban propagation side of https://github.com/the-draupnir-project/Draupnir/issues/622. --- src/protections/BanPropagation.tsx | 94 +++++++++++++++++++++++------- 1 file changed, 73 insertions(+), 21 deletions(-) diff --git a/src/protections/BanPropagation.tsx b/src/protections/BanPropagation.tsx index b99c119f..781f7048 100644 --- a/src/protections/BanPropagation.tsx +++ b/src/protections/BanPropagation.tsx @@ -7,7 +7,10 @@ // This modified file incorporates work from mjolnir // https://github.com/matrix-org/mjolnir // -import { DeadDocumentJSX } from "@the-draupnir-project/interface-manager"; +import { + DeadDocumentJSX, + DocumentNode, +} from "@the-draupnir-project/interface-manager"; import { renderMentionPill, renderRoomPill, @@ -138,18 +141,43 @@ async function promptBanPropagation( ); } -async function promptUnbanPropagation( - draupnir: Draupnir, +function renderUnbanPrompt( membershipChange: MembershipChange, roomID: StringRoomID, - rulesMatchingUser: ListMatches[] -): Promise { - const reactionMap = new Map( - Object.entries({ "unban from all": "unban from all" }) - ); - const promptSendResult = await sendMatrixEventsFromDeadDocument( - draupnir.clientPlatform.toRoomMessageSender(), - draupnir.managementRoomID, + rulesMatchingUser: ListMatches[], + roomsBannedFrom: MatrixRoomID[] +): DocumentNode { + const renderRules = () => { + if (rulesMatchingUser.length === 0) { + return

There are no rules matching this user.

; + } + return ( +

+ There are rules in Draupnir's watched lists matching this user: +

+

+ ); + }; + const renderRoomBans = () => { + if (roomsBannedFrom.length === 0) { + return

The user is not banned from any rooms.

; + } + return ( +

+ The user is banned from the following rooms: +

+

+ ); + }; + return ( The user{" "} {renderMentionPill( @@ -160,16 +188,33 @@ async function promptUnbanPropagation( {renderRoomPill(MatrixRoomReference.fromRoomID(roomID))} by{" "} {membershipChange.sender} for{" "} {membershipChange.content.reason ?? ""} - .
- However there are rules in Draupnir's watched lists matching this user: -
    - {rulesMatchingUser.map((match) => ( -
  • {renderListRules(match)}
  • - ))} -
+ {renderRules()} + {renderRoomBans()} Would you like to remove these rules and unban the user from all protected rooms? -
, + + ); +} + +async function promptUnbanPropagation( + draupnir: Draupnir, + membershipChange: MembershipChange, + roomID: StringRoomID, + rulesMatchingUser: ListMatches[], + roomsBannedFrom: MatrixRoomID[] +): Promise { + const reactionMap = new Map( + Object.entries({ "unban from all": "unban from all" }) + ); + const promptSendResult = await sendMatrixEventsFromDeadDocument( + draupnir.clientPlatform.toRoomMessageSender(), + draupnir.managementRoomID, + renderUnbanPrompt( + membershipChange, + roomID, + rulesMatchingUser, + roomsBannedFrom + ), { additionalContent: draupnir.reactionHandler.createAnnotation( UNBAN_PROPAGATION_PROMPT_LISTENER, @@ -302,7 +347,13 @@ export class BanPropagationProtection draupnir.protectedRoomsSet.issuerManager, draupnir.policyRoomManager ); - if (rulesMatchingUser.length === 0) { + const roomsBannedFrom = + this.protectedRoomsSet.setMembership.allRooms.filter( + (revision) => + revision.membershipForUser(change.userID)?.membership === + Membership.Ban + ); + if (rulesMatchingUser.length === 0 && roomsBannedFrom.length === 0) { return; // user is already unbanned. } const addRule = ( @@ -339,7 +390,8 @@ export class BanPropagationProtection matches: rules, profile: info.watchedListProfile, }; - }) + }), + roomsBannedFrom.map((revision) => revision.room) ); }