Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 9b2da3f

Browse files
author
Charly Nguyen
committed
Allow creating knock rooms
Signed-off-by: Charly Nguyen <charly.nguyen@nordeck.net>
1 parent 90b572f commit 9b2da3f

File tree

10 files changed

+108
-2
lines changed

10 files changed

+108
-2
lines changed

res/css/views/dialogs/_JoinRuleDropdown.pcss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ limitations under the License.
5454
padding: 1px;
5555
}
5656

57+
.mx_JoinRuleDropdown_knock::before {
58+
mask-image: url("$(res)/img/element-icons/hand.svg");
59+
mask-size: contain;
60+
}
61+
5762
.mx_JoinRuleDropdown_public::before {
5863
mask-image: url("$(res)/img/globe.svg");
5964
mask-size: 12px;

res/img/element-icons/hand.svg

Lines changed: 1 addition & 0 deletions
Loading

src/components/views/dialogs/CreateRoomDialog.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import JoinRuleDropdown from "../elements/JoinRuleDropdown";
3434
import { getKeyBindingsManager } from "../../../KeyBindingsManager";
3535
import { KeyBindingAction } from "../../../accessibility/KeyboardShortcuts";
3636
import { privateShouldBeEncrypted } from "../../../utils/rooms";
37+
import SettingsStore from "../../../settings/SettingsStore";
3738

3839
interface IProps {
3940
type?: RoomType;
@@ -59,13 +60,15 @@ interface IState {
5960
}
6061

6162
export default class CreateRoomDialog extends React.Component<IProps, IState> {
63+
private readonly knockingEnabled: boolean;
6264
private readonly supportsRestricted: boolean;
6365
private nameField = createRef<Field>();
6466
private aliasField = createRef<RoomAliasField>();
6567

6668
public constructor(props: IProps) {
6769
super(props);
6870

71+
this.knockingEnabled = SettingsStore.getValue("feature_knocking");
6972
this.supportsRestricted = !!this.props.parentSpace;
7073

7174
let joinRule = JoinRule.Invite;
@@ -126,6 +129,10 @@ export default class CreateRoomDialog extends React.Component<IProps, IState> {
126129
opts.joinRule = JoinRule.Restricted;
127130
}
128131

132+
if (this.state.joinRule === JoinRule.Knock) {
133+
opts.joinRule = JoinRule.Knock;
134+
}
135+
129136
return opts;
130137
}
131138

@@ -283,6 +290,14 @@ export default class CreateRoomDialog extends React.Component<IProps, IState> {
283290
{_t("You can change this at any time from room settings.")}
284291
</p>
285292
);
293+
} else if (this.state.joinRule === JoinRule.Knock) {
294+
publicPrivateLabel = (
295+
<p>
296+
{_t(
297+
"Anyone can request to join, but admins or moderators need to grant access. You can change this later.",
298+
)}
299+
</p>
300+
);
286301
}
287302

288303
let e2eeSection: JSX.Element | undefined;
@@ -332,7 +347,7 @@ export default class CreateRoomDialog extends React.Component<IProps, IState> {
332347
let title: string;
333348
if (isVideoRoom) {
334349
title = _t("Create a video room");
335-
} else if (this.props.parentSpace) {
350+
} else if (this.props.parentSpace || this.state.joinRule === JoinRule.Knock) {
336351
title = _t("Create a room");
337352
} else {
338353
title = this.state.joinRule === JoinRule.Public ? _t("Create a public room") : _t("Create a private room");
@@ -365,6 +380,7 @@ export default class CreateRoomDialog extends React.Component<IProps, IState> {
365380
<JoinRuleDropdown
366381
label={_t("Room visibility")}
367382
labelInvite={_t("Private room (invite only)")}
383+
labelKnock={this.knockingEnabled ? _t("Ask to join room") : undefined}
368384
labelPublic={_t("Public room")}
369385
labelRestricted={this.supportsRestricted ? _t("Visible to space members") : undefined}
370386
value={this.state.joinRule}

src/components/views/elements/JoinRuleDropdown.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ interface IProps {
2525
label: string;
2626
width?: number;
2727
labelInvite: string;
28+
labelKnock?: string;
2829
labelPublic: string;
2930
labelRestricted?: string; // if omitted then this option will be hidden, e.g if unsupported
3031
onChange(value: JoinRule): void;
@@ -33,6 +34,7 @@ interface IProps {
3334
const JoinRuleDropdown: React.FC<IProps> = ({
3435
label,
3536
labelInvite,
37+
labelKnock,
3638
labelPublic,
3739
labelRestricted,
3840
value,
@@ -48,6 +50,16 @@ const JoinRuleDropdown: React.FC<IProps> = ({
4850
</div>,
4951
] as NonEmptyArray<ReactElement & { key: string }>;
5052

53+
if (labelKnock) {
54+
options.unshift(
55+
(
56+
<div key={JoinRule.Knock} className="mx_JoinRuleDropdown_knock">
57+
{labelKnock}
58+
</div>
59+
) as ReactElement & { key: string },
60+
);
61+
}
62+
5163
if (labelRestricted) {
5264
options.unshift(
5365
(

src/createRoom.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ export default async function createRoom(client: MatrixClient, opts: IOpts): Pro
222222
});
223223
}
224224

225+
if (opts.joinRule === JoinRule.Knock) {
226+
createOpts.room_version = PreferredRoomVersions.KnockRooms;
227+
}
228+
225229
if (opts.parentSpace) {
226230
createOpts.initial_state.push(makeSpaceParentEvent(opts.parentSpace, true));
227231
if (!opts.historyVisibility) {

src/i18n/strings/en_EN.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,7 @@
999999
"Insert a trailing colon after user mentions at the start of a message": "Insert a trailing colon after user mentions at the start of a message",
10001000
"Hide notification dot (only display counters badges)": "Hide notification dot (only display counters badges)",
10011001
"Enable intentional mentions": "Enable intentional mentions",
1002+
"Enable knocking": "Enable knocking",
10021003
"Use a more compact 'Modern' layout": "Use a more compact 'Modern' layout",
10031004
"Show a placeholder for removed messages": "Show a placeholder for removed messages",
10041005
"Show join/leave messages (invites/removes/bans unaffected)": "Show join/leave messages (invites/removes/bans unaffected)",
@@ -2780,6 +2781,7 @@
27802781
"Anyone will be able to find and join this room, not just members of <SpaceName/>.": "Anyone will be able to find and join this room, not just members of <SpaceName/>.",
27812782
"Anyone will be able to find and join this room.": "Anyone will be able to find and join this room.",
27822783
"Only people invited will be able to find and join this room.": "Only people invited will be able to find and join this room.",
2784+
"Anyone can request to join, but admins or moderators need to grant access. You can change this later.": "Anyone can request to join, but admins or moderators need to grant access. You can change this later.",
27832785
"You can't disable this later. The room will be encrypted but the embedded call will not.": "You can't disable this later. The room will be encrypted but the embedded call will not.",
27842786
"You can't disable this later. Bridges & most bots won't work yet.": "You can't disable this later. Bridges & most bots won't work yet.",
27852787
"Your server requires encryption to be enabled in private rooms.": "Your server requires encryption to be enabled in private rooms.",
@@ -2793,6 +2795,7 @@
27932795
"Topic (optional)": "Topic (optional)",
27942796
"Room visibility": "Room visibility",
27952797
"Private room (invite only)": "Private room (invite only)",
2798+
"Ask to join room": "Ask to join room",
27962799
"Visible to space members": "Visible to space members",
27972800
"Block anyone not part of %(serverName)s from ever joining this room.": "Block anyone not part of %(serverName)s from ever joining this room.",
27982801
"Create video room": "Create video room",

src/settings/Settings.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,13 @@ export const SETTINGS: { [setting: string]: ISetting } = {
547547
["org.matrix.msc3952_intentional_mentions"],
548548
]),
549549
},
550+
"feature_knocking": {
551+
default: false,
552+
displayName: _td("Enable knocking"),
553+
isFeature: true,
554+
labsGroup: LabGroup.Rooms,
555+
supportedLevels: LEVELS_FEATURE,
556+
},
550557
"useCompactLayout": {
551558
supportedLevels: LEVELS_DEVICE_ONLY_SETTINGS,
552559
displayName: _td("Use a more compact 'Modern' layout"),

src/utils/PreferredRoomVersions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ limitations under the License.
2323
* Loosely follows https://spec.matrix.org/latest/rooms/#feature-matrix
2424
*/
2525
export class PreferredRoomVersions {
26+
/**
27+
* The room version to use when creating "knock" rooms.
28+
*/
29+
public static readonly KnockRooms = "7";
30+
2631
/**
2732
* The room version to use when creating "restricted" rooms.
2833
*/

test/PreferredRoomVersions-test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ describe("doesRoomVersionSupport", () => {
3636
expect(doesRoomVersionSupport("3.1", "2.2")).toBe(true); // newer
3737
});
3838

39+
it("should detect knock rooms in v7 and above", () => {
40+
expect(doesRoomVersionSupport("6", PreferredRoomVersions.KnockRooms)).toBe(false);
41+
expect(doesRoomVersionSupport("7", PreferredRoomVersions.KnockRooms)).toBe(true);
42+
expect(doesRoomVersionSupport("8", PreferredRoomVersions.KnockRooms)).toBe(true);
43+
expect(doesRoomVersionSupport("9", PreferredRoomVersions.KnockRooms)).toBe(true);
44+
expect(doesRoomVersionSupport("10", PreferredRoomVersions.KnockRooms)).toBe(true);
45+
});
46+
3947
it("should detect restricted rooms in v9 and v10", () => {
4048
// Dev note: we consider it a feature that v8 rooms have to upgrade considering the bug in v8.
4149
// https://spec.matrix.org/v1.3/rooms/v8/#redactions

test/components/views/dialogs/CreateRoomDialog-test.tsx

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ limitations under the License.
1616

1717
import React from "react";
1818
import { fireEvent, render, screen, within } from "@testing-library/react";
19-
import { Preset, Visibility } from "matrix-js-sdk/src/matrix";
19+
import { JoinRule, Preset, Visibility } from "matrix-js-sdk/src/matrix";
2020

2121
import CreateRoomDialog from "../../../../src/components/views/dialogs/CreateRoomDialog";
2222
import { flushPromises, getMockClientWithEventEmitter, mockClientMethodsUser } from "../../../test-utils";
23+
import SettingsStore from "../../../../src/settings/SettingsStore";
2324

2425
describe("<CreateRoomDialog />", () => {
2526
const userId = "@alice:server.org";
@@ -208,6 +209,50 @@ describe("<CreateRoomDialog />", () => {
208209
});
209210
});
210211

212+
describe("for a knock room", () => {
213+
it("should not have the option to create a knock room", async () => {
214+
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
215+
getComponent();
216+
fireEvent.click(screen.getByLabelText("Room visibility"));
217+
218+
expect(screen.queryByRole("option", { name: "Ask to join room" })).not.toBeInTheDocument();
219+
});
220+
221+
it("should create a knock room", async () => {
222+
jest.spyOn(SettingsStore, "getValue").mockImplementation((setting) => setting === "feature_knocking");
223+
const onFinished = jest.fn();
224+
getComponent({ onFinished });
225+
await flushPromises();
226+
227+
const roomName = "Test Room Name";
228+
fireEvent.change(screen.getByLabelText("Name"), { target: { value: roomName } });
229+
230+
fireEvent.click(screen.getByLabelText("Room visibility"));
231+
fireEvent.click(screen.getByRole("option", { name: "Ask to join room" }));
232+
233+
fireEvent.click(screen.getByText("Create room"));
234+
await flushPromises();
235+
236+
expect(screen.getByText("Create a room")).toBeInTheDocument();
237+
238+
expect(
239+
screen.getByText(
240+
"Anyone can request to join, but admins or moderators need to grant access. You can change this later.",
241+
),
242+
).toBeInTheDocument();
243+
244+
expect(onFinished).toHaveBeenCalledWith(true, {
245+
createOpts: {
246+
name: roomName,
247+
},
248+
encryption: true,
249+
joinRule: JoinRule.Knock,
250+
parentSpace: undefined,
251+
roomType: undefined,
252+
});
253+
});
254+
});
255+
211256
describe("for a public room", () => {
212257
it("should set join rule to public defaultPublic is truthy", async () => {
213258
const onFinished = jest.fn();

0 commit comments

Comments
 (0)