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

Commit

Permalink
Use knock rooms sync to reflect the knock state
Browse files Browse the repository at this point in the history
Signed-off-by: Charly Nguyen <[email protected]>
  • Loading branch information
Charly Nguyen committed Sep 9, 2023
1 parent a4b8616 commit efe04de
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 46 deletions.
6 changes: 1 addition & 5 deletions src/components/structures/RoomView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ export interface IRoomState {

canAskToJoin: boolean;
promptAskToJoin: boolean;
knocked: boolean;
}

interface LocalRoomViewProps {
Expand Down Expand Up @@ -458,7 +457,6 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
msc3946ProcessDynamicPredecessor: SettingsStore.getValue("feature_dynamic_room_predecessors"),
canAskToJoin: this.askToJoinEnabled,
promptAskToJoin: false,
knocked: false,
};

this.dispatcherRef = dis.register(this.onAction);
Expand Down Expand Up @@ -664,7 +662,6 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
: false,
activeCall: roomId ? CallStore.instance.getActiveCall(roomId) : null,
promptAskToJoin: this.context.roomViewStore.promptAskToJoin(),
knocked: this.context.roomViewStore.knocked(),
};

if (
Expand Down Expand Up @@ -2118,7 +2115,6 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
signUrl={this.props.threepidInvite?.signUrl}
roomId={this.state.roomId}
promptAskToJoin={this.state.promptAskToJoin}
knocked={this.state.knocked}
onSubmitAskToJoin={this.onSubmitAskToJoin}
onCancelAskToJoin={this.onCancelAskToJoin}
/>
Expand Down Expand Up @@ -2202,7 +2198,7 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
<RoomPreviewBar
room={this.state.room}
promptAskToJoin={myMembership === "leave" || this.state.promptAskToJoin}
knocked={myMembership === "knock" || this.state.knocked}
knocked={myMembership === "knock"}
onSubmitAskToJoin={this.onSubmitAskToJoin}
onCancelAskToJoin={this.onCancelAskToJoin}
onForgetClick={this.onForgetClick}
Expand Down
1 change: 0 additions & 1 deletion src/contexts/RoomContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ const RoomContext = createContext<
msc3946ProcessDynamicPredecessor: false,
canAskToJoin: false,
promptAskToJoin: false,
knocked: false,
});
RoomContext.displayName = "RoomContext";
export default RoomContext;
Expand Down
22 changes: 4 additions & 18 deletions src/stores/RoomViewStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ interface State {
viewingCall: boolean;

promptAskToJoin: boolean;
knocked: boolean;
}

const INITIAL_STATE: State = {
Expand All @@ -141,7 +140,6 @@ const INITIAL_STATE: State = {
wasContextSwitch: false,
viewingCall: false,
promptAskToJoin: false,
knocked: false,
};

type Listener = (isActive: boolean) => void;
Expand Down Expand Up @@ -775,15 +773,6 @@ export class RoomViewStore extends EventEmitter {
return this.state.promptAskToJoin;
}

/**
* Gets the current state of the 'knocked' property.
*
* @returns {boolean} The value of the 'knocked' property.
*/
public knocked(): boolean {
return this.state.knocked;
}

/**
* Submits a request to join a room by sending a knock request.
*
Expand All @@ -793,15 +782,13 @@ export class RoomViewStore extends EventEmitter {
private submitAskToJoin(payload: SubmitAskToJoinPayload): void {
MatrixClientPeg.safeGet()
.knockRoom(payload.roomId, { viaServers: this.state.viaServers, ...payload.opts })
.then(() => this.setState({ promptAskToJoin: false, knocked: true }))
.catch((err: MatrixError) => {
this.setState({ promptAskToJoin: false });

.catch((err: MatrixError) =>
Modal.createDialog(ErrorDialog, {
title: _t("Failed to join"),
description: err.httpStatus === 403 ? _t("You need an invite to access this room.") : err.message,
});
});
}),
)
.finally(() => this.setState({ promptAskToJoin: false }));
}

/**
Expand All @@ -813,7 +800,6 @@ export class RoomViewStore extends EventEmitter {
private cancelAskToJoin(payload: CancelAskToJoinPayload): void {
MatrixClientPeg.safeGet()
.leave(payload.roomId)
.then(() => this.setState({ knocked: false }))
.catch((err: MatrixError) =>
Modal.createDialog(ErrorDialog, { title: _t("Failed to cancel"), description: err.message }),
);
Expand Down
1 change: 0 additions & 1 deletion test/components/views/rooms/SendMessageComposer-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ describe("<SendMessageComposer/>", () => {
msc3946ProcessDynamicPredecessor: false,
canAskToJoin: false,
promptAskToJoin: false,
knocked: false,
};
describe("createMessageContent", () => {
const permalinkCreator = jest.fn() as any;
Expand Down
25 changes: 5 additions & 20 deletions test/stores/RoomViewStore-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,39 +507,25 @@ describe("RoomViewStore", function () {
});
});

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 () => {
it("calls knockRoom() and sets promptAskToJoin state to false", 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 () => {
it("calls knockRoom(), sets promptAskToJoin to 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",
Expand All @@ -550,6 +536,7 @@ describe("RoomViewStore", function () {
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",
Expand All @@ -563,21 +550,19 @@ describe("RoomViewStore", function () {
await dispatchSubmitAskToJoin(roomId);
});

it("calls leave() and sets knocked state to false", async () => {
it("calls leave()", 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 () => {
it("calls leave() 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",
Expand Down
1 change: 0 additions & 1 deletion test/test-utils/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export function getRoomContext(room: Room, override: Partial<IRoomState>): IRoom
msc3946ProcessDynamicPredecessor: false,
canAskToJoin: false,
promptAskToJoin: false,
knocked: false,

...override,
};
Expand Down

0 comments on commit efe04de

Please sign in to comment.