From 8e804a26b05eae7a471f8b64ee2c757ee95d2da8 Mon Sep 17 00:00:00 2001 From: Benedikt Ziemons Date: Sun, 10 Oct 2021 20:21:42 +0200 Subject: [PATCH] Add upschoolings settings, set ratelimit --- lib/Service/MatrixService.php | 85 ++++++++++++++++++++++++++++++++--- src/App.vue | 4 +- 2 files changed, 79 insertions(+), 10 deletions(-) diff --git a/lib/Service/MatrixService.php b/lib/Service/MatrixService.php index 8fc905a..65f9f4c 100644 --- a/lib/Service/MatrixService.php +++ b/lib/Service/MatrixService.php @@ -10,6 +10,7 @@ use Aryess\PhpMatrixSdk\MatrixClient; use Aryess\PhpMatrixSdk\Room; use OCA\UPschooling\Db\MatrixUser; use OCA\UPschooling\Exceptions\RoomNotFoundException; +use OCP\IConfig; use Psr\Log\LoggerInterface; class MatrixService @@ -18,11 +19,26 @@ class MatrixService /** @var LoggerInterface */ private $logger; + /** @var IConfig */ + private $config; + /** @var MatrixClient */ private $client; /** @var string */ - private $registrationSecret = "oyYh_iEJ7Aim.iB+ye.Xk;Gl3iHFab5*8K,zv~IulT85P=c-38"; + private $registrationSecret; + + /** @var string Matrix server URL */ + private $serverUrl; + + /** @var string Matrix server part */ + private $server; + + /** @var string Matrix admin user */ + private $superuser; + + /** @var string Matrix authentication token */ + private $token; /** * @throws MatrixRequestException @@ -30,14 +46,38 @@ class MatrixService * @throws ValidationException * @throws MatrixException */ - public function __construct(LoggerInterface $logger) + public function __construct(IConfig $config, LoggerInterface $logger) { $this->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); + $this->config = $config; + $this->registrationSecret = $this->config->getSystemValueString( + "upschooling.matrix_registration_secret", + "oyYh_iEJ7Aim.iB+ye.Xk;Gl3iHFab5*8K,zv~IulT85P=c-38" + ); + $this->serverUrl = $this->config->getSystemValueString( + "upschooling.matrix_server_url", + "http://synapse:8008" + ); + $this->server = $this->config->getSystemValueString( + "upschooling.matrix_server", + "synapse" + ); + $this->superuser = $this->config->getSystemValueString( + "upschooling.matrix_superuser", + "upschooling" + ); + $this->token = $this->config->getSystemValueString("upschooling.matrix_auth_token"); + if ($this->token != "") { + $this->client = new MatrixClient($this->serverUrl, $this->token); + $this->logger->debug("Using previous login as " . $this->superuser . " on server " . $this->serverUrl); + } else { + $this->client = new MatrixClient($this->serverUrl); + $token = $this->client->login($this->superuser, "secret", true); + $this->logger->debug("Logged in as " . $this->superuser . " on server " . $this->serverUrl); + $this->config->setSystemValue("upschooling.matrix_auth_token", $token); + } + + $this->checkRateLimit(); } /** @@ -176,4 +216,35 @@ class MatrixService return $roomId; } + private function checkRateLimit() + { + $fullSuperuserId = "@" . $this->superuser . ":" . $this->server; + $rateLimitResponse = $this->client->api()->send( + 'GET', + '/users/' . rawurlencode($fullSuperuserId) . '/override_ratelimit', + null, + [], + [], + '/_synapse/admin/v1', + true + ); + if (array_has($rateLimitResponse, "messages_per_second") && + array_has($rateLimitResponse, "burst_count")) { + $this->logger->debug("Ratelimit setting found for " . $this->superuser); + } else { + $this->client->api()->send( + 'POST', + '/users/' . rawurlencode($fullSuperuserId) . '/override_ratelimit', + array( + "messages_per_second" => 0, + "burst_count" => 0, + ), + [], + [], + '/_synapse/admin/v1', + true + ); + $this->logger->debug("No ratelimiting for " . $this->superuser); + } + } } diff --git a/src/App.vue b/src/App.vue index d93efd1..b4f3753 100644 --- a/src/App.vue +++ b/src/App.vue @@ -69,9 +69,7 @@ export default { 'api/v1/tickets/1', {}, { headers: { 'Content-Type': 'application/json', Accept: 'application/json' } } - ).then((response) => { - console.error('added a helper to ticket 2') - }).catch(console.error) + ).catch(console.error) }, fetchTickets() { axios.get(