2016-09-13 21:47:56 +08:00
|
|
|
/*
|
2021-06-16 17:18:32 +08:00
|
|
|
Copyright 2016 - 2021 The Matrix.org Foundation C.I.C.
|
2016-09-13 21:47:56 +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 { MatrixError } from "matrix-js-sdk/src/http-api";
|
2021-07-02 05:50:06 +08:00
|
|
|
import { defer, IDeferred } from "matrix-js-sdk/src/utils";
|
2021-10-23 06:23:32 +08:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-01-28 18:02:37 +08:00
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
|
|
|
import { EventType } from "matrix-js-sdk/src/@types/event";
|
|
|
|
import { HistoryVisibility } from "matrix-js-sdk/src/@types/partials";
|
2021-06-16 17:18:32 +08:00
|
|
|
|
|
|
|
import { MatrixClientPeg } from '../MatrixClientPeg';
|
|
|
|
import { AddressType, getAddressType } from '../UserAddress';
|
|
|
|
import { _t } from "../languageHandler";
|
2019-01-11 12:43:21 +08:00
|
|
|
import Modal from "../Modal";
|
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
2021-06-16 17:18:32 +08:00
|
|
|
import AskInviteAnywayDialog from "../components/views/dialogs/AskInviteAnywayDialog";
|
|
|
|
|
|
|
|
export enum InviteState {
|
|
|
|
Invited = "invited",
|
|
|
|
Error = "error",
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IError {
|
|
|
|
errorText: string;
|
|
|
|
errcode: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const UNKNOWN_PROFILE_ERRORS = ['M_NOT_FOUND', 'M_USER_NOT_FOUND', 'M_PROFILE_UNDISCLOSED', 'M_PROFILE_NOT_FOUND'];
|
|
|
|
|
|
|
|
export type CompletionStates = Record<string, InviteState>;
|
2016-09-13 21:47:56 +08:00
|
|
|
|
2021-07-12 18:32:06 +08:00
|
|
|
const USER_ALREADY_JOINED = "IO.ELEMENT.ALREADY_JOINED";
|
|
|
|
const USER_ALREADY_INVITED = "IO.ELEMENT.ALREADY_INVITED";
|
|
|
|
|
2016-09-13 21:47:56 +08:00
|
|
|
/**
|
2022-03-23 07:07:37 +08:00
|
|
|
* Invites multiple addresses to a room, handling rate limiting from the server
|
2016-09-13 21:47:56 +08:00
|
|
|
*/
|
|
|
|
export default class MultiInviter {
|
2022-01-28 18:02:37 +08:00
|
|
|
private readonly matrixClient: MatrixClient;
|
2021-06-16 17:18:32 +08:00
|
|
|
|
|
|
|
private canceled = false;
|
|
|
|
private addresses: string[] = [];
|
|
|
|
private busy = false;
|
|
|
|
private _fatal = false;
|
|
|
|
private completionStates: CompletionStates = {}; // State of each address (invited or error)
|
|
|
|
private errors: Record<string, IError> = {}; // { address: {errorText, errcode} }
|
|
|
|
private deferred: IDeferred<CompletionStates> = null;
|
|
|
|
private reason: string = null;
|
|
|
|
|
2017-08-16 21:58:30 +08:00
|
|
|
/**
|
2022-03-23 07:07:37 +08:00
|
|
|
* @param {string} roomId The ID of the room to invite to
|
2021-09-30 20:43:59 +08:00
|
|
|
* @param {function} progressCallback optional callback, fired after each invite.
|
2017-08-16 21:58:30 +08:00
|
|
|
*/
|
2022-03-23 07:07:37 +08:00
|
|
|
constructor(private roomId: string, private readonly progressCallback?: () => void) {
|
2022-01-28 18:02:37 +08:00
|
|
|
this.matrixClient = MatrixClientPeg.get();
|
2021-06-16 17:18:32 +08:00
|
|
|
}
|
2016-09-13 21:47:56 +08:00
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
public get fatal() {
|
|
|
|
return this._fatal;
|
2016-09-13 21:47:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invite users to this room. This may only be called once per
|
|
|
|
* instance of the class.
|
|
|
|
*
|
2021-06-16 17:18:32 +08:00
|
|
|
* @param {array} addresses Array of addresses to invite
|
2021-02-27 06:09:54 +08:00
|
|
|
* @param {string} reason Reason for inviting (optional)
|
2022-01-28 18:02:37 +08:00
|
|
|
* @param {boolean} sendSharedHistoryKeys whether to share e2ee keys with the invitees if applicable.
|
2016-09-13 21:47:56 +08:00
|
|
|
* @returns {Promise} Resolved when all invitations in the queue are complete
|
|
|
|
*/
|
2022-01-28 18:02:37 +08:00
|
|
|
public invite(addresses, reason?: string, sendSharedHistoryKeys = false): Promise<CompletionStates> {
|
2021-06-16 17:18:32 +08:00
|
|
|
if (this.addresses.length > 0) {
|
2016-09-13 21:47:56 +08:00
|
|
|
throw new Error("Already inviting/invited");
|
|
|
|
}
|
2021-06-16 17:18:32 +08:00
|
|
|
this.addresses.push(...addresses);
|
2021-02-27 06:09:54 +08:00
|
|
|
this.reason = reason;
|
2016-09-13 21:47:56 +08:00
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
for (const addr of this.addresses) {
|
2016-09-13 21:47:56 +08:00
|
|
|
if (getAddressType(addr) === null) {
|
2021-06-16 17:18:32 +08:00
|
|
|
this.completionStates[addr] = InviteState.Error;
|
2019-01-11 12:43:21 +08:00
|
|
|
this.errors[addr] = {
|
|
|
|
errcode: 'M_INVALID',
|
|
|
|
errorText: _t('Unrecognised address'),
|
|
|
|
};
|
2016-09-13 21:47:56 +08:00
|
|
|
}
|
|
|
|
}
|
2021-06-16 17:18:32 +08:00
|
|
|
this.deferred = defer<CompletionStates>();
|
|
|
|
this.inviteMore(0);
|
2016-09-13 21:47:56 +08:00
|
|
|
|
2022-01-28 18:02:37 +08:00
|
|
|
if (!sendSharedHistoryKeys || !this.roomId || !this.matrixClient.isRoomEncrypted(this.roomId)) {
|
|
|
|
return this.deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
const room = this.matrixClient.getRoom(this.roomId);
|
|
|
|
const visibilityEvent = room?.currentState.getStateEvents(EventType.RoomHistoryVisibility, "");
|
|
|
|
const visibility = visibilityEvent?.getContent().history_visibility;
|
|
|
|
|
|
|
|
if (visibility !== HistoryVisibility.WorldReadable && visibility !== HistoryVisibility.Shared) {
|
|
|
|
return this.deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.deferred.promise.then(async states => {
|
|
|
|
const invitedUsers = [];
|
|
|
|
for (const [addr, state] of Object.entries(states)) {
|
|
|
|
if (state === InviteState.Invited && getAddressType(addr) === AddressType.MatrixUserId) {
|
|
|
|
invitedUsers.push(addr);
|
|
|
|
}
|
|
|
|
}
|
2022-03-11 17:11:41 +08:00
|
|
|
|
2022-01-28 18:02:37 +08:00
|
|
|
logger.log("Sharing history with", invitedUsers);
|
2022-03-11 17:11:41 +08:00
|
|
|
this.matrixClient.sendSharedHistoryKeys(this.roomId, invitedUsers); // do this in the background
|
2022-01-28 18:02:37 +08:00
|
|
|
|
|
|
|
return states;
|
|
|
|
});
|
2016-09-13 21:47:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops inviting. Causes promises returned by invite() to be rejected.
|
|
|
|
*/
|
2021-06-16 17:18:32 +08:00
|
|
|
public cancel(): void {
|
2016-09-13 21:47:56 +08:00
|
|
|
if (!this.busy) return;
|
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
this.canceled = true;
|
2016-09-13 21:47:56 +08:00
|
|
|
this.deferred.reject(new Error('canceled'));
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
public getCompletionState(addr: string): InviteState {
|
2016-09-13 21:47:56 +08:00
|
|
|
return this.completionStates[addr];
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
public getErrorText(addr: string): string {
|
2019-01-11 12:43:21 +08:00
|
|
|
return this.errors[addr] ? this.errors[addr].errorText : null;
|
2016-09-13 21:47:56 +08:00
|
|
|
}
|
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
private async inviteToRoom(roomId: string, addr: string, ignoreProfile = false): Promise<{}> {
|
2018-11-30 06:05:53 +08:00
|
|
|
const addrType = getAddressType(addr);
|
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
if (addrType === AddressType.Email) {
|
2022-01-28 18:02:37 +08:00
|
|
|
return this.matrixClient.inviteByEmail(roomId, addr);
|
2021-06-16 17:18:32 +08:00
|
|
|
} else if (addrType === AddressType.MatrixUserId) {
|
2022-01-28 18:02:37 +08:00
|
|
|
const room = this.matrixClient.getRoom(roomId);
|
2019-03-02 04:36:24 +08:00
|
|
|
if (!room) throw new Error("Room not found");
|
|
|
|
|
|
|
|
const member = room.getMember(addr);
|
2021-07-13 18:37:31 +08:00
|
|
|
if (member?.membership === "join") {
|
2021-07-12 18:32:06 +08:00
|
|
|
throw new MatrixError({
|
|
|
|
errcode: USER_ALREADY_JOINED,
|
|
|
|
error: "Member already joined",
|
|
|
|
});
|
2021-07-13 18:37:31 +08:00
|
|
|
} else if (member?.membership === "invite") {
|
2021-07-12 18:32:06 +08:00
|
|
|
throw new MatrixError({
|
|
|
|
errcode: USER_ALREADY_INVITED,
|
2021-06-16 17:18:32 +08:00
|
|
|
error: "Member already invited",
|
|
|
|
});
|
2019-03-02 04:36:24 +08:00
|
|
|
}
|
|
|
|
|
2019-01-16 23:07:30 +08:00
|
|
|
if (!ignoreProfile && SettingsStore.getValue("promptBeforeInviteUnknownUsers", this.roomId)) {
|
2022-05-05 15:02:48 +08:00
|
|
|
try {
|
|
|
|
await this.matrixClient.getProfileInfo(addr);
|
|
|
|
} catch (err) {
|
|
|
|
// The error handling during the invitation process covers any API.
|
|
|
|
// Some errors must to me mapped from profile API errors to more specific ones to avoid collisions.
|
|
|
|
switch (err.errcode) {
|
|
|
|
case 'M_FORBIDDEN':
|
|
|
|
throw new MatrixError({ errcode: 'M_PROFILE_UNDISCLOSED' });
|
|
|
|
case 'M_NOT_FOUND':
|
|
|
|
throw new MatrixError({ errcode: 'M_USER_NOT_FOUND' });
|
|
|
|
default:
|
|
|
|
throw err;
|
|
|
|
}
|
2019-01-11 12:43:21 +08:00
|
|
|
}
|
2018-11-30 06:05:53 +08:00
|
|
|
}
|
|
|
|
|
2022-01-28 18:02:37 +08:00
|
|
|
return this.matrixClient.invite(roomId, addr, undefined, this.reason);
|
2018-11-30 06:05:53 +08:00
|
|
|
} else {
|
|
|
|
throw new Error('Unsupported address');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
private doInvite(address: string, ignoreProfile = false): Promise<void> {
|
|
|
|
return new Promise<void>((resolve, reject) => {
|
2021-09-21 23:48:09 +08:00
|
|
|
logger.log(`Inviting ${address}`);
|
2019-01-12 06:46:03 +08:00
|
|
|
|
2022-03-23 07:07:37 +08:00
|
|
|
const doInvite = this.inviteToRoom(this.roomId, address, ignoreProfile);
|
2019-01-11 12:43:21 +08:00
|
|
|
doInvite.then(() => {
|
2021-06-16 17:18:32 +08:00
|
|
|
if (this.canceled) {
|
2019-01-11 12:43:21 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
this.completionStates[address] = InviteState.Invited;
|
2019-01-11 12:43:21 +08:00
|
|
|
delete this.errors[address];
|
|
|
|
|
|
|
|
resolve();
|
2021-09-30 20:43:59 +08:00
|
|
|
this.progressCallback?.();
|
2019-01-11 12:43:21 +08:00
|
|
|
}).catch((err) => {
|
2021-06-16 17:18:32 +08:00
|
|
|
if (this.canceled) {
|
2019-01-11 12:43:21 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:30:53 +08:00
|
|
|
logger.error(err);
|
2019-11-28 11:29:11 +08:00
|
|
|
|
2022-03-29 22:02:12 +08:00
|
|
|
const isSpace = this.roomId && this.matrixClient.getRoom(this.roomId)?.isSpaceRoom();
|
|
|
|
|
|
|
|
let errorText: string;
|
2019-01-11 12:43:21 +08:00
|
|
|
let fatal = false;
|
2021-07-12 18:32:06 +08:00
|
|
|
switch (err.errcode) {
|
|
|
|
case "M_FORBIDDEN":
|
2022-03-29 22:02:12 +08:00
|
|
|
if (isSpace) {
|
|
|
|
errorText = _t('You do not have permission to invite people to this space.');
|
|
|
|
} else {
|
|
|
|
errorText = _t('You do not have permission to invite people to this room.');
|
|
|
|
}
|
2021-07-12 18:32:06 +08:00
|
|
|
fatal = true;
|
|
|
|
break;
|
|
|
|
case USER_ALREADY_INVITED:
|
2022-03-29 22:02:12 +08:00
|
|
|
if (isSpace) {
|
|
|
|
errorText = _t("User is already invited to the space");
|
|
|
|
} else {
|
|
|
|
errorText = _t("User is already invited to the room");
|
|
|
|
}
|
2021-07-12 18:32:06 +08:00
|
|
|
break;
|
|
|
|
case USER_ALREADY_JOINED:
|
2022-03-29 22:02:12 +08:00
|
|
|
if (isSpace) {
|
|
|
|
errorText = _t("User is already in the space");
|
|
|
|
} else {
|
|
|
|
errorText = _t("User is already in the room");
|
|
|
|
}
|
2021-07-12 18:32:06 +08:00
|
|
|
break;
|
|
|
|
case "M_LIMIT_EXCEEDED":
|
|
|
|
// we're being throttled so wait a bit & try again
|
|
|
|
setTimeout(() => {
|
|
|
|
this.doInvite(address, ignoreProfile).then(resolve, reject);
|
|
|
|
}, 5000);
|
|
|
|
return;
|
|
|
|
case "M_NOT_FOUND":
|
|
|
|
case "M_USER_NOT_FOUND":
|
2022-03-29 22:02:12 +08:00
|
|
|
errorText = _t("User does not exist");
|
2021-07-12 18:32:06 +08:00
|
|
|
break;
|
|
|
|
case "M_PROFILE_UNDISCLOSED":
|
2022-03-29 22:02:12 +08:00
|
|
|
errorText = _t("User may or may not exist");
|
2021-07-12 18:32:06 +08:00
|
|
|
break;
|
|
|
|
case "M_PROFILE_NOT_FOUND":
|
|
|
|
if (!ignoreProfile) {
|
|
|
|
// Invite without the profile check
|
2021-10-15 22:31:29 +08:00
|
|
|
logger.warn(`User ${address} does not have a profile - inviting anyways automatically`);
|
2021-07-12 18:32:06 +08:00
|
|
|
this.doInvite(address, true).then(resolve, reject);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "M_BAD_STATE":
|
|
|
|
errorText = _t("The user must be unbanned before they can be invited.");
|
|
|
|
break;
|
|
|
|
case "M_UNSUPPORTED_ROOM_VERSION":
|
2022-03-29 22:02:12 +08:00
|
|
|
if (isSpace) {
|
|
|
|
errorText = _t("The user's homeserver does not support the version of the space.");
|
|
|
|
} else {
|
|
|
|
errorText = _t("The user's homeserver does not support the version of the room.");
|
|
|
|
}
|
2021-07-12 18:32:06 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!errorText) {
|
2019-01-11 12:43:21 +08:00
|
|
|
errorText = _t('Unknown server error');
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
this.completionStates[address] = InviteState.Error;
|
|
|
|
this.errors[address] = { errorText, errcode: err.errcode };
|
2019-01-11 12:43:21 +08:00
|
|
|
|
|
|
|
this.busy = !fatal;
|
2021-06-16 17:18:32 +08:00
|
|
|
this._fatal = fatal;
|
2019-01-11 12:43:21 +08:00
|
|
|
|
|
|
|
if (fatal) {
|
2021-06-16 17:18:32 +08:00
|
|
|
reject(err);
|
2019-01-11 12:43:21 +08:00
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-11-30 06:05:53 +08:00
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
private inviteMore(nextIndex: number, ignoreProfile = false): void {
|
|
|
|
if (this.canceled) {
|
2016-09-13 21:47:56 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
if (nextIndex === this.addresses.length) {
|
2016-09-13 21:47:56 +08:00
|
|
|
this.busy = false;
|
2022-03-23 07:07:37 +08:00
|
|
|
if (Object.keys(this.errors).length > 0) {
|
2019-01-11 12:43:21 +08:00
|
|
|
// There were problems inviting some people - see if we can invite them
|
|
|
|
// without caring if they exist or not.
|
2021-06-16 17:18:32 +08:00
|
|
|
const unknownProfileUsers = Object.keys(this.errors)
|
|
|
|
.filter(a => UNKNOWN_PROFILE_ERRORS.includes(this.errors[a].errcode));
|
2019-01-11 12:43:21 +08:00
|
|
|
|
2019-01-12 06:46:03 +08:00
|
|
|
if (unknownProfileUsers.length > 0) {
|
|
|
|
const inviteUnknowns = () => {
|
2021-06-16 17:18:32 +08:00
|
|
|
const promises = unknownProfileUsers.map(u => this.doInvite(u, true));
|
2019-01-11 12:43:21 +08:00
|
|
|
Promise.all(promises).then(() => this.deferred.resolve(this.completionStates));
|
|
|
|
};
|
|
|
|
|
2019-01-16 23:07:30 +08:00
|
|
|
if (!SettingsStore.getValue("promptBeforeInviteUnknownUsers", this.roomId)) {
|
2019-01-12 06:46:03 +08:00
|
|
|
inviteUnknowns();
|
2019-01-11 12:43:21 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-21 23:48:09 +08:00
|
|
|
logger.log("Showing failed to invite dialog...");
|
2022-06-15 00:51:51 +08:00
|
|
|
Modal.createDialog(AskInviteAnywayDialog, {
|
2021-06-16 17:18:32 +08:00
|
|
|
unknownProfileUsers: unknownProfileUsers.map(u => ({
|
|
|
|
userId: u,
|
|
|
|
errorText: this.errors[u].errorText,
|
|
|
|
})),
|
2019-01-12 06:46:03 +08:00
|
|
|
onInviteAnyways: () => inviteUnknowns(),
|
2019-01-11 12:43:21 +08:00
|
|
|
onGiveUp: () => {
|
|
|
|
// Fake all the completion states because we already warned the user
|
2019-01-12 06:46:03 +08:00
|
|
|
for (const addr of unknownProfileUsers) {
|
2021-06-16 17:18:32 +08:00
|
|
|
this.completionStates[addr] = InviteState.Invited;
|
2019-01-11 12:43:21 +08:00
|
|
|
}
|
|
|
|
this.deferred.resolve(this.completionStates);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-09-13 21:47:56 +08:00
|
|
|
this.deferred.resolve(this.completionStates);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
const addr = this.addresses[nextIndex];
|
2016-09-13 21:47:56 +08:00
|
|
|
|
|
|
|
// don't try to invite it if it's an invalid address
|
|
|
|
// (it will already be marked as an error though,
|
|
|
|
// so no need to do so again)
|
|
|
|
if (getAddressType(addr) === null) {
|
2021-06-16 17:18:32 +08:00
|
|
|
this.inviteMore(nextIndex + 1);
|
2016-09-13 21:47:56 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't re-invite (there's no way in the UI to do this, but
|
|
|
|
// for sanity's sake)
|
2021-06-16 17:18:32 +08:00
|
|
|
if (this.completionStates[addr] === InviteState.Invited) {
|
|
|
|
this.inviteMore(nextIndex + 1);
|
2016-09-13 21:47:56 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-16 17:18:32 +08:00
|
|
|
this.doInvite(addr, ignoreProfile).then(() => {
|
|
|
|
this.inviteMore(nextIndex + 1, ignoreProfile);
|
2019-01-11 12:43:21 +08:00
|
|
|
}).catch(() => this.deferred.resolve(this.completionStates));
|
2016-09-13 21:47:56 +08:00
|
|
|
}
|
|
|
|
}
|