2016-02-02 12:46:14 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2019-02-20 23:13:35 +00:00
|
|
|
Copyright 2018, 2019 New Vector Ltd
|
2016-02-02 12:46:14 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2017-07-12 12:58:14 +00:00
|
|
|
import Promise from 'bluebird';
|
2017-10-11 16:56:17 +00:00
|
|
|
const React = require('react');
|
2017-12-26 01:03:18 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-10-11 16:56:17 +00:00
|
|
|
const ObjectUtils = require("../../../ObjectUtils");
|
|
|
|
const MatrixClientPeg = require('../../../MatrixClientPeg');
|
|
|
|
const sdk = require("../../../index");
|
2017-05-25 10:39:08 +00:00
|
|
|
import { _t } from '../../../languageHandler';
|
2019-01-26 03:53:38 +00:00
|
|
|
import Field from "../elements/Field";
|
2019-02-20 23:13:35 +00:00
|
|
|
import ErrorDialog from "../dialogs/ErrorDialog";
|
2017-10-11 16:56:17 +00:00
|
|
|
const Modal = require("../../../Modal");
|
2016-02-02 12:46:14 +00:00
|
|
|
|
2019-02-20 23:13:35 +00:00
|
|
|
export default class AliasSettings extends React.Component {
|
|
|
|
static propTypes = {
|
2017-12-26 01:03:18 +00:00
|
|
|
roomId: PropTypes.string.isRequired,
|
|
|
|
canSetCanonicalAlias: PropTypes.bool.isRequired,
|
|
|
|
canSetAliases: PropTypes.bool.isRequired,
|
|
|
|
aliasEvents: PropTypes.array, // [MatrixEvent]
|
|
|
|
canonicalAliasEvent: PropTypes.object, // MatrixEvent
|
2019-02-20 23:13:35 +00:00
|
|
|
};
|
2016-02-02 12:46:14 +00:00
|
|
|
|
2019-02-20 23:13:35 +00:00
|
|
|
static defaultProps = {
|
|
|
|
canSetAliases: false,
|
|
|
|
canSetCanonicalAlias: false,
|
|
|
|
aliasEvents: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-02-02 12:46:14 +00:00
|
|
|
|
2019-02-20 23:13:35 +00:00
|
|
|
const aliasState = this.recalculateState(props.aliasEvents, props.canonicalAliasEvent);
|
|
|
|
this.state = Object.assign({newItem: ""}, aliasState);
|
|
|
|
}
|
2016-02-02 12:46:14 +00:00
|
|
|
|
2019-02-20 23:13:35 +00:00
|
|
|
recalculateState(aliasEvents, canonicalAliasEvent) {
|
2016-02-02 12:46:14 +00:00
|
|
|
aliasEvents = aliasEvents || [];
|
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
const state = {
|
2016-02-02 12:46:14 +00:00
|
|
|
domainToAliases: {}, // { domain.com => [#alias1:domain.com, #alias2:domain.com] }
|
|
|
|
remoteDomains: [], // [ domain.com, foobar.com ]
|
2017-10-11 16:56:17 +00:00
|
|
|
canonicalAlias: null, // #canonical:domain.com
|
2016-02-02 12:46:14 +00:00
|
|
|
};
|
2017-10-11 16:56:17 +00:00
|
|
|
const localDomain = MatrixClientPeg.get().getDomain();
|
2016-02-02 12:46:14 +00:00
|
|
|
|
|
|
|
state.domainToAliases = this.aliasEventsToDictionary(aliasEvents);
|
|
|
|
|
2018-02-02 08:12:56 +00:00
|
|
|
state.remoteDomains = Object.keys(state.domainToAliases).filter((domain) => {
|
|
|
|
return domain !== localDomain && state.domainToAliases[domain].length > 0;
|
2016-02-02 12:46:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (canonicalAliasEvent) {
|
|
|
|
state.canonicalAlias = canonicalAliasEvent.getContent().alias;
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
2019-02-20 23:13:35 +00:00
|
|
|
}
|
2016-02-02 12:46:14 +00:00
|
|
|
|
2019-02-20 23:13:35 +00:00
|
|
|
saveSettings() {
|
2017-10-11 16:56:17 +00:00
|
|
|
let promises = [];
|
2016-02-02 12:46:14 +00:00
|
|
|
|
|
|
|
// save new aliases for m.room.aliases
|
2017-10-11 16:56:17 +00:00
|
|
|
const aliasOperations = this.getAliasOperations();
|
|
|
|
for (let i = 0; i < aliasOperations.length; i++) {
|
|
|
|
const alias_operation = aliasOperations[i];
|
2016-02-02 12:46:14 +00:00
|
|
|
console.log("alias %s %s", alias_operation.place, alias_operation.val);
|
|
|
|
switch (alias_operation.place) {
|
|
|
|
case 'add':
|
|
|
|
promises.push(
|
|
|
|
MatrixClientPeg.get().createAlias(
|
2017-10-11 16:56:17 +00:00
|
|
|
alias_operation.val, this.props.roomId,
|
|
|
|
),
|
2016-02-02 12:46:14 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'del':
|
|
|
|
promises.push(
|
|
|
|
MatrixClientPeg.get().deleteAlias(
|
2017-10-11 16:56:17 +00:00
|
|
|
alias_operation.val,
|
|
|
|
),
|
2016-02-02 12:46:14 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.log("Unknown alias operation, ignoring: " + alias_operation.place);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
let oldCanonicalAlias = null;
|
2016-09-16 17:01:14 +00:00
|
|
|
if (this.props.canonicalAliasEvent) {
|
|
|
|
oldCanonicalAlias = this.props.canonicalAliasEvent.getContent().alias;
|
|
|
|
}
|
2018-09-20 00:07:01 +00:00
|
|
|
|
2018-10-27 03:50:35 +00:00
|
|
|
const newCanonicalAlias = this.state.canonicalAlias;
|
2018-09-20 00:07:01 +00:00
|
|
|
|
|
|
|
if (this.props.canSetCanonicalAlias && oldCanonicalAlias !== newCanonicalAlias) {
|
2016-09-16 17:01:14 +00:00
|
|
|
console.log("AliasSettings: Updating canonical alias");
|
2017-07-12 13:04:20 +00:00
|
|
|
promises = [Promise.all(promises).then(
|
2016-09-16 17:01:14 +00:00
|
|
|
MatrixClientPeg.get().sendStateEvent(
|
|
|
|
this.props.roomId, "m.room.canonical_alias", {
|
2018-09-20 00:07:01 +00:00
|
|
|
alias: newCanonicalAlias,
|
2017-10-11 16:56:17 +00:00
|
|
|
}, "",
|
|
|
|
),
|
2017-01-20 14:22:27 +00:00
|
|
|
)];
|
2016-09-16 17:01:14 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 15:26:12 +00:00
|
|
|
return promises;
|
2019-02-20 23:13:35 +00:00
|
|
|
}
|
2016-02-02 12:46:14 +00:00
|
|
|
|
2019-02-20 23:13:35 +00:00
|
|
|
aliasEventsToDictionary(aliasEvents) { // m.room.alias events
|
2017-10-11 16:56:17 +00:00
|
|
|
const dict = {};
|
2016-02-02 12:46:14 +00:00
|
|
|
aliasEvents.forEach((event) => {
|
|
|
|
dict[event.getStateKey()] = (
|
|
|
|
(event.getContent().aliases || []).slice() // shallow-copy
|
|
|
|
);
|
|
|
|
});
|
|
|
|
return dict;
|
2019-02-20 23:13:35 +00:00
|
|
|
}
|
2016-02-02 12:46:14 +00:00
|
|
|
|
2019-02-20 23:13:35 +00:00
|
|
|
isAliasValid(alias) {
|
2018-12-13 23:05:34 +00:00
|
|
|
// XXX: FIXME https://github.com/matrix-org/matrix-doc/issues/668
|
2016-02-02 12:46:14 +00:00
|
|
|
return (alias.match(/^#([^\/:,]+?):(.+)$/) && encodeURI(alias) === alias);
|
2019-02-20 23:13:35 +00:00
|
|
|
}
|
2016-02-02 12:46:14 +00:00
|
|
|
|
2019-02-20 23:13:35 +00:00
|
|
|
getAliasOperations() {
|
2017-10-11 16:56:17 +00:00
|
|
|
const oldAliases = this.aliasEventsToDictionary(this.props.aliasEvents);
|
2016-02-02 12:46:14 +00:00
|
|
|
return ObjectUtils.getKeyValueArrayDiffs(oldAliases, this.state.domainToAliases);
|
2019-02-20 23:13:35 +00:00
|
|
|
}
|
2016-02-02 12:46:14 +00:00
|
|
|
|
2019-02-20 23:13:35 +00:00
|
|
|
onNewAliasChanged = (value) => {
|
2017-10-04 09:00:01 +00:00
|
|
|
this.setState({newAlias: value});
|
2019-02-20 23:13:35 +00:00
|
|
|
};
|
2017-10-04 09:00:01 +00:00
|
|
|
|
2019-02-20 23:13:35 +00:00
|
|
|
onLocalAliasAdded = (alias) => {
|
2016-02-02 12:46:14 +00:00
|
|
|
if (!alias || alias.length === 0) return; // ignore attempts to create blank aliases
|
|
|
|
|
2017-10-04 09:00:01 +00:00
|
|
|
const localDomain = MatrixClientPeg.get().getDomain();
|
2018-09-20 00:38:25 +00:00
|
|
|
if (!alias.includes(':')) alias += ':' + localDomain;
|
2017-10-04 09:00:01 +00:00
|
|
|
if (this.isAliasValid(alias) && alias.endsWith(localDomain)) {
|
2019-02-20 23:13:35 +00:00
|
|
|
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.props.canonicalAlias) {
|
|
|
|
this.setState({
|
|
|
|
canonicalAlias: 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."
|
|
|
|
),
|
|
|
|
});
|
2016-02-02 12:46:14 +00:00
|
|
|
});
|
2017-10-04 09:00:01 +00:00
|
|
|
} else {
|
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
2017-07-27 16:19:18 +00:00
|
|
|
Modal.createTrackedDialog('Invalid alias format', '', ErrorDialog, {
|
2017-05-23 14:16:31 +00:00
|
|
|
title: _t('Invalid alias format'),
|
2017-05-27 17:20:35 +00:00
|
|
|
description: _t('\'%(alias)s\' is not a valid format for an alias', { alias: alias }),
|
2016-02-02 12:46:14 +00:00
|
|
|
});
|
|
|
|
}
|
2019-02-20 23:13:35 +00:00
|
|
|
};
|
2018-09-20 00:07:01 +00:00
|
|
|
|
2019-02-20 23:13:35 +00:00
|
|
|
onLocalAliasDeleted = (index) => {
|
2017-10-04 09:00:01 +00:00
|
|
|
const localDomain = MatrixClientPeg.get().getDomain();
|
2016-02-02 12:46:14 +00:00
|
|
|
// It's a bit naughty to directly manipulate this.state, and React would
|
|
|
|
// normally whine at you, but it can't see us doing the splice. Given we
|
|
|
|
// promptly setState anyway, it's just about acceptable. The alternative
|
|
|
|
// would be to arbitrarily deepcopy to a temp variable and then setState
|
|
|
|
// that, but why bother when we can cut this corner.
|
2018-09-20 00:07:01 +00:00
|
|
|
const alias = this.state.domainToAliases[localDomain].splice(index, 1);
|
2017-01-20 14:22:27 +00:00
|
|
|
this.setState({
|
2017-10-04 09:00:01 +00:00
|
|
|
domainToAliases: this.state.domainToAliases,
|
2016-02-02 12:46:14 +00:00
|
|
|
});
|
2018-09-20 00:07:01 +00:00
|
|
|
if (this.props.canonicalAlias === alias) {
|
|
|
|
this.setState({
|
|
|
|
canonicalAlias: null,
|
|
|
|
});
|
|
|
|
}
|
2019-02-20 23:13:35 +00:00
|
|
|
};
|
2016-02-02 12:46:14 +00:00
|
|
|
|
2019-02-20 23:13:35 +00:00
|
|
|
onCanonicalAliasChange = (event) => {
|
2016-02-02 12:46:14 +00:00
|
|
|
this.setState({
|
2017-10-11 16:56:17 +00:00
|
|
|
canonicalAlias: event.target.value,
|
2016-02-02 12:46:14 +00:00
|
|
|
});
|
2019-02-20 23:13:35 +00:00
|
|
|
};
|
2016-02-02 12:46:14 +00:00
|
|
|
|
2019-02-20 23:13:35 +00:00
|
|
|
render() {
|
2017-10-11 16:56:17 +00:00
|
|
|
const EditableItemList = sdk.getComponent("elements.EditableItemList");
|
|
|
|
const localDomain = MatrixClientPeg.get().getDomain();
|
2016-02-02 12:46:14 +00:00
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
let canonical_alias_section;
|
2016-02-02 12:46:14 +00:00
|
|
|
if (this.props.canSetCanonicalAlias) {
|
2018-09-20 00:23:29 +00:00
|
|
|
let found = false;
|
2018-10-19 20:18:05 +00:00
|
|
|
const canonicalValue = this.state.canonicalAlias || "";
|
2016-02-02 12:46:14 +00:00
|
|
|
canonical_alias_section = (
|
2019-01-26 03:53:38 +00:00
|
|
|
<Field onChange={this.onCanonicalAliasChange} value={canonicalValue}
|
|
|
|
element='select' id='canonicalAlias' label={_t('Main address')}>
|
2017-05-23 14:16:31 +00:00
|
|
|
<option value="" key="unset">{ _t('not specified') }</option>
|
2016-02-02 12:46:14 +00:00
|
|
|
{
|
2019-02-20 23:13:35 +00:00
|
|
|
Object.keys(this.state.domainToAliases).map((domain, i) => {
|
|
|
|
return this.state.domainToAliases[domain].map((alias, j) => {
|
2018-09-20 00:23:29 +00:00
|
|
|
if (alias === this.state.canonicalAlias) found = true;
|
2016-02-02 12:46:14 +00:00
|
|
|
return (
|
2017-10-11 16:56:17 +00:00
|
|
|
<option value={alias} key={i + "_" + j}>
|
2016-02-02 12:46:14 +00:00
|
|
|
{ alias }
|
|
|
|
</option>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
2018-09-20 00:23:29 +00:00
|
|
|
{
|
2019-02-20 23:13:35 +00:00
|
|
|
found || !this.state.canonicalAlias ? '' :
|
2018-09-20 00:23:29 +00:00
|
|
|
<option value={ this.state.canonicalAlias } key='arbitrary'>
|
|
|
|
{ this.state.canonicalAlias }
|
|
|
|
</option>
|
|
|
|
}
|
2019-01-26 03:53:38 +00:00
|
|
|
</Field>
|
2016-02-02 12:46:14 +00:00
|
|
|
);
|
2017-10-11 16:56:17 +00:00
|
|
|
} else {
|
2016-02-02 12:46:14 +00:00
|
|
|
canonical_alias_section = (
|
2017-05-23 14:16:31 +00:00
|
|
|
<b>{ this.state.canonicalAlias || _t('not set') }</b>
|
2016-02-02 12:46:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-10-11 16:56:17 +00:00
|
|
|
let remote_aliases_section;
|
2016-02-02 12:46:14 +00:00
|
|
|
if (this.state.remoteDomains.length) {
|
|
|
|
remote_aliases_section = (
|
|
|
|
<div>
|
2019-02-04 20:25:26 +00:00
|
|
|
<div>
|
2017-10-11 16:56:17 +00:00
|
|
|
{ _t("Remote addresses for this room:") }
|
2016-02-02 12:46:14 +00:00
|
|
|
</div>
|
2019-02-08 16:11:30 +00:00
|
|
|
<ul>
|
2016-02-02 12:46:14 +00:00
|
|
|
{ this.state.remoteDomains.map((domain, i) => {
|
2019-02-08 16:11:30 +00:00
|
|
|
return this.state.domainToAliases[domain].map((alias, j) => {
|
|
|
|
return <li key={i + "_" + j}>{alias}</li>;
|
2016-02-02 12:46:14 +00:00
|
|
|
});
|
2017-10-11 16:56:17 +00:00
|
|
|
}) }
|
2019-02-08 16:11:30 +00:00
|
|
|
</ul>
|
2016-02-02 12:46:14 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-01-28 18:34:21 +00:00
|
|
|
<div className='mx_AliasSettings'>
|
2019-01-26 03:53:38 +00:00
|
|
|
{canonical_alias_section}
|
2017-10-04 09:00:01 +00:00
|
|
|
<EditableItemList
|
|
|
|
className={"mx_RoomSettings_localAliases"}
|
|
|
|
items={this.state.domainToAliases[localDomain] || []}
|
|
|
|
newItem={this.state.newAlias}
|
2017-10-04 09:26:43 +00:00
|
|
|
onNewItemChanged={this.onNewAliasChanged}
|
2017-10-24 15:19:09 +00:00
|
|
|
canEdit={this.props.canSetAliases}
|
2017-10-04 09:00:01 +00:00
|
|
|
onItemAdded={this.onLocalAliasAdded}
|
|
|
|
onItemRemoved={this.onLocalAliasDeleted}
|
|
|
|
itemsLabel={_t('Local addresses for this room:')}
|
|
|
|
noItemsLabel={_t('This room has no local addresses')}
|
|
|
|
placeholder={_t(
|
|
|
|
'New address (e.g. #foo:%(localDomain)s)', {localDomain: localDomain},
|
|
|
|
)}
|
|
|
|
/>
|
2016-02-02 12:46:14 +00:00
|
|
|
{ remote_aliases_section }
|
|
|
|
</div>
|
|
|
|
);
|
2019-02-20 23:13:35 +00:00
|
|
|
}
|
|
|
|
};
|