From b62c2ba53766fcf883546524439fa78835525d64 Mon Sep 17 00:00:00 2001 From: Finn Date: Sat, 18 Dec 2021 14:05:10 +0100 Subject: [PATCH] add autojoin for matrix invites --- lib/BackgroundJob/MatrixListener.php | 28 ++++++++++++ lib/Events/NewMessageEvent.php | 14 ++++++ lib/Service/MatrixListenerService.php | 66 +++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 lib/BackgroundJob/MatrixListener.php create mode 100644 lib/Events/NewMessageEvent.php create mode 100644 lib/Service/MatrixListenerService.php diff --git a/lib/BackgroundJob/MatrixListener.php b/lib/BackgroundJob/MatrixListener.php new file mode 100644 index 0000000..733a7b9 --- /dev/null +++ b/lib/BackgroundJob/MatrixListener.php @@ -0,0 +1,28 @@ +logger = $logger; + $this->matrixListenerService = $matrixListenerService; + } + + protected function run($argument) { + $this->matrixListenerService->listen(); + } +} diff --git a/lib/Events/NewMessageEvent.php b/lib/Events/NewMessageEvent.php new file mode 100644 index 0000000..15ea058 --- /dev/null +++ b/lib/Events/NewMessageEvent.php @@ -0,0 +1,14 @@ +logger = $logger; + $serverUrl = "http://synapse:8008"; + $user = "upschooling"; + $this->client = new MatrixClient($serverUrl); + $this->client->login($user, "secret", true); + $this->logger->debug("Logged in as " . $user . " on server " . $serverUrl); + } + + /** + * Handle invites + * + * @throws MatrixException + */ + public function handleInvites(int $roomId, array $someState) { + $this->client->joinRoom($roomId); + } + + public static function test() { + var_dump("Test"); + } + + public function listen() { + $syncResponse = $this->client->api()->sync(); + foreach (array_get($syncResponse, 'rooms.invite', []) as $roomId => $inviteRoom) { + try { + $this->client->joinRoom($roomId); + } catch (\Exception $e) { + $this->logger->info("Join for room: " . $roomId . " failed."); + } + $this->logger->debug("joined " . $roomId); + } + } +}