2016-08-10 16:11:49 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2018-11-29 22:05:53 +00:00
|
|
|
Copyright 2017, 2018 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.
|
|
|
|
*/
|
|
|
|
|
2018-11-29 22:05:53 +00:00
|
|
|
import React from 'react';
|
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-15 09:57:24 +00:00
|
|
|
import Modal from './Modal';
|
2017-08-15 12:42:23 +00:00
|
|
|
import { getAddressType } from './UserAddress';
|
2017-08-14 16:43:00 +00:00
|
|
|
import createRoom from './createRoom';
|
2017-08-15 09:57:24 +00:00
|
|
|
import sdk from './';
|
2017-10-27 15:20:17 +00:00
|
|
|
import dis from './dispatcher';
|
|
|
|
import DMRoomMap from './utils/DMRoomMap';
|
2017-08-14 16:43:00 +00:00
|
|
|
import { _t } from './languageHandler';
|
2016-08-10 16:11:49 +00:00
|
|
|
|
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.
|
|
|
|
*
|
2017-08-15 12:50:15 +00:00
|
|
|
* @param {string} roomId The ID of the room to invite to
|
|
|
|
* @param {string[]} addrs Array of strings of addresses to invite. May be matrix IDs or 3pids.
|
|
|
|
* @returns {Promise} Promise
|
2016-09-13 13:47:56 +00:00
|
|
|
*/
|
2018-11-29 22:05:53 +00:00
|
|
|
function inviteMultipleToRoom(roomId, addrs) {
|
2017-01-20 14:22:27 +00:00
|
|
|
const inviter = new MultiInviter(roomId);
|
2018-12-05 18:27:48 +00:00
|
|
|
return inviter.invite(addrs).then(states => Promise.resolve({states, inviter}));
|
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() {
|
2017-09-21 15:53:10 +00:00
|
|
|
const AddressPickerDialog = sdk.getComponent("dialogs.AddressPickerDialog");
|
2019-08-07 10:41:00 +00:00
|
|
|
|
2019-09-09 14:03:00 +00:00
|
|
|
const validAddressTypes = ['mx-user-id', 'email'];
|
2019-08-07 10:41:00 +00:00
|
|
|
|
2017-09-21 15:53:10 +00:00
|
|
|
Modal.createTrackedDialog('Start a chat', '', AddressPickerDialog, {
|
2017-08-14 16:43:00 +00:00
|
|
|
title: _t('Start a chat'),
|
|
|
|
description: _t("Who would you like to communicate with?"),
|
2019-08-29 14:20:14 +00:00
|
|
|
placeholder: (validAddressTypes) => {
|
|
|
|
// The set of valid address type can be mutated inside the dialog
|
|
|
|
// when you first have no IS but agree to use one in the dialog.
|
|
|
|
if (validAddressTypes.includes('email')) {
|
|
|
|
return _t("Email, name or Matrix ID");
|
|
|
|
}
|
|
|
|
return _t("Name or Matrix ID");
|
|
|
|
},
|
2019-08-07 10:41:00 +00:00
|
|
|
validAddressTypes,
|
2017-08-14 16:43:00 +00:00
|
|
|
button: _t("Start Chat"),
|
2019-08-07 18:01:51 +00:00
|
|
|
onFinished: _onStartDmFinished,
|
2017-08-14 16:43:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function showRoomInviteDialog(roomId) {
|
2017-09-21 15:53:10 +00:00
|
|
|
const AddressPickerDialog = sdk.getComponent("dialogs.AddressPickerDialog");
|
2019-08-07 10:41:00 +00:00
|
|
|
|
|
|
|
const validAddressTypes = ['mx-user-id'];
|
|
|
|
if (MatrixClientPeg.get().getIdentityServerUrl()) {
|
|
|
|
validAddressTypes.push('email');
|
|
|
|
}
|
|
|
|
|
2017-09-21 15:53:10 +00:00
|
|
|
Modal.createTrackedDialog('Chat Invite', '', AddressPickerDialog, {
|
2017-08-14 16:43:00 +00:00
|
|
|
title: _t('Invite new room members'),
|
|
|
|
button: _t('Send Invites'),
|
2019-08-29 14:20:14 +00:00
|
|
|
placeholder: (validAddressTypes) => {
|
|
|
|
// The set of valid address type can be mutated inside the dialog
|
|
|
|
// when you first have no IS but agree to use one in the dialog.
|
|
|
|
if (validAddressTypes.includes('email')) {
|
|
|
|
return _t("Email, name or Matrix ID");
|
|
|
|
}
|
|
|
|
return _t("Name or Matrix ID");
|
|
|
|
},
|
2019-08-07 10:41:00 +00:00
|
|
|
validAddressTypes,
|
2017-08-14 16:43:00 +00:00
|
|
|
onFinished: (shouldInvite, addrs) => {
|
|
|
|
_onRoomInviteFinished(roomId, shouldInvite, addrs);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-29 17:45:07 +00:00
|
|
|
/**
|
|
|
|
* Checks if the given MatrixEvent is a valid 3rd party user invite.
|
|
|
|
* @param {MatrixEvent} event The event to check
|
|
|
|
* @returns {boolean} True if valid, false otherwise
|
|
|
|
*/
|
|
|
|
export function isValid3pidInvite(event) {
|
|
|
|
if (!event || event.getType() !== "m.room.third_party_invite") return false;
|
|
|
|
|
|
|
|
// any events without these keys are not valid 3pid invites, so we ignore them
|
|
|
|
const requiredKeys = ['key_validity_url', 'public_key', 'display_name'];
|
|
|
|
for (let i = 0; i < requiredKeys.length; ++i) {
|
|
|
|
if (!event.getContent()[requiredKeys[i]]) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid enough by our standards
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-07 18:01:51 +00:00
|
|
|
// TODO: Immutable DMs replaces this
|
|
|
|
function _onStartDmFinished(shouldInvite, addrs) {
|
2017-08-14 16:43:00 +00:00
|
|
|
if (!shouldInvite) return;
|
|
|
|
|
2017-08-15 12:50:15 +00:00
|
|
|
const addrTexts = addrs.map((addr) => addr.address);
|
2017-08-14 16:43:00 +00:00
|
|
|
|
|
|
|
if (_isDmChat(addrTexts)) {
|
2017-10-27 15:20:17 +00:00
|
|
|
const rooms = _getDirectMessageRooms(addrTexts[0]);
|
|
|
|
if (rooms.length > 0) {
|
2019-08-07 18:01:51 +00:00
|
|
|
// A Direct Message room already exists for this user, so reuse it
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
|
|
|
room_id: rooms[0],
|
|
|
|
should_peek: false,
|
|
|
|
joining: false,
|
|
|
|
});
|
2017-10-27 15:20:17 +00:00
|
|
|
} else {
|
|
|
|
// Start a new DM chat
|
|
|
|
createRoom({dmUserId: addrTexts[0]}).catch((err) => {
|
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
2019-08-07 18:01:51 +00:00
|
|
|
Modal.createTrackedDialog('Failed to start chat', '', ErrorDialog, {
|
|
|
|
title: _t("Failed to start chat"),
|
2017-10-27 15:20:17 +00:00
|
|
|
description: ((err && err.message) ? err.message : _t("Operation failed")),
|
|
|
|
});
|
2017-08-14 16:43:00 +00:00
|
|
|
});
|
2017-10-27 15:20:17 +00:00
|
|
|
}
|
2018-01-25 09:54:31 +00:00
|
|
|
} else if (addrTexts.length === 1) {
|
|
|
|
// Start a new DM chat
|
|
|
|
createRoom({dmUserId: addrTexts[0]}).catch((err) => {
|
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
2019-08-07 18:01:51 +00:00
|
|
|
Modal.createTrackedDialog('Failed to start chat', '', ErrorDialog, {
|
|
|
|
title: _t("Failed to start chat"),
|
2018-01-25 09:54:31 +00:00
|
|
|
description: ((err && err.message) ? err.message : _t("Operation failed")),
|
|
|
|
});
|
|
|
|
});
|
2017-08-14 16:43:00 +00:00
|
|
|
} else {
|
|
|
|
// Start multi user chat
|
|
|
|
let room;
|
|
|
|
createRoom().then((roomId) => {
|
|
|
|
room = MatrixClientPeg.get().getRoom(roomId);
|
|
|
|
return inviteMultipleToRoom(roomId, addrTexts);
|
2018-11-29 22:05:53 +00:00
|
|
|
}).then((result) => {
|
2018-12-05 18:27:48 +00:00
|
|
|
return _showAnyInviteErrors(result.states, room, result.inviter);
|
2017-08-14 16:43:00 +00:00
|
|
|
}).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;
|
|
|
|
|
2017-08-15 12:50:15 +00:00
|
|
|
const addrTexts = addrs.map((addr) => addr.address);
|
2017-08-14 16:43:00 +00:00
|
|
|
|
|
|
|
// Invite new users to a room
|
2018-11-29 22:05:53 +00:00
|
|
|
inviteMultipleToRoom(roomId, addrTexts).then((result) => {
|
2017-08-14 16:43:00 +00:00
|
|
|
const room = MatrixClientPeg.get().getRoom(roomId);
|
2018-12-05 18:27:48 +00:00
|
|
|
return _showAnyInviteErrors(result.states, room, result.inviter);
|
2017-08-14 16:43:00 +00:00
|
|
|
}).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")),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-07 18:01:51 +00:00
|
|
|
// TODO: Immutable DMs replaces this
|
2017-08-14 16:43:00 +00:00
|
|
|
function _isDmChat(addrTexts) {
|
2017-10-20 09:16:51 +00:00
|
|
|
if (addrTexts.length === 1 && getAddressType(addrTexts[0]) === 'mx-user-id') {
|
2017-08-14 16:43:00 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 22:05:53 +00:00
|
|
|
function _showAnyInviteErrors(addrs, room, inviter) {
|
2017-08-14 16:43:00 +00:00
|
|
|
// Show user any errors
|
2018-11-29 22:05:53 +00:00
|
|
|
const failedUsers = Object.keys(addrs).filter(a => addrs[a] === 'error');
|
|
|
|
if (failedUsers.length === 1 && inviter.fatal) {
|
|
|
|
// Just get the first message because there was a fatal problem on the first
|
|
|
|
// user. This usually means that no other users were attempted, making it
|
|
|
|
// pointless for us to list who failed exactly.
|
2017-08-14 16:43:00 +00:00
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
2018-11-29 22:05:53 +00:00
|
|
|
Modal.createTrackedDialog('Failed to invite users to the room', '', ErrorDialog, {
|
|
|
|
title: _t("Failed to invite users to the room:", {roomName: room.name}),
|
|
|
|
description: inviter.getErrorText(failedUsers[0]),
|
2017-08-14 16:43:00 +00:00
|
|
|
});
|
2018-11-29 22:05:53 +00:00
|
|
|
} else {
|
|
|
|
const errorList = [];
|
|
|
|
for (const addr of failedUsers) {
|
|
|
|
if (addrs[addr] === "error") {
|
|
|
|
const reason = inviter.getErrorText(addr);
|
|
|
|
errorList.push(addr + ": " + reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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(<br />),
|
|
|
|
});
|
|
|
|
}
|
2017-08-14 16:43:00 +00:00
|
|
|
}
|
2018-11-29 22:05:53 +00:00
|
|
|
|
2017-08-14 16:43:00 +00:00
|
|
|
return addrs;
|
|
|
|
}
|
|
|
|
|
2017-10-27 15:20:17 +00:00
|
|
|
function _getDirectMessageRooms(addr) {
|
|
|
|
const dmRoomMap = new DMRoomMap(MatrixClientPeg.get());
|
|
|
|
const dmRooms = dmRoomMap.getDMRoomsForUserId(addr);
|
2018-07-23 12:18:45 +00:00
|
|
|
const rooms = dmRooms.filter((dmRoom) => {
|
2017-10-27 15:29:56 +00:00
|
|
|
const room = MatrixClientPeg.get().getRoom(dmRoom);
|
2017-10-27 15:20:17 +00:00
|
|
|
if (room) {
|
2018-08-02 09:42:05 +00:00
|
|
|
return room.getMyMembership() === 'join';
|
2017-10-27 15:20:17 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return rooms;
|
|
|
|
}
|
|
|
|
|