Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MDL-81951 changes to power levels #11

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion application/src/Controller/BackOfficeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ public function getAllRooms(string $serverID): JSONResponse
'rooms' => array_map(function ($room) {
$roomdata = $room->jsonSerialize();
$roomdata->members = array_map(
fn($membership) => $membership->getUser()->jsonSerialize(),
function (RoomMember $membership) {
$memberdata = $membership->getUser()->jsonSerialize();
$memberdata->powerlevel = $membership->getPowerLevel() ?? 0;
return $memberdata;
},
array_filter(
$room->getMembers()->toArray(),
function (RoomMember $membership): bool {
Expand Down
116 changes: 69 additions & 47 deletions application/src/Controller/MatrixController.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public function roomState(string $serverID, string $roomID, string $eventType, R
// 1. Check call auth.
// 2. Check HTTP method is accepted.

$accessCheck = $this->authHttpCheck(['PUT'], $request);
$accessCheck = $this->authHttpCheck(['PUT', 'GET'], $request);
if (!$accessCheck['status']) {
return $accessCheck['message'];
}
Expand All @@ -417,61 +417,83 @@ public function roomState(string $serverID, string $roomID, string $eventType, R
return $this->getUnknownRoomResponse();
}

$payload = json_decode($request->getContent());
$method = $request->getMethod();

if ($eventType == 'm.room.topic') {
$check = $this->validateRequest((array)$payload, ['topic']);
if (!$check['status']) {
return $check['message'];
}
$room->setTopic($payload->topic);
// PUT requests.
if ($method == 'PUT') {

} elseif ($eventType == 'm.room.name') {
// Update room name.
$check = $this->validateRequest((array)$payload, ['name']);
if (!$check['status']) {
return $check['message'];
}
$room->setName($payload->name);
$payload = json_decode($request->getContent());

} elseif ($eventType == 'm.room.avatar') {
// Update room avatar.
$check = $this->validateRequest((array)$payload, ['url']);
if (!$check['status']) {
return $check['message'];
}
$room->setAvatar($payload->url);
} elseif ($eventType == 'm.room.power_levels') {
// Room power level api call validation to check if the users are passed in the payload to update the power level.
$check = $this->validateRequest((array)$payload, ['users']);
if (!$check['status']) {
return $check['message'];
}
// Update power levels for each room member.
foreach ($payload->users as $userid => $level) {
if (!empty($level)) {
$user = $entityManager->getRepository(User::class)->findOneBy([
'serverid' => $serverID,
'userid' => $userid,
]);
if (!empty($user)) {
$membership = $entityManager->getRepository(RoomMember::class)->findOneBy([
if ($eventType == 'm.room.topic') {
$check = $this->validateRequest((array)$payload, ['topic']);
if (!$check['status']) {
return $check['message'];
}
$room->setTopic($payload->topic);

} elseif ($eventType == 'm.room.name') {
// Update room name.
$check = $this->validateRequest((array)$payload, ['name']);
if (!$check['status']) {
return $check['message'];
}
$room->setName($payload->name);

} elseif ($eventType == 'm.room.avatar') {
// Update room avatar.
$check = $this->validateRequest((array)$payload, ['url']);
if (!$check['status']) {
return $check['message'];
}
$room->setAvatar($payload->url);
} elseif ($eventType == 'm.room.power_levels') {
// Room power level api call validation to check if the users are passed in the payload to update the power level.
$check = $this->validateRequest((array)$payload, ['users']);
if (!$check['status']) {
return $check['message'];
}
// Update power levels for each room member.
foreach ($payload->users as $userid => $level) {
if (!empty($level)) {
$user = $entityManager->getRepository(User::class)->findOneBy([
'serverid' => $serverID,
'room' => $room,
'user' => $user,
'userid' => $userid,
]);
if(!empty($membership)) {
$membership->setPowerLevel($level);
if (!empty($user)) {
$membership = $entityManager->getRepository(RoomMember::class)->findOneBy([
'serverid' => $serverID,
'room' => $room,
'user' => $user,
]);
if(!empty($membership)) {
$membership->setPowerLevel($level);
}
}
}
}
} else {
// Unknown state.
return new JsonResponse((object) [
'errcode' => 'M_UNRECOGNIZED',
'error' => 'Unrecognized request'
], 404);
}

// GET requests.
} else if ($method == 'GET') {

if ($eventType == 'm.room.power_levels') {

$powerLevels = $room->getPowerLevels($request);
return new JsonResponse((object) $powerLevels, 200);

} else {
// Unknown state.
return new JsonResponse((object) [
'errcode' => 'M_UNRECOGNIZED',
'error' => 'Unrecognized request'
], 404);
}
} else {
// Unknown state.
return new JsonResponse((object) [
'errcode' => 'M_UNRECOGNIZED',
'error' => 'Unrecognized request'
], 404);
}

$entityManager = $this->getDoctrine()->getManager();
Expand Down
69 changes: 49 additions & 20 deletions application/src/Entity/Room.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,18 @@ public function getRoomState(): array
return $roomData;
}

protected function getPowerLevelValues(): array
{
return [
'default' => 0,
'moderator' => 50,
'maximum' => 100,
];
}

protected function getPowerLevelState(): array
{
// Power level values.
$powerLevelDefault = 0;
$powerLevelModerator = 50;
$powerLevelMaximum = 100;
$powerLevels = $this->getPowerLevelValues();

// Get the members of the room who have a powerlevel set.
$members = $this->getMembers()->filter(function (RoomMember $member) {
Expand All @@ -237,25 +243,48 @@ protected function getPowerLevelState(): array
'type' => 'm.room.power_levels',
'content' => [
'users' => (object) $memberInfo,
'users_default' => $powerLevelDefault,
'users_default' => $powerLevels['default'],
'events' => [
'm.room.name' => $powerLevelModerator,
'm.room.power_levels' => $powerLevelMaximum,
'm.room.history_visibility' => $powerLevelMaximum,
'm.room.canonical_alias' => $powerLevelModerator,
'm.room.avatar' => $powerLevelModerator,
'm.room.tombstone' => $powerLevelMaximum,
'm.room.server_acl' => $powerLevelMaximum,
'm.room.encryption' => $powerLevelMaximum,
'm.room.name' => $powerLevels['moderator'],
'm.room.power_levels' => $powerLevels['maximum'],
'm.room.history_visibility' => $powerLevels['maximum'],
'm.room.canonical_alias' => $powerLevels['moderator'],
'm.room.avatar' => $powerLevels['moderator'],
'm.room.tombstone' => $powerLevels['maximum'],
'm.room.server_acl' => $powerLevels['maximum'],
'm.room.encryption' => $powerLevels['maximum'],
],
'events_default' => $powerLevelDefault,
'state_default' => $powerLevelModerator,
'ban' => $powerLevelModerator,
'kick' => $powerLevelModerator,
'redact' => $powerLevelModerator,
'invite' => $powerLevelDefault,
'historical' => $powerLevelMaximum,
'events_default' => $powerLevels['default'],
'state_default' => $powerLevels['moderator'],
'ban' => $powerLevels['moderator'],
'kick' => $powerLevels['moderator'],
'redact' => $powerLevels['moderator'],
'invite' => $powerLevels['moderator'],
'historical' => $powerLevels['maximum'],
],
];
}

public function getPowerLevels(): array
{
$powerLevels = $this->getPowerLevelValues();

// Get the members of the room who have a power level set.
$members = $this->getMembers()->filter(function (RoomMember $member) {
return $member->getPowerLevel() !== null;
});

// Build list of users and their power level.
$memberInfo = array_merge(...$members->map(fn(RoomMember $member) => [
$member->getUser()->getUserid() => $member->getPowerLevel(),
])->toArray());

return [
'users' => (object) $memberInfo,
'ban' => $powerLevels['moderator'],
'kick' => $powerLevels['moderator'],
'redact' => $powerLevels['moderator'],
'invite' => $powerLevels['moderator'],
];
}
}