/* Copyright 2017 Michael Telatynski <7t3chguy@gmail.com> Copyright 2020, 2021 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React, { ChangeEvent, createRef, KeyboardEvent, SyntheticEvent } from "react"; import { Room } from "matrix-js-sdk/src/models/room"; import { JoinRule, Preset, Visibility } from "matrix-js-sdk/src/@types/partials"; import SdkConfig from '../../../SdkConfig'; import withValidation, { IFieldState } from '../elements/Validation'; import { _t } from '../../../languageHandler'; import { MatrixClientPeg } from '../../../MatrixClientPeg'; import { Key } from "../../../Keyboard"; import { IOpts, privateShouldBeEncrypted } from "../../../createRoom"; import { CommunityPrototypeStore } from "../../../stores/CommunityPrototypeStore"; import { replaceableComponent } from "../../../utils/replaceableComponent"; import Field from "../elements/Field"; import RoomAliasField from "../elements/RoomAliasField"; import LabelledToggleSwitch from "../elements/LabelledToggleSwitch"; import DialogButtons from "../elements/DialogButtons"; import BaseDialog from "../dialogs/BaseDialog"; import Dropdown from "../elements/Dropdown"; import SpaceStore from "../../../stores/SpaceStore"; interface IProps { defaultPublic?: boolean; defaultName?: string; parentSpace?: Room; onFinished(proceed: boolean, opts?: IOpts): void; } interface IState { joinRule: JoinRule; isEncrypted: boolean; name: string; topic: string; alias: string; detailsOpen: boolean; noFederate: boolean; nameIsValid: boolean; canChangeEncryption: boolean; } @replaceableComponent("views.dialogs.CreateRoomDialog") export default class CreateRoomDialog extends React.Component { private readonly supportsRestricted: boolean; private nameField = createRef(); private aliasField = createRef(); constructor(props) { super(props); this.supportsRestricted = this.props.parentSpace && !!SpaceStore.instance.restrictedJoinRuleSupport?.preferred; let joinRule = JoinRule.Invite; if (this.props.defaultPublic) { joinRule = JoinRule.Public; } else if (this.supportsRestricted) { joinRule = JoinRule.Restricted; } const config = SdkConfig.get(); this.state = { joinRule, isEncrypted: privateShouldBeEncrypted(), name: this.props.defaultName || "", topic: "", alias: "", detailsOpen: false, noFederate: config.default_federate === false, nameIsValid: false, canChangeEncryption: true, }; MatrixClientPeg.get().doesServerForceEncryptionForPreset(Preset.PrivateChat) .then(isForced => this.setState({ canChangeEncryption: !isForced })); } private roomCreateOptions() { const opts: IOpts = {}; const createOpts: IOpts["createOpts"] = opts.createOpts = {}; createOpts.name = this.state.name; if (this.state.joinRule === JoinRule.Public) { createOpts.visibility = Visibility.Public; createOpts.preset = Preset.PublicChat; opts.guestAccess = false; const { alias } = this.state; createOpts.room_alias_name = alias.substr(1, alias.indexOf(":") - 1); } else { // If we cannot change encryption we pass `true` for safety, the server should automatically do this for us. opts.encryption = this.state.canChangeEncryption ? this.state.isEncrypted : true; } if (this.state.topic) { createOpts.topic = this.state.topic; } if (this.state.noFederate) { createOpts.creation_content = { 'm.federate': false }; } if (CommunityPrototypeStore.instance.getSelectedCommunityId()) { opts.associatedWithCommunity = CommunityPrototypeStore.instance.getSelectedCommunityId(); } if (this.props.parentSpace && this.state.joinRule === JoinRule.Restricted) { opts.parentSpace = this.props.parentSpace; opts.joinRule = JoinRule.Restricted; } return opts; } componentDidMount() { // move focus to first field when showing dialog this.nameField.current.focus(); } componentWillUnmount() { } private onKeyDown = (event: KeyboardEvent) => { if (event.key === Key.ENTER) { this.onOk(); event.preventDefault(); event.stopPropagation(); } }; private onOk = async () => { const activeElement = document.activeElement as HTMLElement; if (activeElement) { activeElement.blur(); } await this.nameField.current.validate({ allowEmpty: false }); if (this.aliasField.current) { await this.aliasField.current.validate({ allowEmpty: false }); } // Validation and state updates are async, so we need to wait for them to complete // first. Queue a `setState` callback and wait for it to resolve. await new Promise(resolve => this.setState({}, resolve)); if (this.state.nameIsValid && (!this.aliasField.current || this.aliasField.current.isValid)) { this.props.onFinished(true, this.roomCreateOptions()); } else { let field; if (!this.state.nameIsValid) { field = this.nameField.current; } else if (this.aliasField.current && !this.aliasField.current.isValid) { field = this.aliasField.current; } if (field) { field.focus(); field.validate({ allowEmpty: false, focused: true }); } } }; private onCancel = () => { this.props.onFinished(false); }; private onNameChange = (ev: ChangeEvent) => { this.setState({ name: ev.target.value }); }; private onTopicChange = (ev: ChangeEvent) => { this.setState({ topic: ev.target.value }); }; private onJoinRuleChange = (joinRule: JoinRule) => { this.setState({ joinRule }); }; private onEncryptedChange = (isEncrypted: boolean) => { this.setState({ isEncrypted }); }; private onAliasChange = (alias: string) => { this.setState({ alias }); }; private onDetailsToggled = (ev: SyntheticEvent) => { this.setState({ detailsOpen: (ev.target as HTMLDetailsElement).open }); }; private onNoFederateChange = (noFederate: boolean) => { this.setState({ noFederate }); }; private onNameValidate = async (fieldState: IFieldState) => { const result = await CreateRoomDialog.validateRoomName(fieldState); this.setState({ nameIsValid: result.valid }); return result; }; private static validateRoomName = withValidation({ rules: [ { key: "required", test: async ({ value }) => !!value, invalid: () => _t("Please enter a name for the room"), }, ], }); render() { let aliasField; if (this.state.joinRule === JoinRule.Public) { const domain = MatrixClientPeg.get().getDomain(); aliasField = (
); } let publicPrivateLabel: JSX.Element; if (CommunityPrototypeStore.instance.getSelectedCommunityId()) { publicPrivateLabel =

{ _t( "Private rooms can be found and joined by invitation only. Public rooms can be " + "found and joined by anyone in this community.", ) }

; } else if (this.state.joinRule === JoinRule.Restricted) { publicPrivateLabel =

{ _t( "Everyone in will be able to find and join this room.", {}, { SpaceName: () => { this.props.parentSpace.name }, }, ) }   { _t("You can change this at any time from room settings.") }

; } else if (this.state.joinRule === JoinRule.Public) { publicPrivateLabel =

{ _t( "Anyone will be able to find and join this room, not just members of .", {}, { SpaceName: () => { this.props.parentSpace.name }, }, ) }   { _t("You can change this at any time from room settings.") }

; } else if (this.state.joinRule === JoinRule.Invite) { publicPrivateLabel =

{ _t( "Only people invited will be able to find and join this room.", ) }   { _t("You can change this at any time from room settings.") }

; } let e2eeSection; if (this.state.joinRule !== JoinRule.Public) { let microcopy; if (privateShouldBeEncrypted()) { if (this.state.canChangeEncryption) { microcopy = _t("You can’t disable this later. Bridges & most bots won’t work yet."); } else { microcopy = _t("Your server requires encryption to be enabled in private rooms."); } } else { microcopy = _t("Your server admin has disabled end-to-end encryption by default " + "in private rooms & Direct Messages."); } e2eeSection =

{ microcopy }

; } let federateLabel = _t( "You might enable this if the room will only be used for collaborating with internal " + "teams on your homeserver. This cannot be changed later.", ); if (SdkConfig.get().default_federate === false) { // We only change the label if the default setting is different to avoid jarring text changes to the // user. They will have read the implications of turning this off/on, so no need to rephrase for them. federateLabel = _t( "You might disable this if the room will be used for collaborating with external " + "teams who have their own homeserver. This cannot be changed later.", ); } let title = _t("Create a room"); if (CommunityPrototypeStore.instance.getSelectedCommunityId()) { const name = CommunityPrototypeStore.instance.getSelectedCommunityName(); title = _t("Create a room in %(communityName)s", { communityName: name }); } else if (!this.props.parentSpace) { title = this.state.joinRule === JoinRule.Public ? _t('Create a public room') : _t('Create a private room'); } const options = [
{ _t("Private room (invite only)") }
,
{ _t("Public room") }
, ]; if (this.supportsRestricted) { options.unshift(
{ _t("Visible to space members") }
); } return (
{ options } { publicPrivateLabel } { e2eeSection } { aliasField }
{ this.state.detailsOpen ? _t('Hide advanced') : _t('Show advanced') }

{ federateLabel }

); } }