Add first revision of MatrixService

Does not work, because of a GuzzleHttp version clash
in nextcloud/3rdparty and php-matrix-sdk.
This commit is contained in:
Ben 2021-09-15 17:19:48 +02:00
parent 0113cdad85
commit 9090d384ef
Signed by: ben
GPG key ID: 0F54A7ED232D3319
3 changed files with 87 additions and 1 deletions

View file

@ -3,11 +3,25 @@
namespace OCA\UPschooling\AppInfo;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
class Application extends App {
class Application extends App implements IBootstrap {
public const APP_ID = 'upschooling';
public function __construct() {
parent::__construct(self::APP_ID);
}
public function register(IRegistrationContext $context): void
{
// Register the composer autoloader for packages shipped by this app, if applicable
include_once __DIR__ . '/../../vendor/autoload.php';
}
public function boot(IBootContext $context): void
{
// nothing to boot?
}
}

View file

@ -3,15 +3,20 @@
namespace OCA\UPschooling\Controller;
use OCA\UPschooling\AppInfo\Application;
use OCA\UPschooling\Service\MatrixService;
use OCA\UPschooling\Service\NoteService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\IRequest;
class NoteController extends Controller {
/** @var NoteService */
private $service;
/** @var MatrixService */
private $matrix;
/** @var string */
private $userId;
@ -19,9 +24,11 @@ class NoteController extends Controller {
public function __construct(IRequest $request,
NoteService $service,
MatrixService $matrix,
$userId) {
parent::__construct(Application::APP_ID, $request);
$this->service = $service;
$this->matrix = $matrix;
$this->userId = $userId;
}

View file

@ -0,0 +1,65 @@
<?php
namespace OCA\UPschooling\Service;
use Aryess\PhpMatrixSdk\Exceptions\MatrixRequestException;
use \Psr\Log\LoggerInterface;
use Aryess\PhpMatrixSdk\Exceptions\MatrixException;
use Aryess\PhpMatrixSdk\MatrixClient;
use Aryess\PhpMatrixSdk\Room;
class MatrixService
{
/** @var LoggerInterface */
private $logger;
/** @var string */
private $channel;
/** @var MatrixClient */
private $client;
/** @var string */
private $token;
/** @var Room */
private $room;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
$this->channel = "#issue:synapse";
$this->client = new MatrixClient("http://synapse:8008");
$this->token = $this->client->login("upschooling", "secret");
try {
$this->room = $this->client->createRoom($this->channel, false, array());
} catch (MatrixException $e) {
$this->logger->error("Could not create room ".$this->channel, array('exception' => $e));
}
}
/**
* Sets a property on a channel.
*
* @param string $eventType a unique property identifier with reverse domain notation, e.g. com.example.property.
* @param array $content the contents as a JSON serializable array.
* @throws MatrixException
*/
public function setProperty(string $eventType, array $content)
{
$this->room->sendStateEvent($eventType, $content);
}
/**
* @param string $eventType a unique property identifier with reverse domain notation, e.g. com.example.property.
* @return array the contents of the room state.
* @throws MatrixException|MatrixRequestException
*/
public function getProperty(string $eventType): array
{
// first parameter should be $this->room->roomId
return $this->client->api()->getStateEvent($this->channel, $eventType);
}
}