2016-09-05 14:03:16 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
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';
|
|
|
|
import classNames from 'classnames';
|
2017-05-25 13:39:08 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2017-01-25 21:51:28 +03:00
|
|
|
import sdk from '../../../index';
|
2017-01-28 00:57:34 +03:00
|
|
|
import { getAddressType, inviteMultipleToRoom } from '../../../Invite';
|
2017-01-25 21:51:28 +03:00
|
|
|
import createRoom from '../../../createRoom';
|
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
|
|
|
import DMRoomMap from '../../../utils/DMRoomMap';
|
|
|
|
import rate_limited_func from '../../../ratelimitedfunc';
|
|
|
|
import dis from '../../../dispatcher';
|
|
|
|
import Modal from '../../../Modal';
|
2017-01-25 01:41:52 +03:00
|
|
|
import AccessibleButton from '../elements/AccessibleButton';
|
2017-01-25 21:51:28 +03:00
|
|
|
import q from 'q';
|
2016-09-05 14:03:16 +03:00
|
|
|
|
2016-09-06 17:46:58 +03:00
|
|
|
const TRUNCATE_QUERY_LIST = 40;
|
2016-09-05 19:28:08 +03:00
|
|
|
|
2016-09-05 14:03:16 +03:00
|
|
|
module.exports = React.createClass({
|
2016-09-05 16:16:21 +03:00
|
|
|
displayName: "ChatInviteDialog",
|
2016-09-05 14:03:16 +03:00
|
|
|
propTypes: {
|
2017-05-29 19:39:03 +03:00
|
|
|
title: React.PropTypes.string.isRequired,
|
2016-09-05 14:03:16 +03:00
|
|
|
description: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.element,
|
|
|
|
React.PropTypes.string,
|
|
|
|
]),
|
|
|
|
value: React.PropTypes.string,
|
2016-09-05 16:29:21 +03:00
|
|
|
placeholder: React.PropTypes.string,
|
2016-09-13 12:29:17 +03:00
|
|
|
roomId: React.PropTypes.string,
|
2016-09-05 14:03:16 +03:00
|
|
|
button: React.PropTypes.string,
|
|
|
|
focus: React.PropTypes.bool,
|
|
|
|
onFinished: React.PropTypes.func.isRequired
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
value: "",
|
|
|
|
focus: true
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
|
|
|
// List of AddressTile.InviteAddressType objects represeting
|
|
|
|
// the list of addresses we're going to invite
|
2016-09-12 16:48:00 +03:00
|
|
|
inviteList: [],
|
2017-01-25 18:23:50 +03:00
|
|
|
|
|
|
|
// List of AddressTile.InviteAddressType objects represeting
|
|
|
|
// the set of autocompletion results for the current search
|
|
|
|
// 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-05 19:28:08 +03:00
|
|
|
this._updateUserList();
|
2016-09-05 14:03:16 +03:00
|
|
|
},
|
|
|
|
|
2016-09-13 14:07:49 +03:00
|
|
|
onButtonClick: function() {
|
2017-01-19 20:03:16 +03:00
|
|
|
let inviteList = this.state.inviteList.slice();
|
2016-09-14 17:09:23 +03:00
|
|
|
// Check the text input field to see if user has an unconverted address
|
|
|
|
// If there is and it's valid add it to the local inviteList
|
2017-01-19 20:03:16 +03:00
|
|
|
if (this.refs.textinput.value !== '') {
|
|
|
|
inviteList = this._addInputToList();
|
2017-01-19 21:13:27 +03:00
|
|
|
if (inviteList === null) return;
|
2016-09-14 17:09:23 +03:00
|
|
|
}
|
|
|
|
|
2017-03-03 16:48:37 +03:00
|
|
|
const addrTexts = inviteList.map(addr => addr.address);
|
2016-09-14 17:09:23 +03:00
|
|
|
if (inviteList.length > 0) {
|
2017-03-03 16:48:37 +03:00
|
|
|
if (this._isDmChat(addrTexts)) {
|
|
|
|
const userId = inviteList[0].address;
|
2016-09-13 18:06:04 +03:00
|
|
|
// Direct Message chat
|
2017-03-03 16:48:37 +03:00
|
|
|
const rooms = this._getDirectMessageRooms(userId);
|
|
|
|
if (rooms.length > 0) {
|
|
|
|
// A Direct Message room already exists for this user, so select a
|
|
|
|
// room from a list that is similar to the one in MemberInfo panel
|
|
|
|
const ChatCreateOrReuseDialog = sdk.getComponent(
|
|
|
|
"views.dialogs.ChatCreateOrReuseDialog"
|
|
|
|
);
|
|
|
|
Modal.createDialog(ChatCreateOrReuseDialog, {
|
|
|
|
userId: userId,
|
|
|
|
onFinished: (success) => {
|
|
|
|
if (success) {
|
|
|
|
this.props.onFinished(true, inviteList[0]);
|
|
|
|
}
|
|
|
|
// else show this ChatInviteDialog again
|
|
|
|
}
|
2016-09-13 18:06:04 +03:00
|
|
|
});
|
|
|
|
} else {
|
2016-09-14 17:09:23 +03:00
|
|
|
this._startChat(inviteList);
|
2016-09-13 18:06:04 +03:00
|
|
|
}
|
2016-09-08 13:45:09 +03:00
|
|
|
} else {
|
2016-09-13 18:06:04 +03:00
|
|
|
// Multi invite chat
|
2016-09-14 17:09:23 +03:00
|
|
|
this._startChat(inviteList);
|
2016-09-08 13:45:09 +03:00
|
|
|
}
|
2016-09-06 19:20:37 +03:00
|
|
|
} else {
|
2016-09-13 18:06:04 +03:00
|
|
|
// No addresses supplied
|
|
|
|
this.setState({ error: true });
|
2016-09-06 17:24:23 +03:00
|
|
|
}
|
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-01-18 20:51:39 +03:00
|
|
|
this.addressSelector.moveSelectionUp();
|
2016-09-06 15:07:06 +03:00
|
|
|
} else if (e.keyCode === 40) { // down arrow
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2017-01-18 20:51:39 +03:00
|
|
|
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-01-18 20:51:39 +03:00
|
|
|
this.addressSelector.chooseSelection();
|
2016-09-17 17:21:08 +03:00
|
|
|
} else if (this.refs.textinput.value.length === 0 && this.state.inviteList.length && e.keyCode === 8) { // backspace
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
this.onDismissed(this.state.inviteList.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-05-30 14:34:19 +03:00
|
|
|
const query = ev.target.value.toLowerCase();
|
2017-01-18 18:21:50 +03:00
|
|
|
let queryList = [];
|
2016-09-06 17:24:23 +03:00
|
|
|
|
2017-02-23 12:03:20 +03:00
|
|
|
if (query.length < 2) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-18 18:21:50 +03:00
|
|
|
|
2017-02-23 12:03:20 +03:00
|
|
|
if (this.queryChangedDebouncer) {
|
|
|
|
clearTimeout(this.queryChangedDebouncer);
|
|
|
|
}
|
|
|
|
this.queryChangedDebouncer = setTimeout(() => {
|
|
|
|
// Only do search if there is something to search
|
|
|
|
if (query.length > 0 && query != '@') {
|
2017-05-30 14:34:19 +03:00
|
|
|
this._userList.forEach((user) => {
|
|
|
|
if (user.userId.toLowerCase().indexOf(query) === -1 &&
|
|
|
|
user.displayName.toLowerCase().indexOf(query) === -1
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-23 12:03:20 +03:00
|
|
|
// Return objects, structure of which is defined
|
|
|
|
// by InviteAddressType
|
2017-05-30 14:34:19 +03:00
|
|
|
queryList.push({
|
2017-02-23 12:03:20 +03:00
|
|
|
addressType: 'mx',
|
|
|
|
address: user.userId,
|
|
|
|
displayName: user.displayName,
|
|
|
|
avatarMxc: user.avatarUrl,
|
|
|
|
isKnown: true,
|
2017-05-30 14:34:19 +03:00
|
|
|
order: user.getLastActiveTs(),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
queryList = queryList.sort((a,b) => {
|
|
|
|
return a.order < b.order;
|
2017-02-23 12:03:20 +03:00
|
|
|
});
|
|
|
|
|
2017-03-27 18:40:01 +03:00
|
|
|
// If the query is a valid address, add an entry for that
|
2017-03-27 19:03:59 +03:00
|
|
|
// This is important, otherwise there's no way to invite
|
2017-03-27 18:40:01 +03:00
|
|
|
// a perfectly valid address if there are close matches.
|
|
|
|
const addrType = getAddressType(query);
|
|
|
|
if (addrType !== null) {
|
|
|
|
queryList.unshift({
|
|
|
|
addressType: addrType,
|
|
|
|
address: query,
|
|
|
|
isKnown: false,
|
|
|
|
});
|
|
|
|
if (this._cancelThreepidLookup) this._cancelThreepidLookup();
|
|
|
|
if (addrType == 'email') {
|
|
|
|
this._lookupThreepid(addrType, query).done();
|
2017-01-25 21:51:28 +03:00
|
|
|
}
|
2017-01-18 18:21:50 +03:00
|
|
|
}
|
|
|
|
}
|
2017-02-23 12:03:20 +03:00
|
|
|
this.setState({
|
|
|
|
queryList: queryList,
|
|
|
|
error: false,
|
2017-02-23 15:12:25 +03:00
|
|
|
}, () => {
|
|
|
|
this.addressSelector.moveSelectionTop();
|
2017-02-23 12:03:20 +03:00
|
|
|
});
|
|
|
|
}, 200);
|
2016-09-05 19:28:08 +03:00
|
|
|
},
|
|
|
|
|
2016-09-12 17:04:32 +03:00
|
|
|
onDismissed: function(index) {
|
|
|
|
var self = this;
|
|
|
|
return function() {
|
|
|
|
var inviteList = self.state.inviteList.slice();
|
|
|
|
inviteList.splice(index, 1);
|
|
|
|
self.setState({
|
|
|
|
inviteList: inviteList,
|
|
|
|
queryList: [],
|
|
|
|
});
|
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) {
|
|
|
|
var self = this;
|
|
|
|
return function() {
|
2016-09-12 16:48:00 +03:00
|
|
|
self.onSelected(index);
|
2016-09-07 18:18:50 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2016-09-12 15:00:44 +03:00
|
|
|
onSelected: function(index) {
|
2016-09-12 16:48:00 +03:00
|
|
|
var inviteList = this.state.inviteList.slice();
|
2017-01-24 21:23:34 +03:00
|
|
|
inviteList.push(this.state.queryList[index]);
|
2016-09-12 15:00:44 +03:00
|
|
|
this.setState({
|
2016-09-12 16:48:00 +03:00
|
|
|
inviteList: inviteList,
|
2016-09-12 15:00:44 +03:00
|
|
|
queryList: [],
|
|
|
|
});
|
2017-01-26 13:54:07 +03:00
|
|
|
if (this._cancelThreepidLookup) this._cancelThreepidLookup();
|
2016-09-08 15:09:54 +03:00
|
|
|
},
|
|
|
|
|
2017-03-03 16:48:37 +03:00
|
|
|
_getDirectMessageRooms: function(addr) {
|
2016-09-08 15:09:54 +03:00
|
|
|
const dmRoomMap = new DMRoomMap(MatrixClientPeg.get());
|
2017-03-03 16:48:37 +03:00
|
|
|
const dmRooms = dmRoomMap.getDMRoomsForUserId(addr);
|
|
|
|
const rooms = [];
|
|
|
|
dmRooms.forEach(dmRoom => {
|
|
|
|
let room = MatrixClientPeg.get().getRoom(dmRoom);
|
|
|
|
if (room) {
|
|
|
|
const me = room.getMember(MatrixClientPeg.get().credentials.userId);
|
|
|
|
if (me.membership == 'join') {
|
|
|
|
rooms.push(room);
|
2016-09-08 15:09:54 +03:00
|
|
|
}
|
|
|
|
}
|
2017-03-03 16:48:37 +03:00
|
|
|
});
|
|
|
|
return rooms;
|
2016-09-06 17:24:23 +03:00
|
|
|
},
|
|
|
|
|
2016-09-13 18:06:04 +03:00
|
|
|
_startChat: function(addrs) {
|
2016-09-17 04:05:27 +03:00
|
|
|
if (MatrixClientPeg.get().isGuest()) {
|
|
|
|
var NeedToRegisterDialog = sdk.getComponent("dialogs.NeedToRegisterDialog");
|
|
|
|
Modal.createDialog(NeedToRegisterDialog, {
|
2017-05-30 17:09:57 +03:00
|
|
|
title: _t("Please Register"),
|
|
|
|
description: _t("Guest users can't invite users. Please register."),
|
2016-09-17 04:05:27 +03:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-24 21:23:34 +03:00
|
|
|
const addrTexts = addrs.map((addr) => {
|
|
|
|
return addr.address;
|
|
|
|
});
|
|
|
|
|
2016-09-13 12:29:17 +03:00
|
|
|
if (this.props.roomId) {
|
2016-09-13 18:06:04 +03:00
|
|
|
// Invite new user to a room
|
2016-09-14 18:19:09 +03:00
|
|
|
var self = this;
|
2017-01-28 00:57:34 +03:00
|
|
|
inviteMultipleToRoom(this.props.roomId, addrTexts)
|
2016-09-14 18:19:09 +03:00
|
|
|
.then(function(addrs) {
|
2016-09-17 04:05:27 +03:00
|
|
|
var room = MatrixClientPeg.get().getRoom(self.props.roomId);
|
2016-09-14 18:28:44 +03:00
|
|
|
return self._showAnyInviteErrors(addrs, room);
|
2016-09-14 18:19:09 +03:00
|
|
|
})
|
2016-09-13 18:06:04 +03:00
|
|
|
.catch(function(err) {
|
2016-09-17 04:05:27 +03:00
|
|
|
console.error(err.stack);
|
2016-09-13 18:06:04 +03:00
|
|
|
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createDialog(ErrorDialog, {
|
2017-05-30 17:09:57 +03:00
|
|
|
title: _t("Failed to invite"),
|
|
|
|
description: ((err && err.message) ? err.message : _t("Operation failed")),
|
2016-09-13 18:06:04 +03:00
|
|
|
});
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
.done();
|
2017-01-24 21:23:34 +03:00
|
|
|
} else if (this._isDmChat(addrTexts)) {
|
2016-09-13 18:06:04 +03:00
|
|
|
// Start the DM chat
|
2017-01-24 21:23:34 +03:00
|
|
|
createRoom({dmUserId: addrTexts[0]})
|
2016-09-13 14:47:31 +03:00
|
|
|
.catch(function(err) {
|
2016-09-17 04:05:27 +03:00
|
|
|
console.error(err.stack);
|
2016-09-13 12:29:17 +03:00
|
|
|
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createDialog(ErrorDialog, {
|
2017-05-30 17:09:57 +03:00
|
|
|
title: _t("Failed to invite user"),
|
|
|
|
description: ((err && err.message) ? err.message : _t("Operation failed")),
|
2016-09-13 12:29:17 +03:00
|
|
|
});
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
.done();
|
|
|
|
} else {
|
2016-09-13 18:06:04 +03:00
|
|
|
// Start multi user chat
|
|
|
|
var self = this;
|
2016-09-14 18:28:44 +03:00
|
|
|
var room;
|
2016-09-13 18:06:04 +03:00
|
|
|
createRoom().then(function(roomId) {
|
2016-09-14 18:28:44 +03:00
|
|
|
room = MatrixClientPeg.get().getRoom(roomId);
|
2017-01-28 00:57:34 +03:00
|
|
|
return inviteMultipleToRoom(roomId, addrTexts);
|
2016-09-13 18:06:04 +03:00
|
|
|
})
|
2016-09-14 18:19:09 +03:00
|
|
|
.then(function(addrs) {
|
2016-09-14 18:28:44 +03:00
|
|
|
return self._showAnyInviteErrors(addrs, room);
|
2016-09-14 18:19:09 +03:00
|
|
|
})
|
2016-09-13 12:29:17 +03:00
|
|
|
.catch(function(err) {
|
2016-09-17 04:05:27 +03:00
|
|
|
console.error(err.stack);
|
2016-09-13 12:29:17 +03:00
|
|
|
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createDialog(ErrorDialog, {
|
2017-05-30 17:09:57 +03:00
|
|
|
title: _t("Failed to invite"),
|
|
|
|
description: ((err && err.message) ? err.message : _t("Operation failed")),
|
2016-09-13 12:29:17 +03:00
|
|
|
});
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
.done();
|
|
|
|
}
|
2016-09-05 16:29:21 +03:00
|
|
|
|
|
|
|
// Close - this will happen before the above, as that is async
|
2017-01-24 21:23:34 +03:00
|
|
|
this.props.onFinished(true, addrTexts);
|
2016-09-05 14:03:16 +03:00
|
|
|
},
|
|
|
|
|
2017-05-30 14:34:19 +03:00
|
|
|
_updateUserList: function() {
|
2016-09-05 19:28:08 +03:00
|
|
|
// Get all the users
|
|
|
|
this._userList = MatrixClientPeg.get().getUsers();
|
2017-02-23 15:12:25 +03:00
|
|
|
// Remove current user
|
|
|
|
const meIx = this._userList.findIndex((u) => {
|
|
|
|
return u.userId === MatrixClientPeg.get().credentials.userId;
|
|
|
|
});
|
|
|
|
this._userList.splice(meIx, 1);
|
2017-05-30 14:34:19 +03:00
|
|
|
},
|
2017-02-23 12:03:20 +03:00
|
|
|
|
2016-09-12 17:48:32 +03:00
|
|
|
_isOnInviteList: function(uid) {
|
|
|
|
for (let i = 0; i < this.state.inviteList.length; i++) {
|
2017-01-24 21:23:34 +03:00
|
|
|
if (
|
|
|
|
this.state.inviteList[i].addressType == 'mx' &&
|
|
|
|
this.state.inviteList[i].address.toLowerCase() === uid
|
|
|
|
) {
|
2016-09-12 17:48:32 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2017-03-03 16:48:37 +03:00
|
|
|
_isDmChat: function(addrTexts) {
|
|
|
|
if (addrTexts.length === 1 &&
|
|
|
|
getAddressType(addrTexts[0]) === "mx" &&
|
|
|
|
!this.props.roomId
|
|
|
|
) {
|
2016-09-13 18:06:04 +03:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-09-14 18:28:44 +03:00
|
|
|
_showAnyInviteErrors: function(addrs, room) {
|
2016-09-14 18:19:09 +03:00
|
|
|
// Show user any errors
|
|
|
|
var errorList = [];
|
|
|
|
for (var addr in addrs) {
|
|
|
|
if (addrs.hasOwnProperty(addr) && addrs[addr] === "error") {
|
|
|
|
errorList.push(addr);
|
|
|
|
}
|
|
|
|
}
|
2016-09-14 18:28:44 +03:00
|
|
|
|
2016-09-14 18:19:09 +03:00
|
|
|
if (errorList.length > 0) {
|
|
|
|
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createDialog(ErrorDialog, {
|
2017-05-30 17:09:57 +03:00
|
|
|
title: _t("Failed to invite the following users to the %(roomName)s room:", {roomName: room.name}),
|
2016-09-14 18:19:09 +03:00
|
|
|
description: errorList.join(", "),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return addrs;
|
|
|
|
},
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
if (addrType == null) {
|
2017-01-19 20:03:16 +03:00
|
|
|
this.setState({ error: true });
|
2017-01-19 21:13:27 +03:00
|
|
|
return null;
|
2017-01-24 21:23:34 +03:00
|
|
|
} else if (addrType == 'mx') {
|
|
|
|
const user = MatrixClientPeg.get().getUser(addrObj.address);
|
|
|
|
if (user) {
|
|
|
|
addrObj.displayName = user.displayName;
|
|
|
|
addrObj.avatarMxc = user.avatarUrl;
|
|
|
|
addrObj.isKnown = true;
|
|
|
|
}
|
2017-01-19 20:03:16 +03:00
|
|
|
}
|
2017-01-24 21:23:34 +03:00
|
|
|
|
|
|
|
const inviteList = this.state.inviteList.slice();
|
|
|
|
inviteList.push(addrObj);
|
|
|
|
this.setState({
|
|
|
|
inviteList: inviteList,
|
|
|
|
queryList: [],
|
|
|
|
});
|
2017-01-26 13:54:07 +03:00
|
|
|
if (this._cancelThreepidLookup) this._cancelThreepidLookup();
|
2017-01-24 21:23:34 +03:00
|
|
|
return inviteList;
|
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-01-25 21:51:28 +03:00
|
|
|
// wait a bit to let the user finish typing
|
|
|
|
return q.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: [{
|
|
|
|
// an InviteAddressType
|
|
|
|
addressType: medium,
|
|
|
|
address: address,
|
|
|
|
displayName: res.displayname,
|
|
|
|
avatarMxc: res.avatar_url,
|
|
|
|
isKnown: true,
|
|
|
|
}]
|
|
|
|
});
|
2017-01-25 21:51:28 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-09-05 14:03:16 +03:00
|
|
|
render: function() {
|
2017-01-24 21:23:34 +03:00
|
|
|
const TintableSvg = sdk.getComponent("elements.TintableSvg");
|
|
|
|
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
|
|
|
|
2016-09-12 17:21:17 +03:00
|
|
|
var query = [];
|
|
|
|
// create the invite list
|
2016-09-12 16:48:00 +03:00
|
|
|
if (this.state.inviteList.length > 0) {
|
2016-09-05 19:28:08 +03:00
|
|
|
var AddressTile = sdk.getComponent("elements.AddressTile");
|
2016-09-12 17:04:32 +03:00
|
|
|
for (let i = 0; i < this.state.inviteList.length; i++) {
|
|
|
|
query.push(
|
2016-09-12 18:52:04 +03:00
|
|
|
<AddressTile key={i} address={this.state.inviteList[i]} canDismiss={true} onDismissed={ this.onDismissed(i) } />
|
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(
|
|
|
|
<textarea key={this.state.inviteList.length}
|
|
|
|
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}>
|
|
|
|
</textarea>
|
|
|
|
);
|
2016-09-05 19:28:08 +03:00
|
|
|
|
2016-09-13 13:02:59 +03:00
|
|
|
var error;
|
|
|
|
var addressSelector;
|
|
|
|
if (this.state.error) {
|
2017-01-20 17:22:27 +03:00
|
|
|
error = <div className="mx_ChatInviteDialog_error">You have entered an invalid contact. Try using their Matrix ID or email address.</div>;
|
2016-09-13 13:02:59 +03:00
|
|
|
} else {
|
2017-01-17 17:48:50 +03:00
|
|
|
const addressSelectorHeader = <div className="mx_ChatInviteDialog_addressSelectHeader">
|
|
|
|
Searching known users
|
|
|
|
</div>;
|
2016-09-13 13:02:59 +03:00
|
|
|
addressSelector = (
|
2017-01-20 17:22:27 +03:00
|
|
|
<AddressSelector ref={(ref) => {this.addressSelector = ref;}}
|
2016-09-13 13:02:59 +03:00
|
|
|
addressList={ this.state.queryList }
|
|
|
|
onSelected={ this.onSelected }
|
2017-01-17 17:48:50 +03:00
|
|
|
truncateAt={ TRUNCATE_QUERY_LIST }
|
|
|
|
header={ addressSelectorHeader }
|
|
|
|
/>
|
2016-09-13 13:02:59 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-09-05 14:03:16 +03:00
|
|
|
return (
|
2016-09-06 19:20:37 +03:00
|
|
|
<div className="mx_ChatInviteDialog" onKeyDown={this.onKeyDown}>
|
2016-09-05 14:03:16 +03:00
|
|
|
<div className="mx_Dialog_title">
|
|
|
|
{this.props.title}
|
|
|
|
</div>
|
2017-01-25 01:41:52 +03:00
|
|
|
<AccessibleButton className="mx_ChatInviteDialog_cancel"
|
|
|
|
onClick={this.onCancel} >
|
2016-09-05 14:03:16 +03:00
|
|
|
<TintableSvg src="img/icons-close-button.svg" width="35" height="35" />
|
2017-01-13 19:25:26 +03:00
|
|
|
</AccessibleButton>
|
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 }
|
2016-09-05 14:03:16 +03:00
|
|
|
</div>
|
|
|
|
<div className="mx_Dialog_buttons">
|
2016-09-13 14:07:49 +03:00
|
|
|
<button className="mx_Dialog_primary" onClick={this.onButtonClick}>
|
2016-09-05 14:03:16 +03:00
|
|
|
{this.props.button}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|