Skip to content

Commit

Permalink
Add leave room warning for last admin
Browse files Browse the repository at this point in the history
If the last room administrator leaves a room, other users cannot
gain admin privilges anymore, leaving the room in an unmoderable
state. To help in avoiding this scenario without actually preventing
an admin from leaving the room if they really want, this commit
adds a new warning message.

Attempts to help with: element-hq/element-web#2855

Signed-off-by: Arne Wilken [email protected]
  • Loading branch information
Arnei committed Oct 19, 2022
1 parent 877c95d commit 2f4fac0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/components/structures/MatrixChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ import { UseCaseSelection } from '../views/elements/UseCaseSelection';
import { ValidatedServerConfig } from '../../utils/ValidatedServerConfig';
import { isLocalRoom } from '../../utils/localRoom/isLocalRoom';
import { viewUserDeviceSettings } from '../../actions/handlers/viewUserDeviceSettings';
import { isNumberArray } from '../../utils/TypeUtils';

// legacy export
export { default as Views } from "../../Views";
Expand Down Expand Up @@ -1125,6 +1126,29 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
));
}
}

const client = MatrixClientPeg.get();
const plEvent = roomToLeave.currentState.getStateEvents(EventType.RoomPowerLevels, '');
const plContent = plEvent ? (plEvent.getContent() || {}) : {};
const userLevels = plContent.users || {};
const currentUserLevel = userLevels[client.getUserId()];
const userLevelValues = Object.values(userLevels);
if (isNumberArray(userLevelValues)) {
const maxUserLevel = Math.max(...userLevelValues);
// If the user is the only user with highest power level
if (maxUserLevel === currentUserLevel &&
userLevelValues.lastIndexOf(maxUserLevel) == userLevelValues.indexOf(maxUserLevel)) {
warnings.push((
<span className="warning" key="last_admin_warning">
{ ' '/* Whitespace, otherwise the sentences get smashed together */ }
{ _t("You are the sole person with the highest role in this room. " +
"If you leave, the room could become unmoderable. Consider giving " +
"another user your role.") }
</span>
));
}
}

return warnings;
}

Expand Down
1 change: 1 addition & 0 deletions src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -3232,6 +3232,7 @@
"You are the only person here. If you leave, no one will be able to join in the future, including you.": "You are the only person here. If you leave, no one will be able to join in the future, including you.",
"This space is not public. You will not be able to rejoin without an invite.": "This space is not public. You will not be able to rejoin without an invite.",
"This room is not public. You will not be able to rejoin without an invite.": "This room is not public. You will not be able to rejoin without an invite.",
"You are the sole person with the highest role in this room. If you leave, the room could become unmoderable. Consider giving another user your role.": "You are the sole person with the highest role in this room. If you leave, the room could become unmoderable. Consider giving another user your role.",
"Are you sure you want to leave the space '%(spaceName)s'?": "Are you sure you want to leave the space '%(spaceName)s'?",
"Are you sure you want to leave the room '%(roomName)s'?": "Are you sure you want to leave the room '%(roomName)s'?",
"Failed to forget room %(errCode)s": "Failed to forget room %(errCode)s",
Expand Down
11 changes: 11 additions & 0 deletions src/utils/TypeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ export function makeType(Type: any, opts: any) {
Object.assign(c, opts);
return c;
}

/**
* Typeguard that checks if an unknown variable is an array of numbers
* @param value the variable to check
* @returns true if the variable is an array of numbers, false otherwise
*/
export function isNumberArray(value: unknown): value is number[] {
return (
Array.isArray(value) && value.every(element => typeof element === "number")
);
}

0 comments on commit 2f4fac0

Please sign in to comment.