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";
|
|
|
|
|
|
|
|
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 { defer, IDeferred } from "./promise";
|
|
|
|
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 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-06-16 09:18:32 +00:00
|
|
|
constructor(targetId: string) {
|
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);
|
|
|
|
if (member && ['join', 'invite'].includes(member.membership)) {
|
2021-06-16 09:18:32 +00:00
|
|
|
throw new new MatrixError({
|
|
|
|
errcode: "RIOT.ALREADY_IN_ROOM",
|
|
|
|
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) => {
|
2019-01-11 22:46:03 +00:00
|
|
|
console.log(`Inviting ${address}`);
|
|
|
|
|
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();
|
|
|
|
}).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;
|
|
|
|
if (err.errcode === 'M_FORBIDDEN') {
|
|
|
|
fatal = true;
|
|
|
|
errorText = _t('You do not have permission to invite people to this room.');
|
2019-03-01 20:36:24 +00:00
|
|
|
} else if (err.errcode === "RIOT.ALREADY_IN_ROOM") {
|
|
|
|
errorText = _t("User %(userId)s is already in the room", {userId: address});
|
2019-01-11 04:43:21 +00:00
|
|
|
} else if (err.errcode === 'M_LIMIT_EXCEEDED') {
|
|
|
|
// we're being throttled so wait a bit & try again
|
|
|
|
setTimeout(() => {
|
2021-06-16 09:18:32 +00:00
|
|
|
this.doInvite(address, ignoreProfile).then(resolve, reject);
|
2019-01-11 04:43:21 +00:00
|
|
|
}, 5000);
|
|
|
|
return;
|
2021-03-24 08:58:08 +00:00
|
|
|
} else if (['M_NOT_FOUND', 'M_USER_NOT_FOUND'].includes(err.errcode)) {
|
2019-01-11 04:43:21 +00:00
|
|
|
errorText = _t("User %(user_id)s does not exist", {user_id: address});
|
2019-01-11 22:46:03 +00:00
|
|
|
} else if (err.errcode === 'M_PROFILE_UNDISCLOSED') {
|
2019-01-11 04:43:21 +00:00
|
|
|
errorText = _t("User %(user_id)s may or may not exist", {user_id: address});
|
|
|
|
} else if (err.errcode === 'M_PROFILE_NOT_FOUND' && !ignoreProfile) {
|
|
|
|
// Invite without the profile check
|
2019-01-11 22:46:03 +00:00
|
|
|
console.warn(`User ${address} does not have a profile - inviting anyways automatically`);
|
2021-06-16 09:18:32 +00:00
|
|
|
this.doInvite(address, true).then(resolve, reject);
|
2019-03-01 20:36:24 +00:00
|
|
|
} else if (err.errcode === "M_BAD_STATE") {
|
|
|
|
errorText = _t("The user must be unbanned before they can be invited.");
|
2019-04-10 01:03:38 +00:00
|
|
|
} else if (err.errcode === "M_UNSUPPORTED_ROOM_VERSION") {
|
|
|
|
errorText = _t("The user's homeserver does not support the version of the room.");
|
2019-01-11 04:43:21 +00:00
|
|
|
} else {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.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
|
|
|
}
|
|
|
|
}
|