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

Add leave room warning for last admin #9452

Merged
merged 8 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { ValidatedServerConfig } from '../../utils/ValidatedServerConfig';
import { isLocalRoom } from '../../utils/localRoom/isLocalRoom';
import { SdkContextClass, SDKContext } from '../../contexts/SDKContext';
import { viewUserDeviceSettings } from '../../actions/handlers/viewUserDeviceSettings';
import { isNumberArray } from '../../utils/TypeUtils';
import { VoiceBroadcastResumer } from '../../voice-broadcast';

// legacy export
Expand Down Expand Up @@ -1132,6 +1133,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 " +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unmoderable doesn't feel like a word. unmoderatable sounds better to me but I also don't see it as a real word so probably need to rearrange the prose to accommodate proper English. Perhaps:

"If you leave, the room might not be able to be moderated anymore."

"another user your role.") }
Arnei marked this conversation as resolved.
Show resolved Hide resolved
</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 @@ -3277,6 +3277,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")
);
}