add basic invite handling

This commit is contained in:
Finn 2022-02-19 13:05:59 +01:00
parent ff17240641
commit 35b5dbeb2d
Signed by: finn
GPG Key ID: C372C6329642D154
1 changed files with 58 additions and 17 deletions

View File

@ -7,10 +7,11 @@ use Aryess\PhpMatrixSdk\Exceptions\MatrixHttpLibException;
use Aryess\PhpMatrixSdk\Exceptions\MatrixRequestException; use Aryess\PhpMatrixSdk\Exceptions\MatrixRequestException;
use Aryess\PhpMatrixSdk\Exceptions\ValidationException; use Aryess\PhpMatrixSdk\Exceptions\ValidationException;
use Aryess\PhpMatrixSdk\MatrixClient; use Aryess\PhpMatrixSdk\MatrixClient;
use OCA\UPschooling\Exceptions\RoomNotFoundException;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
/** /**
* * This service will be used by the matrix listener background job to execute events and handle incoming 'matrix requests'
*/ */
class MatrixListenerService { class MatrixListenerService {
@ -20,47 +21,87 @@ class MatrixListenerService {
/** @var MatrixClient */ /** @var MatrixClient */
private $client; private $client;
/** @var MatrixService */
private $matrixService;
/** @var token */
private $token;
/** @var string */ /** @var string */
private $registrationSecret = "oyYh_iEJ7Aim.iB+ye.Xk;Gl3iHFab5*8K,zv~IulT85P=c-38"; private $registrationSecret = "oyYh_iEJ7Aim.iB+ye.Xk;Gl3iHFab5*8K,zv~IulT85P=c-38";
/** /**
* @param LoggerInterface $logger * @param LoggerInterface $logger
* @param MatrixService $matrixService
* @throws MatrixException * @throws MatrixException
* @throws MatrixHttpLibException * @throws MatrixHttpLibException
* @throws MatrixRequestException * @throws MatrixRequestException
* @throws ValidationException * @throws ValidationException
*/ */
public function __construct(LoggerInterface $logger) { public function __construct(LoggerInterface $logger, MatrixService $matrixService) {
$this->logger = $logger; $this->logger = $logger;
$this->matrixService = $matrixService;
$serverUrl = "http://synapse:8008"; $serverUrl = "http://synapse:8008";
$user = "upschooling"; $user = "upschooling";
$this->client = new MatrixClient($serverUrl); $this->client = new MatrixClient($serverUrl);
$this->client->login($user, "secret", true); //TODO: Replace hard coded pw
$this->token = $this->client->login($user, "secret", true);
$this->logger->debug("Logged in as " . $user . " on server " . $serverUrl); $this->logger->debug("Logged in as " . $user . " on server " . $serverUrl);
} }
/** /**
* Handle invites * listens on all sorts of incoming matrix requests
* *
* @return void
* @throws MatrixException * @throws MatrixException
* @throws MatrixHttpLibException
* @throws MatrixRequestException
* @throws RoomNotFoundException
*/ */
public function handleInvites(int $roomId, array $someState) {
$this->client->joinRoom($roomId);
}
public static function test() {
var_dump("Test");
}
public function listen() { public function listen() {
$syncResponse = $this->client->api()->sync(); $syncResponse = $this->client->api()->sync();
$nextBatch = $syncResponse['next_batch'];
foreach (array_get($syncResponse, 'rooms.invite', []) as $roomId => $inviteRoom) { foreach (array_get($syncResponse, 'rooms.invite', []) as $roomId => $inviteRoom) {
try { $this->handleInvite($roomId);
$this->client->joinRoom($roomId); }
} catch (\Exception $e) {
$this->logger->info("Join for room: " . $roomId . " failed."); $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"]);
} }
$this->logger->debug("joined " . $roomId);
} }
} }
/**
* 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);
}
} }