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:
parent
0113cdad85
commit
9090d384ef
|
@ -3,11 +3,25 @@
|
||||||
namespace OCA\UPschooling\AppInfo;
|
namespace OCA\UPschooling\AppInfo;
|
||||||
|
|
||||||
use OCP\AppFramework\App;
|
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 const APP_ID = 'upschooling';
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
parent::__construct(self::APP_ID);
|
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?
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,15 +3,20 @@
|
||||||
namespace OCA\UPschooling\Controller;
|
namespace OCA\UPschooling\Controller;
|
||||||
|
|
||||||
use OCA\UPschooling\AppInfo\Application;
|
use OCA\UPschooling\AppInfo\Application;
|
||||||
|
use OCA\UPschooling\Service\MatrixService;
|
||||||
use OCA\UPschooling\Service\NoteService;
|
use OCA\UPschooling\Service\NoteService;
|
||||||
use OCP\AppFramework\Controller;
|
use OCP\AppFramework\Controller;
|
||||||
use OCP\AppFramework\Http\DataResponse;
|
use OCP\AppFramework\Http\DataResponse;
|
||||||
use OCP\IRequest;
|
use OCP\IRequest;
|
||||||
|
|
||||||
class NoteController extends Controller {
|
class NoteController extends Controller {
|
||||||
|
|
||||||
/** @var NoteService */
|
/** @var NoteService */
|
||||||
private $service;
|
private $service;
|
||||||
|
|
||||||
|
/** @var MatrixService */
|
||||||
|
private $matrix;
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
private $userId;
|
private $userId;
|
||||||
|
|
||||||
|
@ -19,9 +24,11 @@ class NoteController extends Controller {
|
||||||
|
|
||||||
public function __construct(IRequest $request,
|
public function __construct(IRequest $request,
|
||||||
NoteService $service,
|
NoteService $service,
|
||||||
|
MatrixService $matrix,
|
||||||
$userId) {
|
$userId) {
|
||||||
parent::__construct(Application::APP_ID, $request);
|
parent::__construct(Application::APP_ID, $request);
|
||||||
$this->service = $service;
|
$this->service = $service;
|
||||||
|
$this->matrix = $matrix;
|
||||||
$this->userId = $userId;
|
$this->userId = $userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
65
lib/Service/MatrixService.php
Normal file
65
lib/Service/MatrixService.php
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue