Add upschoolings settings, set ratelimit

This commit is contained in:
Ben 2021-10-10 20:21:42 +02:00
parent e66350b726
commit 8e804a26b0
Signed by: ben
GPG Key ID: 0F54A7ED232D3319
2 changed files with 79 additions and 10 deletions

View File

@ -10,6 +10,7 @@ use Aryess\PhpMatrixSdk\MatrixClient;
use Aryess\PhpMatrixSdk\Room; use Aryess\PhpMatrixSdk\Room;
use OCA\UPschooling\Db\MatrixUser; use OCA\UPschooling\Db\MatrixUser;
use OCA\UPschooling\Exceptions\RoomNotFoundException; use OCA\UPschooling\Exceptions\RoomNotFoundException;
use OCP\IConfig;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
class MatrixService class MatrixService
@ -18,11 +19,26 @@ class MatrixService
/** @var LoggerInterface */ /** @var LoggerInterface */
private $logger; private $logger;
/** @var IConfig */
private $config;
/** @var MatrixClient */ /** @var MatrixClient */
private $client; private $client;
/** @var string */ /** @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 * @throws MatrixRequestException
@ -30,14 +46,38 @@ class MatrixService
* @throws ValidationException * @throws ValidationException
* @throws MatrixException * @throws MatrixException
*/ */
public function __construct(LoggerInterface $logger) public function __construct(IConfig $config, LoggerInterface $logger)
{ {
$this->logger = $logger; $this->logger = $logger;
$serverUrl = "http://synapse:8008"; $this->config = $config;
$user = "upschooling"; $this->registrationSecret = $this->config->getSystemValueString(
$this->client = new MatrixClient($serverUrl); "upschooling.matrix_registration_secret",
$this->client->login($user, "secret", true); "oyYh_iEJ7Aim.iB+ye.Xk;Gl3iHFab5*8K,zv~IulT85P=c-38"
$this->logger->debug("Logged in as " . $user . " on server " . $serverUrl); );
$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; 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);
}
}
} }

View File

@ -69,9 +69,7 @@ export default {
'api/v1/tickets/1', 'api/v1/tickets/1',
{}, {},
{ headers: { 'Content-Type': 'application/json', Accept: 'application/json' } } { headers: { 'Content-Type': 'application/json', Accept: 'application/json' } }
).then((response) => { ).catch(console.error)
console.error('added a helper to ticket 2')
}).catch(console.error)
}, },
fetchTickets() { fetchTickets() {
axios.get( axios.get(