Merge pull request #2171 from matrix-org/matthew/canonical_aliases

show canonical aliases in timeline, and set/remove implicit ones
This commit is contained in:
Matthew Hodgson 2018-09-20 10:34:12 +01:00 committed by GitHub
commit a8d224736c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 8 deletions

View File

@ -240,6 +240,24 @@ function textForRoomAliasesEvent(ev) {
} }
} }
function textForCanonicalAliasEvent(ev) {
const senderName = ev.sender && ev.sender.name ? ev.sender.name : ev.getSender();
const oldAlias = ev.getPrevContent().alias;
const newAlias = ev.getContent().alias;
if (newAlias) {
return _t('%(senderName)s set the canonical address for this room to %(address)s.', {
senderName: senderName,
address: ev.getContent().alias,
});
}
else if (oldAlias) {
return _t('%(senderName)s removed the canonical address for this room.', {
senderName: senderName,
});
}
}
function textForCallAnswerEvent(event) { function textForCallAnswerEvent(event) {
const senderName = event.sender ? event.sender.name : _t('Someone'); const senderName = event.sender ? event.sender.name : _t('Someone');
const supported = MatrixClientPeg.get().supportsVoip() ? '' : _t('(not supported by this browser)'); const supported = MatrixClientPeg.get().supportsVoip() ? '' : _t('(not supported by this browser)');
@ -402,6 +420,7 @@ const handlers = {
const stateHandlers = { const stateHandlers = {
'm.room.aliases': textForRoomAliasesEvent, 'm.room.aliases': textForRoomAliasesEvent,
'm.room.canonical_alias': textForCanonicalAliasEvent,
'm.room.name': textForRoomNameEvent, 'm.room.name': textForRoomNameEvent,
'm.room.topic': textForTopicEvent, 'm.room.topic': textForTopicEvent,
'm.room.member': textForMemberEvent, 'm.room.member': textForMemberEvent,

View File

@ -1,5 +1,6 @@
/* /*
Copyright 2016 OpenMarket Ltd Copyright 2016 OpenMarket Ltd
Copyright 2018 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -97,18 +98,19 @@ module.exports = React.createClass({
} }
} }
// save new canonical alias
let oldCanonicalAlias = null; let oldCanonicalAlias = null;
if (this.props.canonicalAliasEvent) { if (this.props.canonicalAliasEvent) {
oldCanonicalAlias = this.props.canonicalAliasEvent.getContent().alias; oldCanonicalAlias = this.props.canonicalAliasEvent.getContent().alias;
} }
if (oldCanonicalAlias !== this.state.canonicalAlias) {
let newCanonicalAlias = this.state.canonicalAlias;
if (this.props.canSetCanonicalAlias && oldCanonicalAlias !== newCanonicalAlias) {
console.log("AliasSettings: Updating canonical alias"); console.log("AliasSettings: Updating canonical alias");
promises = [Promise.all(promises).then( promises = [Promise.all(promises).then(
MatrixClientPeg.get().sendStateEvent( MatrixClientPeg.get().sendStateEvent(
this.props.roomId, "m.room.canonical_alias", { this.props.roomId, "m.room.canonical_alias", {
alias: this.state.canonicalAlias, alias: newCanonicalAlias,
}, "", }, "",
), ),
)]; )];
@ -145,6 +147,7 @@ module.exports = React.createClass({
if (!alias || alias.length === 0) return; // ignore attempts to create blank aliases if (!alias || alias.length === 0) return; // ignore attempts to create blank aliases
const localDomain = MatrixClientPeg.get().getDomain(); const localDomain = MatrixClientPeg.get().getDomain();
if (!alias.includes(':')) alias += ':' + localDomain;
if (this.isAliasValid(alias) && alias.endsWith(localDomain)) { if (this.isAliasValid(alias) && alias.endsWith(localDomain)) {
this.state.domainToAliases[localDomain] = this.state.domainToAliases[localDomain] || []; this.state.domainToAliases[localDomain] = this.state.domainToAliases[localDomain] || [];
this.state.domainToAliases[localDomain].push(alias); this.state.domainToAliases[localDomain].push(alias);
@ -161,11 +164,18 @@ module.exports = React.createClass({
description: _t('\'%(alias)s\' is not a valid format for an alias', { alias: alias }), description: _t('\'%(alias)s\' is not a valid format for an alias', { alias: alias }),
}); });
} }
if (!this.props.canonicalAlias) {
this.setState({
canonicalAlias: alias
});
}
}, },
onLocalAliasChanged: function(alias, index) { onLocalAliasChanged: function(alias, index) {
if (alias === "") return; // hit the delete button to delete please if (alias === "") return; // hit the delete button to delete please
const localDomain = MatrixClientPeg.get().getDomain(); const localDomain = MatrixClientPeg.get().getDomain();
if (!alias.includes(':')) alias += ':' + localDomain;
if (this.isAliasValid(alias) && alias.endsWith(localDomain)) { if (this.isAliasValid(alias) && alias.endsWith(localDomain)) {
this.state.domainToAliases[localDomain][index] = alias; this.state.domainToAliases[localDomain][index] = alias;
} else { } else {
@ -184,10 +194,15 @@ module.exports = React.createClass({
// promptly setState anyway, it's just about acceptable. The alternative // promptly setState anyway, it's just about acceptable. The alternative
// would be to arbitrarily deepcopy to a temp variable and then setState // would be to arbitrarily deepcopy to a temp variable and then setState
// that, but why bother when we can cut this corner. // that, but why bother when we can cut this corner.
this.state.domainToAliases[localDomain].splice(index, 1); const alias = this.state.domainToAliases[localDomain].splice(index, 1);
this.setState({ this.setState({
domainToAliases: this.state.domainToAliases, domainToAliases: this.state.domainToAliases,
}); });
if (this.props.canonicalAlias === alias) {
this.setState({
canonicalAlias: null,
});
}
}, },
onCanonicalAliasChange: function(event) { onCanonicalAliasChange: function(event) {
@ -204,12 +219,14 @@ module.exports = React.createClass({
let canonical_alias_section; let canonical_alias_section;
if (this.props.canSetCanonicalAlias) { if (this.props.canSetCanonicalAlias) {
let found = false;
canonical_alias_section = ( canonical_alias_section = (
<select onChange={this.onCanonicalAliasChange} defaultValue={this.state.canonicalAlias}> <select onChange={this.onCanonicalAliasChange} value={this.state.canonicalAlias}>
<option value="" key="unset">{ _t('not specified') }</option> <option value="" key="unset">{ _t('not specified') }</option>
{ {
Object.keys(self.state.domainToAliases).map(function(domain, i) { Object.keys(self.state.domainToAliases).map((domain, i) => {
return self.state.domainToAliases[domain].map(function(alias, j) { return self.state.domainToAliases[domain].map((alias, j) => {
if (alias === this.state.canonicalAlias) found = true;
return ( return (
<option value={alias} key={i + "_" + j}> <option value={alias} key={i + "_" + j}>
{ alias } { alias }
@ -218,6 +235,12 @@ module.exports = React.createClass({
}); });
}) })
} }
{
found || !this.stateCanonicalAlias ? '' :
<option value={ this.state.canonicalAlias } key='arbitrary'>
{ this.state.canonicalAlias }
</option>
}
</select> </select>
); );
} else { } else {

View File

@ -49,6 +49,7 @@ const eventTileTypes = {
const stateEventTileTypes = { const stateEventTileTypes = {
'm.room.aliases': 'messages.TextualEvent', 'm.room.aliases': 'messages.TextualEvent',
// 'm.room.aliases': 'messages.RoomAliasesEvent', // too complex // 'm.room.aliases': 'messages.RoomAliasesEvent', // too complex
'm.room.canonical_alias': 'messages.TextualEvent',
'm.room.create': 'messages.RoomCreate', 'm.room.create': 'messages.RoomCreate',
'm.room.member': 'messages.TextualEvent', 'm.room.member': 'messages.TextualEvent',
'm.room.name': 'messages.TextualEvent', 'm.room.name': 'messages.TextualEvent',

View File

@ -1251,6 +1251,8 @@
"%(senderName)s removed %(count)s %(removedAddresses)s as addresses for this room.|one": "%(senderName)s removed %(removedAddresses)s as an address for this room.", "%(senderName)s removed %(count)s %(removedAddresses)s as addresses for this room.|one": "%(senderName)s removed %(removedAddresses)s as an address for this room.",
"%(senderName)s removed %(count)s %(removedAddresses)s as addresses for this room.|other": "%(senderName)s removed %(removedAddresses)s as addresses for this room.", "%(senderName)s removed %(count)s %(removedAddresses)s as addresses for this room.|other": "%(senderName)s removed %(removedAddresses)s as addresses for this room.",
"%(senderName)s added %(addedAddresses)s and removed %(removedAddresses)s as addresses for this room.": "%(senderName)s added %(addedAddresses)s and removed %(removedAddresses)s as addresses for this room.", "%(senderName)s added %(addedAddresses)s and removed %(removedAddresses)s as addresses for this room.": "%(senderName)s added %(addedAddresses)s and removed %(removedAddresses)s as addresses for this room.",
"%(senderName)s set the canonical address for this room to %(address)s.": "%(senderName)s set the canonical address for this room to %(address)s.",
"%(senderName)s removed the canonical address for this room.": "%(senderName)s removed the canonical address for this room.",
"File to import": "File to import", "File to import": "File to import",
"Import": "Import", "Import": "Import",
"Failed to set direct chat tag": "Failed to set direct chat tag", "Failed to set direct chat tag": "Failed to set direct chat tag",