Nextcloud-App/lib/Service/MatrixService.php
2021-09-18 19:28:10 +02:00

65 lines
1.9 KiB
PHP

<?php
namespace OCA\UPschooling\Service;
use Aryess\PhpMatrixSdk\Exceptions\MatrixException;
use Aryess\PhpMatrixSdk\Exceptions\MatrixRequestException;
use Aryess\PhpMatrixSdk\MatrixClient;
use Aryess\PhpMatrixSdk\Room;
use Psr\Log\LoggerInterface;
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);
}
}