Allow knocking rooms (#11353)

Signed-off-by: Charly Nguyen <charly.nguyen@nordeck.net>
This commit is contained in:
Charly Nguyen 2023-08-07 08:27:09 +02:00 committed by GitHub
parent e6af09e424
commit 5152aad059
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 689 additions and 7 deletions

View file

@ -39,6 +39,10 @@ import {
} from "../../src/voice-broadcast";
import { mkVoiceBroadcastInfoStateEvent } from "../voice-broadcast/utils/test-utils";
import Modal from "../../src/Modal";
import ErrorDialog from "../../src/components/views/dialogs/ErrorDialog";
import { CancelAskToJoinPayload } from "../../src/dispatcher/payloads/CancelAskToJoinPayload";
import { JoinRoomErrorPayload } from "../../src/dispatcher/payloads/JoinRoomErrorPayload";
import { SubmitAskToJoinPayload } from "../../src/dispatcher/payloads/SubmitAskToJoinPayload";
jest.mock("../../src/Modal");
@ -97,6 +101,8 @@ describe("RoomViewStore", function () {
supportsThreads: jest.fn(),
isInitialSyncComplete: jest.fn().mockResolvedValue(false),
relations: jest.fn(),
knockRoom: jest.fn(),
leave: jest.fn(),
});
const room = new Room(roomId, mockClient, userId);
const room2 = new Room(roomId2, mockClient, userId);
@ -111,6 +117,21 @@ describe("RoomViewStore", function () {
await untilDispatch(Action.ViewRoom, dis);
};
const dispatchPromptAskToJoin = async () => {
dis.dispatch({ action: Action.PromptAskToJoin });
await untilDispatch(Action.PromptAskToJoin, dis);
};
const dispatchSubmitAskToJoin = async (roomId: string, reason?: string) => {
dis.dispatch<SubmitAskToJoinPayload>({ action: Action.SubmitAskToJoin, roomId, opts: { reason } });
await untilDispatch(Action.SubmitAskToJoin, dis);
};
const dispatchCancelAskToJoin = async (roomId: string) => {
dis.dispatch<CancelAskToJoinPayload>({ action: Action.CancelAskToJoin, roomId });
await untilDispatch(Action.CancelAskToJoin, dis);
};
let roomViewStore: RoomViewStore;
let slidingSyncManager: SlidingSyncManager;
let dis: MatrixDispatcher;
@ -436,4 +457,131 @@ describe("RoomViewStore", function () {
});
});
});
describe("Action.JoinRoom", () => {
it("dispatches Action.JoinRoomError and Action.AskToJoin when the join fails", async () => {
const err = new MatrixError();
jest.spyOn(dis, "dispatch");
jest.spyOn(mockClient, "joinRoom").mockRejectedValueOnce(err);
dis.dispatch({ action: Action.JoinRoom, canAskToJoin: true });
await untilDispatch(Action.PromptAskToJoin, dis);
expect(mocked(dis.dispatch).mock.calls[0][0]).toEqual({ action: "join_room", canAskToJoin: true });
expect(mocked(dis.dispatch).mock.calls[1][0]).toEqual({
action: "join_room_error",
roomId: null,
err,
canAskToJoin: true,
});
expect(mocked(dis.dispatch).mock.calls[2][0]).toEqual({ action: "prompt_ask_to_join" });
});
});
describe("Action.JoinRoomError", () => {
const err = new MatrixError();
beforeEach(() => jest.spyOn(roomViewStore, "showJoinRoomError"));
it("calls showJoinRoomError()", async () => {
dis.dispatch<JoinRoomErrorPayload>({ action: Action.JoinRoomError, roomId, err });
await untilDispatch(Action.JoinRoomError, dis);
expect(roomViewStore.showJoinRoomError).toHaveBeenCalledWith(err, roomId);
});
it("does not call showJoinRoomError() when canAskToJoin is true", async () => {
dis.dispatch<JoinRoomErrorPayload>({ action: Action.JoinRoomError, roomId, err, canAskToJoin: true });
await untilDispatch(Action.JoinRoomError, dis);
expect(roomViewStore.showJoinRoomError).not.toHaveBeenCalled();
});
});
describe("askToJoin()", () => {
it("returns false", () => {
expect(roomViewStore.promptAskToJoin()).toBe(false);
});
it("returns true", async () => {
await dispatchPromptAskToJoin();
expect(roomViewStore.promptAskToJoin()).toBe(true);
});
});
describe("knocked()", () => {
it("returns false", () => {
expect(roomViewStore.knocked()).toBe(false);
});
it("returns true", async () => {
jest.spyOn(mockClient, "knockRoom").mockResolvedValue({ room_id: roomId });
await dispatchSubmitAskToJoin(roomId);
expect(roomViewStore.knocked()).toBe(true);
});
});
describe("Action.SubmitAskToJoin", () => {
const reason = "some reason";
beforeEach(async () => await dispatchPromptAskToJoin());
it("calls knockRoom(), sets askToJoin state to false and knocked state to true", async () => {
jest.spyOn(mockClient, "knockRoom").mockResolvedValue({ room_id: roomId });
await dispatchSubmitAskToJoin(roomId, reason);
expect(mockClient.knockRoom).toHaveBeenCalledWith(roomId, { reason, viaServers: [] });
expect(roomViewStore.promptAskToJoin()).toBe(false);
expect(roomViewStore.knocked()).toBe(true);
});
it("calls knockRoom(), sets askToJoin to false, keeps knocked state false and shows an error dialog", async () => {
const error = new MatrixError(undefined, 403);
jest.spyOn(mockClient, "knockRoom").mockRejectedValue(error);
await dispatchSubmitAskToJoin(roomId, reason);
expect(mockClient.knockRoom).toHaveBeenCalledWith(roomId, { reason, viaServers: [] });
expect(roomViewStore.promptAskToJoin()).toBe(false);
expect(roomViewStore.knocked()).toBe(false);
expect(Modal.createDialog).toHaveBeenCalledWith(ErrorDialog, {
description: "You need an invite to access this room.",
title: "Failed to join",
});
});
it("shows an error dialog with a generic error message", async () => {
const error = new MatrixError();
jest.spyOn(mockClient, "knockRoom").mockRejectedValue(error);
await dispatchSubmitAskToJoin(roomId);
expect(Modal.createDialog).toHaveBeenCalledWith(ErrorDialog, {
description: error.message,
title: "Failed to join",
});
});
});
describe("Action.CancelAskToJoin", () => {
beforeEach(async () => {
jest.spyOn(mockClient, "knockRoom").mockResolvedValue({ room_id: roomId });
await dispatchSubmitAskToJoin(roomId);
});
it("calls leave() and sets knocked state to false", async () => {
jest.spyOn(mockClient, "leave").mockResolvedValue({});
await dispatchCancelAskToJoin(roomId);
expect(mockClient.leave).toHaveBeenCalledWith(roomId);
expect(roomViewStore.knocked()).toBe(false);
});
it("calls leave(), keeps knocked state true and shows an error dialog", async () => {
const error = new MatrixError();
jest.spyOn(mockClient, "leave").mockRejectedValue(error);
await dispatchCancelAskToJoin(roomId);
expect(mockClient.leave).toHaveBeenCalledWith(roomId);
expect(roomViewStore.knocked()).toBe(true);
expect(Modal.createDialog).toHaveBeenCalledWith(ErrorDialog, {
description: error.message,
title: "Failed to cancel",
});
});
});
});