2016-08-10 18:11:49 +02:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2017-08-14 18:43:00 +02:00
|
|
|
Copyright 2017 New Vector Ltd
|
2016-08-10 18:11:49 +02: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import MatrixClientPeg from './MatrixClientPeg';
|
2016-09-13 15:56:54 +02:00
|
|
|
import MultiInviter from './utils/MultiInviter';
|
2017-08-15 11:57:24 +02:00
|
|
|
import Modal from './Modal';
|
2017-08-15 14:42:23 +02:00
|
|
|
import { getAddressType } from './UserAddress';
|
2017-08-14 18:43:00 +02:00
|
|
|
import createRoom from './createRoom';
|
2017-08-15 11:57:24 +02:00
|
|
|
import sdk from './';
|
2017-10-27 17:20:17 +02:00
|
|
|
import dis from './dispatcher';
|
|
|
|
import DMRoomMap from './utils/DMRoomMap';
|
2017-08-14 18:43:00 +02:00
|
|
|
import { _t } from './languageHandler';
|
2016-08-10 18:11:49 +02:00
|
|
|
|
|
|
|
export function inviteToRoom(roomId, addr) {
|
|
|
|
const addrType = getAddressType(addr);
|
|
|
|
|
|
|
|
if (addrType == 'email') {
|
|
|
|
return MatrixClientPeg.get().inviteByEmail(roomId, addr);
|
2017-09-28 11:55:09 +02:00
|
|
|
} else if (addrType == 'mx-user-id') {
|
2016-08-10 18:11:49 +02:00
|
|
|
return MatrixClientPeg.get().invite(roomId, addr);
|
|
|
|
} else {
|
|
|
|
throw new Error('Unsupported address');
|
|
|
|
}
|
|
|
|
}
|
2016-09-13 15:47:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Invites multiple addresses to a room
|
|
|
|
* Simpler interface to utils/MultiInviter but with
|
|
|
|
* no option to cancel.
|
|
|
|
*
|
2017-08-15 14:50:15 +02: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 15:47:56 +02:00
|
|
|
*/
|
|
|
|
export function inviteMultipleToRoom(roomId, addrs) {
|
2017-01-20 15:22:27 +01:00
|
|
|
const inviter = new MultiInviter(roomId);
|
|
|
|
return inviter.invite(addrs);
|
2016-09-13 15:55:16 +02:00
|
|
|
}
|
2016-09-13 15:51:46 +02:00
|
|
|
|
2017-08-14 18:43:00 +02:00
|
|
|
export function showStartChatInviteDialog() {
|
2017-09-21 17:53:10 +02:00
|
|
|
const AddressPickerDialog = sdk.getComponent("dialogs.AddressPickerDialog");
|
|
|
|
Modal.createTrackedDialog('Start a chat', '', AddressPickerDialog, {
|
2017-08-14 18:43:00 +02:00
|
|
|
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) {
|
2017-09-21 17:53:10 +02:00
|
|
|
const AddressPickerDialog = sdk.getComponent("dialogs.AddressPickerDialog");
|
|
|
|
Modal.createTrackedDialog('Chat Invite', '', AddressPickerDialog, {
|
2017-08-14 18:43:00 +02:00
|
|
|
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;
|
|
|
|
|
2017-08-15 14:50:15 +02:00
|
|
|
const addrTexts = addrs.map((addr) => addr.address);
|
2017-08-14 18:43:00 +02:00
|
|
|
|
|
|
|
if (_isDmChat(addrTexts)) {
|
2017-10-27 17:20:17 +02:00
|
|
|
const rooms = _getDirectMessageRooms(addrTexts[0]);
|
|
|
|
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",
|
|
|
|
);
|
|
|
|
const close = Modal.createTrackedDialog('Create or Reuse', '', ChatCreateOrReuseDialog, {
|
|
|
|
userId: addrTexts[0],
|
|
|
|
onNewDMClick: () => {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'start_chat',
|
|
|
|
user_id: addrTexts[0],
|
|
|
|
});
|
|
|
|
close(true);
|
|
|
|
},
|
|
|
|
onExistingRoomSelected: (roomId) => {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
|
|
|
room_id: roomId,
|
|
|
|
});
|
|
|
|
close(true);
|
|
|
|
},
|
|
|
|
}).close;
|
|
|
|
} else {
|
|
|
|
// Start a new DM chat
|
|
|
|
createRoom({dmUserId: addrTexts[0]}).catch((err) => {
|
|
|
|
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")),
|
|
|
|
});
|
2017-08-14 18:43:00 +02:00
|
|
|
});
|
2017-10-27 17:20:17 +02:00
|
|
|
}
|
2017-08-14 18:43:00 +02:00
|
|
|
} 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;
|
|
|
|
|
2017-08-15 14:50:15 +02:00
|
|
|
const addrTexts = addrs.map((addr) => addr.address);
|
2017-08-14 18:43:00 +02:00
|
|
|
|
|
|
|
// 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) {
|
2017-10-20 11:16:51 +02:00
|
|
|
if (addrTexts.length === 1 && getAddressType(addrTexts[0]) === 'mx-user-id') {
|
2017-08-14 18:43:00 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _showAnyInviteErrors(addrs, room) {
|
|
|
|
// Show user any errors
|
2017-08-15 14:50:15 +02:00
|
|
|
const errorList = [];
|
|
|
|
for (const addr of Object.keys(addrs)) {
|
2017-08-14 18:43:00 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-27 17:20:17 +02:00
|
|
|
function _getDirectMessageRooms(addr) {
|
|
|
|
const dmRoomMap = new DMRoomMap(MatrixClientPeg.get());
|
|
|
|
const dmRooms = dmRoomMap.getDMRoomsForUserId(addr);
|
|
|
|
const rooms = [];
|
2017-10-27 17:29:56 +02:00
|
|
|
dmRooms.forEach((dmRoom) => {
|
|
|
|
const room = MatrixClientPeg.get().getRoom(dmRoom);
|
2017-10-27 17:20:17 +02:00
|
|
|
if (room) {
|
|
|
|
const me = room.getMember(MatrixClientPeg.get().credentials.userId);
|
|
|
|
if (me.membership == 'join') {
|
|
|
|
rooms.push(room);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return rooms;
|
|
|
|
}
|
|
|
|
|