2016-09-13 13:47:56 +00:00
|
|
|
/*
|
2021-06-16 09:18:32 +00:00
|
|
|
Copyright 2016 - 2021 The Matrix.org Foundation C.I.C.
|
2016-09-13 13:47:56 +00: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 09:18:32 +00:00
|
|
|
import { MatrixError } from "matrix-js-sdk/src/http-api";
|
2021-07-01 21:50:06 +00:00
|
|
|
import { defer, IDeferred } from "matrix-js-sdk/src/utils";
|
2021-06-16 09:18:32 +00:00
|
|
|
|
|
|
|
import { MatrixClientPeg } from '../MatrixClientPeg';
|
|
|
|
import { AddressType, getAddressType } from '../UserAddress';
|
2018-05-01 10:18:45 +00:00
|
|
|
import GroupStore from '../stores/GroupStore';
|
2021-06-16 09:18:32 +00:00
|
|
|
import { _t } from "../languageHandler";
|
2019-01-11 04:43:21 +00:00
|
|
|
import Modal from "../Modal";
|
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
2021-06-16 09:18:32 +00:00
|
|
|
import AskInviteAnywayDialog from "../components/views/dialogs/AskInviteAnywayDialog";
|
|
|
|
|
2021-09-21 15:48:09 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
2021-06-16 09:18:32 +00:00
|
|
|
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 13:47:56 +00:00
|
|
|
|
2021-07-12 10:32:06 +00:00
|
|
|
const USER_ALREADY_JOINED = "IO.ELEMENT.ALREADY_JOINED";
|
|
|
|
const USER_ALREADY_INVITED = "IO.ELEMENT.ALREADY_INVITED";
|
|
|
|
|
2016-09-13 13:47:56 +00:00
|
|
|
/**
|
2017-08-16 13:58:30 +00:00
|
|
|
* Invites multiple addresses to a room or group, handling rate limiting from the server
|
2016-09-13 13:47:56 +00:00
|
|
|
*/
|
|
|
|
export default class MultiInviter {
|
2021-06-16 09:18:32 +00:00
|
|
|
private readonly roomId?: string;
|
|
|
|
private readonly groupId?: string;
|
|
|
|
|
|
|
|
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 13:58:30 +00:00
|
|
|
/**
|
|
|
|
* @param {string} targetId The ID of the room or group to invite to
|
2021-09-30 12:43:59 +00:00
|
|
|
* @param {function} progressCallback optional callback, fired after each invite.
|
2017-08-16 13:58:30 +00:00
|
|
|
*/
|
2021-09-30 12:43:59 +00:00
|
|
|
constructor(targetId: string, private readonly progressCallback?: () => void) {
|
2017-08-16 13:58:30 +00:00
|
|
|
if (targetId[0] === '+') {
|
|
|
|
this.roomId = null;
|
|
|
|
this.groupId = targetId;
|
|
|
|
} else {
|
|
|
|
this.roomId = targetId;
|
|
|
|
this.groupId = null;
|
|
|
|
}
|
2021-06-16 09:18:32 +00:00
|
|
|
}
|
2016-09-13 13:47:56 +00:00
|
|
|
|
2021-06-16 09:18:32 +00:00
|
|
|
public get fatal() {
|
|
|
|
return this._fatal;
|
2016-09-13 13:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invite users to this room. This may only be called once per
|
|
|
|
* instance of the class.
|
|
|
|
*
|
2021-06-16 09:18:32 +00:00
|
|
|
* @param {array} addresses Array of addresses to invite
|
2021-02-26 22:09:54 +00:00
|
|
|
* @param {string} reason Reason for inviting (optional)
|
2016-09-13 13:47:56 +00:00
|
|
|
* @returns {Promise} Resolved when all invitations in the queue are complete
|
|
|
|
*/
|
2021-06-16 09:18:32 +00:00
|
|
|
public invite(addresses, reason?: string): Promise<CompletionStates> {
|
|
|
|
if (this.addresses.length > 0) {
|
2016-09-13 13:47:56 +00:00
|
|
|
throw new Error("Already inviting/invited");
|
|
|
|
}
|
2021-06-16 09:18:32 +00:00
|
|
|
this.addresses.push(...addresses);
|
2021-02-26 22:09:54 +00:00
|
|
|
this.reason = reason;
|
2016-09-13 13:47:56 +00:00
|
|
|
|
2021-06-16 09:18:32 +00:00
|
|
|
for (const addr of this.addresses) {
|
2016-09-13 13:47:56 +00:00
|
|
|
if (getAddressType(addr) === null) {
|
2021-06-16 09:18:32 +00:00
|
|
|
this.completionStates[addr] = InviteState.Error;
|
2019-01-11 04:43:21 +00:00
|
|
|
this.errors[addr] = {
|
|
|
|
errcode: 'M_INVALID',
|
|
|
|
errorText: _t('Unrecognised address'),
|
|
|
|
};
|
2016-09-13 13:47:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-16 09:18:32 +00:00
|
|
|
this.deferred = defer<CompletionStates>();
|
|
|
|
this.inviteMore(0);
|
2016-09-13 13:47:56 +00:00
|
|
|
|
|
|
|
return this.deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops inviting. Causes promises returned by invite() to be rejected.
|
|
|
|
*/
|
2021-06-16 09:18:32 +00:00
|
|
|
public cancel(): void {
|
2016-09-13 13:47:56 +00:00
|
|
|
if (!this.busy) return;
|
|
|
|
|
2021-06-16 09:18:32 +00:00
|
|
|
this.canceled = true;
|
2016-09-13 13:47:56 +00:00
|
|
|
this.deferred.reject(new Error('canceled'));
|
|
|
|
}
|
|
|
|
|
2021-06-16 09:18:32 +00:00
|
|
|
public getCompletionState(addr: string): InviteState {
|
2016-09-13 13:47:56 +00:00
|
|
|
return this.completionStates[addr];
|
|
|
|
}
|
|
|
|
|
2021-06-16 09:18:32 +00:00
|
|
|
public getErrorText(addr: string): string {
|
2019-01-11 04:43:21 +00:00
|
|
|
return this.errors[addr] ? this.errors[addr].errorText : null;
|
2016-09-13 13:47:56 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 09:18:32 +00:00
|
|
|
private async inviteToRoom(roomId: string, addr: string, ignoreProfile = false): Promise<{}> {
|
2018-11-29 22:05:53 +00:00
|
|
|
const addrType = getAddressType(addr);
|
|
|
|
|
2021-06-16 09:18:32 +00:00
|
|
|
if (addrType === AddressType.Email) {
|
2018-11-29 22:05:53 +00:00
|
|
|
return MatrixClientPeg.get().inviteByEmail(roomId, addr);
|
2021-06-16 09:18:32 +00:00
|
|
|
} else if (addrType === AddressType.MatrixUserId) {
|
2019-03-01 20:36:24 +00:00
|
|
|
const room = MatrixClientPeg.get().getRoom(roomId);
|
|
|
|
if (!room) throw new Error("Room not found");
|
|
|
|
|
|
|
|
const member = room.getMember(addr);
|
2021-07-13 10:37:31 +00:00
|
|
|
if (member?.membership === "join") {
|
2021-07-12 10:32:06 +00:00
|
|
|
throw new MatrixError({
|
|
|
|
errcode: USER_ALREADY_JOINED,
|
|
|
|
error: "Member already joined",
|
|
|
|
});
|
2021-07-13 10:37:31 +00:00
|
|
|
} else if (member?.membership === "invite") {
|
2021-07-12 10:32:06 +00:00
|
|
|
throw new MatrixError({
|
|
|
|
errcode: USER_ALREADY_INVITED,
|
2021-06-16 09:18:32 +00:00
|
|
|
error: "Member already invited",
|
|
|
|
});
|
2019-03-01 20:36:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 15:07:30 +00:00
|
|
|
if (!ignoreProfile && SettingsStore.getValue("promptBeforeInviteUnknownUsers", this.roomId)) {
|
2021-03-23 18:24:05 +00:00
|
|
|
const profile = await MatrixClientPeg.get().getProfileInfo(addr);
|
|
|
|
if (!profile) {
|
|
|
|
// noinspection ExceptionCaughtLocallyJS
|
|
|
|
throw new Error("User has no profile");
|
2019-01-11 04:43:21 +00:00
|
|
|
}
|
2018-11-29 22:05:53 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 22:09:54 +00:00
|
|
|
return MatrixClientPeg.get().invite(roomId, addr, undefined, this.reason);
|
2018-11-29 22:05:53 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('Unsupported address');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 09:18:32 +00:00
|
|
|
private doInvite(address: string, ignoreProfile = false): Promise<void> {
|
|
|
|
return new Promise<void>((resolve, reject) => {
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log(`Inviting ${address}`);
|
2019-01-11 22:46:03 +00:00
|
|
|
|
2019-01-11 04:43:21 +00:00
|
|
|
let doInvite;
|
|
|
|
if (this.groupId !== null) {
|
|
|
|
doInvite = GroupStore.inviteUserToGroup(this.groupId, address);
|
|
|
|
} else {
|
2021-06-16 09:18:32 +00:00
|
|
|
doInvite = this.inviteToRoom(this.roomId, address, ignoreProfile);
|
2019-01-11 04:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
doInvite.then(() => {
|
2021-06-16 09:18:32 +00:00
|
|
|
if (this.canceled) {
|
2019-01-11 04:43:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-16 09:18:32 +00:00
|
|
|
this.completionStates[address] = InviteState.Invited;
|
2019-01-11 04:43:21 +00:00
|
|
|
delete this.errors[address];
|
|
|
|
|
|
|
|
resolve();
|
2021-09-30 12:43:59 +00:00
|
|
|
this.progressCallback?.();
|
2019-01-11 04:43:21 +00:00
|
|
|
}).catch((err) => {
|
2021-06-16 09:18:32 +00:00
|
|
|
if (this.canceled) {
|
2019-01-11 04:43:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-28 03:29:11 +00:00
|
|
|
console.error(err);
|
|
|
|
|
2019-01-11 04:43:21 +00:00
|
|
|
let errorText;
|
|
|
|
let fatal = false;
|
2021-07-12 10:32:06 +00:00
|
|
|
switch (err.errcode) {
|
|
|
|
case "M_FORBIDDEN":
|
|
|
|
errorText = _t('You do not have permission to invite people to this room.');
|
|
|
|
fatal = true;
|
|
|
|
break;
|
|
|
|
case USER_ALREADY_INVITED:
|
|
|
|
errorText = _t("User %(userId)s is already invited to the room", { userId: address });
|
|
|
|
break;
|
|
|
|
case USER_ALREADY_JOINED:
|
|
|
|
errorText = _t("User %(userId)s is already in the room", { userId: address });
|
|
|
|
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":
|
|
|
|
errorText = _t("User %(user_id)s does not exist", { user_id: address });
|
|
|
|
break;
|
|
|
|
case "M_PROFILE_UNDISCLOSED":
|
|
|
|
errorText = _t("User %(user_id)s may or may not exist", { user_id: address });
|
|
|
|
break;
|
|
|
|
case "M_PROFILE_NOT_FOUND":
|
|
|
|
if (!ignoreProfile) {
|
|
|
|
// Invite without the profile check
|
|
|
|
console.warn(`User ${address} does not have a profile - inviting anyways automatically`);
|
|
|
|
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":
|
|
|
|
errorText = _t("The user's homeserver does not support the version of the room.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!errorText) {
|
2019-01-11 04:43:21 +00:00
|
|
|
errorText = _t('Unknown server error');
|
|
|
|
}
|
|
|
|
|
2021-06-16 09:18:32 +00:00
|
|
|
this.completionStates[address] = InviteState.Error;
|
|
|
|
this.errors[address] = { errorText, errcode: err.errcode };
|
2019-01-11 04:43:21 +00:00
|
|
|
|
|
|
|
this.busy = !fatal;
|
2021-06-16 09:18:32 +00:00
|
|
|
this._fatal = fatal;
|
2019-01-11 04:43:21 +00:00
|
|
|
|
|
|
|
if (fatal) {
|
2021-06-16 09:18:32 +00:00
|
|
|
reject(err);
|
2019-01-11 04:43:21 +00:00
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-11-29 22:05:53 +00:00
|
|
|
|
2021-06-16 09:18:32 +00:00
|
|
|
private inviteMore(nextIndex: number, ignoreProfile = false): void {
|
|
|
|
if (this.canceled) {
|
2016-09-13 13:47:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-16 09:18:32 +00:00
|
|
|
if (nextIndex === this.addresses.length) {
|
2016-09-13 13:47:56 +00:00
|
|
|
this.busy = false;
|
2019-01-11 04:43:21 +00:00
|
|
|
if (Object.keys(this.errors).length > 0 && !this.groupId) {
|
|
|
|
// There were problems inviting some people - see if we can invite them
|
|
|
|
// without caring if they exist or not.
|
2021-06-16 09:18:32 +00:00
|
|
|
const unknownProfileUsers = Object.keys(this.errors)
|
|
|
|
.filter(a => UNKNOWN_PROFILE_ERRORS.includes(this.errors[a].errcode));
|
2019-01-11 04:43:21 +00:00
|
|
|
|
2019-01-11 22:46:03 +00:00
|
|
|
if (unknownProfileUsers.length > 0) {
|
|
|
|
const inviteUnknowns = () => {
|
2021-06-16 09:18:32 +00:00
|
|
|
const promises = unknownProfileUsers.map(u => this.doInvite(u, true));
|
2019-01-11 04:43:21 +00:00
|
|
|
Promise.all(promises).then(() => this.deferred.resolve(this.completionStates));
|
|
|
|
};
|
|
|
|
|
2019-01-16 15:07:30 +00:00
|
|
|
if (!SettingsStore.getValue("promptBeforeInviteUnknownUsers", this.roomId)) {
|
2019-01-11 22:46:03 +00:00
|
|
|
inviteUnknowns();
|
2019-01-11 04:43:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-21 15:48:09 +00:00
|
|
|
logger.log("Showing failed to invite dialog...");
|
2021-03-25 10:11:52 +00:00
|
|
|
Modal.createTrackedDialog('Failed to invite', '', AskInviteAnywayDialog, {
|
2021-06-16 09:18:32 +00:00
|
|
|
unknownProfileUsers: unknownProfileUsers.map(u => ({
|
|
|
|
userId: u,
|
|
|
|
errorText: this.errors[u].errorText,
|
|
|
|
})),
|
2019-01-11 22:46:03 +00:00
|
|
|
onInviteAnyways: () => inviteUnknowns(),
|
2019-01-11 04:43:21 +00:00
|
|
|
onGiveUp: () => {
|
|
|
|
// Fake all the completion states because we already warned the user
|
2019-01-11 22:46:03 +00:00
|
|
|
for (const addr of unknownProfileUsers) {
|
2021-06-16 09:18:32 +00:00
|
|
|
this.completionStates[addr] = InviteState.Invited;
|
2019-01-11 04:43:21 +00:00
|
|
|
}
|
|
|
|
this.deferred.resolve(this.completionStates);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-09-13 13:47:56 +00:00
|
|
|
this.deferred.resolve(this.completionStates);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-16 09:18:32 +00:00
|
|
|
const addr = this.addresses[nextIndex];
|
2016-09-13 13:47:56 +00: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 09:18:32 +00:00
|
|
|
this.inviteMore(nextIndex + 1);
|
2016-09-13 13:47:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't re-invite (there's no way in the UI to do this, but
|
|
|
|
// for sanity's sake)
|
2021-06-16 09:18:32 +00:00
|
|
|
if (this.completionStates[addr] === InviteState.Invited) {
|
|
|
|
this.inviteMore(nextIndex + 1);
|
2016-09-13 13:47:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-16 09:18:32 +00:00
|
|
|
this.doInvite(addr, ignoreProfile).then(() => {
|
|
|
|
this.inviteMore(nextIndex + 1, ignoreProfile);
|
2019-01-11 04:43:21 +00:00
|
|
|
}).catch(() => this.deferred.resolve(this.completionStates));
|
2016-09-13 13:47:56 +00:00
|
|
|
}
|
|
|
|
}
|