From 48bed5ce1a59f0063f545a8074f0de991d20e4d2 Mon Sep 17 00:00:00 2001
From: Thomas Hackl <hackl@data-quest.de>
Date: Fri, 9 Feb 2024 08:00:36 +0100
Subject: [PATCH] catch exception if user has already left the matrix room so
 that they can't be removed anymore

---
 MatrixPlugin.php | 51 +++++++++++++++++++++++++-----------------------
 plugin.manifest  |  2 +-
 2 files changed, 28 insertions(+), 25 deletions(-)

diff --git a/MatrixPlugin.php b/MatrixPlugin.php
index c1618ca..938395c 100644
--- a/MatrixPlugin.php
+++ b/MatrixPlugin.php
@@ -157,30 +157,33 @@ class MatrixPlugin extends StudIPPlugin implements StandardPlugin
         $matrix = MatrixAccount::findOneByUser_id($membership->user_id);
         $room = MatrixRoom::findOneByRange_id($membership->seminar_id);
         if (is_a($matrix, MatrixAccount::class) && is_a($room, MatrixRoom::class)) {
-            /*
-             * leaveRoom can only be used if we don't authorize our Matrix users with SSO and login token stuff.
-             * Otherwise we use the Stud.IP system account to kick the user from the room.
-             */
-            if (Config::get()->MATRIX_SYSTEM_ACCOUNT_ACCESS_TOKEN) {
-                // POST body.
-                $data = [
-                    'user_id' => $matrix->matrix_account_id,
-                    'reason' => sprintf('Not a member of course "%s" anymore.', $membership->course->getFullname())
-                ];
-
-                MatrixClient::get()->requestServer(
-                    sprintf('/_matrix/client/r0/rooms/%s/kick', $room->matrix_room_id),
-                    'POST',
-                    $data,
-                    [],
-                    MatrixAccount::requireSystemAccount()
-                );
-            } else {
-                MatrixClient::get()->leaveRoom(
-                    $matrix->getLinkedAccount(),
-                    $room->getLinkedRoom()
-                );
-            }
+
+            try {
+                /*
+                 * leaveRoom can only be used if we don't authorize our Matrix users with SSO and login token stuff.
+                 * Otherwise we use the Stud.IP system account to kick the user from the room.
+                 */
+                if (Config::get()->MATRIX_SYSTEM_ACCOUNT_ACCESS_TOKEN) {
+                    // POST body.
+                    $data = [
+                        'user_id' => $matrix->matrix_account_id,
+                        'reason' => sprintf('Not a member of course "%s" anymore.', $membership->course->getFullname())
+                    ];
+
+                    MatrixClient::get()->requestServer(
+                        sprintf('/_matrix/client/r0/rooms/%s/kick', $room->matrix_room_id),
+                        'POST',
+                        $data,
+                        [],
+                        MatrixAccount::requireSystemAccount()
+                    );
+                } else {
+                    MatrixClient::get()->leaveRoom(
+                        $matrix->getLinkedAccount(),
+                        $room->getLinkedRoom()
+                    );
+                }
+            } catch (Exception $e) {}
         }
     }
 
diff --git a/plugin.manifest b/plugin.manifest
index 1e7a6c2..c104c45 100644
--- a/plugin.manifest
+++ b/plugin.manifest
@@ -1,7 +1,7 @@
 pluginname=Matrix-Chat
 pluginclassname=MatrixPlugin
 origin=data-quest
-version=1.4.6
+version=1.4.7
 screenshot=assets/images/matrix_logo.png
 description=Matrix chat for Stud.IP courses
 studipMinVersion=4.5
-- 
GitLab