From 9090d384ef824c492e0f59a4dde22906842e7532 Mon Sep 17 00:00:00 2001 From: Benedikt Ziemons Date: Wed, 15 Sep 2021 17:19:48 +0200 Subject: [PATCH] Add first revision of MatrixService Does not work, because of a GuzzleHttp version clash in nextcloud/3rdparty and php-matrix-sdk. --- lib/AppInfo/Application.php | 16 +++++++- lib/Controller/NoteController.php | 7 ++++ lib/Service/MatrixService.php | 65 +++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 lib/Service/MatrixService.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 86080dc..7644a05 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -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? + } } diff --git a/lib/Controller/NoteController.php b/lib/Controller/NoteController.php index 4c37699..b8fad83 100644 --- a/lib/Controller/NoteController.php +++ b/lib/Controller/NoteController.php @@ -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; } diff --git a/lib/Service/MatrixService.php b/lib/Service/MatrixService.php new file mode 100644 index 0000000..66aea08 --- /dev/null +++ b/lib/Service/MatrixService.php @@ -0,0 +1,65 @@ +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); + } + +}