2016-08-10 19:11:49 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2016-2021 The Matrix.org Foundation C.I.C.
|
2016-08-10 19:11:49 +03:00
|
|
|
|
2024-09-09 16:57:16 +03:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2016-08-10 19:11:49 +03:00
|
|
|
*/
|
|
|
|
|
2023-02-28 13:31:48 +03:00
|
|
|
import React, { ComponentProps } from "react";
|
2023-08-04 10:36:16 +03:00
|
|
|
import { Room, MatrixEvent, MatrixClient, User, EventType } from "matrix-js-sdk/src/matrix";
|
2021-10-23 01:23:32 +03:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2021-06-16 12:18:32 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
import MultiInviter, { CompletionStates } from "./utils/MultiInviter";
|
|
|
|
import Modal from "./Modal";
|
|
|
|
import { _t } from "./languageHandler";
|
2022-03-25 01:30:53 +03:00
|
|
|
import InviteDialog from "./components/views/dialogs/InviteDialog";
|
2021-06-24 12:03:32 +03:00
|
|
|
import BaseAvatar from "./components/views/avatars/BaseAvatar";
|
|
|
|
import { mediaFromMxc } from "./customisations/Media";
|
2021-07-02 13:12:41 +03:00
|
|
|
import ErrorDialog from "./components/views/dialogs/ErrorDialog";
|
2023-02-28 13:31:48 +03:00
|
|
|
import { InviteKind } from "./components/views/dialogs/InviteDialogTypes";
|
2022-04-14 18:52:12 +03:00
|
|
|
import { Member } from "./utils/direct-messages";
|
2016-08-10 19:11:49 +03:00
|
|
|
|
2021-06-16 13:48:14 +03:00
|
|
|
export interface IInviteResult {
|
|
|
|
states: CompletionStates;
|
|
|
|
inviter: MultiInviter;
|
|
|
|
}
|
|
|
|
|
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
|
2021-06-16 12:18:32 +03:00
|
|
|
* @param {string[]} addresses Array of strings of addresses to invite. May be matrix IDs or 3pids.
|
2021-09-30 15:43:59 +03:00
|
|
|
* @param {function} progressCallback optional callback, fired after each invite.
|
2017-08-15 15:50:15 +03:00
|
|
|
* @returns {Promise} Promise
|
2016-09-13 16:47:56 +03:00
|
|
|
*/
|
2024-10-18 17:38:49 +03:00
|
|
|
export async function inviteMultipleToRoom(
|
2023-05-23 18:24:12 +03:00
|
|
|
client: MatrixClient,
|
2021-09-30 15:43:59 +03:00
|
|
|
roomId: string,
|
|
|
|
addresses: string[],
|
|
|
|
progressCallback?: () => void,
|
|
|
|
): Promise<IInviteResult> {
|
2023-05-23 18:24:12 +03:00
|
|
|
const inviter = new MultiInviter(client, roomId, progressCallback);
|
2024-10-18 17:17:58 +03:00
|
|
|
return { states: await inviter.invite(addresses), inviter };
|
2016-09-13 16:55:16 +03:00
|
|
|
}
|
2016-09-13 16:51:46 +03:00
|
|
|
|
2021-06-16 12:18:32 +03:00
|
|
|
export function showStartChatInviteDialog(initialText = ""): void {
|
2020-01-23 07:14:53 +03:00
|
|
|
// This dialog handles the room creation internally - we don't need to worry about it.
|
2022-06-14 19:51:51 +03:00
|
|
|
Modal.createDialog(
|
2022-12-12 14:24:14 +03:00
|
|
|
InviteDialog,
|
2023-02-28 13:31:48 +03:00
|
|
|
{ kind: InviteKind.Dm, initialText },
|
2022-12-12 14:24:14 +03:00
|
|
|
/*className=*/ "mx_InviteDialog_flexWrapper",
|
|
|
|
/*isPriority=*/ false,
|
|
|
|
/*isStatic=*/ true,
|
2020-01-23 07:14:53 +03:00
|
|
|
);
|
2017-08-14 19:43:00 +03:00
|
|
|
}
|
|
|
|
|
2021-06-16 12:18:32 +03:00
|
|
|
export function showRoomInviteDialog(roomId: string, initialText = ""): void {
|
2020-01-23 07:14:53 +03:00
|
|
|
// This dialog handles the room creation internally - we don't need to worry about it.
|
2022-06-14 19:51:51 +03:00
|
|
|
Modal.createDialog(
|
2022-12-12 14:24:14 +03:00
|
|
|
InviteDialog,
|
|
|
|
{
|
2023-02-28 13:31:48 +03:00
|
|
|
kind: InviteKind.Invite,
|
2021-03-24 18:36:20 +03:00
|
|
|
initialText,
|
2021-03-02 13:07:43 +03:00
|
|
|
roomId,
|
2023-02-28 13:31:48 +03:00
|
|
|
} as Omit<ComponentProps<typeof InviteDialog>, "onFinished">,
|
2022-12-12 14:24:14 +03:00
|
|
|
/*className=*/ "mx_InviteDialog_flexWrapper",
|
|
|
|
/*isPriority=*/ false,
|
|
|
|
/*isStatic=*/ true,
|
2020-01-23 07:14:53 +03:00
|
|
|
);
|
2017-08-14 19:43:00 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2021-06-16 12:18:32 +03:00
|
|
|
export function isValid3pidInvite(event: MatrixEvent): boolean {
|
2022-05-04 00:04:37 +03:00
|
|
|
if (!event || event.getType() !== EventType.RoomThirdPartyInvite) return false;
|
2019-03-29 20:45:07 +03:00
|
|
|
|
|
|
|
// any events without these keys are not valid 3pid invites, so we ignore them
|
2022-12-12 14:24:14 +03:00
|
|
|
const requiredKeys = ["key_validity_url", "public_key", "display_name"];
|
|
|
|
if (requiredKeys.some((key) => !event.getContent()[key])) {
|
2022-05-04 00:04:37 +03:00
|
|
|
return false;
|
2019-03-29 20:45:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Valid enough by our standards
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-28 13:02:37 +03:00
|
|
|
export function inviteUsersToRoom(
|
2023-05-23 18:24:12 +03:00
|
|
|
client: MatrixClient,
|
2022-01-28 13:02:37 +03:00
|
|
|
roomId: string,
|
|
|
|
userIds: string[],
|
|
|
|
progressCallback?: () => void,
|
|
|
|
): Promise<void> {
|
2024-06-14 16:13:41 +03:00
|
|
|
return inviteMultipleToRoom(client, roomId, userIds, progressCallback)
|
2022-12-12 14:24:14 +03:00
|
|
|
.then((result) => {
|
2023-05-30 12:36:34 +03:00
|
|
|
const room = client.getRoom(roomId)!;
|
2022-12-12 14:24:14 +03:00
|
|
|
showAnyInviteErrors(result.states, room, result.inviter);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
logger.error(err.stack);
|
|
|
|
Modal.createDialog(ErrorDialog, {
|
2023-09-22 18:39:40 +03:00
|
|
|
title: _t("invite|failed_title"),
|
2024-10-16 19:38:22 +03:00
|
|
|
description: err?.message ?? _t("invite|failed_generic"),
|
2022-12-12 14:24:14 +03:00
|
|
|
});
|
2017-08-14 19:43:00 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-24 12:03:32 +03:00
|
|
|
export function showAnyInviteErrors(
|
|
|
|
states: CompletionStates,
|
|
|
|
room: Room,
|
|
|
|
inviter: MultiInviter,
|
|
|
|
userMap?: Map<string, Member>,
|
|
|
|
): boolean {
|
2017-08-14 19:43:00 +03:00
|
|
|
// Show user any errors
|
2022-12-12 14:24:14 +03:00
|
|
|
const failedUsers = Object.keys(states).filter((a) => states[a] === "error");
|
2018-11-30 01:05:53 +03: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.
|
2022-06-14 19:51:51 +03:00
|
|
|
Modal.createDialog(ErrorDialog, {
|
2023-09-22 18:39:40 +03:00
|
|
|
title: _t("invite|room_failed_title", { roomName: room.name }),
|
2018-11-30 01:05:53 +03:00
|
|
|
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 {
|
2023-02-15 16:36:22 +03:00
|
|
|
const errorList: string[] = [];
|
2018-11-30 01:05:53 +03:00
|
|
|
for (const addr of failedUsers) {
|
2021-06-16 12:18:32 +03:00
|
|
|
if (states[addr] === "error") {
|
2018-11-30 01:05:53 +03:00
|
|
|
const reason = inviter.getErrorText(addr);
|
|
|
|
errorList.push(addr + ": " + reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 12:36:34 +03:00
|
|
|
const cli = room.client;
|
2018-11-30 01:05:53 +03:00
|
|
|
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
|
2022-12-12 14:24:14 +03:00
|
|
|
const description = (
|
|
|
|
<div className="mx_InviteDialog_multiInviterError">
|
|
|
|
<h4>
|
|
|
|
{_t(
|
2023-09-22 18:39:40 +03:00
|
|
|
"invite|room_failed_partial",
|
2022-12-12 14:24:14 +03:00
|
|
|
{},
|
|
|
|
{
|
2024-09-13 14:09:41 +03:00
|
|
|
RoomName: () => <strong>{room.name}</strong>,
|
2022-12-12 14:24:14 +03:00
|
|
|
},
|
|
|
|
)}
|
|
|
|
</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_tile mx_InviteDialog_tile--inviterError">
|
|
|
|
<div className="mx_InviteDialog_tile_avatarStack">
|
|
|
|
<BaseAvatar
|
2023-02-15 16:36:22 +03:00
|
|
|
url={
|
|
|
|
(avatarUrl && mediaFromMxc(avatarUrl).getSquareThumbnailHttp(24)) ??
|
|
|
|
undefined
|
|
|
|
}
|
|
|
|
name={name!}
|
2023-02-13 20:01:43 +03:00
|
|
|
idName={user?.userId}
|
2023-08-24 06:48:35 +03:00
|
|
|
size="36px"
|
2022-12-12 14:24:14 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="mx_InviteDialog_tile_nameStack">
|
|
|
|
<span className="mx_InviteDialog_tile_nameStack_name">{name}</span>
|
2023-02-13 20:01:43 +03:00
|
|
|
<span className="mx_InviteDialog_tile_nameStack_userId">{user?.userId}</span>
|
2022-12-12 14:24:14 +03:00
|
|
|
</div>
|
|
|
|
<div className="mx_InviteDialog_tile--inviterError_errorText">
|
|
|
|
{inviter.getErrorText(addr)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
2021-06-24 12:03:32 +03:00
|
|
|
</div>
|
2022-12-12 14:24:14 +03:00
|
|
|
);
|
2019-11-27 20:48:05 +03:00
|
|
|
|
2022-06-14 19:51:51 +03:00
|
|
|
Modal.createDialog(ErrorDialog, {
|
2023-09-22 18:39:40 +03:00
|
|
|
title: _t("invite|room_failed_partial_title"),
|
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
|
|
|
}
|