add autojoin for matrix invites
This commit is contained in:
parent
a403b6b614
commit
b62c2ba537
28
lib/BackgroundJob/MatrixListener.php
Normal file
28
lib/BackgroundJob/MatrixListener.php
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace OCA\UPschooling\BackgroundJob;
|
||||||
|
|
||||||
|
use OCA\UPschooling\Service\MatrixListenerService;
|
||||||
|
use OCP\AppFramework\Utility\ITimeFactory;
|
||||||
|
use OCP\BackgroundJob\TimedJob;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
|
class MatrixListener extends TimedJob {
|
||||||
|
/** @var MatrixListenerService */
|
||||||
|
private $matrixListenerService;
|
||||||
|
|
||||||
|
/** @var LoggerInterface */
|
||||||
|
private $logger;
|
||||||
|
|
||||||
|
public function __construct(ITimeFactory $time, MatrixListenerService $matrixListenerService, LoggerInterface $logger) {
|
||||||
|
parent::__construct($time);
|
||||||
|
|
||||||
|
parent::setInterval(1);
|
||||||
|
$this->logger = $logger;
|
||||||
|
$this->matrixListenerService = $matrixListenerService;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function run($argument) {
|
||||||
|
$this->matrixListenerService->listen();
|
||||||
|
}
|
||||||
|
}
|
14
lib/Events/NewMessageEvent.php
Normal file
14
lib/Events/NewMessageEvent.php
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace OCA\UPschooling\Events;
|
||||||
|
|
||||||
|
use OCP\EventDispatcher\Event;
|
||||||
|
|
||||||
|
class NewMessageEvent extends Event {
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
66
lib/Service/MatrixListenerService.php
Normal file
66
lib/Service/MatrixListenerService.php
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace OCA\UPschooling\Service;
|
||||||
|
|
||||||
|
use Aryess\PhpMatrixSdk\Exceptions\MatrixException;
|
||||||
|
use Aryess\PhpMatrixSdk\Exceptions\MatrixHttpLibException;
|
||||||
|
use Aryess\PhpMatrixSdk\Exceptions\MatrixRequestException;
|
||||||
|
use Aryess\PhpMatrixSdk\Exceptions\ValidationException;
|
||||||
|
use Aryess\PhpMatrixSdk\MatrixClient;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class MatrixListenerService {
|
||||||
|
|
||||||
|
/** @var LoggerInterface */
|
||||||
|
private $logger;
|
||||||
|
|
||||||
|
/** @var MatrixClient */
|
||||||
|
private $client;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
private $registrationSecret = "oyYh_iEJ7Aim.iB+ye.Xk;Gl3iHFab5*8K,zv~IulT85P=c-38";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param LoggerInterface $logger
|
||||||
|
* @throws MatrixException
|
||||||
|
* @throws MatrixHttpLibException
|
||||||
|
* @throws MatrixRequestException
|
||||||
|
* @throws ValidationException
|
||||||
|
*/
|
||||||
|
public function __construct(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue