Fix multiinviter user already in room and clean up code

pull/21833/head
Michael Telatynski 2021-07-12 11:32:06 +01:00
parent a3ea120150
commit 27f74dd3f1
1 changed files with 52 additions and 27 deletions

View File

@ -39,6 +39,9 @@ const UNKNOWN_PROFILE_ERRORS = ['M_NOT_FOUND', 'M_USER_NOT_FOUND', 'M_PROFILE_UN
export type CompletionStates = Record<string, InviteState>; export type CompletionStates = Record<string, InviteState>;
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 * 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"); if (!room) throw new Error("Room not found");
const member = room.getMember(addr); const member = room.getMember(addr);
if (member && ['join', 'invite'].includes(member.membership)) { if (member.membership === "join") {
throw new new MatrixError({ throw new MatrixError({
errcode: "RIOT.ALREADY_IN_ROOM", errcode: USER_ALREADY_JOINED,
error: "Member already joined",
});
} else if (member.membership === "invite") {
throw new MatrixError({
errcode: USER_ALREADY_INVITED,
error: "Member already invited", error: "Member already invited",
}); });
} }
@ -180,30 +188,47 @@ export default class MultiInviter {
let errorText; let errorText;
let fatal = false; let fatal = false;
if (err.errcode === 'M_FORBIDDEN') { switch (err.errcode) {
fatal = true; case "M_FORBIDDEN":
errorText = _t('You do not have permission to invite people to this room.'); errorText = _t('You do not have permission to invite people to this room.');
} else if (err.errcode === "RIOT.ALREADY_IN_ROOM") { fatal = true;
errorText = _t("User %(userId)s is already in the room", { userId: address }); break;
} else if (err.errcode === 'M_LIMIT_EXCEEDED') { case USER_ALREADY_INVITED:
// we're being throttled so wait a bit & try again errorText = _t("User %(userId)s is already invited to the room", { userId: address });
setTimeout(() => { break;
this.doInvite(address, ignoreProfile).then(resolve, reject); case USER_ALREADY_JOINED:
}, 5000); errorText = _t("User %(userId)s is already in the room", { userId: address });
return; break;
} else if (['M_NOT_FOUND', 'M_USER_NOT_FOUND'].includes(err.errcode)) { case "M_LIMIT_EXCEEDED":
errorText = _t("User %(user_id)s does not exist", { user_id: address }); // we're being throttled so wait a bit & try again
} else if (err.errcode === 'M_PROFILE_UNDISCLOSED') { setTimeout(() => {
errorText = _t("User %(user_id)s may or may not exist", { user_id: address }); this.doInvite(address, ignoreProfile).then(resolve, reject);
} else if (err.errcode === 'M_PROFILE_NOT_FOUND' && !ignoreProfile) { }, 5000);
// Invite without the profile check return;
console.warn(`User ${address} does not have a profile - inviting anyways automatically`); case "M_NOT_FOUND":
this.doInvite(address, true).then(resolve, reject); case "M_USER_NOT_FOUND":
} else if (err.errcode === "M_BAD_STATE") { errorText = _t("User %(user_id)s does not exist", { user_id: address });
errorText = _t("The user must be unbanned before they can be invited."); break;
} else if (err.errcode === "M_UNSUPPORTED_ROOM_VERSION") { case "M_PROFILE_UNDISCLOSED":
errorText = _t("The user's homeserver does not support the version of the room."); errorText = _t("User %(user_id)s may or may not exist", { user_id: address });
} else { 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'); errorText = _t('Unknown server error');
} }