2016-08-10 16:11:49 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2017-08-14 16:43:00 +00:00
|
|
|
Copyright 2017 New Vector Ltd
|
2016-08-10 16:11:49 +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-08-15 07:58:08 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2016-08-10 16:11:49 +00:00
|
|
|
import MatrixClientPeg from './MatrixClientPeg';
|
2016-09-13 13:56:54 +00:00
|
|
|
import MultiInviter from './utils/MultiInviter';
|
2017-08-14 16:43:00 +00:00
|
|
|
import Modal from "./Modal";
|
|
|
|
import createRoom from './createRoom';
|
|
|
|
import sdk from "./";
|
|
|
|
import { _t } from './languageHandler';
|
2016-08-10 16:11:49 +00:00
|
|
|
|
|
|
|
const emailRegex = /^\S+@\S+\.\S+$/;
|
|
|
|
|
2017-02-21 14:49:30 +00:00
|
|
|
const mxidRegex = /^@\S+:\S+$/
|
2017-01-18 15:21:50 +00:00
|
|
|
|
2017-08-14 16:43:00 +00:00
|
|
|
export const addressTypes = [
|
|
|
|
'mx', 'email',
|
|
|
|
];
|
|
|
|
|
2017-08-15 07:58:08 +00:00
|
|
|
// PropType definition for an object describing
|
2017-08-14 16:43:00 +00:00
|
|
|
// an address that can be invited to a room (which
|
|
|
|
// could be a third party identifier or a matrix ID)
|
|
|
|
// along with some additional information about the
|
|
|
|
// address / target.
|
2017-08-15 07:58:08 +00:00
|
|
|
export const InviteAddressType = PropTypes.shape({
|
|
|
|
addressType: PropTypes.oneOf(addressTypes).isRequired,
|
|
|
|
address: PropTypes.string.isRequired,
|
|
|
|
displayName: PropTypes.string,
|
|
|
|
avatarMxc: PropTypes.string,
|
2017-08-14 16:43:00 +00:00
|
|
|
// true if the address is known to be a valid address (eg. is a real
|
|
|
|
// user we've seen) or false otherwise (eg. is just an address the
|
|
|
|
// user has entered)
|
2017-08-15 07:58:08 +00:00
|
|
|
isKnown: PropTypes.bool,
|
2017-08-14 16:43:00 +00:00
|
|
|
});
|
|
|
|
|
2016-08-10 16:11:49 +00:00
|
|
|
export function getAddressType(inputText) {
|
2017-01-18 15:21:50 +00:00
|
|
|
const isEmailAddress = emailRegex.test(inputText);
|
|
|
|
const isMatrixId = mxidRegex.test(inputText);
|
2016-08-10 16:11:49 +00:00
|
|
|
|
|
|
|
// sanity check the input for user IDs
|
|
|
|
if (isEmailAddress) {
|
|
|
|
return 'email';
|
|
|
|
} else if (isMatrixId) {
|
|
|
|
return 'mx';
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function inviteToRoom(roomId, addr) {
|
|
|
|
const addrType = getAddressType(addr);
|
|
|
|
|
|
|
|
if (addrType == 'email') {
|
|
|
|
return MatrixClientPeg.get().inviteByEmail(roomId, addr);
|
|
|
|
} else if (addrType == 'mx') {
|
|
|
|
return MatrixClientPeg.get().invite(roomId, addr);
|
|
|
|
} else {
|
|
|
|
throw new Error('Unsupported address');
|
|
|
|
}
|
|
|
|
}
|
2016-09-13 13:47:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Invites multiple addresses to a room
|
|
|
|
* Simpler interface to utils/MultiInviter but with
|
|
|
|
* no option to cancel.
|
|
|
|
*
|
|
|
|
* @param {roomId} The ID of the room to invite to
|
|
|
|
* @param {array} Array of strings of addresses to invite. May be matrix IDs or 3pids.
|
|
|
|
* @returns Promise
|
|
|
|
*/
|
|
|
|
export function inviteMultipleToRoom(roomId, addrs) {
|
2017-01-20 14:22:27 +00:00
|
|
|
const inviter = new MultiInviter(roomId);
|
|
|
|
return inviter.invite(addrs);
|
2016-09-13 13:55:16 +00:00
|
|
|
}
|
2016-09-13 13:51:46 +00:00
|
|
|
|
2017-08-14 16:43:00 +00:00
|
|
|
export function showStartChatInviteDialog() {
|
|
|
|
const UserPickerDialog = sdk.getComponent("dialogs.UserPickerDialog");
|
|
|
|
Modal.createTrackedDialog('Start a chat', '', UserPickerDialog, {
|
|
|
|
title: _t('Start a chat'),
|
|
|
|
description: _t("Who would you like to communicate with?"),
|
|
|
|
placeholder: _t("Email, name or matrix ID"),
|
|
|
|
button: _t("Start Chat"),
|
|
|
|
onFinished: _onStartChatFinished,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function showRoomInviteDialog(roomId) {
|
|
|
|
const UserPickerDialog = sdk.getComponent("dialogs.UserPickerDialog");
|
|
|
|
Modal.createTrackedDialog('Chat Invite', '', UserPickerDialog, {
|
|
|
|
title: _t('Invite new room members'),
|
|
|
|
description: _t('Who would you like to add to this room?'),
|
|
|
|
button: _t('Send Invites'),
|
|
|
|
placeholder: _t("Email, name or matrix ID"),
|
|
|
|
onFinished: (shouldInvite, addrs) => {
|
|
|
|
_onRoomInviteFinished(roomId, shouldInvite, addrs);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function _onStartChatFinished(shouldInvite, addrs) {
|
|
|
|
if (!shouldInvite) return;
|
|
|
|
|
|
|
|
const addrTexts = addrs.map(addr => addr.address);
|
|
|
|
|
|
|
|
if (_isDmChat(addrTexts)) {
|
|
|
|
// Start a new DM chat
|
|
|
|
createRoom({dmUserId: addrTexts[0]}).catch((err) => {
|
|
|
|
console.error(err.stack);
|
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createTrackedDialog('Failed to invite user', '', ErrorDialog, {
|
|
|
|
title: _t("Failed to invite user"),
|
|
|
|
description: ((err && err.message) ? err.message : _t("Operation failed")),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Start multi user chat
|
|
|
|
let room;
|
|
|
|
createRoom().then((roomId) => {
|
|
|
|
room = MatrixClientPeg.get().getRoom(roomId);
|
|
|
|
return inviteMultipleToRoom(roomId, addrTexts);
|
|
|
|
}).then((addrs) => {
|
|
|
|
return _showAnyInviteErrors(addrs, room);
|
|
|
|
}).catch((err) => {
|
|
|
|
console.error(err.stack);
|
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createTrackedDialog('Failed to invite', '', ErrorDialog, {
|
|
|
|
title: _t("Failed to invite"),
|
|
|
|
description: ((err && err.message) ? err.message : _t("Operation failed")),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _onRoomInviteFinished(roomId, shouldInvite, addrs) {
|
|
|
|
if (!shouldInvite) return;
|
|
|
|
|
|
|
|
const addrTexts = addrs.map(addr => addr.address);
|
|
|
|
|
|
|
|
// Invite new users to a room
|
|
|
|
inviteMultipleToRoom(roomId, addrTexts).then((addrs) => {
|
|
|
|
const room = MatrixClientPeg.get().getRoom(roomId);
|
|
|
|
return _showAnyInviteErrors(addrs, room);
|
|
|
|
}).catch((err) => {
|
|
|
|
console.error(err.stack);
|
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createTrackedDialog('Failed to invite', '', ErrorDialog, {
|
|
|
|
title: _t("Failed to invite"),
|
|
|
|
description: ((err && err.message) ? err.message : _t("Operation failed")),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function _isDmChat(addrTexts) {
|
|
|
|
if (addrTexts.length === 1 && getAddressType(addrTexts[0])) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _showAnyInviteErrors(addrs, room) {
|
|
|
|
// Show user any errors
|
|
|
|
let errorList = [];
|
|
|
|
for (let addr of Object.keys(addrs)) {
|
|
|
|
if (addrs[addr] === "error") {
|
|
|
|
errorList.push(addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errorList.length > 0) {
|
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
Modal.createTrackedDialog('Failed to invite the following users to the room', '', ErrorDialog, {
|
|
|
|
title: _t("Failed to invite the following users to the %(roomName)s room:", {roomName: room.name}),
|
|
|
|
description: errorList.join(", "),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return addrs;
|
|
|
|
}
|
|
|
|
|