Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow intradef users to invite users from other homeserver in a private room #446

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions src/components/views/dialogs/TchapCreateRoomDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ interface IState {
name: string;
nameIsValid: boolean;
tchapRoomType: TchapRoomType;
isFederated: boolean;
forumFederationSwitchValue: boolean;
showFederateSwitch: boolean;
}

export default class TchapCreateRoomDialog extends React.Component<IProps, IState> {
private nameField = createRef<Field>();

constructor(props) {
public constructor(props) {
super(props);

const federationOptions = TchapUtils.getRoomFederationOptions();
Expand All @@ -57,8 +57,8 @@ export default class TchapCreateRoomDialog extends React.Component<IProps, IStat
name: this.props.defaultName || "",
nameIsValid: false,
tchapRoomType: TchapRoomType.Private,
isFederated: federationOptions.roomFederationDefault,
showFederateSwitch: federationOptions.showRoomFederationOption,
forumFederationSwitchValue: federationOptions.forumFederationSwitchDefaultValue,
showFederateSwitch: federationOptions.showForumFederationSwitch,
};
}

Expand All @@ -67,22 +67,19 @@ export default class TchapCreateRoomDialog extends React.Component<IProps, IStat
this.nameField.current.focus();
}

componentWillUnmount() {
}

private onCancel = () => {
this.props.onFinished(false);
};

private onFederatedChange = (isFederated: boolean) => {
this.setState({ isFederated: isFederated });
private onForumFederatedChange = (forumFederationSwitchValue: boolean): void => {
this.setState({ forumFederationSwitchValue });
};

private onTchapRoomTypeChange = (tchapRoomType: TchapRoomType) => {
private onTchapRoomTypeChange = (tchapRoomType: TchapRoomType): void => {
this.setState({ tchapRoomType: tchapRoomType });
};

private onNameChange = (ev: ChangeEvent<HTMLInputElement>) => {
private onNameChange = (ev: ChangeEvent<HTMLInputElement>): void => {
this.setState({ name: ev.target.value });
};

Expand All @@ -102,7 +99,7 @@ export default class TchapCreateRoomDialog extends React.Component<IProps, IStat
],
});

private onKeyDown = (event: KeyboardEvent) => {
private onKeyDown = (event: KeyboardEvent): void => {
const action = getKeyBindingsManager().getAccessibilityAction(event);
switch (action) {
case KeyBindingAction.Enter:
Expand All @@ -113,6 +110,10 @@ export default class TchapCreateRoomDialog extends React.Component<IProps, IStat
}
};

private isSelectedRoomFederated = (): boolean => {
return this.state.tchapRoomType === TchapRoomType.Forum && this.state.showFederateSwitch ? this.state.forumFederationSwitchValue : true;
}

private onOk = async () => {
const activeElement = document.activeElement as HTMLElement;
if (activeElement) {
Expand All @@ -126,7 +127,7 @@ export default class TchapCreateRoomDialog extends React.Component<IProps, IStat
this.props.onFinished(true, TchapCreateRoom.roomCreateOptions(
this.state.name,
this.state.tchapRoomType,
this.state.isFederated));
this.isSelectedRoomFederated()));
} else {
let field;
if (!this.state.nameIsValid) {
Expand All @@ -139,7 +140,7 @@ export default class TchapCreateRoomDialog extends React.Component<IProps, IStat
}
};

