Allow creating knock rooms (#11182)

Signed-off-by: Charly Nguyen <charly.nguyen@nordeck.net>
This commit is contained in:
Charly Nguyen 2023-07-10 10:01:03 +02:00 committed by GitHub
parent 01bd80fe59
commit fd749172e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 197 additions and 3 deletions

View file

@ -36,6 +36,14 @@ describe("doesRoomVersionSupport", () => {
expect(doesRoomVersionSupport("3.1", "2.2")).toBe(true); // newer
});
it("should detect knock rooms in v7 and above", () => {
expect(doesRoomVersionSupport("6", PreferredRoomVersions.KnockRooms)).toBe(false);
expect(doesRoomVersionSupport("7", PreferredRoomVersions.KnockRooms)).toBe(true);
expect(doesRoomVersionSupport("8", PreferredRoomVersions.KnockRooms)).toBe(true);
expect(doesRoomVersionSupport("9", PreferredRoomVersions.KnockRooms)).toBe(true);
expect(doesRoomVersionSupport("10", PreferredRoomVersions.KnockRooms)).toBe(true);
});
it("should detect restricted rooms in v9 and v10", () => {
// Dev note: we consider it a feature that v8 rooms have to upgrade considering the bug in v8.
// https://spec.matrix.org/v1.3/rooms/v8/#redactions

View file

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import { EventType, MatrixClient, MatrixEvent, Room, RoomMember } from "matrix-js-sdk/src/matrix";
import { EventType, JoinRule, MatrixClient, MatrixEvent, Room, RoomMember } from "matrix-js-sdk/src/matrix";
import { render } from "@testing-library/react";
import { ReactElement } from "react";
import { Mocked, mocked } from "jest-mock";
@ -512,4 +512,63 @@ describe("TextForEvent", () => {
).toMatchInlineSnapshot(`"Andy changed their display name and profile picture"`);
});
});
describe("textForJoinRulesEvent()", () => {
type TestCase = [string, { result: string }];
const testCases: TestCase[] = [
[JoinRule.Public, { result: "@a made the room public to whoever knows the link." }],
[JoinRule.Invite, { result: "@a made the room invite only." }],
[JoinRule.Knock, { result: "@a changed the join rule to ask to join." }],
[JoinRule.Restricted, { result: "@a changed who can join this room." }],
];
it.each(testCases)("returns correct message when room join rule changed to %s", (joinRule, { result }) => {
expect(
textForEvent(
new MatrixEvent({
type: "m.room.join_rules",
sender: "@a",
content: {
join_rule: joinRule,
},
state_key: "",
}),
mockClient,
),
).toEqual(result);
});
it(`returns correct JSX message when room join rule changed to ${JoinRule.Restricted}`, () => {
expect(
textForEvent(
new MatrixEvent({
type: "m.room.join_rules",
sender: "@a",
content: {
join_rule: JoinRule.Restricted,
},
state_key: "",
}),
mockClient,
true,
),
).toMatchSnapshot();
});
it("returns correct default message", () => {
expect(
textForEvent(
new MatrixEvent({
type: "m.room.join_rules",
sender: "@a",
content: {
join_rule: "a not implemented one",
},
state_key: "",
}),
mockClient,
),
).toEqual("@a changed the join rule to a not implemented one");
});
});
});

View file

@ -0,0 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`TextForEvent textForJoinRulesEvent() returns correct JSX message when room join rule changed to restricted 1`] = `
<span>
<span>
@a changed who can join this room.
<AccessibleButton
kind="link_inline"
onClick={[Function]}
role="button"
tabIndex={0}
>
View settings
</AccessibleButton>
.
</span>
</span>
`;

View file

@ -16,10 +16,11 @@ limitations under the License.
import React from "react";
import { fireEvent, render, screen, within } from "@testing-library/react";
import { MatrixError, Preset, Visibility } from "matrix-js-sdk/src/matrix";
import { JoinRule, MatrixError, Preset, Visibility } from "matrix-js-sdk/src/matrix";
import CreateRoomDialog from "../../../../src/components/views/dialogs/CreateRoomDialog";
import { flushPromises, getMockClientWithEventEmitter, mockClientMethodsUser } from "../../../test-utils";
import SettingsStore from "../../../../src/settings/SettingsStore";
describe("<CreateRoomDialog />", () => {
const userId = "@alice:server.org";
@ -208,6 +209,50 @@ describe("<CreateRoomDialog />", () => {
});
});
describe("for a knock room", () => {
it("should not have the option to create a knock room", async () => {
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
getComponent();
fireEvent.click(screen.getByLabelText("Room visibility"));
expect(screen.queryByRole("option", { name: "Ask to join" })).not.toBeInTheDocument();
});
it("should create a knock room", async () => {
jest.spyOn(SettingsStore, "getValue").mockImplementation((setting) => setting === "feature_ask_to_join");
const onFinished = jest.fn();
getComponent({ onFinished });
await flushPromises();
const roomName = "Test Room Name";
fireEvent.change(screen.getByLabelText("Name"), { target: { value: roomName } });
fireEvent.click(screen.getByLabelText("Room visibility"));
fireEvent.click(screen.getByRole("option", { name: "Ask to join" }));
fireEvent.click(screen.getByText("Create room"));
await flushPromises();
expect(screen.getByText("Create a room")).toBeInTheDocument();
expect(
screen.getByText(
"Anyone can request to join, but admins or moderators need to grant access. You can change this later.",
),
).toBeInTheDocument();
expect(onFinished).toHaveBeenCalledWith(true, {
createOpts: {
name: roomName,
},
encryption: true,
joinRule: JoinRule.Knock,
parentSpace: undefined,
roomType: undefined,
});
});
});
describe("for a public room", () => {
it("should set join rule to public defaultPublic is truthy", async () => {
const onFinished = jest.fn();