Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a1d72ea
Add settings for MSC4155
Half-Shot Mar 27, 2025
794695a
copyright
Half-Shot Mar 27, 2025
d213a22
Tweak to not use js-sdk
Half-Shot Mar 27, 2025
c0b881e
Merge remote-tracking branch 'origin/develop' into hs/invite-filterin…
Half-Shot May 29, 2025
acafd03
Update for latest MSC
Half-Shot May 29, 2025
287b8b6
Merge remote-tracking branch 'origin/develop' into hs/invite-filterin…
Half-Shot Jun 2, 2025
44a5fca
Various tidyups
Half-Shot Jun 2, 2025
ff7551a
Move tab
Half-Shot Jun 2, 2025
05066d5
i18n
Half-Shot Jun 2, 2025
d67bdac
update .snap
Half-Shot Jun 2, 2025
caa29f3
mvvm
Half-Shot Jun 2, 2025
0297a40
lint
Half-Shot Jun 2, 2025
1d9d17c
add header
Half-Shot Jun 3, 2025
5a86c7c
Merge remote-tracking branch 'origin/develop' into hs/invite-filterin…
Half-Shot Jun 3, 2025
004fc1a
Remove capability check
Half-Shot Jun 6, 2025
86d81e1
fix
Half-Shot Jun 6, 2025
f5c3d3d
Rewrite to use Settings
Half-Shot Jun 9, 2025
f29c03d
lint
Half-Shot Jun 9, 2025
f26ddaa
Merge remote-tracking branch 'origin/develop' into hs/invite-filterin…
Half-Shot Jun 9, 2025
d614893
lint
Half-Shot Jun 9, 2025
134c7fc
fix test
Half-Shot Jun 9, 2025
fcb1e30
Tweaks
Half-Shot Jun 9, 2025
c01ca6c
lint
Half-Shot Jun 9, 2025
2e895ee
Merge branch 'develop' into hs/invite-filtering-settings
Half-Shot Jun 9, 2025
e805aae
revert copyright
Half-Shot Jun 9, 2025
79ba365
update screenshot
Half-Shot Jun 9, 2025
793fe53
cleanup
Half-Shot Jun 10, 2025
30e2475
Merge branch 'develop' into hs/invite-filtering-settings
Half-Shot Jun 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/@types/matrix-js-sdk.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ declare module "matrix-js-sdk/src/types" {
};
}

export interface InviteConfigAccountData {
allowed_users?: string[];
blocked_users?: string[];
ignored_users?: string[];
allowed_servers?: string[];
blocked_servers?: string[];
ignored_servers?: string[];
}

