get local aliases from /aliases in room settings

This commit is contained in:
Bruno Windels 2020-02-18 15:57:17 +01:00
parent 27f65c17b5
commit e6ca581f10
2 changed files with 40 additions and 18 deletions

View file

@ -74,7 +74,6 @@ export default class AliasSettings extends React.Component {
roomId: PropTypes.string.isRequired, roomId: PropTypes.string.isRequired,
canSetCanonicalAlias: PropTypes.bool.isRequired, canSetCanonicalAlias: PropTypes.bool.isRequired,
canSetAliases: PropTypes.bool.isRequired, canSetAliases: PropTypes.bool.isRequired,
aliasEvents: PropTypes.array, // [MatrixEvent]
canonicalAliasEvent: PropTypes.object, // MatrixEvent canonicalAliasEvent: PropTypes.object, // MatrixEvent
}; };
@ -94,27 +93,49 @@ export default class AliasSettings extends React.Component {
updatingCanonicalAlias: false, updatingCanonicalAlias: false,
}; };
const localDomain = MatrixClientPeg.get().getDomain();
state.domainToAliases = this.aliasEventsToDictionary(props.aliasEvents || []);
state.remoteDomains = Object.keys(state.domainToAliases).filter((domain) => {
return domain !== localDomain && state.domainToAliases[domain].length > 0;
});
if (props.canonicalAliasEvent) { if (props.canonicalAliasEvent) {
const cli = MatrixClientPeg.get();
const localDomain = cli.getDomain();
state.domainToAliases = this.aliasesToDictionary(this._getAltAliases());
state.remoteDomains = Object.keys(state.domainToAliases).filter((domain) => {
return domain !== localDomain && state.domainToAliases[domain].length > 0;
});
state.canonicalAlias = props.canonicalAliasEvent.getContent().alias; state.canonicalAlias = props.canonicalAliasEvent.getContent().alias;
} }
this.state = state; this.state = state;
} }
aliasEventsToDictionary(aliasEvents) { // m.room.alias events async componentWillMount() {
const dict = {}; const cli = MatrixClientPeg.get();
aliasEvents.forEach((event) => { const response = await cli.getLocalAliases(this.props.roomId);
dict[event.getStateKey()] = ( const localAliases = response.aliases;
(event.getContent().aliases || []).slice() // shallow-copy const localDomain = cli.getDomain();
); const domainToAliases = Object.assign(
}); {},
return dict; this.state.domainToAliases,
{[localDomain]: localAliases || []},
);
this.setState({ domainToAliases });
}
aliasesToDictionary(aliases) {
return aliases.reduce((dict, alias) => {
const domain = alias.split(":")[1];
dict[domain] = dict[domain] || [];
dict[domain].push(alias);
return dict;
}, {});
}
_getAltAliases() {
if (this.props.canonicalAliasEvent) {
const altAliases = this.props.canonicalAliasEvent.getContent().alt_aliases;
if (Array.isArray(altAliases)) {
return altAliases;
}
}
return [];
} }
changeCanonicalAlias(alias) { changeCanonicalAlias(alias) {
@ -125,7 +146,9 @@ export default class AliasSettings extends React.Component {
updatingCanonicalAlias: true, updatingCanonicalAlias: true,
}); });
const eventContent = {}; const eventContent = {
alt_aliases: this._getAltAliases(),
};
if (alias) eventContent["alias"] = alias; if (alias) eventContent["alias"] = alias;
MatrixClientPeg.get().sendStateEvent(this.props.roomId, "m.room.canonical_alias", MatrixClientPeg.get().sendStateEvent(this.props.roomId, "m.room.canonical_alias",

View file

@ -78,7 +78,6 @@ export default class GeneralRoomSettingsTab extends React.Component {
const canActuallySetAliases = room.currentState.mayClientSendStateEvent("m.room.aliases", client); const canActuallySetAliases = room.currentState.mayClientSendStateEvent("m.room.aliases", client);
const canSetCanonical = room.currentState.mayClientSendStateEvent("m.room.canonical_alias", client); const canSetCanonical = room.currentState.mayClientSendStateEvent("m.room.canonical_alias", client);
const canonicalAliasEv = room.currentState.getStateEvents("m.room.canonical_alias", ''); const canonicalAliasEv = room.currentState.getStateEvents("m.room.canonical_alias", '');
const aliasEvents = room.currentState.getStateEvents("m.room.aliases");
const canChangeGroups = room.currentState.mayClientSendStateEvent("m.room.related_groups", client); const canChangeGroups = room.currentState.mayClientSendStateEvent("m.room.related_groups", client);
const groupsEvent = room.currentState.getStateEvents("m.room.related_groups", ""); const groupsEvent = room.currentState.getStateEvents("m.room.related_groups", "");
@ -94,7 +93,7 @@ export default class GeneralRoomSettingsTab extends React.Component {
<div className='mx_SettingsTab_section mx_SettingsTab_subsectionText'> <div className='mx_SettingsTab_section mx_SettingsTab_subsectionText'>
<AliasSettings roomId={this.props.roomId} <AliasSettings roomId={this.props.roomId}
canSetCanonicalAlias={canSetCanonical} canSetAliases={canSetAliases} canSetCanonicalAlias={canSetCanonical} canSetAliases={canSetAliases}
canonicalAliasEvent={canonicalAliasEv} aliasEvents={aliasEvents} /> canonicalAliasEvent={canonicalAliasEv} />
</div> </div>
<div className='mx_SettingsTab_section'> <div className='mx_SettingsTab_section'>
<LabelledToggleSwitch value={this.state.isRoomPublished} <LabelledToggleSwitch value={this.state.isRoomPublished}