Implement MSC4155: Invite filtering (#29603)

* Add settings for MSC4155

* copyright

* Tweak to not use js-sdk

* Update for latest MSC

* Various tidyups

* Move tab

* i18n

* update .snap

* mvvm

* lint

* add header

* Remove capability check

* fix

* Rewrite to use Settings

* lint

* lint

* fix test

* Tweaks

* lint

* revert copyright

* update screenshot

* cleanup
This commit is contained in:
Will Hunt
2025-06-10 11:47:33 +01:00
committed by GitHub
parent d638691fbd
commit a333856c50
14 changed files with 450 additions and 2 deletions

View File

@@ -127,7 +127,7 @@ describe("<MatrixChat />", () => {
setGuest: jest.fn(),
setNotifTimelineSet: jest.fn(),
getAccountData: jest.fn(),
doesServerSupportUnstableFeature: jest.fn(),
doesServerSupportUnstableFeature: jest.fn().mockResolvedValue(false),
getDevices: jest.fn().mockResolvedValue({ devices: [] }),
getProfileInfo: jest.fn().mockResolvedValue({
displayname: "Ernie",

View File

@@ -0,0 +1,76 @@
/*
Copyright 2025 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import React from "react";
import { render } from "jest-matrix-react";
import userEvent from "@testing-library/user-event";
import { InviteRulesAccountSetting } from "../../../../../../../src/components/views/settings/tabs/user/InviteRulesAccountSettings";
import SettingsStore from "../../../../../../../src/settings/SettingsStore";
import { type ComputedInviteConfig } from "../../../../../../../src/@types/invite-rules";
import { SettingLevel } from "../../../../../../../src/settings/SettingLevel";
function mockSetting(mediaPreviews: ComputedInviteConfig, supported = true) {
jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName) => {
if (settingName === "inviteRules") {
return mediaPreviews;
}
throw Error(`Unexpected setting ${settingName}`);
});
jest.spyOn(SettingsStore, "disabledMessage").mockImplementation((settingName) => {
if (settingName === "inviteRules") {
return supported ? undefined : "test-not-supported";
}
throw Error(`Unexpected setting ${settingName}`);
});
}
describe("InviteRulesAccountSetting", () => {
afterEach(() => {
jest.restoreAllMocks();
});
it("does not render if not supported", async () => {
mockSetting({ allBlocked: false }, false);
const { findByText, findByLabelText } = render(<InviteRulesAccountSetting />);
const input = await findByLabelText("Allow users to invite you to rooms");
await userEvent.hover(input);
const result = await findByText("test-not-supported");
expect(result).toBeInTheDocument();
});
it("renders correct state when invites are not blocked", async () => {
mockSetting({ allBlocked: false }, true);
const { findByLabelText } = render(<InviteRulesAccountSetting />);
const result = await findByLabelText("Allow users to invite you to rooms");
expect(result).toBeChecked();
});
it("renders correct state when invites are blocked", async () => {
mockSetting({ allBlocked: true }, true);
const { findByLabelText } = render(<InviteRulesAccountSetting />);
const result = await findByLabelText("Allow users to invite you to rooms");
expect(result).not.toBeChecked();
});
it("handles disabling all invites", async () => {
mockSetting({ allBlocked: false }, true);
jest.spyOn(SettingsStore, "setValue").mockImplementation();
const { findByLabelText } = render(<InviteRulesAccountSetting />);
const result = await findByLabelText("Allow users to invite you to rooms");
await userEvent.click(result);
expect(SettingsStore.setValue).toHaveBeenCalledWith("inviteRules", null, SettingLevel.ACCOUNT, {
allBlocked: true,
});
});
it("handles enabling all invites", async () => {
mockSetting({ allBlocked: true }, true);
jest.spyOn(SettingsStore, "setValue").mockImplementation();
const { findByLabelText } = render(<InviteRulesAccountSetting />);
const result = await findByLabelText("Allow users to invite you to rooms");
await userEvent.click(result);
expect(SettingsStore.setValue).toHaveBeenCalledWith("inviteRules", null, SettingLevel.ACCOUNT, {
allBlocked: false,
});
});
});

View File

@@ -1297,6 +1297,36 @@ exports[`PreferencesUserSettingsTab should render 1`] = `
</div>
</div>
</form>
<form
class="_root_19upo_16 mx_MediaPreviewAccountSetting_Form"
>
<div
class="mx_SettingsFlag mx_MediaPreviewAccountSetting_ToggleSwitch"
>
<span
class="mx_SettingsFlag_label"
>
<div
id="mx_LabelledToggleSwitch_«re»"
>
Allow users to invite you to rooms
</div>
</span>
<div
aria-checked="true"
aria-disabled="true"
aria-label="Your server does not implement this feature."
aria-labelledby="mx_LabelledToggleSwitch_«re»"
class="mx_AccessibleButton mx_ToggleSwitch mx_ToggleSwitch_on"
role="switch"
tabindex="0"
>
<div
class="mx_ToggleSwitch_ball"
/>
</div>
</div>
</form>
</div>
<div
class="_separator_7ckbw_8"

View File

@@ -0,0 +1,156 @@
/*
Copyright 2025 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
import MatrixClientBackedController from "../../../../src/settings/controllers/MatrixClientBackedController";
import InviteRulesConfigController from "../../../../src/settings/controllers/InviteRulesConfigController";
import { SettingLevel } from "../../../../src/settings/SettingLevel";
import { getMockClientWithEventEmitter, mockClientMethodsServer } from "../../../test-utils";
import { INVITE_RULES_ACCOUNT_DATA_TYPE, type InviteConfigAccountData } from "../../../../src/@types/invite-rules";
describe("InviteRulesConfigController", () => {
afterEach(() => {
jest.restoreAllMocks();
});
it("gets the default settings when none are specified.", () => {
const controller = new InviteRulesConfigController();
MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(null),
});
const value = controller.getValueOverride(SettingLevel.ACCOUNT);
expect(value).toEqual(InviteRulesConfigController.default);
});
it("gets the default settings when the setting is empty.", () => {
const controller = new InviteRulesConfigController();
MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest
.fn()
.mockReturnValue(new MatrixEvent({ type: INVITE_RULES_ACCOUNT_DATA_TYPE, content: {} })),
});
const value = controller.getValueOverride(SettingLevel.ACCOUNT);
expect(value).toEqual(InviteRulesConfigController.default);
});
it.each<InviteConfigAccountData>([{ blocked_users: ["foo_bar"] }, { blocked_users: [] }, {}])(
"calculates blockAll to be false",
(content: InviteConfigAccountData) => {
const controller = new InviteRulesConfigController();
MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(
new MatrixEvent({
type: INVITE_RULES_ACCOUNT_DATA_TYPE,
content,
}),
),
});
const globalValue = controller.getValueOverride(SettingLevel.ACCOUNT);
expect(globalValue.allBlocked).toEqual(false);
},
);
it.each<InviteConfigAccountData>([
{ blocked_users: ["*"] },
{ blocked_users: ["*", "bob"] },
{ allowed_users: ["*"], blocked_users: ["*"] },
])("calculates blockAll to be true", (content: InviteConfigAccountData) => {
const controller = new InviteRulesConfigController();
MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(
new MatrixEvent({
type: INVITE_RULES_ACCOUNT_DATA_TYPE,
content,
}),
),
});
const globalValue = controller.getValueOverride(SettingLevel.ACCOUNT);
expect(globalValue.allBlocked).toEqual(true);
});
it("sets the account data correctly for blockAll = true", async () => {
const controller = new InviteRulesConfigController();
const client = (MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(
new MatrixEvent({
type: INVITE_RULES_ACCOUNT_DATA_TYPE,
content: {
existing_content: {},
allowed_servers: ["*"],
},
}),
),
setAccountData: jest.fn(),
}));
expect(await controller.beforeChange(SettingLevel.ACCOUNT, null, { allBlocked: true })).toBe(true);
expect(client.setAccountData).toHaveBeenCalledWith(INVITE_RULES_ACCOUNT_DATA_TYPE, {
existing_content: {},
allowed_servers: ["*"],
blocked_users: ["*"],
});
});
it("sets the account data correctly for blockAll = false", async () => {
const controller = new InviteRulesConfigController();
const client = (MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(
new MatrixEvent({
type: INVITE_RULES_ACCOUNT_DATA_TYPE,
content: {
existing_content: {},
allowed_servers: ["*"],
blocked_users: ["extra_user", "*"],
},
}),
),
setAccountData: jest.fn(),
}));
expect(await controller.beforeChange(SettingLevel.ACCOUNT, null, { allBlocked: false })).toBe(true);
expect(client.setAccountData).toHaveBeenCalledWith(INVITE_RULES_ACCOUNT_DATA_TYPE, {
existing_content: {},
allowed_servers: ["*"],
blocked_users: ["extra_user"],
});
});
it.each([true, false])("ignores a no-op when allBlocked = %s", async (allBlocked) => {
const controller = new InviteRulesConfigController();
const client = (MatrixClientBackedController.matrixClient = getMockClientWithEventEmitter({
...mockClientMethodsServer(),
getAccountData: jest.fn().mockReturnValue(
new MatrixEvent({
type: INVITE_RULES_ACCOUNT_DATA_TYPE,
content: {
existing_content: {},
allowed_servers: ["*"],
blocked_users: allBlocked ? ["*"] : [],
},
}),
),
setAccountData: jest.fn(),
}));
expect(await controller.beforeChange(SettingLevel.ACCOUNT, null, { allBlocked })).toBe(false);
expect(client.setAccountData).not.toHaveBeenCalled();
});
});