2021-12-18 13:05:10 +00:00
|
|
|
<?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;
|
2022-02-19 12:05:59 +00:00
|
|
|
use OCA\UPschooling\Exceptions\RoomNotFoundException;
|
2021-12-18 13:05:10 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
|
|
/**
|
2022-02-19 12:05:59 +00:00
|
|
|
* This service will be used by the matrix listener background job to execute events and handle incoming 'matrix requests'
|
2021-12-18 13:05:10 +00:00
|
|
|
*/
|
|
|
|
class MatrixListenerService {
|
|
|
|
|
|
|
|
/** @var LoggerInterface */
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
/** @var MatrixClient */
|
|
|
|
private $client;
|
|
|
|
|
2022-02-19 12:05:59 +00:00
|
|
|
/** @var MatrixService */
|
|
|
|
private $matrixService;
|
|
|
|
|
|
|
|
/** @var token */
|
|
|
|
private $token;
|
|
|
|
|
2021-12-18 13:05:10 +00:00
|
|
|
/** @var string */
|
|
|
|
private $registrationSecret = "oyYh_iEJ7Aim.iB+ye.Xk;Gl3iHFab5*8K,zv~IulT85P=c-38";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param LoggerInterface $logger
|
2022-02-19 12:05:59 +00:00
|
|
|
* @param MatrixService $matrixService
|
2021-12-18 13:05:10 +00:00
|
|
|
* @throws MatrixException
|
|
|
|
* @throws MatrixHttpLibException
|
|
|
|
* @throws MatrixRequestException
|
|
|
|
* @throws ValidationException
|
|
|
|
*/
|
2022-02-19 12:05:59 +00:00
|
|
|
public function __construct(LoggerInterface $logger, MatrixService $matrixService) {
|
2021-12-18 13:05:10 +00:00
|
|
|
$this->logger = $logger;
|
2022-02-19 12:05:59 +00:00
|
|
|
$this->matrixService = $matrixService;
|
|
|
|
|
2021-12-18 13:05:10 +00:00
|
|
|
$serverUrl = "http://synapse:8008";
|
|
|
|
$user = "upschooling";
|
2022-02-19 12:05:59 +00:00
|
|
|
|
2021-12-18 13:05:10 +00:00
|
|
|
$this->client = new MatrixClient($serverUrl);
|
2022-02-19 12:05:59 +00:00
|
|
|
//TODO: Replace hard coded pw
|
|
|
|
$this->token = $this->client->login($user, "secret", true);
|
2021-12-18 13:05:10 +00:00
|
|
|
$this->logger->debug("Logged in as " . $user . " on server " . $serverUrl);
|
2022-02-19 12:05:59 +00:00
|
|
|
|
2021-12-18 13:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-02-19 12:05:59 +00:00
|
|
|
* listens on all sorts of incoming matrix requests
|
2021-12-18 13:05:10 +00:00
|
|
|
*
|
2022-02-19 12:05:59 +00:00
|
|
|
* @return void
|
2021-12-18 13:05:10 +00:00
|
|
|
* @throws MatrixException
|
2022-02-19 12:05:59 +00:00
|
|
|
* @throws MatrixHttpLibException
|
|
|
|
* @throws MatrixRequestException
|
|
|
|
* @throws RoomNotFoundException
|
2021-12-18 13:05:10 +00:00
|
|
|
*/
|
|
|
|
public function listen() {
|
|
|
|
$syncResponse = $this->client->api()->sync();
|
2022-02-19 12:05:59 +00:00
|
|
|
$nextBatch = $syncResponse['next_batch'];
|
2021-12-18 13:05:10 +00:00
|
|
|
foreach (array_get($syncResponse, 'rooms.invite', []) as $roomId => $inviteRoom) {
|
2022-02-19 12:05:59 +00:00
|
|
|
$this->handleInvite($roomId);
|
|
|
|
}
|
|
|
|
|
|
|
|
$rooms = $this->client->getRooms();
|
|
|
|
|
|
|
|
foreach ($rooms as $roomId => $room) {
|
|
|
|
$messages = $this->client->api()->getRoomMessages($roomId, $nextBatch, "b", 100)["chunk"];
|
|
|
|
foreach ($messages as $message) {
|
|
|
|
if ($message["type"] == "m.room.message" && $message["content"]["msgtype"] == "m.text")
|
|
|
|
var_dump($message["content"]["body"]);
|
2021-12-18 13:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-19 12:05:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TBD: Check if the user on the other side is actually allowed to talk to this matrix instance
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function auth() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given invitation object it'll enter the room
|
|
|
|
*
|
|
|
|
* @param int $roomId the roomID
|
|
|
|
* @return void
|
|
|
|
* @throws MatrixException|RoomNotFoundException
|
|
|
|
*/
|
|
|
|
private function handleInvite(int $roomId) {
|
|
|
|
try {
|
|
|
|
$this->client->joinRoom($roomId);
|
|
|
|
$this->client->api()->sendMessageEvent($roomId, "upschooling.matrix.accepted", []);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->info("Join for room: " . $roomId . " failed.");
|
|
|
|
}
|
|
|
|
$this->matrixService->setProperty($roomId, "upschooling.s2s", ["approved" => true]);
|
|
|
|
$this->logger->debug("joined " . $roomId);
|
|
|
|
}
|
2021-12-18 13:05:10 +00:00
|
|
|
}
|