Update for compatibility with v12 rooms (#30452)

* Update for compatibility with v12 rooms

Stop using powerLevelNorm and reading PL events manually.

To support https://github.com/matrix-org/matrix-js-sdk/pull/4937

* Add test for leave space dialog

* Don't compute stuff if we don't need it

* Use room.client

* Use getSafeUserId

* Remove client arg

* Use getJoinedMembers

and add doc

* Fix tests

* Fix more tests

* Fix other test

* Clarify comment

Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>

---------

Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
David Baker
2025-08-05 12:10:30 +01:00
committed by GitHub
parent 12927cc4a7
commit 6a8493c6eb
5 changed files with 105 additions and 33 deletions

View File

@@ -131,3 +131,23 @@ export async function waitForMember(
client.removeListener(RoomStateEvent.NewMember, handler);
});
}
/**
* Check if the user is the only joined admin in the room
* This function will *not* cause lazy loading of room members, so if these should be included then
* the caller needs to make sure members have been loaded.
* @param room The room to check if the user is the only admin.
* @returns True if the user is the only user with the highest power level, false otherwise
*/
export function isOnlyAdmin(room: Room): boolean {
const currentUserLevel = room.getMember(room.client.getSafeUserId())?.powerLevel;
const userLevelValues = room.getJoinedMembers().map((m) => m.powerLevel);
const maxUserLevel = Math.max(...userLevelValues.filter((x) => typeof x === "number"));
// If the user is the only user with highest power level
return (
maxUserLevel === currentUserLevel &&
userLevelValues.lastIndexOf(maxUserLevel) == userLevelValues.indexOf(maxUserLevel)
);
}