From 7be6f59782295e74d21453693daa757644849830 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 2 May 2022 14:27:32 -0600 Subject: [PATCH 1/5] Add an option to ignore (block) a user when reporting their events This is primarily useful if the content being reported really doesn't belong on your screen, and the room moderators are slow to react. Ideally we'd use the word "block" instead of "ignore", but we call it "ignore user" everywhere else. See https://github.com/vector-im/element-web/issues/19590 for further context on the word choice. This change includes a minor refactor to the styles of labelled toggles (for reusability). --- res/css/_components.scss | 1 - .../_WidgetCapabilitiesPromptDialog.scss | 9 ------ .../_WidgetOpenIDPermissionsDialog.scss | 28 ---------------- res/css/views/elements/_SettingsFlag.scss | 13 ++++++++ .../views/dialogs/ReportEventDialog.tsx | 32 ++++++++++++++++++- .../views/elements/LabelledToggleSwitch.tsx | 8 +++-- src/i18n/strings/en_EN.json | 1 + 7 files changed, 51 insertions(+), 41 deletions(-) delete mode 100644 res/css/views/dialogs/_WidgetOpenIDPermissionsDialog.scss diff --git a/res/css/_components.scss b/res/css/_components.scss index 3f371478d12..89189882be0 100644 --- a/res/css/_components.scss +++ b/res/css/_components.scss @@ -131,7 +131,6 @@ @import "./views/dialogs/_UploadConfirmDialog.scss"; @import "./views/dialogs/_UserSettingsDialog.scss"; @import "./views/dialogs/_WidgetCapabilitiesPromptDialog.scss"; -@import "./views/dialogs/_WidgetOpenIDPermissionsDialog.scss"; @import "./views/dialogs/security/_AccessSecretStorageDialog.scss"; @import "./views/dialogs/security/_CreateCrossSigningDialog.scss"; @import "./views/dialogs/security/_CreateKeyBackupDialog.scss"; diff --git a/res/css/views/dialogs/_WidgetCapabilitiesPromptDialog.scss b/res/css/views/dialogs/_WidgetCapabilitiesPromptDialog.scss index 8786defed38..c559467c9de 100644 --- a/res/css/views/dialogs/_WidgetCapabilitiesPromptDialog.scss +++ b/res/css/views/dialogs/_WidgetCapabilitiesPromptDialog.scss @@ -46,10 +46,6 @@ limitations under the License. font-size: $font-12px; .mx_ToggleSwitch { - display: inline-block; - vertical-align: middle; - margin-right: 8px; - // downsize the switch + ball width: $font-32px; height: $font-15px; @@ -64,10 +60,5 @@ limitations under the License. border-radius: $font-15px; } } - - .mx_SettingsFlag_label { - display: inline-block; - vertical-align: middle; - } } } diff --git a/res/css/views/dialogs/_WidgetOpenIDPermissionsDialog.scss b/res/css/views/dialogs/_WidgetOpenIDPermissionsDialog.scss deleted file mode 100644 index a419c105a9a..00000000000 --- a/res/css/views/dialogs/_WidgetOpenIDPermissionsDialog.scss +++ /dev/null @@ -1,28 +0,0 @@ -/* -Copyright 2019 Travis Ralston - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -.mx_WidgetOpenIDPermissionsDialog .mx_SettingsFlag { - .mx_ToggleSwitch { - display: inline-block; - vertical-align: middle; - margin-right: 8px; - } - - .mx_SettingsFlag_label { - display: inline-block; - vertical-align: middle; - } -} diff --git a/res/css/views/elements/_SettingsFlag.scss b/res/css/views/elements/_SettingsFlag.scss index c6f4cf6ec5c..6d941e94eb5 100644 --- a/res/css/views/elements/_SettingsFlag.scss +++ b/res/css/views/elements/_SettingsFlag.scss @@ -24,6 +24,19 @@ limitations under the License. .mx_ToggleSwitch { flex: 0 0 auto; } + + &.mx_SettingsFlag_toggleInFront { + .mx_ToggleSwitch { + display: inline-block; + vertical-align: middle; + margin-right: 8px; + } + + .mx_SettingsFlag_label { + display: inline-block; + vertical-align: middle; + } + } } .mx_SettingsFlag_label { diff --git a/src/components/views/dialogs/ReportEventDialog.tsx b/src/components/views/dialogs/ReportEventDialog.tsx index 7a2d51889f8..a7221e99216 100644 --- a/src/components/views/dialogs/ReportEventDialog.tsx +++ b/src/components/views/dialogs/ReportEventDialog.tsx @@ -30,6 +30,7 @@ import BaseDialog from "./BaseDialog"; import DialogButtons from "../elements/DialogButtons"; import Field from "../elements/Field"; import Spinner from "../elements/Spinner"; +import LabelledToggleSwitch from "../elements/LabelledToggleSwitch"; interface IProps extends IDialogProps { mxEvent: MatrixEvent; @@ -42,6 +43,7 @@ interface IState { err?: string; // If we know it, the nature of the abuse, as specified by MSC3215. nature?: ExtendedNature; + ignoreUserToo: boolean; // if true, user will be ignored/blocked on submit } const MODERATED_BY_STATE_EVENT_TYPE = [ @@ -160,9 +162,14 @@ export default class ReportEventDialog extends React.Component { err: null, // If specified, the nature of the abuse, as specified by MSC3215. nature: null, + ignoreUserToo: false, // default false, for now. Could easily be argued as default true }; } + private onIgnoreUserTooChanged = (newVal: boolean): void => { + this.setState({ ignoreUserToo: newVal }); + }; + // The user has written down a freeform description of the abuse. private onReasonChange = ({ target: { value: reason } }): void => { this.setState({ reason }); @@ -232,6 +239,15 @@ export default class ReportEventDialog extends React.Component { // Report to homeserver admin through the dedicated Matrix API. await client.reportEvent(ev.getRoomId(), ev.getId(), -100, this.state.reason.trim()); } + + // if the user should also be ignored, do that + if (this.state.ignoreUserToo) { + await client.setIgnoredUsers([ + ...client.getIgnoredUsers(), + ev.getSender(), + ]); + } + this.props.onFinished(true); } catch (e) { logger.error(e); @@ -242,7 +258,7 @@ export default class ReportEventDialog extends React.Component { } }; - render() { + public render() { let error = null; if (this.state.err) { error =
@@ -387,6 +403,13 @@ export default class ReportEventDialog extends React.Component { /> { progress } { error } +
{ /> { progress } { error } + { - render() { + public render() { // This is a minimal version of a SettingsFlag let firstPart = { this.props.label }; @@ -52,7 +53,10 @@ export default class LabelledToggleSwitch extends React.PureComponent { secondPart = temp; } - const classes = `mx_SettingsFlag ${this.props.className || ""}`; + const classes = classNames("mx_SettingsFlag", { + [this.props.className]: true, + "mx_SettingsFlag_toggleInFront": this.props.toggleInFront, + }); return (
{ firstPart } diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 31c6f3383fd..03f01aed498 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -2691,6 +2691,7 @@ "Illegal Content": "Illegal Content", "Spam or propaganda": "Spam or propaganda", "Report the entire room": "Report the entire room", + "Ignore the user (hide all current and future messages from this user)": "Ignore the user (hide all current and future messages from this user)", "Send report": "Send report", "Report Content to Your Homeserver Administrator": "Report Content to Your Homeserver Administrator", "Reporting this message will send its unique 'event ID' to the administrator of your homeserver. If messages in this room are encrypted, your homeserver administrator will not be able to read the message text or view any files or images.": "Reporting this message will send its unique 'event ID' to the administrator of your homeserver. If messages in this room are encrypted, your homeserver administrator will not be able to read the message text or view any files or images.", From 0d86dd4be20a2ae8d777922004c53047986c6c3e Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 2 May 2022 14:40:44 -0600 Subject: [PATCH 2/5] Appease the linter --- src/components/views/elements/LabelledToggleSwitch.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/views/elements/LabelledToggleSwitch.tsx b/src/components/views/elements/LabelledToggleSwitch.tsx index cf0f74fdb1e..13ce55d29fe 100644 --- a/src/components/views/elements/LabelledToggleSwitch.tsx +++ b/src/components/views/elements/LabelledToggleSwitch.tsx @@ -1,5 +1,5 @@ /* -Copyright 2019 - 2021 The Matrix.org Foundation C.I.C. +Copyright 2019 - 2022 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -15,9 +15,9 @@ limitations under the License. */ import React from "react"; +import classNames from "classnames"; import ToggleSwitch from "./ToggleSwitch"; -import classNames from "classnames"; interface IProps { // The value for the toggle switch @@ -54,7 +54,7 @@ export default class LabelledToggleSwitch extends React.PureComponent { } const classes = classNames("mx_SettingsFlag", { - [this.props.className]: true, + [this.props.className ?? ""]: true, "mx_SettingsFlag_toggleInFront": this.props.toggleInFront, }); return ( From 33c044b6a70efc04731ed640f0d2b08263138df9 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Thu, 12 May 2022 15:23:34 -0600 Subject: [PATCH 3/5] Use a checkbox instead of toggle in the dialog --- res/css/_components.scss | 3 +- res/css/views/elements/_LabelledCheckbox.scss | 39 ++++++ .../views/dialogs/ReportEventDialog.tsx | 27 ++-- .../views/elements/LabelledCheckbox.tsx | 44 +++++++ src/i18n/strings/en_EN.json | 3 +- .../views/elements/LabelledCheckbox-test.tsx | 124 ++++++++++++++++++ .../LabelledCheckbox-test.tsx.snap | 108 +++++++++++++++ 7 files changed, 331 insertions(+), 17 deletions(-) create mode 100644 res/css/views/elements/_LabelledCheckbox.scss create mode 100644 src/components/views/elements/LabelledCheckbox.tsx create mode 100644 test/components/views/elements/LabelledCheckbox-test.tsx create mode 100644 test/components/views/elements/__snapshots__/LabelledCheckbox-test.tsx.snap diff --git a/res/css/_components.scss b/res/css/_components.scss index 39d8562ed30..12e0aaa705c 100644 --- a/res/css/_components.scss +++ b/res/css/_components.scss @@ -142,7 +142,6 @@ @import "./views/elements/_AddressSelector.scss"; @import "./views/elements/_AddressTile.scss"; @import "./views/elements/_CopyableText.scss"; -@import "./views/elements/_SearchWarning.scss"; @import "./views/elements/_DesktopCapturerSourcePicker.scss"; @import "./views/elements/_DialPadBackspaceButton.scss"; @import "./views/elements/_DirectorySearchBox.scss"; @@ -159,6 +158,7 @@ @import "./views/elements/_InlineSpinner.scss"; @import "./views/elements/_InteractiveTooltip.scss"; @import "./views/elements/_InviteReason.scss"; +@import "./views/elements/_LabelledCheckbox.scss"; @import "./views/elements/_ManageIntegsButton.scss"; @import "./views/elements/_MiniAvatarUploader.scss"; @import "./views/elements/_Pill.scss"; @@ -171,6 +171,7 @@ @import "./views/elements/_RoleButton.scss"; @import "./views/elements/_RoomAliasField.scss"; @import "./views/elements/_SSOButtons.scss"; +@import "./views/elements/_SearchWarning.scss"; @import "./views/elements/_ServerPicker.scss"; @import "./views/elements/_SettingsFlag.scss"; @import "./views/elements/_Slider.scss"; diff --git a/res/css/views/elements/_LabelledCheckbox.scss b/res/css/views/elements/_LabelledCheckbox.scss new file mode 100644 index 00000000000..c97b1b341a1 --- /dev/null +++ b/res/css/views/elements/_LabelledCheckbox.scss @@ -0,0 +1,39 @@ +/* +Copyright 2022 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +.mx_LabelledCheckbox { + display: flex; + flex-direction: row; + + .mx_Checkbox { + margin-top: 3px; // visually align with label text + } + + .mx_LabelledCheckbox_labels { + flex: 1; + + .mx_LabelledCheckbox_label { + vertical-align: middle; + } + + .mx_LabelledCheckbox_byline { + display: block; + padding-top: $spacing-4; + color: $muted-fg-color; + font-size: $font-11px; + } + } +} diff --git a/src/components/views/dialogs/ReportEventDialog.tsx b/src/components/views/dialogs/ReportEventDialog.tsx index a7221e99216..929c33b128e 100644 --- a/src/components/views/dialogs/ReportEventDialog.tsx +++ b/src/components/views/dialogs/ReportEventDialog.tsx @@ -1,5 +1,6 @@ /* Copyright 2019 Michael Telatynski <7t3chguy@gmail.com> +Copyright 2022 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -30,7 +31,7 @@ import BaseDialog from "./BaseDialog"; import DialogButtons from "../elements/DialogButtons"; import Field from "../elements/Field"; import Spinner from "../elements/Spinner"; -import LabelledToggleSwitch from "../elements/LabelledToggleSwitch"; +import LabelledCheckbox from "../elements/LabelledCheckbox"; interface IProps extends IDialogProps { mxEvent: MatrixEvent; @@ -275,6 +276,14 @@ export default class ReportEventDialog extends React.Component { ); } + const ignoreUserCheckbox = ; + const adminMessageMD = SdkConfig .getObject("report_event")?.get("admin_message_md", "adminMessageMD"); let adminMessage; @@ -403,13 +412,7 @@ export default class ReportEventDialog extends React.Component { /> { progress } { error } - + { ignoreUserCheckbox }
{ /> { progress } { error } - + { ignoreUserCheckbox } = ({ value, label, byline, disabled, onChange }) => { + return +}; + +export default LabelledCheckbox; diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index f89728001d4..7858bb93441 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -2675,6 +2675,8 @@ "Just a heads up, if you don't add an email and forget your password, you could permanently lose access to your account.": "Just a heads up, if you don't add an email and forget your password, you could permanently lose access to your account.", "Email (optional)": "Email (optional)", "Please fill why you're reporting.": "Please fill why you're reporting.", + "Ignore user": "Ignore user", + "Check if you want to hide all current and future messages from this user.": "Check if you want to hide all current and future messages from this user.", "What this user is writing is wrong.\nThis will be reported to the room moderators.": "What this user is writing is wrong.\nThis will be reported to the room moderators.", "This user is displaying toxic behaviour, for instance by insulting other users or sharing adult-only content in a family-friendly room or otherwise violating the rules of this room.\nThis will be reported to the room moderators.": "This user is displaying toxic behaviour, for instance by insulting other users or sharing adult-only content in a family-friendly room or otherwise violating the rules of this room.\nThis will be reported to the room moderators.", "This user is displaying illegal behaviour, for instance by doxing people or threatening violence.\nThis will be reported to the room moderators who may escalate this to legal authorities.": "This user is displaying illegal behaviour, for instance by doxing people or threatening violence.\nThis will be reported to the room moderators who may escalate this to legal authorities.", @@ -2689,7 +2691,6 @@ "Illegal Content": "Illegal Content", "Spam or propaganda": "Spam or propaganda", "Report the entire room": "Report the entire room", - "Ignore the user (hide all current and future messages from this user)": "Ignore the user (hide all current and future messages from this user)", "Send report": "Send report", "Report Content to Your Homeserver Administrator": "Report Content to Your Homeserver Administrator", "Reporting this message will send its unique 'event ID' to the administrator of your homeserver. If messages in this room are encrypted, your homeserver administrator will not be able to read the message text or view any files or images.": "Reporting this message will send its unique 'event ID' to the administrator of your homeserver. If messages in this room are encrypted, your homeserver administrator will not be able to read the message text or view any files or images.", diff --git a/test/components/views/elements/LabelledCheckbox-test.tsx b/test/components/views/elements/LabelledCheckbox-test.tsx new file mode 100644 index 00000000000..b03ee0c1f18 --- /dev/null +++ b/test/components/views/elements/LabelledCheckbox-test.tsx @@ -0,0 +1,124 @@ +/* +Copyright 2022 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import React from 'react'; +import { mount } from 'enzyme'; +import { act } from "react-dom/test-utils"; + +import LabelledCheckbox from "../../../../src/components/views/elements/LabelledCheckbox"; + +// Fake random strings to give a predictable snapshot for checkbox IDs +jest.mock( + 'matrix-js-sdk/src/randomstring', + () => { + return { + randomString: () => "abdefghi", + }; + }, +); + +describe('', () => { + type CompProps = React.ComponentProps; + const getComponent = (props: CompProps) => mount(); + type CompClass = ReturnType; + + const getCheckbox = (component: CompClass) => component.find(`input[type="checkbox"]`); + const getLabel = (component: CompClass) => component.find(`.mx_LabelledCheckbox_label`); + const getByline = (component: CompClass) => component.find(`.mx_LabelledCheckbox_byline`); + + const isChecked = (checkbox: ReturnType) => checkbox.is(`[checked=true]`); + const isDisabled = (checkbox: ReturnType) => checkbox.is(`[disabled=true]`); + const getText = (span: ReturnType) => span.length > 0 ? span.at(0).text() : null; + + test.each([null, "this is a byline"])( + "should render with byline of %p", + (byline) => { + const props: CompProps = { + label: "Hello world", + value: true, + byline: byline, + onChange: jest.fn(), + }; + const component = getComponent(props); + const checkbox = getCheckbox(component); + + expect(component).toMatchSnapshot(); + expect(isChecked(checkbox)).toBe(true); + expect(isDisabled(checkbox)).toBe(false); + expect(getText(getLabel(component))).toBe(props.label); + expect(getText(getByline(component))).toBe(byline); + }, + ); + + it('should support unchecked by default', () => { + const props: CompProps = { + label: "Hello world", + value: false, + onChange: jest.fn(), + }; + const component = getComponent(props); + + expect(isChecked(getCheckbox(component))).toBe(false); + }); + + it('should be possible to disable the checkbox', () => { + const props: CompProps = { + label: "Hello world", + value: false, + disabled: true, + onChange: jest.fn(), + }; + const component = getComponent(props); + + expect(isDisabled(getCheckbox(component))).toBe(true); + }); + + it('should emit onChange calls', () => { + const props: CompProps = { + label: "Hello world", + value: false, + onChange: jest.fn(), + }; + const component = getComponent(props); + + expect(props.onChange).not.toHaveBeenCalled(); + + act(() => { + getCheckbox(component).simulate('change'); + }); + + expect(props.onChange).toHaveBeenCalledTimes(1); + }); + + it('should react to value and disabled prop changes', () => { + const props: CompProps = { + label: "Hello world", + value: false, + onChange: jest.fn(), + }; + const component = getComponent(props); + let checkbox = getCheckbox(component); + + expect(isChecked(checkbox)).toBe(false); + expect(isDisabled(checkbox)).toBe(false); + + component.setProps({ value: true, disabled: true }); + checkbox = getCheckbox(component); // refresh reference to checkbox + + expect(isChecked(checkbox)).toBe(true); + expect(isDisabled(checkbox)).toBe(true); + }); +}); diff --git a/test/components/views/elements/__snapshots__/LabelledCheckbox-test.tsx.snap b/test/components/views/elements/__snapshots__/LabelledCheckbox-test.tsx.snap new file mode 100644 index 00000000000..34cdbe59be9 --- /dev/null +++ b/test/components/views/elements/__snapshots__/LabelledCheckbox-test.tsx.snap @@ -0,0 +1,108 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` should render with byline of "this is a byline" 1`] = ` + +