Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit c06d42d

Browse files
authored
Merge pull request #2317 from matrix-org/travis/invite-errors
Check if users exist before inviting them and communicate errors
2 parents c553323 + 28f4752 commit c06d42d

File tree

5 files changed

+91
-45
lines changed

5 files changed

+91
-45
lines changed

src/RoomInvite.js

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Copyright 2016 OpenMarket Ltd
3-
Copyright 2017 New Vector Ltd
3+
Copyright 2017, 2018 New Vector Ltd
44
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@ See the License for the specific language governing permissions and
1515
limitations under the License.
1616
*/
1717

18+
import React from 'react';
1819
import MatrixClientPeg from './MatrixClientPeg';
1920
import MultiInviter from './utils/MultiInviter';
2021
import Modal from './Modal';
@@ -25,18 +26,6 @@ import dis from './dispatcher';
2526
import DMRoomMap from './utils/DMRoomMap';
2627
import { _t } from './languageHandler';
2728

28-
export function inviteToRoom(roomId, addr) {
29-
const addrType = getAddressType(addr);
30-
31-
if (addrType == 'email') {
32-
return MatrixClientPeg.get().inviteByEmail(roomId, addr);
33-
} else if (addrType == 'mx-user-id') {
34-
return MatrixClientPeg.get().invite(roomId, addr);
35-
} else {
36-
throw new Error('Unsupported address');
37-
}
38-
}
39-
4029
/**
4130
* Invites multiple addresses to a room
4231
* Simpler interface to utils/MultiInviter but with
@@ -46,9 +35,9 @@ export function inviteToRoom(roomId, addr) {
4635
* @param {string[]} addrs Array of strings of addresses to invite. May be matrix IDs or 3pids.
4736
* @returns {Promise} Promise
4837
*/
49-
export function inviteMultipleToRoom(roomId, addrs) {
38+
function inviteMultipleToRoom(roomId, addrs) {
5039
const inviter = new MultiInviter(roomId);
51-
return inviter.invite(addrs);
40+
return inviter.invite(addrs).then(states => Promise.resolve({states, inviter}));
5241
}
5342

5443
export function showStartChatInviteDialog() {
@@ -129,8 +118,8 @@ function _onStartChatFinished(shouldInvite, addrs) {
129118
createRoom().then((roomId) => {
130119
room = MatrixClientPeg.get().getRoom(roomId);
131120
return inviteMultipleToRoom(roomId, addrTexts);
132-
}).then((addrs) => {
133-
return _showAnyInviteErrors(addrs, room);
121+
}).then((result) => {
122+
return _showAnyInviteErrors(result.states, room, result.inviter);
134123
}).catch((err) => {
135124
console.error(err.stack);
136125
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
@@ -148,9 +137,9 @@ function _onRoomInviteFinished(roomId, shouldInvite, addrs) {
148137
const addrTexts = addrs.map((addr) => addr.address);
149138

150139
// Invite new users to a room
151-
inviteMultipleToRoom(roomId, addrTexts).then((addrs) => {
140+
inviteMultipleToRoom(roomId, addrTexts).then((result) => {
152141
const room = MatrixClientPeg.get().getRoom(roomId);
153-
return _showAnyInviteErrors(addrs, room);
142+
return _showAnyInviteErrors(result.states, room, result.inviter);
154143
}).catch((err) => {
155144
console.error(err.stack);
156145
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
@@ -169,22 +158,36 @@ function _isDmChat(addrTexts) {
169158
}
170159
}
171160

172-
function _showAnyInviteErrors(addrs, room) {
161+
function _showAnyInviteErrors(addrs, room, inviter) {
173162
// Show user any errors
174-
const errorList = [];
175-
for (const addr of Object.keys(addrs)) {
176-
if (addrs[addr] === "error") {
177-
errorList.push(addr);
178-
}
179-
}
180-
181-
if (errorList.length > 0) {
163+
const failedUsers = Object.keys(addrs).filter(a => addrs[a] === 'error');
164+
if (failedUsers.length === 1 && inviter.fatal) {
165+
// Just get the first message because there was a fatal problem on the first
166+
// user. This usually means that no other users were attempted, making it
167+
// pointless for us to list who failed exactly.
182168
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
183-
Modal.createTrackedDialog('Failed to invite the following users to the room', '', ErrorDialog, {
184-
title: _t("Failed to invite the following users to the %(roomName)s room:", {roomName: room.name}),
185-
description: errorList.join(", "),
169+
Modal.createTrackedDialog('Failed to invite users to the room', '', ErrorDialog, {
170+
title: _t("Failed to invite users to the room:", {roomName: room.name}),
171+
description: inviter.getErrorText(failedUsers[0]),
186172
});
173+
} else {
174+
const errorList = [];
175+
for (const addr of failedUsers) {
176+
if (addrs[addr] === "error") {
177+
const reason = inviter.getErrorText(addr);
178+
errorList.push(addr + ": " + reason);
179+
}
180+
}
181+
182+
if (errorList.length > 0) {
183+
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
184+
Modal.createTrackedDialog('Failed to invite the following users to the room', '', ErrorDialog, {
185+
title: _t("Failed to invite the following users to the %(roomName)s room:", {roomName: room.name}),
186+
description: errorList.join(<br />),
187+
});
188+
}
187189
}
190+
188191
return addrs;
189192
}
190193

src/SlashCommands.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import Modal from './Modal';
2626
import SettingsStore, {SettingLevel} from './settings/SettingsStore';
2727
import {MATRIXTO_URL_PATTERN} from "./linkify-matrix";
2828
import * as querystring from "querystring";
29+
import MultiInviter from './utils/MultiInviter';
2930

3031

3132
class Command {
@@ -142,7 +143,15 @@ export const CommandMap = {
142143
if (args) {
143144
const matches = args.match(/^(\S+)$/);
144145
if (matches) {
145-
return success(MatrixClientPeg.get().invite(roomId, matches[1]));
146+
// We use a MultiInviter to re-use the invite logic, even though
147+
// we're only inviting one user.
148+
const userId = matches[1];
149+
const inviter = new MultiInviter(roomId);
150+
return success(inviter.invite([userId]).then(() => {
151+
if (inviter.getCompletionState(userId) !== "invited") {
152+
throw new Error(inviter.getErrorText(userId));
153+
}
154+
}));
146155
}
147156
}
148157
return reject(this.getUsage());

src/components/views/rooms/MemberInfo.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import withMatrixClient from '../../../wrappers/withMatrixClient';
4141
import AccessibleButton from '../elements/AccessibleButton';
4242
import RoomViewStore from '../../../stores/RoomViewStore';
4343
import SdkConfig from '../../../SdkConfig';
44+
import MultiInviter from "../../../utils/MultiInviter";
4445

4546
module.exports = withMatrixClient(React.createClass({
4647
displayName: 'MemberInfo',
@@ -714,12 +715,18 @@ module.exports = withMatrixClient(React.createClass({
714715
const roomId = member && member.roomId ? member.roomId : RoomViewStore.getRoomId();
715716
const onInviteUserButton = async() => {
716717
try {
717-
await cli.invite(roomId, member.userId);
718+
// We use a MultiInviter to re-use the invite logic, even though
719+
// we're only inviting one user.
720+
const inviter = new MultiInviter(roomId);
721+
await inviter.invite([member.userId]).then(() => {
722+
if (inviter.getCompletionState(userId) !== "invited")
723+
throw new Error(inviter.getErrorText(userId));
724+
});
718725
} catch (err) {
719726
const ErrorDialog = sdk.getComponent('dialogs.ErrorDialog');
720727
Modal.createTrackedDialog('Failed to invite', '', ErrorDialog, {
721728
title: _t('Failed to invite'),
722-
description: ((err && err.message) ? err.message : "Operation failed"),
729+
description: ((err && err.message) ? err.message : _t("Operation failed")),
723730
});
724731
}
725732
};

src/i18n/strings/en_EN.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
"Failed to invite user": "Failed to invite user",
108108
"Operation failed": "Operation failed",
109109
"Failed to invite": "Failed to invite",
110+
"Failed to invite users to the room:": "Failed to invite users to the room:",
110111
"Failed to invite the following users to the %(roomName)s room:": "Failed to invite the following users to the %(roomName)s room:",
111112
"You need to be logged in.": "You need to be logged in.",
112113
"You need to be able to invite users to do that.": "You need to be able to invite users to do that.",
@@ -246,6 +247,9 @@
246247
"A word by itself is easy to guess": "A word by itself is easy to guess",
247248
"Names and surnames by themselves are easy to guess": "Names and surnames by themselves are easy to guess",
248249
"Common names and surnames are easy to guess": "Common names and surnames are easy to guess",
250+
"You do not have permission to invite people to this room.": "You do not have permission to invite people to this room.",
251+
"User %(user_id)s does not exist": "User %(user_id)s does not exist",
252+
"Unknown server error": "Unknown server error",
249253
"Sorry, your homeserver is too old to participate in this room.": "Sorry, your homeserver is too old to participate in this room.",
250254
"Please contact your homeserver administrator.": "Please contact your homeserver administrator.",
251255
"Failed to join room": "Failed to join room",

src/utils/MultiInviter.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Copyright 2016 OpenMarket Ltd
3-
Copyright 2017 New Vector Ltd
3+
Copyright 2017, 2018 New Vector Ltd
44
55
Licensed under the Apache License, Version 2.0 (the "License");
66
you may not use this file except in compliance with the License.
@@ -17,9 +17,9 @@ limitations under the License.
1717

1818
import MatrixClientPeg from '../MatrixClientPeg';
1919
import {getAddressType} from '../UserAddress';
20-
import {inviteToRoom} from '../RoomInvite';
2120
import GroupStore from '../stores/GroupStore';
2221
import Promise from 'bluebird';
22+
import {_t} from "../languageHandler";
2323

2424
/**
2525
* Invites multiple addresses to a room or group, handling rate limiting from the server
@@ -49,7 +49,7 @@ export default class MultiInviter {
4949
* Invite users to this room. This may only be called once per
5050
* instance of the class.
5151
*
52-
* @param {array} addresses Array of addresses to invite
52+
* @param {array} addrs Array of addresses to invite
5353
* @returns {Promise} Resolved when all invitations in the queue are complete
5454
*/
5555
invite(addrs) {
@@ -88,12 +88,30 @@ export default class MultiInviter {
8888
return this.errorTexts[addr];
8989
}
9090

91+
async _inviteToRoom(roomId, addr) {
92+
const addrType = getAddressType(addr);
93+
94+
if (addrType === 'email') {
95+
return MatrixClientPeg.get().inviteByEmail(roomId, addr);
96+
} else if (addrType === 'mx-user-id') {
97+
const profile = await MatrixClientPeg.get().getProfileInfo(addr);
98+
if (!profile) {
99+
return Promise.reject({errcode: "M_NOT_FOUND", error: "User does not have a profile."});
100+
}
101+
102+
return MatrixClientPeg.get().invite(roomId, addr);
103+
} else {
104+
throw new Error('Unsupported address');
105+
}
106+
}
107+
108+
91109
_inviteMore(nextIndex) {
92110
if (this._canceled) {
93111
return;
94112
}
95113

96-
if (nextIndex == this.addrs.length) {
114+
if (nextIndex === this.addrs.length) {
97115
this.busy = false;
98116
this.deferred.resolve(this.completionStates);
99117
return;
@@ -111,7 +129,7 @@ export default class MultiInviter {
111129

112130
// don't re-invite (there's no way in the UI to do this, but
113131
// for sanity's sake)
114-
if (this.completionStates[addr] == 'invited') {
132+
if (this.completionStates[addr] === 'invited') {
115133
this._inviteMore(nextIndex + 1);
116134
return;
117135
}
@@ -120,7 +138,7 @@ export default class MultiInviter {
120138
if (this.groupId !== null) {
121139
doInvite = GroupStore.inviteUserToGroup(this.groupId, addr);
122140
} else {
123-
doInvite = inviteToRoom(this.roomId, addr);
141+
doInvite = this._inviteToRoom(this.roomId, addr);
124142
}
125143

126144
doInvite.then(() => {
@@ -129,29 +147,34 @@ export default class MultiInviter {
129147
this.completionStates[addr] = 'invited';
130148

131149
this._inviteMore(nextIndex + 1);
132-
}, (err) => {
150+
}).catch((err) => {
133151
if (this._canceled) { return; }
134152

135153
let errorText;
136154
let fatal = false;
137-
if (err.errcode == 'M_FORBIDDEN') {
155+
if (err.errcode === 'M_FORBIDDEN') {
138156
fatal = true;
139-
errorText = 'You do not have permission to invite people to this room.';
140-
} else if (err.errcode == 'M_LIMIT_EXCEEDED') {
157+
errorText = _t('You do not have permission to invite people to this room.');
158+
} else if (err.errcode === 'M_LIMIT_EXCEEDED') {
141159
// we're being throttled so wait a bit & try again
142160
setTimeout(() => {
143161
this._inviteMore(nextIndex);
144162
}, 5000);
145163
return;
164+
} else if(err.errcode === "M_NOT_FOUND") {
165+
errorText = _t("User %(user_id)s does not exist", {user_id: addr});
146166
} else {
147-
errorText = 'Unknown server error';
167+
errorText = _t('Unknown server error');
148168
}
149169
this.completionStates[addr] = 'error';
150170
this.errorTexts[addr] = errorText;
151171
this.busy = !fatal;
172+
this.fatal = fatal;
152173

153174
if (!fatal) {
154175
this._inviteMore(nextIndex + 1);
176+
} else {
177+
this.deferred.resolve(this.completionStates);
155178
}
156179
});
157180
}

0 commit comments

Comments
 (0)