From e52a4ef6e53f8d17fab8f63fb2df6b3538018796 Mon Sep 17 00:00:00 2001 From: David Woloszyn Date: Thu, 14 Dec 2023 14:48:48 +1100 Subject: [PATCH 1/2] MDL-80403 communication_matrix: Update membership checks membership --- .../matrix/classes/communication_feature.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/communication/provider/matrix/classes/communication_feature.php b/communication/provider/matrix/classes/communication_feature.php index cb8ed85229a..d784df3f171 100644 --- a/communication/provider/matrix/classes/communication_feature.php +++ b/communication/provider/matrix/classes/communication_feature.php @@ -190,6 +190,24 @@ class communication_feature implements } public function update_room_membership(array $userids): void { + + // Filter out any users that are not room members yet. + $response = $this->matrixapi->get_room_members( + roomid: $this->get_room_id(), + ); + $body = self::get_body($response); + + if (isset($body->joined)) { + foreach ($userids as $key => $userid) { + $matrixuserid = matrix_user_manager::get_matrixid_from_moodle( + userid: $userid, + ); + if (!array_key_exists($matrixuserid, (array) $body->joined)) { + unset($userids[$key]); + } + } + } + $this->set_matrix_power_levels(); // Mark the users as synced for the updated members. $this->processor->mark_users_as_synced($userids); From edffece53c97809259569cb523f5b1961c79c48c Mon Sep 17 00:00:00 2001 From: David Woloszyn Date: Tue, 19 Dec 2023 12:57:20 +1100 Subject: [PATCH 2/2] MDL-80403 communication_matrix: Fixed convert power levels to array The curent patch revealed a bug inside set_matrix_power_levels where returned user power levels contained an array of objects. Other methods were failing and giving incorrect responses as they were expecting arrays. --- .../provider/matrix/classes/communication_feature.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/communication/provider/matrix/classes/communication_feature.php b/communication/provider/matrix/classes/communication_feature.php index d784df3f171..337bfdf2ef1 100644 --- a/communication/provider/matrix/classes/communication_feature.php +++ b/communication/provider/matrix/classes/communication_feature.php @@ -598,6 +598,12 @@ class communication_feature implements $currentpowerlevels = $this->get_current_powerlevel_data(); $currentuserpowerlevels = (array) $currentpowerlevels->users ?? []; + // Ensure each user entry is an array. + $currentuserpowerlevels = array_map( + fn ($user) => (array) $user, + $currentuserpowerlevels, + ); + // Get all the current users who need to be in the room. $userlist = $this->processor->get_all_userids_for_instance(); @@ -656,7 +662,10 @@ class communication_feature implements private function get_users_with_custom_power_level(array $users): array { return array_filter( $users, - function ($level): bool { + function ($user): bool { + // Isolate the level value. + $level = array_values($user); + $level = reset($level); switch ($level) { case matrix_constants::POWER_LEVEL_DEFAULT: case matrix_constants::POWER_LEVEL_MOODLE_SITE_ADMIN: