|
| 1 | +/* |
| 2 | +Copyright 2025 New Vector Ltd. |
| 3 | +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only |
| 4 | +Please see LICENSE files in the repository root for full details. |
| 5 | +*/ |
| 6 | + |
| 7 | +import { logger } from "@sentry/browser"; |
| 8 | +import { type Room } from "matrix-js-sdk/src/matrix"; |
| 9 | +import { KnownMembership } from "matrix-js-sdk/src/types"; |
| 10 | + |
| 11 | +import { useMatrixClientContext } from "../../../../../contexts/MatrixClientContext"; |
| 12 | +import { _t } from "../../../../../languageHandler"; |
| 13 | +import Modal from "../../../../../Modal"; |
| 14 | +import { bulkSpaceBehaviour } from "../../../../../utils/space"; |
| 15 | +import ConfirmSpaceUserActionDialog from "../../../../views/dialogs/ConfirmSpaceUserActionDialog"; |
| 16 | +import ConfirmUserActionDialog from "../../../../views/dialogs/ConfirmUserActionDialog"; |
| 17 | +import ErrorDialog from "../../../../views/dialogs/ErrorDialog"; |
| 18 | +import { type RoomAdminToolsProps } from "./UserInfoAdminToolsContainerViewModel"; |
| 19 | + |
| 20 | +interface RoomKickButtonState { |
| 21 | + /** |
| 22 | + * The function to call when the button is clicked |
| 23 | + */ |
| 24 | + onKickClick: () => Promise<void>; |
| 25 | + /** |
| 26 | + * Whether the user can be kicked based on membership value. If the user already join or was invited, it can be kicked |
| 27 | + */ |
| 28 | + canUserBeKicked: boolean; |
| 29 | + /** |
| 30 | + * The label of the kick button can be kick or disinvite |
| 31 | + */ |
| 32 | + kickLabel: string; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * The view model for the room kick button used in the UserInfoAdminToolsContainer |
| 37 | + * @param {RoomAdminToolsProps} props - the object containing the necceray props for kickButton the view model |
| 38 | + * @param {Room} props.room - the room to kick/disinvite the user from |
| 39 | + * @param {RoomMember} props.member - the member to kick/disinvite |
| 40 | + * @param {boolean} props.isUpdating - whether the operation is currently in progress |
| 41 | + * @param {function} props.startUpdating - callback function to start the operation |
| 42 | + * @param {function} props.stopUpdating - callback function to stop the operation |
| 43 | + * @returns {KickButtonState} the room kick/disinvite button state |
| 44 | + */ |
| 45 | +export function useRoomKickButtonViewModel(props: RoomAdminToolsProps): RoomKickButtonState { |
| 46 | + const { isUpdating, startUpdating, stopUpdating, room, member } = props; |
| 47 | + |
| 48 | + const cli = useMatrixClientContext(); |
| 49 | + |
| 50 | + const onKickClick = async (): Promise<void> => { |
| 51 | + if (isUpdating) return; // only allow one operation at a time |
| 52 | + startUpdating(); |
| 53 | + |
| 54 | + const commonProps = { |
| 55 | + member, |
| 56 | + action: room.isSpaceRoom() |
| 57 | + ? member.membership === KnownMembership.Invite |
| 58 | + ? _t("user_info|disinvite_button_space") |
| 59 | + : _t("user_info|kick_button_space") |
| 60 | + : member.membership === KnownMembership.Invite |
| 61 | + ? _t("user_info|disinvite_button_room") |
| 62 | + : _t("user_info|kick_button_room"), |
| 63 | + title: |
| 64 | + member.membership === KnownMembership.Invite |
| 65 | + ? _t("user_info|disinvite_button_room_name", { roomName: room.name }) |
| 66 | + : _t("user_info|kick_button_room_name", { roomName: room.name }), |
| 67 | + askReason: member.membership === KnownMembership.Join, |
| 68 | + danger: true, |
| 69 | + }; |
| 70 | + |
| 71 | + let finished: Promise<[success?: boolean, reason?: string, rooms?: Room[]]>; |
| 72 | + |
| 73 | + if (room.isSpaceRoom()) { |
| 74 | + ({ finished } = Modal.createDialog( |
| 75 | + ConfirmSpaceUserActionDialog, |
| 76 | + { |
| 77 | + ...commonProps, |
| 78 | + space: room, |
| 79 | + spaceChildFilter: (child: Room) => { |
| 80 | + // Return true if the target member is not banned and we have sufficient PL to ban them |
| 81 | + const myMember = child.getMember(cli.credentials.userId || ""); |
| 82 | + const theirMember = child.getMember(member.userId); |
| 83 | + return ( |
| 84 | + !!myMember && |
| 85 | + !!theirMember && |
| 86 | + theirMember.membership === member.membership && |
| 87 | + myMember.powerLevel > theirMember.powerLevel && |
| 88 | + child.currentState.hasSufficientPowerLevelFor("kick", myMember.powerLevel) |
| 89 | + ); |
| 90 | + }, |
| 91 | + allLabel: _t("user_info|kick_button_space_everything"), |
| 92 | + specificLabel: _t("user_info|kick_space_specific"), |
| 93 | + warningMessage: _t("user_info|kick_space_warning"), |
| 94 | + }, |
| 95 | + "mx_ConfirmSpaceUserActionDialog_wrapper", |
| 96 | + )); |
| 97 | + } else { |
| 98 | + ({ finished } = Modal.createDialog(ConfirmUserActionDialog, commonProps)); |
| 99 | + } |
| 100 | + |
| 101 | + const [proceed, reason, rooms = []] = await finished; |
| 102 | + if (!proceed) { |
| 103 | + stopUpdating(); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + bulkSpaceBehaviour(room, rooms, (room) => cli.kick(room.roomId, member.userId, reason || undefined)) |
| 108 | + .then( |
| 109 | + () => { |
| 110 | + // NO-OP; rely on the m.room.member event coming down else we could |
| 111 | + // get out of sync if we force setState here! |
| 112 | + logger.info("Kick success"); |
| 113 | + }, |
| 114 | + function (err) { |
| 115 | + logger.error("Kick error: " + err); |
| 116 | + Modal.createDialog(ErrorDialog, { |
| 117 | + title: _t("user_info|error_kicking_user"), |
| 118 | + description: err?.message ?? "Operation failed", |
| 119 | + }); |
| 120 | + }, |
| 121 | + ) |
| 122 | + .finally(() => { |
| 123 | + stopUpdating(); |
| 124 | + }); |
| 125 | + }; |
| 126 | + |
| 127 | + const canUserBeKicked = member.membership === KnownMembership.Invite || member.membership === KnownMembership.Join; |
| 128 | + |
| 129 | + const kickLabel = room.isSpaceRoom() |
| 130 | + ? member.membership === KnownMembership.Invite |
| 131 | + ? _t("user_info|disinvite_button_space") |
| 132 | + : _t("user_info|kick_button_space") |
| 133 | + : member.membership === KnownMembership.Invite |
| 134 | + ? _t("user_info|disinvite_button_room") |
| 135 | + : _t("user_info|kick_button_room"); |
| 136 | + |
| 137 | + return { |
| 138 | + onKickClick, |
| 139 | + canUserBeKicked, |
| 140 | + kickLabel, |
| 141 | + }; |
| 142 | +} |
0 commit comments