Use knock rooms sync to reflect the knock state (#11596)

Signed-off-by: Charly Nguyen <charly.nguyen@nordeck.net>
pull/28217/head
Charly Nguyen 2023-09-11 18:33:40 +02:00 committed by GitHub
parent bb91df1d3c
commit 5cf2e1514b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 10 additions and 46 deletions

View File

@ -245,7 +245,6 @@ export interface IRoomState {
canAskToJoin: boolean;
promptAskToJoin: boolean;
knocked: boolean;
}
interface LocalRoomViewProps {
@ -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);
@ -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 (
@ -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}
/>
@ -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}

View File

@ -73,7 +73,6 @@ const RoomContext = createContext<
msc3946ProcessDynamicPredecessor: false,
canAskToJoin: false,
promptAskToJoin: false,
knocked: false,
});
RoomContext.displayName = "RoomContext";
export default RoomContext;

View File

@ -119,7 +119,6 @@ interface State {
viewingCall: boolean;
promptAskToJoin: boolean;
knocked: boolean;
}
const INITIAL_STATE: State = {
@ -141,7 +140,6 @@ const INITIAL_STATE: State = {
wasContextSwitch: false,
viewingCall: false,
promptAskToJoin: false,
knocked: false,
};
type Listener = (isActive: boolean) => void;
@ -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.
*
@ -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 }));
}
/**
@ -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 }),
);

View File

@ -85,7 +85,6 @@ describe("<SendMessageComposer/>", () => {
msc3946ProcessDynamicPredecessor: false,
canAskToJoin: false,
promptAskToJoin: false,
knocked: false,
};
describe("createMessageContent", () => {
const permalinkCreator = jest.fn() as any;

View File

@ -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 state 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",
@ -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",
@ -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",

View File

@ -90,7 +90,6 @@ export function getRoomContext(room: Room, override: Partial<IRoomState>): IRoom
msc3946ProcessDynamicPredecessor: false,
canAskToJoin: false,
promptAskToJoin: false,
knocked: false,
...override,
};