render() {
public render() {
const shortDomain: string = TchapUtils.getShortDomain();

const title = _t("Create a room");
Expand All @@ -162,13 +163,13 @@ export default class TchapCreateRoomDialog extends React.Component<IProps, IStat
/>

<TchapRoomTypeSelector
onChange={this.onTchapRoomTypeChange}
value={this.state.tchapRoomType}
label={_t("Type of room")}
showFederateSwitch={this.state.showFederateSwitch}
shortDomain={shortDomain}
isFederated={this.state.isFederated}
onFederatedChange={this.onFederatedChange}
forumFederationSwitchValue={this.state.forumFederationSwitchValue}
setForumFederationSwitchValue={this.onForumFederatedChange}
setRoomType={this.onTchapRoomTypeChange}
/>

</div>
Expand Down
14 changes: 7 additions & 7 deletions src/components/views/elements/TchapRoomTypeSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ interface IProps {
width?: number;
showFederateSwitch: boolean;
shortDomain: string;
isFederated: boolean;
onChange(value: TchapRoomType, isFederated?: boolean): void;
onFederatedChange(isFederated?: boolean): void;
forumFederationSwitchValue?: boolean;
setRoomType(value: TchapRoomType): void;
setForumFederationSwitchValue(forumFederationSwitchValue: boolean): void;
}

interface IState {
roomType: TchapRoomType;
}

export default class TchapRoomTypeSelector extends React.Component<IProps, IState> {
constructor(props: IProps) {
public constructor(props: IProps) {
super(props);

this.state = {
Expand All @@ -40,7 +40,7 @@ export default class TchapRoomTypeSelector extends React.Component<IProps, IStat
const roomType = e.target.value as TchapRoomType;

this.setState({ roomType: roomType });
this.props.onChange(roomType, null);
this.props.setRoomType(roomType);
};

public render(): JSX.Element {
Expand Down Expand Up @@ -72,8 +72,8 @@ export default class TchapRoomTypeSelector extends React.Component<IProps, IStat
"Allow access to this room to all users, even outside \"%(domain)s\" domain",
{ domain: this.props.shortDomain },
)}
onChange={this.props.onFederatedChange}
value={this.props.isFederated} />
onChange={this.props.setForumFederationSwitchValue}
value={this.props.forumFederationSwitchValue} />
</div>
);
}
Expand Down
16 changes: 12 additions & 4 deletions src/util/TchapUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,27 @@ export default class TchapUtils {
/**
* For the current user, get the room federation options.
*
* @returns { showRoomFederationOption: boolean, roomFederationDefault: boolean } options
* @returns { showForumFederationSwitch: boolean, forumFederationSwitchDefaultValue?: boolean } options
*/
static getRoomFederationOptions(): { showRoomFederationOption: boolean, roomFederationDefault: boolean } {
public static getRoomFederationOptions(): {
showForumFederationSwitch: boolean,
forumFederationSwitchDefaultValue?: boolean
} {
const cli = MatrixClientPeg.get();
const baseDomain = cli.getDomain();

// Only show the federate switch to defense users : it's difficult to understand, so we avoid
// displaying it unless it's really necessary.
if (baseDomain === 'agent.intradef.tchap.gouv.fr') {
return { showRoomFederationOption: true, roomFederationDefault: false };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be : {showForumFederationSwitch: true, forumFederationSwitchDefaultValue: false}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified

return {
showForumFederationSwitch: true,
forumFederationSwitchDefaultValue: false
}
}

return { showRoomFederationOption: false, roomFederationDefault: true };
return {
showForumFederationSwitch: false
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ describe("TchapCreateRoomDialog", () => {
//mock tchap utils
jest.spyOn(TchapUtils, 'getShortDomain').mockReturnValue("AGENT");
jest.spyOn(TchapUtils, 'getRoomFederationOptions').mockReturnValue(
{ showRoomFederationOption: true, roomFederationDefault: false });
{ showForumFederationSwitch: true, forumFederationSwitchDefaultValue: false });
});

it('should render the whole component with with the allow access switch', () => {
jest.spyOn(TchapUtils, 'getRoomFederationOptions').mockReturnValue(
{ showRoomFederationOption: true, roomFederationDefault: false });
{ showForumFederationSwitch: true, forumFederationSwitchDefaultValue: false });
const component = getComponent();
const allowAccessSwitch = component.find(".mx_SettingsFlag");
expect(toJson(allowAccessSwitch)).toMatchSnapshot(
Expand All @@ -86,7 +86,7 @@ describe("TchapCreateRoomDialog", () => {

it('should render the room dialog without the allow access switch', () => {
jest.spyOn(TchapUtils, 'getRoomFederationOptions').mockReturnValue(
{ showRoomFederationOption: false, roomFederationDefault: false });
{ showForumFederationSwitch: false, forumFederationSwitchDefaultValue: false });
const component = getComponent();
const allowAccessSwitch = component.find(".mx_SettingsFlag");
expect(allowAccessSwitch).toEqual({});
Expand Down Expand Up @@ -158,7 +158,7 @@ describe("TchapCreateRoomDialog", () => {
wrapper.setState({
name: roomName,
tchapRoomType: TchapRoomType.Private,
isFederated: true,
showFederateSwitch: false,
});
});

Expand Down Expand Up @@ -200,7 +200,8 @@ describe("TchapCreateRoomDialog", () => {
wrapper.setState({
name: roomName,
tchapRoomType: TchapRoomType.Forum,
isFederated: false,
forumFederationSwitchValue: false,
showFederateSwitch: true,
});
});

Expand All @@ -209,7 +210,7 @@ describe("TchapCreateRoomDialog", () => {
expect(onFinished).toHaveBeenCalledWith(true, publicRoomWithoutFederationExpectedOpts);
});

it("Should create a public room with federation", async () => {
it("Should create a public room with federation and switch", async () => {
const onFinished = jest.fn();

const publicRoomWithFederationExpectedOpts = {
Expand Down Expand Up @@ -242,7 +243,50 @@ describe("TchapCreateRoomDialog", () => {
wrapper.setState({
name: roomName,
tchapRoomType: TchapRoomType.Forum,
isFederated: true,
forumFederationSwitchValue: true,
showFederateSwitch: true,
});
});

await submitForm(wrapper);

expect(onFinished).toHaveBeenCalledWith(true, publicRoomWithFederationExpectedOpts);
});

it("Should create a public room with federation but no switch", async () => {
const onFinished = jest.fn();

const publicRoomWithFederationExpectedOpts = {
createOpts: {
name: roomName,
creation_content: {
"m.federate": true,
},
initial_state: [
{
"content": {
"rule": "restricted",
},
"state_key": "",
"type": "im.vector.room.access_rules",
},
],
visibility: "public",
preset: "public_chat",
},
guestAccess: false,
joinRule: "public",
encryption: false,
historyVisibility: "shared",
};
const wrapper = getComponent({ onFinished });

// set state in component
act(() => {
wrapper.setState({
name: roomName,
tchapRoomType: TchapRoomType.Forum,
showFederateSwitch: false,
});
});

Expand Down Expand Up @@ -284,7 +328,7 @@ describe("TchapCreateRoomDialog", () => {
wrapper.setState({
name: roomName,
tchapRoomType: TchapRoomType.External,
isFederated: true,
showFederateSwitch: false,
});
});

Expand Down