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

Delegate all room alias validation to the RoomAliasField validator #3807

Merged
merged 2 commits into from
Jan 6, 2020
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
83 changes: 45 additions & 38 deletions src/components/views/room_settings/AliasSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.

import EditableItemList from "../elements/EditableItemList";

const React = require('react');
import React, {createRef} from 'react';
import PropTypes from 'prop-types';
const MatrixClientPeg = require('../../../MatrixClientPeg');
const sdk = require("../../../index");
Expand All @@ -28,22 +28,41 @@ import AccessibleButton from "../elements/AccessibleButton";
const Modal = require("../../../Modal");

class EditableAliasesList extends EditableItemList {
constructor(props) {
super(props);

this._aliasField = createRef();
}

_onAliasAdded = async () => {
await this._aliasField.current.validate({ allowEmpty: false });

if (this._aliasField.current.isValid) {
if (this.props.onItemAdded) this.props.onItemAdded(this.props.newItem);
return;
}

this._aliasField.current.focus();
this._aliasField.current.validate({ allowEmpty: false, focused: true });
};

_renderNewItemField() {
const RoomAliasField = sdk.getComponent('views.elements.RoomAliasField');
const onChange = (alias) => this._onNewItemChanged({target: {value: alias}});
return (
<form
onSubmit={this._onItemAdded}
onSubmit={this._onAliasAdded}
autoComplete="off"
noValidate={true}
className="mx_EditableItemList_newItem"
>
<RoomAliasField
id={`mx_EditableItemList_new_${this.props.id}`}
ref={this._aliasField}
onChange={onChange}
value={this.props.newItem || ""}
domain={this.props.domain} />
<AccessibleButton onClick={this._onItemAdded} kind="primary">
<AccessibleButton onClick={this._onAliasAdded} kind="primary">
{ _t("Add") }
</AccessibleButton>
</form>
Expand Down Expand Up @@ -99,11 +118,6 @@ export default class AliasSettings extends React.Component {
return dict;
}

isAliasValid(alias) {
// XXX: FIXME https://github.com/matrix-org/matrix-doc/issues/668
return (alias.match(/^#([^/:,]+?):(.+)$/) && encodeURI(alias) === alias);
t3chguy marked this conversation as resolved.
Show resolved Hide resolved
}

changeCanonicalAlias(alias) {
if (!this.props.canSetCanonicalAlias) return;

Expand Down Expand Up @@ -139,38 +153,31 @@ export default class AliasSettings extends React.Component {

const localDomain = MatrixClientPeg.get().getDomain();
if (!alias.includes(':')) alias += ':' + localDomain;
if (this.isAliasValid(alias) && alias.endsWith(localDomain)) {
MatrixClientPeg.get().createAlias(alias, this.props.roomId).then(() => {
const localAliases = this.state.domainToAliases[localDomain] || [];
const domainAliases = Object.assign({}, this.state.domainToAliases);
domainAliases[localDomain] = [...localAliases, alias];

this.setState({
domainToAliases: domainAliases,
// Reset the add field
newAlias: "",
});

if (!this.state.canonicalAlias) {
this.changeCanonicalAlias(alias);
}
}).catch((err) => {
console.error(err);
Modal.createTrackedDialog('Error creating alias', '', ErrorDialog, {
title: _t("Error creating alias"),
description: _t(
"There was an error creating that alias. It may not be allowed by the server " +
"or a temporary failure occurred.",
),
});

MatrixClientPeg.get().createAlias(alias, this.props.roomId).then(() => {
const localAliases = this.state.domainToAliases[localDomain] || [];
const domainAliases = Object.assign({}, this.state.domainToAliases);
domainAliases[localDomain] = [...localAliases, alias];

this.setState({
domainToAliases: domainAliases,
// Reset the add field
newAlias: "",
});
} else {
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
Modal.createTrackedDialog('Invalid alias format', '', ErrorDialog, {
title: _t('Invalid alias format'),
description: _t('\'%(alias)s\' is not a valid format for an alias', { alias: alias }),

if (!this.state.canonicalAlias) {
this.changeCanonicalAlias(alias);
}
}).catch((err) => {
console.error(err);
Modal.createTrackedDialog('Error creating alias', '', ErrorDialog, {
title: _t("Error creating alias"),
description: _t(
"There was an error creating that alias. It may not be allowed by the server " +
"or a temporary failure occurred.",
),
});
}
});
};

onLocalAliasDeleted = (index) => {
Expand Down
2 changes: 0 additions & 2 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -1081,8 +1081,6 @@
"There was an error updating the room's main address. It may not be allowed by the server or a temporary failure occurred.": "There was an error updating the room's main address. It may not be allowed by the server or a temporary failure occurred.",
"Error creating alias": "Error creating alias",
"There was an error creating that alias. It may not be allowed by the server or a temporary failure occurred.": "There was an error creating that alias. It may not be allowed by the server or a temporary failure occurred.",
"Invalid alias format": "Invalid alias format",
"'%(alias)s' is not a valid format for an alias": "'%(alias)s' is not a valid format for an alias",
"Error removing alias": "Error removing alias",
"There was an error removing that alias. It may no longer exist or a temporary error occurred.": "There was an error removing that alias. It may no longer exist or a temporary error occurred.",
"Main address": "Main address",
Expand Down