2016-09-13 16:47:56 +03:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2018-11-30 01:05:53 +03:00
|
|
|
Copyright 2017, 2018 New Vector Ltd
|
2016-09-13 16:47:56 +03: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.
|
|
|
|
*/
|
|
|
|
|
2017-08-16 16:58:30 +03:00
|
|
|
import MatrixClientPeg from '../MatrixClientPeg';
|
2017-08-15 15:42:23 +03:00
|
|
|
import {getAddressType} from '../UserAddress';
|
2018-05-01 13:18:45 +03:00
|
|
|
import GroupStore from '../stores/GroupStore';
|
2017-07-12 15:58:14 +03:00
|
|
|
import Promise from 'bluebird';
|
2018-11-30 01:05:53 +03:00
|
|
|
import {_t} from "../languageHandler";
|
2016-09-13 16:47:56 +03:00
|
|
|
|
|
|
|
/**
|
2017-08-16 16:58:30 +03:00
|
|
|
* Invites multiple addresses to a room or group, handling rate limiting from the server
|
2016-09-13 16:47:56 +03:00
|
|
|
*/
|
|
|
|
export default class MultiInviter {
|
2017-08-16 16:58:30 +03:00
|
|
|
/**
|
|
|
|
* @param {string} targetId The ID of the room or group to invite to
|
|
|
|
*/
|
|
|
|
constructor(targetId) {
|
|
|
|
if (targetId[0] === '+') {
|
|
|
|
this.roomId = null;
|
|
|
|
this.groupId = targetId;
|
|
|
|
} else {
|
|
|
|
this.roomId = targetId;
|
|
|
|
this.groupId = null;
|
|
|
|
}
|
2016-09-13 16:47:56 +03:00
|
|
|
|
|
|
|
this.canceled = false;
|
|
|
|
this.addrs = [];
|
|
|
|
this.busy = false;
|
|
|
|
this.completionStates = {}; // State of each address (invited or error)
|
|
|
|
this.errorTexts = {}; // Textual error per address
|
|
|
|
this.deferred = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invite users to this room. This may only be called once per
|
|
|
|
* instance of the class.
|
|
|
|
*
|
2018-11-30 01:05:53 +03:00
|
|
|
* @param {array} addrs Array of addresses to invite
|
2016-09-13 16:47:56 +03:00
|
|
|
* @returns {Promise} Resolved when all invitations in the queue are complete
|
|
|
|
*/
|
|
|
|
invite(addrs) {
|
|
|
|
if (this.addrs.length > 0) {
|
|
|
|
throw new Error("Already inviting/invited");
|
|
|
|
}
|
|
|
|
this.addrs.push(...addrs);
|
|
|
|
|
|
|
|
for (const addr of this.addrs) {
|
|
|
|
if (getAddressType(addr) === null) {
|
|
|
|
this.completionStates[addr] = 'error';
|
|
|
|
this.errorTexts[addr] = 'Unrecognised address';
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 16:04:20 +03:00
|
|
|
this.deferred = Promise.defer();
|
2016-09-13 16:47:56 +03:00
|
|
|
this._inviteMore(0);
|
|
|
|
|
|
|
|
return this.deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops inviting. Causes promises returned by invite() to be rejected.
|
|
|
|
*/
|
|
|
|
cancel() {
|
|
|
|
if (!this.busy) return;
|
|
|
|
|
|
|
|
this._canceled = true;
|
|
|
|
this.deferred.reject(new Error('canceled'));
|
|
|
|
}
|
|
|
|
|
|
|
|
getCompletionState(addr) {
|
|
|
|
return this.completionStates[addr];
|
|
|
|
}
|
|
|
|
|
|
|
|
getErrorText(addr) {
|
|
|
|
return this.errorTexts[addr];
|
|
|
|
}
|
|
|
|
|
2018-11-30 01:05:53 +03:00
|
|
|
async _inviteToRoom(roomId, addr) {
|
|
|
|
const addrType = getAddressType(addr);
|
|
|
|
|
|
|
|
if (addrType === 'email') {
|
|
|
|
return MatrixClientPeg.get().inviteByEmail(roomId, addr);
|
|
|
|
} else if (addrType === 'mx-user-id') {
|
|
|
|
const profile = await MatrixClientPeg.get().getProfileInfo(addr);
|
|
|
|
if (!profile) {
|
|
|
|
return Promise.reject({errcode: "M_NOT_FOUND", error: "User does not have a profile."});
|
|
|
|
}
|
|
|
|
|
|
|
|
return MatrixClientPeg.get().invite(roomId, addr);
|
|
|
|
} else {
|
|
|
|
throw new Error('Unsupported address');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-13 16:47:56 +03:00
|
|
|
_inviteMore(nextIndex) {
|
|
|
|
if (this._canceled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-30 01:05:53 +03:00
|
|
|
if (nextIndex === this.addrs.length) {
|
2016-09-13 16:47:56 +03:00
|
|
|
this.busy = false;
|
|
|
|
this.deferred.resolve(this.completionStates);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const addr = this.addrs[nextIndex];
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
this._inviteMore(nextIndex + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't re-invite (there's no way in the UI to do this, but
|
|
|
|
// for sanity's sake)
|
2018-11-30 01:05:53 +03:00
|
|
|
if (this.completionStates[addr] === 'invited') {
|
2016-09-13 16:47:56 +03:00
|
|
|
this._inviteMore(nextIndex + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-16 16:58:30 +03:00
|
|
|
let doInvite;
|
|
|
|
if (this.groupId !== null) {
|
2018-05-01 13:18:45 +03:00
|
|
|
doInvite = GroupStore.inviteUserToGroup(this.groupId, addr);
|
2017-08-16 16:58:30 +03:00
|
|
|
} else {
|
2018-11-30 01:05:53 +03:00
|
|
|
doInvite = this._inviteToRoom(this.roomId, addr);
|
2017-08-16 16:58:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
doInvite.then(() => {
|
2016-09-13 16:47:56 +03:00
|
|
|
if (this._canceled) { return; }
|
|
|
|
|
|
|
|
this.completionStates[addr] = 'invited';
|
|
|
|
|
|
|
|
this._inviteMore(nextIndex + 1);
|
2018-11-30 01:05:53 +03:00
|
|
|
}).catch((err) => {
|
2016-09-13 16:47:56 +03:00
|
|
|
if (this._canceled) { return; }
|
|
|
|
|
|
|
|
let errorText;
|
|
|
|
let fatal = false;
|
2018-11-30 01:05:53 +03:00
|
|
|
if (err.errcode === 'M_FORBIDDEN') {
|
2016-09-13 16:47:56 +03:00
|
|
|
fatal = true;
|
2018-11-30 01:05:53 +03:00
|
|
|
errorText = _t('You do not have permission to invite people to this room.');
|
|
|
|
} else if (err.errcode === 'M_LIMIT_EXCEEDED') {
|
2016-09-13 16:47:56 +03:00
|
|
|
// we're being throttled so wait a bit & try again
|
|
|
|
setTimeout(() => {
|
|
|
|
this._inviteMore(nextIndex);
|
|
|
|
}, 5000);
|
|
|
|
return;
|
2018-11-30 01:05:53 +03:00
|
|
|
} else if(err.errcode === "M_NOT_FOUND") {
|
|
|
|
errorText = _t("User %(user_id)s does not exist", {user_id: addr});
|
2016-09-13 16:47:56 +03:00
|
|
|
} else {
|
2018-11-30 01:05:53 +03:00
|
|
|
errorText = _t('Unknown server error');
|
2016-09-13 16:47:56 +03:00
|
|
|
}
|
|
|
|
this.completionStates[addr] = 'error';
|
|
|
|
this.errorTexts[addr] = errorText;
|
|
|
|
this.busy = !fatal;
|
2018-11-30 01:05:53 +03:00
|
|
|
this.fatal = fatal;
|
2016-09-13 16:47:56 +03:00
|
|
|
|
|
|
|
if (!fatal) {
|
|
|
|
this._inviteMore(nextIndex + 1);
|
2018-11-30 01:05:53 +03:00
|
|
|
} else {
|
|
|
|
this.deferred.resolve(this.completionStates);
|
2016-09-13 16:47:56 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|