2016-09-05 14:03:16 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2018-06-21 02:33:24 +03:00
|
|
|
Copyright 2017, 2018 New Vector Ltd
|
2016-09-05 14:03:16 +03: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-01-25 21:51:28 +03:00
|
|
|
import React from 'react';
|
2017-08-14 19:43:00 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2017-05-25 13:39:08 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2017-01-25 21:51:28 +03:00
|
|
|
import sdk from '../../../index';
|
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
2017-07-12 15:58:14 +03:00
|
|
|
import Promise from 'bluebird';
|
2017-08-15 15:42:23 +03:00
|
|
|
import { addressTypes, getAddressType } from '../../../UserAddress.js';
|
2018-05-01 13:18:45 +03:00
|
|
|
import GroupStore from '../../../stores/GroupStore';
|
2016-09-05 14:03:16 +03:00
|
|
|
|
2016-09-06 17:46:58 +03:00
|
|
|
const TRUNCATE_QUERY_LIST = 40;
|
2017-06-07 17:58:21 +03:00
|
|
|
const QUERY_USER_DIRECTORY_DEBOUNCE_MS = 200;
|
2016-09-05 19:28:08 +03:00
|
|
|
|
2016-09-05 14:03:16 +03:00
|
|
|
module.exports = React.createClass({
|
2017-09-21 18:53:10 +03:00
|
|
|
displayName: "AddressPickerDialog",
|
2017-08-14 19:43:00 +03:00
|
|
|
|
2016-09-05 14:03:16 +03:00
|
|
|
propTypes: {
|
2017-08-14 19:43:00 +03:00
|
|
|
title: PropTypes.string.isRequired,
|
2017-08-15 12:57:24 +03:00
|
|
|
description: PropTypes.node,
|
2017-11-07 21:51:41 +03:00
|
|
|
// Extra node inserted after picker input, dropdown and errors
|
|
|
|
extraNode: PropTypes.node,
|
2017-08-14 19:43:00 +03:00
|
|
|
value: PropTypes.string,
|
|
|
|
placeholder: PropTypes.string,
|
|
|
|
roomId: PropTypes.string,
|
|
|
|
button: PropTypes.string,
|
|
|
|
focus: PropTypes.bool,
|
2017-08-21 18:30:49 +03:00
|
|
|
validAddressTypes: PropTypes.arrayOf(PropTypes.oneOf(addressTypes)),
|
2017-08-14 19:43:00 +03:00
|
|
|
onFinished: PropTypes.func.isRequired,
|
2017-09-20 17:29:31 +03:00
|
|
|
groupId: PropTypes.string,
|
2017-09-27 13:04:41 +03:00
|
|
|
// The type of entity to search for. Default: 'user'.
|
2017-09-21 18:53:10 +03:00
|
|
|
pickerType: PropTypes.oneOf(['user', 'room']),
|
2017-09-27 13:52:05 +03:00
|
|
|
// Whether the current user should be included in the addresses returned. Only
|
|
|
|
// applicable when pickerType is `user`. Default: false.
|
|
|
|
includeSelf: PropTypes.bool,
|
2016-09-05 14:03:16 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
value: "",
|
2017-06-07 17:58:21 +03:00
|
|
|
focus: true,
|
2017-08-14 19:43:00 +03:00
|
|
|
validAddressTypes: addressTypes,
|
2017-09-21 18:53:10 +03:00
|
|
|
pickerType: 'user',
|
2017-09-27 13:52:05 +03:00
|
|
|
includeSelf: false,
|
2016-09-05 14:03:16 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-09-05 19:28:08 +03:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2016-09-13 13:02:59 +03:00
|
|
|
error: false,
|
2017-01-25 18:23:50 +03:00
|
|
|
|
2017-08-15 12:57:24 +03:00
|
|
|
// List of UserAddressType objects representing
|
2017-01-25 18:23:50 +03:00
|
|
|
// the list of addresses we're going to invite
|
2017-08-15 15:30:13 +03:00
|
|
|
userList: [],
|
2017-01-25 18:23:50 +03:00
|
|
|
|
2017-06-07 17:58:21 +03:00
|
|
|
// Whether a search is ongoing
|
|
|
|
busy: false,
|
2017-06-07 19:33:15 +03:00
|
|
|
// An error message generated during the user directory search
|
|
|
|
searchError: null,
|
2017-06-07 19:40:09 +03:00
|
|
|
// Whether the server supports the user_directory API
|
|
|
|
serverSupportsUserDirectory: true,
|
2017-06-07 17:58:21 +03:00
|
|
|
// The query being searched for
|
|
|
|
query: "",
|
2017-08-15 12:57:24 +03:00
|
|
|
// List of UserAddressType objects representing
|
2017-06-07 17:58:21 +03:00
|
|
|
// the set of auto-completion results for the current search
|
2017-01-25 18:23:50 +03:00
|
|
|
// query.
|
2016-09-12 15:00:44 +03:00
|
|
|
queryList: [],
|
2016-09-05 19:28:08 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-09-05 14:03:16 +03:00
|
|
|
componentDidMount: function() {
|
|
|
|
if (this.props.focus) {
|
|
|
|
// Set the cursor at the end of the text input
|
|
|
|
this.refs.textinput.value = this.props.value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-09-13 14:07:49 +03:00
|
|
|
onButtonClick: function() {
|
2017-08-15 15:30:13 +03:00
|
|
|
let userList = this.state.userList.slice();
|
2016-09-14 17:09:23 +03:00
|
|
|
// Check the text input field to see if user has an unconverted address
|
2017-08-15 15:30:13 +03:00
|
|
|
// If there is and it's valid add it to the local userList
|
2017-01-19 20:03:16 +03:00
|
|
|
if (this.refs.textinput.value !== '') {
|
2017-08-15 15:30:13 +03:00
|
|
|
userList = this._addInputToList();
|
|
|
|
if (userList === null) return;
|
2016-09-14 17:09:23 +03:00
|
|
|
}
|
2017-08-15 15:30:13 +03:00
|
|
|
this.props.onFinished(true, userList);
|
2016-09-05 16:16:21 +03:00
|
|
|
},
|
|
|
|
|
2016-09-05 19:28:08 +03:00
|
|
|
onCancel: function() {
|
|
|
|
this.props.onFinished(false);
|
|
|
|
},
|
|
|
|
|
|
|
|
onKeyDown: function(e) {
|
|
|
|
if (e.keyCode === 27) { // escape
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
this.props.onFinished(false);
|
2016-09-06 15:07:06 +03:00
|
|
|
} else if (e.keyCode === 38) { // up arrow
|
2016-09-05 19:28:08 +03:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2017-06-07 17:58:21 +03:00
|
|
|
if (this.addressSelector) this.addressSelector.moveSelectionUp();
|
2016-09-06 15:07:06 +03:00
|
|
|
} else if (e.keyCode === 40) { // down arrow
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2017-06-07 17:58:21 +03:00
|
|
|
if (this.addressSelector) this.addressSelector.moveSelectionDown();
|
2017-01-19 19:50:09 +03:00
|
|
|
} else if (this.state.queryList.length > 0 && (e.keyCode === 188 || e.keyCode === 13 || e.keyCode === 9)) { // comma or enter or tab
|
2016-09-06 15:07:06 +03:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2017-06-07 17:58:21 +03:00
|
|
|
if (this.addressSelector) this.addressSelector.chooseSelection();
|
2017-08-15 15:30:13 +03:00
|
|
|
} else if (this.refs.textinput.value.length === 0 && this.state.userList.length && e.keyCode === 8) { // backspace
|
2016-09-17 17:21:08 +03:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2017-08-15 15:30:13 +03:00
|
|
|
this.onDismissed(this.state.userList.length - 1)();
|
2016-09-15 14:17:32 +03:00
|
|
|
} else if (e.keyCode === 13) { // enter
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2017-01-19 19:35:40 +03:00
|
|
|
if (this.refs.textinput.value == '') {
|
|
|
|
// if there's nothing in the input box, submit the form
|
|
|
|
this.onButtonClick();
|
2017-01-18 21:32:38 +03:00
|
|
|
} else {
|
2017-01-19 20:03:16 +03:00
|
|
|
this._addInputToList();
|
2017-01-18 21:32:38 +03:00
|
|
|
}
|
2016-09-17 17:21:08 +03:00
|
|
|
} else if (e.keyCode === 188 || e.keyCode === 9) { // comma or tab
|
2016-09-12 19:06:43 +03:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2017-01-19 20:03:16 +03:00
|
|
|
this._addInputToList();
|
2016-09-05 19:28:08 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onQueryChanged: function(ev) {
|
2017-06-20 17:46:54 +03:00
|
|
|
const query = ev.target.value;
|
2017-02-23 12:03:20 +03:00
|
|
|
if (this.queryChangedDebouncer) {
|
|
|
|
clearTimeout(this.queryChangedDebouncer);
|
|
|
|
}
|
2017-06-07 17:58:21 +03:00
|
|
|
// Only do search if there is something to search
|
|
|
|
if (query.length > 0 && query != '@' && query.length >= 2) {
|
|
|
|
this.queryChangedDebouncer = setTimeout(() => {
|
2017-09-21 18:53:10 +03:00
|
|
|
if (this.props.pickerType === 'user') {
|
|
|
|
if (this.props.groupId) {
|
|
|
|
this._doNaiveGroupSearch(query);
|
|
|
|
} else if (this.state.serverSupportsUserDirectory) {
|
|
|
|
this._doUserDirectorySearch(query);
|
|
|
|
} else {
|
|
|
|
this._doLocalSearch(query);
|
|
|
|
}
|
|
|
|
} else if (this.props.pickerType === 'room') {
|
|
|
|
if (this.props.groupId) {
|
|
|
|
this._doNaiveGroupRoomSearch(query);
|
|
|
|
} else {
|
2017-09-26 16:49:13 +03:00
|
|
|
this._doRoomSearch(query);
|
2017-09-21 18:53:10 +03:00
|
|
|
}
|
2017-06-07 19:40:09 +03:00
|
|
|
} else {
|
2017-09-21 18:53:10 +03:00
|
|
|
console.error('Unknown pickerType', this.props.pickerType);
|
2017-06-07 19:40:09 +03:00
|
|
|
}
|
2017-06-07 17:58:21 +03:00
|
|
|
}, QUERY_USER_DIRECTORY_DEBOUNCE_MS);
|
|
|
|
} else {
|
2017-02-23 12:03:20 +03:00
|
|
|
this.setState({
|
2017-06-07 17:58:21 +03:00
|
|
|
queryList: [],
|
|
|
|
query: "",
|
2017-06-07 19:33:15 +03:00
|
|
|
searchError: null,
|
2017-02-23 12:03:20 +03:00
|
|
|
});
|
2017-06-07 17:58:21 +03:00
|
|
|
}
|
2016-09-05 19:28:08 +03:00
|
|
|
},
|
|
|
|
|
2016-09-12 17:04:32 +03:00
|
|
|
onDismissed: function(index) {
|
2017-06-07 17:58:21 +03:00
|
|
|
return () => {
|
2017-08-15 15:30:13 +03:00
|
|
|
const userList = this.state.userList.slice();
|
|
|
|
userList.splice(index, 1);
|
2017-08-14 19:43:00 +03:00
|
|
|
this.setState({
|
2017-08-15 15:30:13 +03:00
|
|
|
userList: userList,
|
2016-09-12 17:04:32 +03:00
|
|
|
queryList: [],
|
2017-06-07 17:58:21 +03:00
|
|
|
query: "",
|
2016-09-12 17:04:32 +03:00
|
|
|
});
|
2017-01-26 13:54:07 +03:00
|
|
|
if (this._cancelThreepidLookup) this._cancelThreepidLookup();
|
2017-01-20 17:22:27 +03:00
|
|
|
};
|
2016-09-05 19:28:08 +03:00
|
|
|
},
|
|
|
|
|
2016-09-07 18:18:50 +03:00
|
|
|
onClick: function(index) {
|
2017-08-14 19:43:00 +03:00
|
|
|
return () => {
|
|
|
|
this.onSelected(index);
|
2016-09-07 18:18:50 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-09-12 15:00:44 +03:00
|
|
|
onSelected: function(index) {
|
2017-08-15 15:30:13 +03:00
|
|
|
const userList = this.state.userList.slice();
|
|
|
|
userList.push(this.state.queryList[index]);
|
2016-09-12 15:00:44 +03:00
|
|
|
this.setState({
|
2017-08-15 15:30:13 +03:00
|
|
|
userList: userList,
|
2016-09-12 15:00:44 +03:00
|
|
|
queryList: [],
|
2017-06-07 17:58:21 +03:00
|
|
|
query: "",
|
2016-09-12 15:00:44 +03:00
|
|
|
});
|
2017-01-26 13:54:07 +03:00
|
|
|
if (this._cancelThreepidLookup) this._cancelThreepidLookup();
|
2016-09-08 15:09:54 +03:00
|
|
|
},
|
|
|
|
|
2017-09-20 17:29:31 +03:00
|
|
|
_doNaiveGroupSearch: function(query) {
|
2017-09-21 12:52:28 +03:00
|
|
|
const lowerCaseQuery = query.toLowerCase();
|
2017-09-20 17:29:31 +03:00
|
|
|
this.setState({
|
|
|
|
busy: true,
|
|
|
|
query,
|
|
|
|
searchError: null,
|
|
|
|
});
|
|
|
|
MatrixClientPeg.get().getGroupUsers(this.props.groupId).then((resp) => {
|
2017-09-21 12:52:28 +03:00
|
|
|
const results = [];
|
|
|
|
resp.chunk.forEach((u) => {
|
|
|
|
const userIdMatch = u.user_id.toLowerCase().includes(lowerCaseQuery);
|
|
|
|
const displayNameMatch = (u.displayname || '').toLowerCase().includes(lowerCaseQuery);
|
|
|
|
if (!(userIdMatch || displayNameMatch)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
results.push({
|
2017-09-20 18:32:02 +03:00
|
|
|
user_id: u.user_id,
|
|
|
|
avatar_url: u.avatar_url,
|
|
|
|
display_name: u.displayname,
|
2017-09-21 12:52:28 +03:00
|
|
|
});
|
2017-09-20 18:32:02 +03:00
|
|
|
});
|
|
|
|
this._processResults(results, query);
|
2017-09-21 18:53:10 +03:00
|
|
|
}).catch((err) => {
|
|
|
|
console.error('Error whilst searching group rooms: ', err);
|
|
|
|
this.setState({
|
|
|
|
searchError: err.errcode ? err.message : _t('Something went wrong!'),
|
|
|
|
});
|
|
|
|
}).done(() => {
|
|
|
|
this.setState({
|
|
|
|
busy: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_doNaiveGroupRoomSearch: function(query) {
|
|
|
|
const lowerCaseQuery = query.toLowerCase();
|
2017-10-05 16:30:04 +03:00
|
|
|
const results = [];
|
2018-05-01 13:18:45 +03:00
|
|
|
GroupStore.getGroupRooms(this.props.groupId).forEach((r) => {
|
2017-10-05 16:30:04 +03:00
|
|
|
const nameMatch = (r.name || '').toLowerCase().includes(lowerCaseQuery);
|
|
|
|
const topicMatch = (r.topic || '').toLowerCase().includes(lowerCaseQuery);
|
|
|
|
const aliasMatch = (r.canonical_alias || '').toLowerCase().includes(lowerCaseQuery);
|
|
|
|
if (!(nameMatch || topicMatch || aliasMatch)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
results.push({
|
|
|
|
room_id: r.room_id,
|
|
|
|
avatar_url: r.avatar_url,
|
|
|
|
name: r.name || r.canonical_alias,
|
2017-09-20 17:29:31 +03:00
|
|
|
});
|
|
|
|
});
|
2017-10-05 16:30:04 +03:00
|
|
|
this._processResults(results, query);
|
|
|
|
this.setState({
|
|
|
|
busy: false,
|
|
|
|
});
|
2017-09-20 17:29:31 +03:00
|
|
|
},
|
|
|
|
|
2017-09-26 16:49:13 +03:00
|
|
|
_doRoomSearch: function(query) {
|
2017-09-26 19:11:54 +03:00
|
|
|
const lowerCaseQuery = query.toLowerCase();
|
|
|
|
const rooms = MatrixClientPeg.get().getRooms();
|
|
|
|
const results = [];
|
|
|
|
rooms.forEach((room) => {
|
2017-11-08 13:47:45 +03:00
|
|
|
let rank = Infinity;
|
2017-09-26 19:11:54 +03:00
|
|
|
const nameEvent = room.currentState.getStateEvents('m.room.name', '');
|
|
|
|
const name = nameEvent ? nameEvent.getContent().name : '';
|
|
|
|
const canonicalAlias = room.getCanonicalAlias();
|
2017-10-25 14:42:58 +03:00
|
|
|
const aliasEvents = room.currentState.getStateEvents('m.room.aliases');
|
|
|
|
const aliases = aliasEvents.map((ev) => ev.getContent().aliases).reduce((a, b) => {
|
|
|
|
return a.concat(b);
|
|
|
|
}, []);
|
2017-09-26 19:11:54 +03:00
|
|
|
|
|
|
|
const nameMatch = (name || '').toLowerCase().includes(lowerCaseQuery);
|
2017-11-08 13:47:45 +03:00
|
|
|
let aliasMatch = false;
|
|
|
|
let shortestMatchingAliasLength = Infinity;
|
|
|
|
aliases.forEach((alias) => {
|
|
|
|
if ((alias || '').toLowerCase().includes(lowerCaseQuery)) {
|
|
|
|
aliasMatch = true;
|
|
|
|
if (shortestMatchingAliasLength > alias.length) {
|
|
|
|
shortestMatchingAliasLength = alias.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-11-08 13:32:48 +03:00
|
|
|
if (!(nameMatch || aliasMatch)) {
|
2017-09-26 19:11:54 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-11-08 13:47:45 +03:00
|
|
|
|
|
|
|
if (aliasMatch) {
|
|
|
|
// A shorter matching alias will give a better rank
|
|
|
|
rank = shortestMatchingAliasLength;
|
|
|
|
}
|
|
|
|
|
2017-09-26 19:11:54 +03:00
|
|
|
const avatarEvent = room.currentState.getStateEvents('m.room.avatar', '');
|
|
|
|
const avatarUrl = avatarEvent ? avatarEvent.getContent().url : undefined;
|
2017-10-25 14:37:27 +03:00
|
|
|
|
2017-09-26 19:11:54 +03:00
|
|
|
results.push({
|
2017-11-08 13:47:45 +03:00
|
|
|
rank,
|
2017-09-26 19:11:54 +03:00
|
|
|
room_id: room.roomId,
|
|
|
|
avatar_url: avatarUrl,
|
2017-10-25 14:37:27 +03:00
|
|
|
name: name || canonicalAlias || aliases[0] || _t('Unnamed Room'),
|
2017-09-26 16:49:13 +03:00
|
|
|
});
|
|
|
|
});
|
2017-11-08 13:47:45 +03:00
|
|
|
|
|
|
|
// Sort by rank ascending (a high rank being less relevant)
|
|
|
|
const sortedResults = results.sort((a, b) => {
|
|
|
|
return a.rank - b.rank;
|
|
|
|
});
|
|
|
|
|
|
|
|
this._processResults(sortedResults, query);
|
2017-09-26 19:11:54 +03:00
|
|
|
this.setState({
|
|
|
|
busy: false,
|
|
|
|
});
|
2017-09-26 16:49:13 +03:00
|
|
|
},
|
|
|
|
|
2017-06-07 19:40:09 +03:00
|
|
|
_doUserDirectorySearch: function(query) {
|
|
|
|
this.setState({
|
|
|
|
busy: true,
|
|
|
|
query,
|
|
|
|
searchError: null,
|
|
|
|
});
|
|
|
|
MatrixClientPeg.get().searchUserDirectory({
|
|
|
|
term: query,
|
|
|
|
}).then((resp) => {
|
2017-06-13 13:03:22 +03:00
|
|
|
// The query might have changed since we sent the request, so ignore
|
|
|
|
// responses for anything other than the latest query.
|
|
|
|
if (this.state.query !== query) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-07 19:40:09 +03:00
|
|
|
this._processResults(resp.results, query);
|
|
|
|
}).catch((err) => {
|
|
|
|
console.error('Error whilst searching user directory: ', err);
|
|
|
|
this.setState({
|
|
|
|
searchError: err.errcode ? err.message : _t('Something went wrong!'),
|
|
|
|
});
|
|
|
|
if (err.errcode === 'M_UNRECOGNIZED') {
|
|
|
|
this.setState({
|
|
|
|
serverSupportsUserDirectory: false,
|
|
|
|
});
|
|
|
|
// Do a local search immediately
|
|
|
|
this._doLocalSearch(query);
|
|
|
|
}
|
|
|
|
}).done(() => {
|
|
|
|
this.setState({
|
|
|
|
busy: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
_doLocalSearch: function(query) {
|
|
|
|
this.setState({
|
|
|
|
query,
|
|
|
|
searchError: null,
|
|
|
|
});
|
2017-06-20 17:46:54 +03:00
|
|
|
const queryLowercase = query.toLowerCase();
|
2017-06-07 19:40:09 +03:00
|
|
|
const results = [];
|
|
|
|
MatrixClientPeg.get().getUsers().forEach((user) => {
|
2017-06-20 17:46:54 +03:00
|
|
|
if (user.userId.toLowerCase().indexOf(queryLowercase) === -1 &&
|
|
|
|
user.displayName.toLowerCase().indexOf(queryLowercase) === -1
|
2017-06-07 19:40:09 +03:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put results in the format of the new API
|
|
|
|
results.push({
|
|
|
|
user_id: user.userId,
|
|
|
|
display_name: user.displayName,
|
|
|
|
avatar_url: user.avatarUrl,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this._processResults(results, query);
|
|
|
|
},
|
|
|
|
|
|
|
|
_processResults: function(results, query) {
|
|
|
|
const queryList = [];
|
2017-09-21 18:53:10 +03:00
|
|
|
results.forEach((result) => {
|
|
|
|
if (result.room_id) {
|
|
|
|
queryList.push({
|
2017-09-27 17:30:58 +03:00
|
|
|
addressType: 'mx-room-id',
|
2017-09-21 18:53:10 +03:00
|
|
|
address: result.room_id,
|
|
|
|
displayName: result.name,
|
|
|
|
avatarMxc: result.avatar_url,
|
|
|
|
isKnown: true,
|
|
|
|
});
|
2017-06-07 19:40:09 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-09-27 13:52:05 +03:00
|
|
|
if (!this.props.includeSelf &&
|
2017-09-27 13:04:41 +03:00
|
|
|
result.user_id === MatrixClientPeg.get().credentials.userId
|
|
|
|
) {
|
2017-09-21 18:53:10 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-07 19:40:09 +03:00
|
|
|
// Return objects, structure of which is defined
|
2017-08-15 12:57:24 +03:00
|
|
|
// by UserAddressType
|
2017-06-07 19:40:09 +03:00
|
|
|
queryList.push({
|
2017-09-27 17:30:58 +03:00
|
|
|
addressType: 'mx-user-id',
|
2017-09-21 18:53:10 +03:00
|
|
|
address: result.user_id,
|
|
|
|
displayName: result.display_name,
|
|
|
|
avatarMxc: result.avatar_url,
|
2017-06-07 19:40:09 +03:00
|
|
|
isKnown: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// If the query is a valid address, add an entry for that
|
|
|
|
// This is important, otherwise there's no way to invite
|
|
|
|
// a perfectly valid address if there are close matches.
|
|
|
|
const addrType = getAddressType(query);
|
2017-08-15 12:57:24 +03:00
|
|
|
if (this.props.validAddressTypes.includes(addrType)) {
|
2017-06-07 19:40:09 +03:00
|
|
|
queryList.unshift({
|
|
|
|
addressType: addrType,
|
|
|
|
address: query,
|
|
|
|
isKnown: false,
|
|
|
|
});
|
|
|
|
if (this._cancelThreepidLookup) this._cancelThreepidLookup();
|
|
|
|
if (addrType == 'email') {
|
|
|
|
this._lookupThreepid(addrType, query).done();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
queryList,
|
|
|
|
error: false,
|
|
|
|
}, () => {
|
|
|
|
if (this.addressSelector) this.addressSelector.moveSelectionTop();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-01-19 20:03:16 +03:00
|
|
|
_addInputToList: function() {
|
2017-01-24 21:23:34 +03:00
|
|
|
const addressText = this.refs.textinput.value.trim();
|
2017-01-25 21:51:28 +03:00
|
|
|
const addrType = getAddressType(addressText);
|
2017-01-24 21:23:34 +03:00
|
|
|
const addrObj = {
|
|
|
|
addressType: addrType,
|
|
|
|
address: addressText,
|
|
|
|
isKnown: false,
|
|
|
|
};
|
2017-09-27 19:11:30 +03:00
|
|
|
if (!this.props.validAddressTypes.includes(addrType)) {
|
2017-01-19 20:03:16 +03:00
|
|
|
this.setState({ error: true });
|
2017-01-19 21:13:27 +03:00
|
|
|
return null;
|
2017-09-27 17:30:58 +03:00
|
|
|
} else if (addrType == 'mx-user-id') {
|
2017-01-24 21:23:34 +03:00
|
|
|
const user = MatrixClientPeg.get().getUser(addrObj.address);
|
|
|
|
if (user) {
|
|
|
|
addrObj.displayName = user.displayName;
|
|
|
|
addrObj.avatarMxc = user.avatarUrl;
|
|
|
|
addrObj.isKnown = true;
|
|
|
|
}
|
2017-09-27 17:30:58 +03:00
|
|
|
} else if (addrType == 'mx-room-id') {
|
|
|
|
const room = MatrixClientPeg.get().getRoom(addrObj.address);
|
|
|
|
if (room) {
|
|
|
|
addrObj.displayName = room.name;
|
|
|
|
addrObj.avatarMxc = room.avatarUrl;
|
|
|
|
addrObj.isKnown = true;
|
|
|
|
}
|
2017-01-19 20:03:16 +03:00
|
|
|
}
|
2017-01-24 21:23:34 +03:00
|
|
|
|
2017-08-15 15:30:13 +03:00
|
|
|
const userList = this.state.userList.slice();
|
|
|
|
userList.push(addrObj);
|
2017-01-24 21:23:34 +03:00
|
|
|
this.setState({
|
2017-08-15 15:30:13 +03:00
|
|
|
userList: userList,
|
2017-01-24 21:23:34 +03:00
|
|
|
queryList: [],
|
2017-06-07 17:58:21 +03:00
|
|
|
query: "",
|
2017-01-24 21:23:34 +03:00
|
|
|
});
|
2017-01-26 13:54:07 +03:00
|
|
|
if (this._cancelThreepidLookup) this._cancelThreepidLookup();
|
2017-08-15 15:30:13 +03:00
|
|
|
return userList;
|
2017-01-19 20:03:16 +03:00
|
|
|
},
|
|
|
|
|
2017-01-26 13:09:33 +03:00
|
|
|
_lookupThreepid: function(medium, address) {
|
2017-01-26 13:54:07 +03:00
|
|
|
let cancelled = false;
|
|
|
|
// Note that we can't safely remove this after we're done
|
|
|
|
// because we don't know that it's the same one, so we just
|
|
|
|
// leave it: it's replacing the old one each time so it's
|
|
|
|
// not like they leak.
|
|
|
|
this._cancelThreepidLookup = function() {
|
|
|
|
cancelled = true;
|
2017-08-15 11:10:13 +03:00
|
|
|
};
|
2017-01-26 13:54:07 +03:00
|
|
|
|
2017-01-25 21:51:28 +03:00
|
|
|
// wait a bit to let the user finish typing
|
2017-07-12 16:04:20 +03:00
|
|
|
return Promise.delay(500).then(() => {
|
2017-01-26 13:54:07 +03:00
|
|
|
if (cancelled) return null;
|
2017-01-25 21:51:28 +03:00
|
|
|
return MatrixClientPeg.get().lookupThreePid(medium, address);
|
|
|
|
}).then((res) => {
|
|
|
|
if (res === null || !res.mxid) return null;
|
2017-01-26 13:54:07 +03:00
|
|
|
if (cancelled) return null;
|
2017-01-25 21:51:28 +03:00
|
|
|
|
|
|
|
return MatrixClientPeg.get().getProfileInfo(res.mxid);
|
|
|
|
}).then((res) => {
|
|
|
|
if (res === null) return null;
|
2017-01-26 13:54:07 +03:00
|
|
|
if (cancelled) return null;
|
2017-01-26 13:08:44 +03:00
|
|
|
this.setState({
|
|
|
|
queryList: [{
|
2017-08-15 12:57:24 +03:00
|
|
|
// a UserAddressType
|
2017-01-26 13:08:44 +03:00
|
|
|
addressType: medium,
|
|
|
|
address: address,
|
|
|
|
displayName: res.displayname,
|
|
|
|
avatarMxc: res.avatar_url,
|
|
|
|
isKnown: true,
|
2017-06-07 17:58:21 +03:00
|
|
|
}],
|
2017-01-26 13:08:44 +03:00
|
|
|
});
|
2017-01-25 21:51:28 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-09-05 14:03:16 +03:00
|
|
|
render: function() {
|
2017-12-18 12:36:12 +03:00
|
|
|
const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog');
|
2017-12-23 00:12:47 +03:00
|
|
|
const DialogButtons = sdk.getComponent('views.elements.DialogButtons');
|
2017-01-24 21:23:34 +03:00
|
|
|
const AddressSelector = sdk.getComponent("elements.AddressSelector");
|
2016-09-06 19:46:00 +03:00
|
|
|
this.scrollElement = null;
|
2016-09-05 19:28:08 +03:00
|
|
|
|
2018-06-21 02:33:24 +03:00
|
|
|
// Use set to avoid O(n*m) operation
|
|
|
|
const selectedAddresses = new Set(this.state.userList.map(({address}) => address));
|
|
|
|
const queryList = this.state.queryList.filter(({address}) => !selectedAddresses.has(address));
|
|
|
|
|
2017-08-15 11:10:13 +03:00
|
|
|
const query = [];
|
2016-09-12 17:21:17 +03:00
|
|
|
// create the invite list
|
2017-08-15 15:30:13 +03:00
|
|
|
if (this.state.userList.length > 0) {
|
2017-08-14 19:43:00 +03:00
|
|
|
const AddressTile = sdk.getComponent("elements.AddressTile");
|
2017-08-15 15:30:13 +03:00
|
|
|
for (let i = 0; i < this.state.userList.length; i++) {
|
2016-09-12 17:04:32 +03:00
|
|
|
query.push(
|
2017-10-17 20:02:35 +03:00
|
|
|
<AddressTile
|
|
|
|
key={i}
|
|
|
|
address={this.state.userList[i]}
|
|
|
|
canDismiss={true}
|
|
|
|
onDismissed={this.onDismissed(i)}
|
|
|
|
showAddress={this.props.pickerType === 'user'} />,
|
2016-09-12 17:04:32 +03:00
|
|
|
);
|
|
|
|
}
|
2016-09-05 19:28:08 +03:00
|
|
|
}
|
2016-09-13 13:02:59 +03:00
|
|
|
|
2016-09-12 17:21:17 +03:00
|
|
|
// Add the query at the end
|
|
|
|
query.push(
|
2017-08-15 15:30:13 +03:00
|
|
|
<textarea key={this.state.userList.length}
|
2016-09-12 17:21:17 +03:00
|
|
|
rows="1"
|
|
|
|
id="textinput"
|
|
|
|
ref="textinput"
|
|
|
|
className="mx_ChatInviteDialog_input"
|
|
|
|
onChange={this.onQueryChanged}
|
|
|
|
placeholder={this.props.placeholder}
|
|
|
|
defaultValue={this.props.value}
|
|
|
|
autoFocus={this.props.focus}>
|
2017-06-07 17:58:21 +03:00
|
|
|
</textarea>,
|
2016-09-12 17:21:17 +03:00
|
|
|
);
|
2016-09-05 19:28:08 +03:00
|
|
|
|
2017-06-07 17:58:21 +03:00
|
|
|
let error;
|
|
|
|
let addressSelector;
|
2016-09-13 13:02:59 +03:00
|
|
|
if (this.state.error) {
|
2017-09-27 19:11:30 +03:00
|
|
|
let tryUsing = '';
|
|
|
|
const validTypeDescriptions = this.props.validAddressTypes.map((t) => {
|
|
|
|
return {
|
|
|
|
'mx-user-id': _t("Matrix ID"),
|
|
|
|
'mx-room-id': _t("Matrix Room ID"),
|
|
|
|
'email': _t("email address"),
|
|
|
|
}[t];
|
|
|
|
});
|
|
|
|
tryUsing = _t("Try using one of the following valid address types: %(validTypesList)s.", {
|
|
|
|
validTypesList: validTypeDescriptions.join(", "),
|
|
|
|
});
|
2017-08-15 11:10:13 +03:00
|
|
|
error = <div className="mx_ChatInviteDialog_error">
|
2017-09-28 13:21:06 +03:00
|
|
|
{ _t("You have entered an invalid address.") }
|
2017-09-27 19:11:30 +03:00
|
|
|
<br />
|
2017-09-28 13:21:06 +03:00
|
|
|
{ tryUsing }
|
2017-08-15 11:10:13 +03:00
|
|
|
</div>;
|
2017-06-07 19:33:15 +03:00
|
|
|
} else if (this.state.searchError) {
|
2017-09-28 13:21:06 +03:00
|
|
|
error = <div className="mx_ChatInviteDialog_error">{ this.state.searchError }</div>;
|
2018-06-21 02:33:24 +03:00
|
|
|
} else if (this.state.query.length > 0 && queryList.length === 0 && !this.state.busy) {
|
2017-09-28 13:21:06 +03:00
|
|
|
error = <div className="mx_ChatInviteDialog_error">{ _t("No results") }</div>;
|
2016-09-13 13:02:59 +03:00
|
|
|
} else {
|
|
|
|
addressSelector = (
|
2017-01-20 17:22:27 +03:00
|
|
|
<AddressSelector ref={(ref) => {this.addressSelector = ref;}}
|
2018-06-21 02:33:24 +03:00
|
|
|
addressList={queryList}
|
2017-10-17 20:02:35 +03:00
|
|
|
showAddress={this.props.pickerType === 'user'}
|
2017-09-28 13:21:06 +03:00
|
|
|
onSelected={this.onSelected}
|
|
|
|
truncateAt={TRUNCATE_QUERY_LIST}
|
2017-01-17 17:48:50 +03:00
|
|
|
/>
|
2016-09-13 13:02:59 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-09-05 14:03:16 +03:00
|
|
|
return (
|
2017-12-18 12:36:12 +03:00
|
|
|
<BaseDialog className="mx_ChatInviteDialog" onKeyDown={this.onKeyDown}
|
|
|
|
onFinished={this.props.onFinished} title={this.props.title}>
|
2016-09-05 14:03:16 +03:00
|
|
|
<div className="mx_ChatInviteDialog_label">
|
2016-09-06 13:34:38 +03:00
|
|
|
<label htmlFor="textinput">{ this.props.description }</label>
|
2016-09-05 14:03:16 +03:00
|
|
|
</div>
|
|
|
|
<div className="mx_Dialog_content">
|
2016-09-06 13:34:38 +03:00
|
|
|
<div className="mx_ChatInviteDialog_inputContainer">{ query }</div>
|
2016-09-13 13:02:59 +03:00
|
|
|
{ error }
|
|
|
|
{ addressSelector }
|
2017-11-07 21:51:41 +03:00
|
|
|
{ this.props.extraNode }
|
2016-09-05 14:03:16 +03:00
|
|
|
</div>
|
2017-12-23 00:12:47 +03:00
|
|
|
<DialogButtons primaryButton={this.props.button}
|
|
|
|
onPrimaryButtonClick={this.onButtonClick}
|
|
|
|
onCancel={this.onCancel} />
|
2017-12-18 12:36:12 +03:00
|
|
|
</BaseDialog>
|
2016-09-05 14:03:16 +03:00
|
|
|
);
|
2017-08-15 11:10:13 +03:00
|
|
|
},
|
2016-09-05 14:03:16 +03:00
|
|
|
});
|