Compare commits

...

4 Commits

Author SHA1 Message Date
Finn 35b5dbeb2d
add basic invite handling 2022-02-19 13:05:59 +01:00
Finn ff17240641
register autojoin job 2021-12-18 19:13:52 +01:00
Finn b62c2ba537
add autojoin for matrix invites 2021-12-18 19:13:52 +01:00
Finn a403b6b614
reformat code 2021-12-18 19:13:47 +01:00
5 changed files with 175 additions and 23 deletions

View File

@ -11,6 +11,9 @@
<namespace>UPschooling</namespace>
<category>tools</category>
<bugs>https://gitea.rs485.network/UPschooling/Nextcloud-App/issues</bugs>
<background-jobs>
<job>OCA\UPschooling\BackgroundJob\MatrixListener</job>
</background-jobs>
<dependencies>
<nextcloud min-version="20" max-version="22"/>
</dependencies>

View 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();
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace OCA\UPschooling\Events;
use OCP\EventDispatcher\Event;
class NewMessageEvent extends Event {
public function __construct()
{
}
}

View File

@ -55,30 +55,30 @@ class Version000000Date20210918151800 extends SimpleMigrationStep {
$table->addIndex(['matrix_room'], 'upschooling_mx_room_id_idx');
}
if (!$schema->hasTable('upschooling_users')) {
$table = $schema->createTable('upschooling_users');
$table->addColumn('id', 'integer', [
'autoincrement' => true,
'notnull' => true,
]);
$table->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 64,
]);
$table->addColumn('matrix_user', 'string', [
'notnull' => true,
'length' => 200,
]);
$table->addColumn('matrix_token', 'string', [
'notnull' => true,
'length' => 200,
]);
if (!$schema->hasTable('upschooling_users')) {
$table = $schema->createTable('upschooling_users');
$table->addColumn('id', 'integer', [
'autoincrement' => true,
'notnull' => true,
]);
$table->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 64,
]);
$table->addColumn('matrix_user', 'string', [
'notnull' => true,
'length' => 200,
]);
$table->addColumn('matrix_token', 'string', [
'notnull' => true,
'length' => 200,
]);
$table->setPrimaryKey(['id']);
$table->addUniqueConstraint(['user_id'], 'upschooling_mx_user_nc_uniq');
$table->addUniqueConstraint(['matrix_user'], 'upschooling_mx_user_mx_uniq');
$table->addForeignKeyConstraint('users', ['user_id'], ['uid'], [], 'upschooling_mx_user_nc_fk');
}
$table->setPrimaryKey(['id']);
$table->addUniqueConstraint(['user_id'], 'upschooling_mx_user_nc_uniq');
$table->addUniqueConstraint(['matrix_user'], 'upschooling_mx_user_mx_uniq');
$table->addForeignKeyConstraint('users', ['user_id'], ['uid'], [], 'upschooling_mx_user_nc_fk');
}
return $schema;
}
}

View File

@ -0,0 +1,107 @@
<?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 OCA\UPschooling\Exceptions\RoomNotFoundException;
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 {
/** @var LoggerInterface */
private $logger;
/** @var MatrixClient */
private $client;
/** @var MatrixService */
private $matrixService;
/** @var token */
private $token;
/** @var string */
private $registrationSecret = "oyYh_iEJ7Aim.iB+ye.Xk;Gl3iHFab5*8K,zv~IulT85P=c-38";
/**
* @param LoggerInterface $logger
* @param MatrixService $matrixService
* @throws MatrixException
* @throws MatrixHttpLibException
* @throws MatrixRequestException
* @throws ValidationException
*/
public function __construct(LoggerInterface $logger, MatrixService $matrixService) {
$this->logger = $logger;
$this->matrixService = $matrixService;
$serverUrl = "http://synapse:8008";
$user = "upschooling";
$this->client = new MatrixClient($serverUrl);
//TODO: Replace hard coded pw
$this->token = $this->client->login($user, "secret", true);
$this->logger->debug("Logged in as " . $user . " on server " . $serverUrl);
}
/**
* listens on all sorts of incoming matrix requests
*
* @return void
* @throws MatrixException
* @throws MatrixHttpLibException
* @throws MatrixRequestException
* @throws RoomNotFoundException
*/
public function listen() {
$syncResponse = $this->client->api()->sync();
$nextBatch = $syncResponse['next_batch'];
foreach (array_get($syncResponse, 'rooms.invite', []) as $roomId => $inviteRoom) {
$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"]);
}
}
}
/**
* 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);
}
}