From 940c7bed6e5fcd1fedcb6209e445d09336d48fc2 Mon Sep 17 00:00:00 2001 From: Benedikt Ziemons Date: Sat, 26 Feb 2022 19:40:21 +0100 Subject: [PATCH] Start implementing matrix chat integration Implement auto-login for element-web --- appinfo/routes.php | 5 ++ extra/element-config.json | 40 ++++++++++++++ lib/Controller/TicketApiController.php | 58 ++++++++++++++++---- lib/Db/MatrixUser.php | 9 ++-- lib/Service/MatrixService.php | 10 +++- lib/Service/TicketService.php | 4 +- run.sh | 1 + src/App.vue | 2 +- src/Ticket.vue | 74 +++++++++++++++++++++++++- 9 files changed, 185 insertions(+), 18 deletions(-) create mode 100644 extra/element-config.json diff --git a/appinfo/routes.php b/appinfo/routes.php index 08627b1..318a217 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -12,5 +12,10 @@ return [ 'verb' => 'OPTIONS', 'requirements' => ['path' => '.+'], ], + [ + 'name' => 'ticket_api#fetch_chat', + 'url' => '/api/v1/tickets/{id}/chat', + 'verb' => 'GET', + ], ], ]; diff --git a/extra/element-config.json b/extra/element-config.json new file mode 100644 index 0000000..2d57fd6 --- /dev/null +++ b/extra/element-config.json @@ -0,0 +1,40 @@ +{ + "default_server_config": { + "m.homeserver": { + "base_url": "http://localhost:8008", + "server_name": "synapse" + }, + "m.identity_server": { + } + }, + "disable_custom_urls": false, + "disable_guests": true, + "disable_login_language_selector": false, + "disable_3pid_login": false, + "brand": "Element", + "integrations_ui_url": "", + "integrations_rest_url": "", + "integrations_widgets_urls": [], + "bug_report_endpoint_url": "https://element.io/bugreports/submit", + "uisi_autorageshake_app": "element-auto-uisi", + "defaultCountryCode": "DE", + "showLabsSettings": false, + "features": { }, + "default_federate": true, + "default_theme": "light", + "roomDirectory": { + "servers": [ + ] + }, + "piwik": { + }, + "enable_presence_by_hs_url": { + }, + "settingDefaults": { + "breadcrumbs": true + }, + "jitsi": { + "preferredDomain": "meet.element.io" + }, + "map_style_url": "https://api.maptiler.com/maps/streets/style.json?key=fU3vlMsMn4Jb6dnEIFsx" +} diff --git a/lib/Controller/TicketApiController.php b/lib/Controller/TicketApiController.php index 0f58359..1cf7bc5 100644 --- a/lib/Controller/TicketApiController.php +++ b/lib/Controller/TicketApiController.php @@ -3,25 +3,48 @@ namespace OCA\UPschooling\Controller; use OCA\UPschooling\AppInfo\Application; +use OCA\UPschooling\Db\TicketMapper; +use OCA\UPschooling\Service\MatrixService; use OCA\UPschooling\Service\TicketService; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http\DataResponse; use OCP\IRequest; +use Psr\Log\LoggerInterface; +use Punic\Data; class TicketApiController extends ApiController { + + /** @var LoggerInterface */ + private $logger; + /** @var TicketService */ - private $service; + private $ticketService; + + /** @var MatrixService */ + private $matrixService; + + /** @var TicketMapper */ + private $ticketMapper; /** @var string */ private $userId; use Errors; - public function __construct(IRequest $request, TicketService $service, $userId) - { + public function __construct( + IRequest $request, + LoggerInterface $logger, + TicketService $ticketService, + TicketMapper $ticketMapper, + MatrixService $matrixService, + string $userId + ) { parent::__construct(Application::APP_ID, $request); - $this->service = $service; + $this->logger = $logger; + $this->ticketService = $ticketService; + $this->ticketMapper = $ticketMapper; + $this->matrixService = $matrixService; $this->userId = $userId; } @@ -30,7 +53,7 @@ class TicketApiController extends ApiController */ public function index(): DataResponse { - return new DataResponse($this->service->findAll($this->userId)); + return new DataResponse($this->ticketService->findAll($this->userId)); } /** @@ -39,7 +62,24 @@ class TicketApiController extends ApiController public function show(int $id): DataResponse { return $this->handleNotFound(function () use ($id) { - return $this->service->find($id, $this->userId); + return $this->ticketService->find($id, $this->userId); + }); + } + + /** + * @NoAdminRequired + */ + public function fetchChat(int $id): DataResponse + { + return $this->handleNotFound(function () use ($id) { + $matrixUser = $this->ticketService->getOrCreateUser($this->userId); + $ticket = $this->ticketMapper->findForUser($id, $matrixUser); + $this->logger->debug("fetchChat found matrix data for room " . $ticket->getMatrixRoom()); + return array( + 'matrixRoom' => $ticket->getMatrixRoom(), // FIXME: wrong room, create one for helper and user + 'matrixAccessToken' => $matrixUser->getMatrixToken(), + 'matrixServerUrl' => $this->matrixService->getServerUrl(), + ); }); } @@ -48,7 +88,7 @@ class TicketApiController extends ApiController */ public function create(string $title, string $content): DataResponse { - return new DataResponse($this->service->create($title, $content, $this->userId)); + return new DataResponse($this->ticketService->create($title, $content, $this->userId)); } /** @@ -56,13 +96,13 @@ class TicketApiController extends ApiController */ public function update(int $id): DataResponse { - return new DataResponse($this->service->assign($id, $this->userId)); + return new DataResponse($this->ticketService->assign($id, $this->userId)); } public function destroy(int $id): DataResponse { return $this->handleNotFound(function () use ($id) { - return $this->service->delete($id, $this->userId); + return $this->ticketService->delete($id, $this->userId); }); } } diff --git a/lib/Db/MatrixUser.php b/lib/Db/MatrixUser.php index a9594b8..c189224 100644 --- a/lib/Db/MatrixUser.php +++ b/lib/Db/MatrixUser.php @@ -10,12 +10,13 @@ class MatrixUser extends Entity implements JsonSerializable { protected $matrixUser; protected $matrixToken; - public function jsonSerialize(): array { + public function jsonSerialize(): array + { return [ 'id' => $this->id, - 'userId' => $this->userId, - 'matrixUser' => $this->matrixUser, - 'matrixToken' => $this->matrixToken, + 'userId' => $this->userId, + 'matrixUser' => $this->matrixUser, + 'matrixToken' => $this->matrixToken, ]; } } diff --git a/lib/Service/MatrixService.php b/lib/Service/MatrixService.php index 65f9f4c..02dba1c 100644 --- a/lib/Service/MatrixService.php +++ b/lib/Service/MatrixService.php @@ -56,7 +56,7 @@ class MatrixService ); $this->serverUrl = $this->config->getSystemValueString( "upschooling.matrix_server_url", - "http://synapse:8008" + "http://localhost:8008" ); $this->server = $this->config->getSystemValueString( "upschooling.matrix_server", @@ -216,6 +216,14 @@ class MatrixService return $roomId; } + /** + * @return string the public matrix server url. + */ + public function getServerUrl(): string + { + return $this->serverUrl; + } + private function checkRateLimit() { $fullSuperuserId = "@" . $this->superuser . ":" . $this->server; diff --git a/lib/Service/TicketService.php b/lib/Service/TicketService.php index d1cc014..c7a0ff7 100644 --- a/lib/Service/TicketService.php +++ b/lib/Service/TicketService.php @@ -104,7 +104,7 @@ class TicketService { } /** - * matrixTicketContent has all the syncronized data + * matrixTicketContent has all the synchronized data * @param MatrixTicket $ticket the database object. * @return array a JSON serializable representation of the resolved ticket, for the frontend. */ @@ -144,7 +144,7 @@ class TicketService { * @throws MultipleObjectsReturnedException * @throws \OCP\DB\Exception */ - private function getOrCreateUser(string $userId): MatrixUser + public function getOrCreateUser(string $userId): MatrixUser { try { return $this->userMapper->find($userId); diff --git a/run.sh b/run.sh index b8ca2c9..73e6a0a 100755 --- a/run.sh +++ b/run.sh @@ -139,6 +139,7 @@ $CONTAINER_RUNTIME run -d \ --name=elementweb \ "--network=container:$($CONTAINER_RUNTIME inspect --format "{{.Id}}" nextcloud)" \ -v "$DIR/extra/element-web-nginx.conf:/etc/nginx/conf.d/default.conf" \ + -v "$DIR/extra/element-config.json:/app/config.json" \ --hostname elementweb \ docker.io/vectorim/element-web diff --git a/src/App.vue b/src/App.vue index 2c5dd09..3fa7c8c 100644 --- a/src/App.vue +++ b/src/App.vue @@ -64,7 +64,7 @@ export default { }).catch(console.error) axios.get( 'api/v1/tickets/1', - { headers: { 'Content-Type': 'application/json', Accept: 'application/json' } }, + { headers: { Accept: 'application/json' } }, ).catch(console.error) axios.put( 'api/v1/tickets/1', diff --git a/src/Ticket.vue b/src/Ticket.vue index d939756..c779a85 100644 --- a/src/Ticket.vue +++ b/src/Ticket.vue @@ -18,12 +18,18 @@ {{ t('upschooling', 'Speichern') }}
-
+