From 27f74dd3f1a2f4b9b6fb0fd82f372d2ca856cc26 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 12 Jul 2021 11:32:06 +0100 Subject: [PATCH] Fix multiinviter user already in room and clean up code --- src/utils/MultiInviter.ts | 79 ++++++++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 27 deletions(-) diff --git a/src/utils/MultiInviter.ts b/src/utils/MultiInviter.ts index a7d1accde1..ddf2643336 100644 --- a/src/utils/MultiInviter.ts +++ b/src/utils/MultiInviter.ts @@ -39,6 +39,9 @@ const UNKNOWN_PROFILE_ERRORS = ['M_NOT_FOUND', 'M_USER_NOT_FOUND', 'M_PROFILE_UN export type CompletionStates = Record; +const USER_ALREADY_JOINED = "IO.ELEMENT.ALREADY_JOINED"; +const USER_ALREADY_INVITED = "IO.ELEMENT.ALREADY_INVITED"; + /** * Invites multiple addresses to a room or group, handling rate limiting from the server */ @@ -130,9 +133,14 @@ export default class MultiInviter { if (!room) throw new Error("Room not found"); const member = room.getMember(addr); - if (member && ['join', 'invite'].includes(member.membership)) { - throw new new MatrixError({ - errcode: "RIOT.ALREADY_IN_ROOM", + if (member.membership === "join") { + throw new MatrixError({ + errcode: USER_ALREADY_JOINED, + error: "Member already joined", + }); + } else if (member.membership === "invite") { + throw new MatrixError({ + errcode: USER_ALREADY_INVITED, error: "Member already invited", }); } @@ -180,30 +188,47 @@ export default class MultiInviter { 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.'); - } 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(() => { - this.doInvite(address, ignoreProfile).then(resolve, reject); - }, 5000); - return; - } else if (['M_NOT_FOUND', 'M_USER_NOT_FOUND'].includes(err.errcode)) { - errorText = _t("User %(user_id)s does not exist", { user_id: address }); - } else if (err.errcode === 'M_PROFILE_UNDISCLOSED') { - 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 - 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 if (err.errcode === "M_UNSUPPORTED_ROOM_VERSION") { - errorText = _t("The user's homeserver does not support the version of the room."); - } else { + switch (err.errcode) { + case "M_FORBIDDEN": + errorText = _t('You do not have permission to invite people to this room.'); + fatal = true; + break; + case USER_ALREADY_INVITED: + errorText = _t("User %(userId)s is already invited to the room", { userId: address }); + break; + case USER_ALREADY_JOINED: + errorText = _t("User %(userId)s is already in the room", { userId: address }); + break; + case "M_LIMIT_EXCEEDED": + // we're being throttled so wait a bit & try again + setTimeout(() => { + this.doInvite(address, ignoreProfile).then(resolve, reject); + }, 5000); + return; + case "M_NOT_FOUND": + case "M_USER_NOT_FOUND": + errorText = _t("User %(user_id)s does not exist", { user_id: address }); + break; + case "M_PROFILE_UNDISCLOSED": + errorText = _t("User %(user_id)s may or may not exist", { user_id: address }); + break; + case "M_PROFILE_NOT_FOUND": + if (!ignoreProfile) { + // Invite without the profile check + console.warn(`User ${address} does not have a profile - inviting anyways automatically`); + this.doInvite(address, true).then(resolve, reject); + return; + } + break; + case "M_BAD_STATE": + errorText = _t("The user must be unbanned before they can be invited."); + break; + case "M_UNSUPPORTED_ROOM_VERSION": + errorText = _t("The user's homeserver does not support the version of the room."); + break; + } + + if (!errorText) { errorText = _t('Unknown server error'); }