2016-08-11 00:11:49 +08:00
|
|
|
/*
|
2021-06-16 17:18:32 +08:00
|
|
|
Copyright 2016 - 2021 The Matrix.org Foundation C.I.C.
|
2016-08-11 00:11:49 +08: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.
|
|
|
|
*/
|
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
import React from "react";
|
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2021-06-24 17:03:32 +08:00
|
|
|
import { User } from "matrix-js-sdk/src/models/user";
|
2021-06-16 17:18:32 +08:00
|
|
|
|
|
|
|
import { MatrixClientPeg } from './MatrixClientPeg';
|
|
|
|
import MultiInviter, { CompletionStates } from './utils/MultiInviter';
|
2017-08-15 17:57:24 +08:00
|
|
|
import Modal from './Modal';
|
2019-12-20 09:19:56 +08:00
|
|
|
import * as sdk from './';
|
2017-08-15 00:43:00 +08:00
|
|
|
import { _t } from './languageHandler';
|
2021-06-16 17:18:32 +08:00
|
|
|
import InviteDialog, { KIND_DM, KIND_INVITE } from "./components/views/dialogs/InviteDialog";
|
2020-08-26 22:50:32 +08:00
|
|
|
import CommunityPrototypeInviteDialog from "./components/views/dialogs/CommunityPrototypeInviteDialog";
|
2021-06-16 17:18:32 +08:00
|
|
|
import { CommunityPrototypeStore } from "./stores/CommunityPrototypeStore";
|
2021-06-24 17:03:32 +08:00
|
|
|
import BaseAvatar from "./components/views/avatars/BaseAvatar";
|
|
|
|
import { mediaFromMxc } from "./customisations/Media";
|
2016-08-11 00:11:49 +08:00
|
|
|
|
2021-06-16 18:48:14 +08:00
|
|
|
export interface IInviteResult {
|
|
|
|
states: CompletionStates;
|
|
|
|
inviter: MultiInviter;
|
|
|
|
}
|
|
|
|
|
2016-09-13 21:47:56 +08:00
|
|
|
/**
|
|
|
|
* Invites multiple addresses to a room
|
|
|
|
* Simpler interface to utils/MultiInviter but with
|
|
|
|
* no option to cancel.
|
|
|
|
*
|
2017-08-15 20:50:15 +08:00
|
|
|
* @param {string} roomId The ID of the room to invite to
|
2021-06-16 17:18:32 +08:00
|
|
|
* @param {string[]} addresses Array of strings of addresses to invite. May be matrix IDs or 3pids.
|
2017-08-15 20:50:15 +08:00
|
|
|
* @returns {Promise} Promise
|
2016-09-13 21:47:56 +08:00
|
|
|
*/
|
2021-06-16 18:48:14 +08:00
|
|
|
export function inviteMultipleToRoom(roomId: string, addresses: string[]): Promise<IInviteResult> {
|
2017-01-20 22:22:27 +08:00
|
|
|
const inviter = new MultiInviter(roomId);
|
2021-06-16 17:18:32 +08:00
|
|
|
return inviter.invite(addresses).then(states => Promise.resolve({ states, inviter }));
|
2016-09-13 21:55:16 +08:00
|
|
|
}
|
2016-09-13 21:51:46 +08:00
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
export function showStartChatInviteDialog(initialText = ""): void {
|
2020-01-23 12:14:53 +08:00
|
|
|
// This dialog handles the room creation internally - we don't need to worry about it.
|
|
|
|
const InviteDialog = sdk.getComponent("dialogs.InviteDialog");
|
|
|
|
Modal.createTrackedDialog(
|
2020-11-11 21:36:17 +08:00
|
|
|
'Start DM', '', InviteDialog, {kind: KIND_DM, initialText},
|
2020-01-23 12:14:53 +08:00
|
|
|
/*className=*/null, /*isPriority=*/false, /*isStatic=*/true,
|
|
|
|
);
|
2017-08-15 00:43:00 +08:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
export function showRoomInviteDialog(roomId: string, initialText = ""): void {
|
2020-01-23 12:14:53 +08:00
|
|
|
// This dialog handles the room creation internally - we don't need to worry about it.
|
|
|
|
Modal.createTrackedDialog(
|
2021-03-03 19:34:29 +08:00
|
|
|
"Invite Users", "", InviteDialog, {
|
|
|
|
kind: KIND_INVITE,
|
2021-03-24 23:36:20 +08:00
|
|
|
initialText,
|
2021-03-02 18:07:43 +08:00
|
|
|
roomId,
|
|
|
|
},
|
2020-01-23 12:14:53 +08:00
|
|
|
/*className=*/null, /*isPriority=*/false, /*isStatic=*/true,
|
|
|
|
);
|
2017-08-15 00:43:00 +08:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
export function showCommunityRoomInviteDialog(roomId: string, communityName: string): void {
|
2020-08-26 11:02:32 +08:00
|
|
|
Modal.createTrackedDialog(
|
2020-08-26 22:50:32 +08:00
|
|
|
'Invite Users to Community', '', CommunityPrototypeInviteDialog, {communityName, roomId},
|
2020-08-26 11:02:32 +08:00
|
|
|
/*className=*/null, /*isPriority=*/false, /*isStatic=*/true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
export function showCommunityInviteDialog(communityId: string): void {
|
2020-09-01 00:12:12 +08:00
|
|
|
const chat = CommunityPrototypeStore.instance.getGeneralChat(communityId);
|
2020-08-29 03:22:20 +08:00
|
|
|
if (chat) {
|
2020-08-29 10:08:12 +08:00
|
|
|
const name = CommunityPrototypeStore.instance.getCommunityName(communityId);
|
2020-08-29 04:56:59 +08:00
|
|
|
showCommunityRoomInviteDialog(chat.roomId, name);
|
2020-08-29 03:22:20 +08:00
|
|
|
} else {
|
|
|
|
throw new Error("Failed to locate appropriate room to start an invite in");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-30 01:45:07 +08: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
|
|
|
|
*/
|
2021-06-16 17:18:32 +08:00
|
|
|
export function isValid3pidInvite(event: MatrixEvent): boolean {
|
2019-03-30 01:45:07 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
export function inviteUsersToRoom(roomId: string, userIds: string[]): Promise<void> {
|
2019-11-28 11:29:11 +08:00
|
|
|
return inviteMultipleToRoom(roomId, userIds).then((result) => {
|
2017-08-15 00:43:00 +08:00
|
|
|
const room = MatrixClientPeg.get().getRoom(roomId);
|
2020-08-26 11:02:32 +08:00
|
|
|
showAnyInviteErrors(result.states, room, result.inviter);
|
2017-08-15 00:43:00 +08: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")),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-24 17:03:32 +08:00
|
|
|
export function showAnyInviteErrors(
|
|
|
|
states: CompletionStates,
|
|
|
|
room: Room,
|
|
|
|
inviter: MultiInviter,
|
|
|
|
userMap?: Map<string, Member>,
|
|
|
|
): boolean {
|
2017-08-15 00:43:00 +08:00
|
|
|
// Show user any errors
|
2021-06-16 17:18:32 +08:00
|
|
|
const failedUsers = Object.keys(states).filter(a => states[a] === 'error');
|
2018-11-30 06:05:53 +08:00
|
|
|
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-15 00:43:00 +08:00
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
2018-11-30 06:05:53 +08: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-15 00:43:00 +08:00
|
|
|
});
|
2020-08-26 11:02:32 +08:00
|
|
|
return false;
|
2018-11-30 06:05:53 +08:00
|
|
|
} else {
|
|
|
|
const errorList = [];
|
|
|
|
for (const addr of failedUsers) {
|
2021-06-16 17:18:32 +08:00
|
|
|
if (states[addr] === "error") {
|
2018-11-30 06:05:53 +08:00
|
|
|
const reason = inviter.getErrorText(addr);
|
|
|
|
errorList.push(addr + ": " + reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 17:03:32 +08:00
|
|
|
const cli = MatrixClientPeg.get();
|
2018-11-30 06:05:53 +08:00
|
|
|
if (errorList.length > 0) {
|
2019-11-28 01:48:05 +08:00
|
|
|
// React 16 doesn't let us use `errorList.join(<br />)` anymore, so this is our solution
|
2021-06-24 17:03:32 +08:00
|
|
|
const description = <div className="mx_InviteDialog_multiInviterError">
|
|
|
|
<h4>{ _t("We sent the others, but the below people couldn't be invited to <RoomName/>", {}, {
|
|
|
|
RoomName: () => <b>{ room.name }</b>,
|
|
|
|
}) }</h4>
|
|
|
|
<div>
|
|
|
|
{ failedUsers.map(addr => {
|
|
|
|
const user = userMap?.get(addr) || cli.getUser(addr);
|
|
|
|
const name = (user as Member).name || (user as User).rawDisplayName;
|
|
|
|
const avatarUrl = (user as Member).getMxcAvatarUrl?.() || (user as User).avatarUrl;
|
|
|
|
return <div key={addr} className="mx_InviteDialog_multiInviterError_entry">
|
|
|
|
<div className="mx_InviteDialog_multiInviterError_entry_userProfile">
|
|
|
|
<BaseAvatar
|
|
|
|
url={avatarUrl ? mediaFromMxc(avatarUrl).getSquareThumbnailHttp(24) : null}
|
|
|
|
name={name}
|
|
|
|
idName={user.userId}
|
|
|
|
width={24}
|
|
|
|
height={24}
|
|
|
|
/>
|
|
|
|
<span className="mx_InviteDialog_multiInviterError_entry_name">{ name }</span>
|
|
|
|
<span className="mx_InviteDialog_multiInviterError_entry_userId">{ user.userId }</span>
|
|
|
|
</div>
|
|
|
|
<div className="mx_InviteDialog_multiInviterError_entry_error">
|
|
|
|
{ inviter.getErrorText(addr) }
|
|
|
|
</div>
|
|
|
|
</div>;
|
|
|
|
}) }
|
|
|
|
</div>
|
|
|
|
</div>;
|
2019-11-28 01:48:05 +08:00
|
|
|
|
2018-11-30 06:05:53 +08:00
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
2021-06-24 17:03:32 +08:00
|
|
|
Modal.createTrackedDialog("Some invites could not be sent", "", ErrorDialog, {
|
|
|
|
title: _t("Some invites couldn't be sent"),
|
2019-11-28 01:44:36 +08:00
|
|
|
description,
|
2018-11-30 06:05:53 +08:00
|
|
|
});
|
2020-08-26 11:02:32 +08:00
|
|
|
return false;
|
2018-11-30 06:05:53 +08:00
|
|
|
}
|
2017-08-15 00:43:00 +08:00
|
|
|
}
|
2018-11-30 06:05:53 +08:00
|
|
|
|
2020-08-26 11:02:32 +08:00
|
|
|
return true;
|
2017-08-15 00:43:00 +08:00
|
|
|
}
|
2021-06-24 17:03:32 +08:00
|
|
|
|
|
|
|
// This is the interface that is expected by various components in the Invite Dialog and RoomInvite.
|
|
|
|
// It is a bit awkward because it also matches the RoomMember class from the js-sdk with some extra support
|
|
|
|
// for 3PIDs/email addresses.
|
|
|
|
export abstract class Member {
|
|
|
|
/**
|
|
|
|
* The display name of this Member. For users this should be their profile's display
|
|
|
|
* name or user ID if none set. For 3PIDs this should be the 3PID address (email).
|
|
|
|
*/
|
|
|
|
public abstract get name(): string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ID of this Member. For users this should be their user ID. For 3PIDs this should
|
|
|
|
* be the 3PID address (email).
|
|
|
|
*/
|
|
|
|
public abstract get userId(): string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the MXC URL of this Member's avatar. For users this should be their profile's
|
|
|
|
* avatar MXC URL or null if none set. For 3PIDs this should always be null.
|
|
|
|
*/
|
|
|
|
public abstract getMxcAvatarUrl(): string;
|
|
|
|
}
|