2016-08-10 19:11:49 +03:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2018-11-30 01:05:53 +03:00
|
|
|
Copyright 2017, 2018 New Vector Ltd
|
2020-01-17 00:40:12 +03:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2016-08-10 19:11:49 +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.
|
|
|
|
*/
|
|
|
|
|
2018-11-30 01:05:53 +03:00
|
|
|
import React from 'react';
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from './MatrixClientPeg';
|
2016-09-13 16:56:54 +03:00
|
|
|
import MultiInviter from './utils/MultiInviter';
|
2017-08-15 12:57:24 +03:00
|
|
|
import Modal from './Modal';
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as sdk from './';
|
2017-08-14 19:43:00 +03:00
|
|
|
import { _t } from './languageHandler';
|
2020-01-17 00:40:12 +03:00
|
|
|
import {KIND_DM, KIND_INVITE} from "./components/views/dialogs/InviteDialog";
|
2020-08-26 06:02:32 +03:00
|
|
|
import PrototypeCommunityInviteDialog from "./components/views/dialogs/PrototypeCommunityInviteDialog";
|
2016-08-10 19:11:49 +03:00
|
|
|
|
2016-09-13 16:47:56 +03:00
|
|
|
/**
|
|
|
|
* Invites multiple addresses to a room
|
|
|
|
* Simpler interface to utils/MultiInviter but with
|
|
|
|
* no option to cancel.
|
|
|
|
*
|
2017-08-15 15:50:15 +03: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 16:47:56 +03:00
|
|
|
*/
|
2020-01-15 09:32:00 +03:00
|
|
|
export function inviteMultipleToRoom(roomId, addrs) {
|
2017-01-20 17:22:27 +03:00
|
|
|
const inviter = new MultiInviter(roomId);
|
2018-12-05 21:27:48 +03:00
|
|
|
return inviter.invite(addrs).then(states => Promise.resolve({states, inviter}));
|
2016-09-13 16:55:16 +03:00
|
|
|
}
|
2016-09-13 16:51:46 +03:00
|
|
|
|
2017-08-14 19:43:00 +03:00
|
|
|
export function showStartChatInviteDialog() {
|
2020-01-23 07:14:53 +03:00
|
|
|
// This dialog handles the room creation internally - we don't need to worry about it.
|
|
|
|
const InviteDialog = sdk.getComponent("dialogs.InviteDialog");
|
|
|
|
Modal.createTrackedDialog(
|
|
|
|
'Start DM', '', InviteDialog, {kind: KIND_DM},
|
|
|
|
/*className=*/null, /*isPriority=*/false, /*isStatic=*/true,
|
|
|
|
);
|
2017-08-14 19:43:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function showRoomInviteDialog(roomId) {
|
2020-01-23 07:14:53 +03:00
|
|
|
// This dialog handles the room creation internally - we don't need to worry about it.
|
|
|
|
const InviteDialog = sdk.getComponent("dialogs.InviteDialog");
|
|
|
|
Modal.createTrackedDialog(
|
|
|
|
'Invite Users', '', InviteDialog, {kind: KIND_INVITE, roomId},
|
|
|
|
/*className=*/null, /*isPriority=*/false, /*isStatic=*/true,
|
|
|
|
);
|
2017-08-14 19:43:00 +03:00
|
|
|
}
|
|
|
|
|
2020-08-26 06:02:32 +03:00
|
|
|
export function showCommunityRoomInviteDialog(roomId, communityName) {
|
|
|
|
Modal.createTrackedDialog(
|
|
|
|
'Invite Users to Community', '', PrototypeCommunityInviteDialog, {communityName, roomId},
|
|
|
|
/*className=*/null, /*isPriority=*/false, /*isStatic=*/true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-29 20:45:07 +03: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-11-28 06:29:11 +03:00
|
|
|
export function inviteUsersToRoom(roomId, userIds) {
|
|
|
|
return inviteMultipleToRoom(roomId, userIds).then((result) => {
|
2017-08-14 19:43:00 +03:00
|
|
|
const room = MatrixClientPeg.get().getRoom(roomId);
|
2020-08-26 06:02:32 +03:00
|
|
|
showAnyInviteErrors(result.states, room, result.inviter);
|
2017-08-14 19:43:00 +03: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")),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-26 06:02:32 +03:00
|
|
|
export function showAnyInviteErrors(addrs, room, inviter) {
|
2017-08-14 19:43:00 +03:00
|
|
|
// Show user any errors
|
2018-11-30 01:05:53 +03: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 19:43:00 +03:00
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
2018-11-30 01:05:53 +03: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 19:43:00 +03:00
|
|
|
});
|
2020-08-26 06:02:32 +03:00
|
|
|
return false;
|
2018-11-30 01:05:53 +03: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) {
|
2019-11-27 20:48:05 +03:00
|
|
|
// React 16 doesn't let us use `errorList.join(<br />)` anymore, so this is our solution
|
2019-11-28 02:57:44 +03:00
|
|
|
const description = <div>{errorList.map(e => <div key={e}>{e}</div>)}</div>;
|
2019-11-27 20:48:05 +03:00
|
|
|
|
2018-11-30 01:05:53 +03:00
|
|
|
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}),
|
2019-11-27 20:44:36 +03:00
|
|
|
description,
|
2018-11-30 01:05:53 +03:00
|
|
|
});
|
2020-08-26 06:02:32 +03:00
|
|
|
return false;
|
2018-11-30 01:05:53 +03:00
|
|
|
}
|
2017-08-14 19:43:00 +03:00
|
|
|
}
|
2018-11-30 01:05:53 +03:00
|
|
|
|
2020-08-26 06:02:32 +03:00
|
|
|
return true;
|
2017-08-14 19:43:00 +03:00
|
|
|
}
|