-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Implement MSC4155: Invite filtering #29603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
a1d72ea
Add settings for MSC4155
Half-Shot 794695a
copyright
Half-Shot d213a22
Tweak to not use js-sdk
Half-Shot c0b881e
Merge remote-tracking branch 'origin/develop' into hs/invite-filterin…
Half-Shot acafd03
Update for latest MSC
Half-Shot 287b8b6
Merge remote-tracking branch 'origin/develop' into hs/invite-filterin…
Half-Shot 44a5fca
Various tidyups
Half-Shot ff7551a
Move tab
Half-Shot 05066d5
i18n
Half-Shot d67bdac
update .snap
Half-Shot caa29f3
mvvm
Half-Shot 0297a40
lint
Half-Shot 1d9d17c
add header
Half-Shot 5a86c7c
Merge remote-tracking branch 'origin/develop' into hs/invite-filterin…
Half-Shot 004fc1a
Remove capability check
Half-Shot 86d81e1
fix
Half-Shot f5c3d3d
Rewrite to use Settings
Half-Shot f29c03d
lint
Half-Shot f26ddaa
Merge remote-tracking branch 'origin/develop' into hs/invite-filterin…
Half-Shot d614893
lint
Half-Shot 134c7fc
fix test
Half-Shot fcb1e30
Tweaks
Half-Shot c01ca6c
lint
Half-Shot 2e895ee
Merge branch 'develop' into hs/invite-filtering-settings
Half-Shot e805aae
revert copyright
Half-Shot 79ba365
update screenshot
Half-Shot 793fe53
cleanup
Half-Shot 30e2475
Merge branch 'develop' into hs/invite-filtering-settings
Half-Shot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file modified
BIN
+4.02 KB
(100%)
...b.spec.ts/Preferences-user-settings-tab-should-be-rendered-properly-1-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
export const INVITE_RULES_ACCOUNT_DATA_TYPE = "org.matrix.msc4155.invite_permission_config"; | ||
|
||
export interface InviteConfigAccountData { | ||
allowed_users?: string[]; | ||
blocked_users?: string[]; | ||
ignored_users?: string[]; | ||
allowed_servers?: string[]; | ||
blocked_servers?: string[]; | ||
ignored_servers?: string[]; | ||
} | ||
|
||
/** | ||
* Computed values based on MSC4155. Currently Element Web only supports | ||
* blocking all invites. | ||
*/ | ||
export interface ComputedInviteConfig extends Record<string, unknown> { | ||
/** | ||
* Are all invites blocked. This is only about blocking all invites, | ||
* but this being false may still block invites through other rules. | ||
*/ | ||
allBlocked: boolean; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/components/views/settings/tabs/user/InviteRulesAccountSettings.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
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 FC, useCallback, useState } from "react"; | ||
import { Root } from "@vector-im/compound-web"; | ||
import { logger } from "matrix-js-sdk/src/logger"; | ||
|
||
import { _t } from "../../../../../languageHandler"; | ||
import { useSettingValue } from "../../../../../hooks/useSettings"; | ||
import SettingsStore from "../../../../../settings/SettingsStore"; | ||
import { SettingLevel } from "../../../../../settings/SettingLevel"; | ||
import LabelledToggleSwitch from "../../../elements/LabelledToggleSwitch"; | ||
|
||
export const InviteRulesAccountSetting: FC = () => { | ||
const rules = useSettingValue("inviteRules"); | ||
const settingsDisabled = SettingsStore.disabledMessage("inviteRules"); | ||
const [busy, setBusy] = useState(false); | ||
|
||
const onChange = useCallback(async (checked: boolean) => { | ||
try { | ||
setBusy(true); | ||
await SettingsStore.setValue("inviteRules", null, SettingLevel.ACCOUNT, { | ||
allBlocked: !checked, | ||
}); | ||
} catch (ex) { | ||
logger.error(`Unable to set invite rules`, ex); | ||
} finally { | ||
setBusy(false); | ||
} | ||
}, []); | ||
return ( | ||
<Root className="mx_MediaPreviewAccountSetting_Form"> | ||
<LabelledToggleSwitch | ||
className="mx_MediaPreviewAccountSetting_ToggleSwitch" | ||
label={_t("settings|invite_controls|default_label")} | ||
value={!rules.allBlocked} | ||
onChange={onChange} | ||
tooltip={settingsDisabled} | ||
disabled={!!settingsDisabled || busy} | ||
/> | ||
</Root> | ||
); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
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 { type MatrixClient, type IContent } from "matrix-js-sdk/src/matrix"; | ||
|
||
import { type SettingLevel } from "../SettingLevel.ts"; | ||
import MatrixClientBackedController from "./MatrixClientBackedController.ts"; | ||
import { | ||
type ComputedInviteConfig as ComputedInviteRules, | ||
INVITE_RULES_ACCOUNT_DATA_TYPE, | ||
type InviteConfigAccountData, | ||
} from "../../@types/invite-rules.ts"; | ||
import { _t } from "../../languageHandler.tsx"; | ||
|
||
/** | ||
* Handles invite filtering rules provided by MSC4155. | ||
* This handler does not make use of the roomId parameter. | ||
*/ | ||
export default class InviteRulesConfigController extends MatrixClientBackedController { | ||
public static readonly default: ComputedInviteRules = { | ||
allBlocked: false, | ||
}; | ||
|
||
private static getValidSettingData(content: IContent): ComputedInviteRules { | ||
const expectedConfig = content as InviteConfigAccountData; | ||
return { | ||
allBlocked: !!expectedConfig.blocked_users?.includes("*"), | ||
}; | ||
} | ||
|
||
public initMatrixClient(newClient: MatrixClient): void { | ||
newClient.doesServerSupportUnstableFeature("org.matrix.msc4155").then((result) => { | ||
this.featureSupported = result; | ||
}); | ||
} | ||
|
||
public featureSupported?: boolean; | ||
|
||
public constructor() { | ||
super(); | ||
this.featureSupported = false; | ||
} | ||
MidhunSureshR marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private getValue = (): ComputedInviteRules => { | ||
const accountData = | ||
this.client?.getAccountData(INVITE_RULES_ACCOUNT_DATA_TYPE)?.getContent<InviteConfigAccountData>() ?? {}; | ||
return InviteRulesConfigController.getValidSettingData(accountData); | ||
}; | ||
|
||
public getValueOverride(_level: SettingLevel): ComputedInviteRules { | ||
return this.getValue(); | ||
} | ||
|
||
public get settingDisabled(): true | string { | ||
return this.featureSupported ? true : _t("settings|not_supported"); | ||
} | ||
|
||
public async beforeChange( | ||
_level: SettingLevel, | ||
_roomId: string | null, | ||
newValue: ComputedInviteRules, | ||
): Promise<boolean> { | ||
if (!this.client) { | ||
return false; | ||
} | ||
const existingContent = this.client | ||
.getAccountData(INVITE_RULES_ACCOUNT_DATA_TYPE) | ||
?.getContent<InviteConfigAccountData>(); | ||
const newContent: InviteConfigAccountData = { | ||
...existingContent, | ||
blocked_users: [...(existingContent?.blocked_users ?? [])], | ||
}; | ||
if (newValue.allBlocked && !newContent.blocked_users!.includes("*")) { | ||
newContent.blocked_users!.push("*"); | ||
} else if (!newValue.allBlocked && newContent.blocked_users?.includes("*")) { | ||
newContent.blocked_users = newContent.blocked_users.filter((u) => u !== "*"); | ||
} else { | ||
// No changes required. | ||
return false; | ||
} | ||
await this.client.setAccountData(INVITE_RULES_ACCOUNT_DATA_TYPE, newContent); | ||
return true; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
test/unit-tests/components/views/settings/tabs/user/InviteRulesAccountSetting-test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was explicitly asked to be part of the M&S section by product.