export interface AccountDataEvents {
// Analytics account data event
"im.vector.analytics": {
Expand Down Expand Up @@ -89,6 +98,8 @@ declare module "matrix-js-sdk/src/types" {
accepted: string[];
};

// MSC4155: Invite filtering
"org.matrix.msc4155.invite_permission_config": InviteConfigAccountData;
"io.element.msc4278.media_preview_config": MediaPreviewConfig;
}

Expand Down
108 changes: 108 additions & 0 deletions src/components/views/settings/InviteControlsPanel.tsx
Comment thread
Half-Shot marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
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, { type ChangeEventHandler, type FC, useCallback, useEffect, useMemo, useState } from "react";
import { type AccountDataEvents } from "matrix-js-sdk/src/types";
import { ErrorMessage, InlineField, Label, Root, ToggleInput, Tooltip } from "@vector-im/compound-web";
import { logger } from "matrix-js-sdk/src/logger";

import { SettingsSubsection } from "./shared/SettingsSubsection";
import { _t } from "../../../languageHandler";
import { useAccountData } from "../../../hooks/useAccountData";
import { useMatrixClientContext } from "../../../contexts/MatrixClientContext";

export const InviteControlsPanel: FC = () => {
const client = useMatrixClientContext();
const [hasError, setHasError] = useState(false);
const [busy, setBusy] = useState(false);
const [canUse, setCanUse] = useState<boolean>();
const inviteConfig = useAccountData<AccountDataEvents["org.matrix.msc4155.invite_permission_config"]>(
client,
"org.matrix.msc4155.invite_permission_config",
);

useEffect(() => {
(async () => {
setCanUse(await client.doesServerSupportUnstableFeature("org.matrix.msc4155"));
})();
}, [client]);
Comment thread
Half-Shot marked this conversation as resolved.
Outdated

// This implements a very basic version of MSC4155 that simply allows
// or disallows all invites by setting a simple glob.
// Keep in mind that users may configure more powerful rules on other
// clients and we should keep those intact.
const isBlockingAll = useMemo(() => {
if (!inviteConfig) {
return false;
}
return inviteConfig["blocked_users"]?.includes("*") === true;
}, [inviteConfig]);

const setValue = useCallback<ChangeEventHandler<HTMLInputElement>>(
async (e) => {
e.preventDefault();
setHasError(false);
setBusy(true);
const newConfig = { ...inviteConfig };
if (newConfig["blocked_users"]?.includes("*")) {
newConfig.blocked_users = newConfig.blocked_users.filter((u) => u !== "*");
} else {
newConfig.blocked_users = [...new Set([...(newConfig.blocked_users ?? []), "*"])];
}
try {
await client.setAccountData("org.matrix.msc4155.invite_permission_config", newConfig);
} catch (ex) {
logger.error("Could not change input config", ex);
setHasError(true);
} finally {
setBusy(false);
}
},
[client, inviteConfig],
);

let content;
if (canUse) {
content = (
<>
<InlineField
name="default"
control={
<ToggleInput
id="mx_invite_controls_default"
disabled={busy || !canUse}
onChange={setValue}
checked={!isBlockingAll}
/>
}
>
<Label htmlFor="mx_invite_controls_default">{_t("settings|invite_controls|default_label")}</Label>
{hasError && <ErrorMessage>{_t("settings|invite_controls|error_message")}</ErrorMessage>}
</InlineField>
</>
);
} else if (canUse === false) {
content = (
<Tooltip description={_t("settings|invite_controls|not_supported")}>
<InlineField
name="default"
control={<ToggleInput id="mx_invite_controls_default" disabled={true} checked={!isBlockingAll} />}
>
<Label htmlFor="mx_invite_controls_default">{_t("settings|invite_controls|default_label")}</Label>
</InlineField>
</Tooltip>
);
} else {
return;
}

return (
<SettingsSubsection heading={_t("settings|invite_controls|title")}>
<Root>{content}</Root>
</SettingsSubsection>
);
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2024 New Vector Ltd.
Copyright 2024,2025 New Vector Ltd.
Copyright 2019-2023 The Matrix.org Foundation C.I.C.

SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
Expand Down Expand Up @@ -32,6 +32,7 @@ import { SettingsSubsection, SettingsSubsectionText } from "../../shared/Setting
import { useOwnDevices } from "../../devices/useOwnDevices";
import { DiscoverySettings } from "../../discovery/DiscoverySettings";
import SetIntegrationManager from "../../SetIntegrationManager";
import { InviteControlsPanel } from "../../InviteControlsPanel";

interface IIgnoredUserProps {
userId: string;
Expand Down Expand Up @@ -362,6 +363,7 @@ export default class SecurityUserSettingsTab extends React.Component<IProps, ISt
{eventIndex}
</SettingsSection>
<SettingsSection heading={_t("common|privacy")}>
<InviteControlsPanel />
<DiscoverySettings />
{posthogSection}
</SettingsSection>
Expand Down
6 changes: 6 additions & 0 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -2688,6 +2688,12 @@
"inline_url_previews_room": "Enable URL previews by default for participants in this room",
"inline_url_previews_room_account": "Enable URL previews for this room (only affects you)",
"insert_trailing_colon_mentions": "Insert a trailing colon after user mentions at the start of a message",
"invite_controls": {
"default_label": "Allow users to invite you to rooms",
"error_message": "An error occured while trying to change this setting",
"not_supported": "Your server does not implement this feature.",
"title": "Invite controls"
},
"jump_to_bottom_on_send": "Jump to the bottom of the timeline when you send a message",
"key_backup": {
"backup_in_progress": "Your keys are being backed up (the first backup could take a few minutes).",
Expand Down
138 changes: 138 additions & 0 deletions test/unit-tests/components/views/settings/InviteControlsPanel-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
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 { type AccountDataEvents } from "matrix-js-sdk/src/types";
import { ClientEvent, MatrixEvent } from "matrix-js-sdk/src/matrix";
import userEvent from "@testing-library/user-event";

import { stubClient } from "../../../../test-utils";
import { InviteControlsPanel } from "../../../../../src/components/views/settings/InviteControlsPanel";
import MatrixClientContext from "../../../../../src/contexts/MatrixClientContext";

describe("InviteControlsPanel", () => {
it("does not render if not supported", async () => {
const client = stubClient();
client.getAccountData = jest.fn().mockReturnValue(undefined);
client.doesServerSupportUnstableFeature = jest.fn().mockResolvedValue(false);
const { findByText, findByLabelText } = render(
<MatrixClientContext.Provider value={client}>
<InviteControlsPanel />
</MatrixClientContext.Provider>,
);
const input = await findByLabelText("Allow users to invite you to rooms");
await userEvent.hover(input);
const result = await findByText("Your server does not implement this feature.");
expect(result).toBeInTheDocument();
});
it("renders correct state when no value is present", async () => {
const client = stubClient();
client.getAccountData = jest.fn().mockReturnValue(undefined);
client.doesServerSupportUnstableFeature = jest.fn().mockImplementation(async (v) => v === "org.matrix.msc4155");
const { findByLabelText } = render(
<MatrixClientContext.Provider value={client}>
<InviteControlsPanel />
</MatrixClientContext.Provider>,
);
const result = await findByLabelText("Allow users to invite you to rooms");
expect((result as HTMLInputElement).checked).toEqual(true);
});
it.each([{}, { blocked_users: ["some"] }, { blocked_users: [" *"] }])(
"renders correct state when permissive values are present",
async (eventData: AccountDataEvents["org.matrix.msc4155.invite_permission_config"]) => {
const client = stubClient();
client.getAccountData = jest
.fn()
.mockImplementation((v) =>
v === "org.matrix.msc4155.invite_permission_config"
? new MatrixEvent({ content: eventData })
: undefined,
);
client.doesServerSupportUnstableFeature = jest
.fn()
.mockImplementation(async (v) => v === "org.matrix.msc4155");
const { findByLabelText } = render(
<MatrixClientContext.Provider value={client}>
<InviteControlsPanel />
</MatrixClientContext.Provider>,
);
const result = await findByLabelText("Allow users to invite you to rooms");
expect((result as HTMLInputElement).checked).toEqual(true);
},
);
it("renders correct state when invites are blocked", async () => {
const client = stubClient();
client.getAccountData = jest
.fn()
.mockImplementation((v) =>
v === "org.matrix.msc4155.invite_permission_config"
? new MatrixEvent({ content: { blocked_users: "*" } })
: undefined,
);
client.doesServerSupportUnstableFeature = jest.fn().mockImplementation(async (v) => v === "org.matrix.msc4155");
const { findByLabelText } = render(
<MatrixClientContext.Provider value={client}>
<InviteControlsPanel />
</MatrixClientContext.Provider>,
);
const result = await findByLabelText("Allow users to invite you to rooms");
expect((result as HTMLInputElement).checked).toEqual(false);
});
it("handles disabling all invites", async () => {
const client = stubClient();
const setAccountData = (client.setAccountData = jest.fn().mockImplementation((type, content) => {
client.emit(ClientEvent.AccountData, new MatrixEvent({ type, content }));
}));
client.getAccountData = jest
.fn()
.mockImplementation((v) =>
v === "org.matrix.msc4155.invite_permission_config"
? new MatrixEvent({ content: { blocked_users: ["other_rules"], foo_bar: true } })
: undefined,
);
client.doesServerSupportUnstableFeature = jest.fn().mockImplementation(async (v) => v === "org.matrix.msc4155");
const { findByLabelText } = render(
<MatrixClientContext.Provider value={client}>
<InviteControlsPanel />
</MatrixClientContext.Provider>,
);
const result = await findByLabelText("Allow users to invite you to rooms");
await userEvent.click(result);
// Preserves other rules that might already be configured.
expect(setAccountData).toHaveBeenCalledWith("org.matrix.msc4155.invite_permission_config", {
blocked_users: ["other_rules", "*"],
foo_bar: true,
});
expect((result as HTMLInputElement).checked).toEqual(false);
});
it("handles enabling invites", async () => {
const client = stubClient();
const setAccountData = (client.setAccountData = jest.fn().mockImplementation((type, content) => {
client.emit(ClientEvent.AccountData, new MatrixEvent({ type, content }));
}));
client.getAccountData = jest
.fn()
.mockImplementation((v) =>
v === "org.matrix.msc4155.invite_permission_config"
? new MatrixEvent({ content: { blocked_users: ["*", "other_rules"], foo_bar: true } })
: undefined,
);
client.doesServerSupportUnstableFeature = jest.fn().mockImplementation(async (v) => v === "org.matrix.msc4155");
const { findByLabelText } = render(
<MatrixClientContext.Provider value={client}>
<InviteControlsPanel />
</MatrixClientContext.Provider>,
);
const result = await findByLabelText("Allow users to invite you to rooms");
await userEvent.click(result);
expect(setAccountData).toHaveBeenCalledWith("org.matrix.msc4155.invite_permission_config", {
blocked_users: ["other_rules"],
foo_bar: true,
});
expect((result as HTMLInputElement).checked).toEqual(true);
});
});
Loading