From 4c4b2eedafe0029d9cccdb45df9de7e18dca7b3f Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Fri, 1 Mar 2019 13:36:24 -0700 Subject: [PATCH] Check if users are already in the room before inviting them Fixes https://github.com/vector-im/riot-web/issues/8965 This also addresses another issue where inviting a banned user shows up as "Unknown server error". --- src/i18n/strings/en_EN.json | 2 ++ src/utils/MultiInviter.js | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 0d7cbd5f7f..19259c6341 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -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", diff --git a/src/utils/MultiInviter.js b/src/utils/MultiInviter.js index 4278ee1c90..2e166b1925 100644 --- a/src/utils/MultiInviter.js +++ b/src/utils/MultiInviter.js @@ -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); @@ -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(() => { @@ -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'); }