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

Commit

Permalink
Check if users are already in the room before inviting them
Browse files Browse the repository at this point in the history
Fixes element-hq/element-web#8965

This also addresses another issue where inviting a banned user shows up as "Unknown server error".
  • Loading branch information
turt2live committed Mar 1, 2019
1 parent 593bd8f commit 4c4b2ee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,10 @@
"Authentication check failed: incorrect password?": "Authentication check failed: incorrect password?",
"Unrecognised address": "Unrecognised address",
"You do not have permission to invite people to this room.": "You do not have permission to invite people to this room.",
"User %(userId)s is already in the room": "User %(userId)s is already in the room",
"User %(user_id)s does not exist": "User %(user_id)s does not exist",
"User %(user_id)s may or may not exist": "User %(user_id)s may or may not exist",
"The user must be unbanned before they can be invited.": "The user must be unbanned before they can be invited.",
"Unknown server error": "Unknown server error",
"Use a few words, avoid common phrases": "Use a few words, avoid common phrases",
"No need for symbols, digits, or uppercase letters": "No need for symbols, digits, or uppercase letters",
Expand Down
12 changes: 12 additions & 0 deletions src/utils/MultiInviter.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ export default class MultiInviter {
if (addrType === 'email') {
return MatrixClientPeg.get().inviteByEmail(roomId, addr);
} else if (addrType === 'mx-user-id') {
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)) {
throw {errcode: "RIOT.ALREADY_IN_ROOM", error: "Member already invited"};
}

if (!ignoreProfile && SettingsStore.getValue("promptBeforeInviteUnknownUsers", this.roomId)) {
try {
const profile = await MatrixClientPeg.get().getProfileInfo(addr);
Expand Down Expand Up @@ -152,6 +160,8 @@ export default class MultiInviter {
if (err.errcode === 'M_FORBIDDEN') {
fatal = true;
errorText = _t('You do not have permission to invite people to this room.');
} else if (err.errcode === "RIOT.ALREADY_IN_ROOM") {
errorText = _t("User %(userId)s is already in the room", {userId: address});
} else if (err.errcode === 'M_LIMIT_EXCEEDED') {
// we're being throttled so wait a bit & try again
setTimeout(() => {
Expand All @@ -166,6 +176,8 @@ export default class MultiInviter {
// Invite without the profile check
console.warn(`User ${address} does not have a profile - inviting anyways automatically`);
this._doInvite(address, true).then(resolve, reject);
} else if (err.errcode === "M_BAD_STATE") {
errorText = _t("The user must be unbanned before they can be invited.");
} else {
errorText = _t('Unknown server error');
}
Expand Down

0 comments on commit 4c4b2ee

Please sign in to comment.