Ben
9090d384ef
Does not work, because of a GuzzleHttp version clash in nextcloud/3rdparty and php-matrix-sdk.
66 lines
1.9 KiB
PHP
66 lines
1.9 KiB
PHP
<?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);
|
|
}
|
|
|
|
}
|