From 3da5e9f2170336759111e304e586ce42d8bc909e Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 18 Dec 2024 10:34:39 +0100 Subject: [PATCH 1/3] enh: Integrate with ContextChat Signed-off-by: Marcel Klehr --- lib/AppInfo/Application.php | 6 ++ lib/ContextChat/ContentProvider.php | 97 ++++++++++++++++++++++++++ lib/Controller/ZammadAPIController.php | 11 +++ lib/Service/ZammadAPIService.php | 10 +++ 4 files changed, 124 insertions(+) create mode 100644 lib/ContextChat/ContentProvider.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 8b75655..a8077dd 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -34,6 +34,8 @@ class Application extends App implements IBootstrap { public const APP_ID = 'integration_zammad'; private IConfig $config; + public static $contextChatEnabled = false; + public function __construct(array $urlParams = []) { parent::__construct(self::APP_ID, $urlParams); @@ -50,6 +52,10 @@ public function register(IRegistrationContext $context): void { $context->registerReferenceProvider(ZammadReferenceProvider::class); $context->registerEventListener(RenderReferenceEvent::class, ZammadReferenceListener::class); + if (class_exists('\OCA\ContextChat\Public\IContentProvider')) { + self::$contextChatEnabled = true; + $context->registerEventListener(\OCA\ContextChat\Event\ContentProviderRegisterEvent::class, \OCA\Zammad\ContextChat\ContentProvider::class); + } } public function boot(IBootContext $context): void { diff --git a/lib/ContextChat/ContentProvider.php b/lib/ContextChat/ContentProvider.php new file mode 100644 index 0000000..fdeae48 --- /dev/null +++ b/lib/ContextChat/ContentProvider.php @@ -0,0 +1,97 @@ +registerContentProvider(Application::APP_ID, self::ID, self::class); +} + + /** + * The ID of the provider + * + * @return string + * @since 1.1.0 + */ + public function getId(): string { + return self::ID; + } + + /** + * The ID of the app making the provider avaialble + * + * @return string + * @since 1.1.0 + */ + public function getAppId(): string { + return Application::APP_ID; + } + + /** + * The absolute URL to the content item + * + * @param string $id + * @return string + * @since 1.1.0 + */ + public function getItemUrl(string $id): string { + $adminZammadOauthUrl = $this->config->getAppValue(Application::APP_ID, 'oauth_instance_url'); + $zammadUrl = $this->config->getUserValue($this->userId, Application::APP_ID, 'url') ?: $adminZammadOauthUrl; + return $zammadUrl . '/#ticket/zoom/' . $id; + } + + /** + * Starts the initial import of content items into content chat + * + * @return void + * @since 1.1.0 + */ + public function triggerInitialImport(): void { + } + + public function importTicket($id) { + $ticketInfo = $this->zammadAPIService->getTicketInfo($this->userId, (int)$id); + $item = new ContentItem( + (string)$id, + $this->getId(), + $ticketInfo['title'], + $this->getContentOfTicket($id), + 'Ticket', + new \DateTime($ticketInfo['updated_at']), + [$this->userId] + ); + $this->contentManager->updateAccess(Application::APP_ID, self::ID, $id, UpdateAccessOp::ALLOW, [$this->userId]); + $this->contentManager->updateAccessProvider(Application::APP_ID, self::ID, UpdateAccessOp::ALLOW, [$this->userId]); + $this->contentManager->submitContent(Application::APP_ID, [$item]); + } + + public function getContentOfTicket($id): string { + return array_reduce($this->zammadAPIService->getArticlesByTicket($this->userId, (int)$id), fn($agg, array $article) => $agg . $article['from'] . ":\n\n" . $article['body'] . "\n\n", ''); + } + +} \ No newline at end of file diff --git a/lib/Controller/ZammadAPIController.php b/lib/Controller/ZammadAPIController.php index 1e493c7..f36aed3 100644 --- a/lib/Controller/ZammadAPIController.php +++ b/lib/Controller/ZammadAPIController.php @@ -85,6 +85,7 @@ public function getNotifications(?string $since = null): DataResponse { return new DataResponse('', Http::STATUS_BAD_REQUEST); } $result = $this->zammadAPIService->getNotifications($this->userId, $since, 7); + $this->importTicketsToContextChat($result); if (!isset($result['error'])) { $response = new DataResponse($result); } else { @@ -93,4 +94,14 @@ public function getNotifications(?string $since = null): DataResponse { return $response; } + private function importTicketsToContextChat(array $notifications): void { + if (!Application::$contextChatEnabled) { + return; + } + $contentProvider = \OCP\Server::get('OCA\Zammad\ContextChat\ContentProvider'); + foreach($notifications as $notification) { + $contentProvider->importTicket($notification['o_id']); + } + } + } diff --git a/lib/Service/ZammadAPIService.php b/lib/Service/ZammadAPIService.php index 6efebf3..230c5d3 100644 --- a/lib/Service/ZammadAPIService.php +++ b/lib/Service/ZammadAPIService.php @@ -435,6 +435,16 @@ public function getOrganizationInfo(string $userId, int $zammadOrgId): array { return $this->request($userId, 'organizations/' . $zammadOrgId); } + /** + * @param string|null $userId + * @param int $ticketId + * @return array + * @throws Exception + */ + public function getArticlesByTicket(?string $userId, int $ticketId): array { + return $this->request($userId, 'ticket_articles/by_ticket/' . $ticketId); + } + /** * @param string $userId * @param string $endPoint From 59761f2f370b926bac306e645a513f1fca608e01 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 18 Dec 2024 10:44:46 +0100 Subject: [PATCH 2/3] fix: run cs:fix Signed-off-by: Marcel Klehr --- lib/ContextChat/ContentProvider.php | 6 +++--- lib/Controller/ZammadAPIController.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/ContextChat/ContentProvider.php b/lib/ContextChat/ContentProvider.php index fdeae48..4ddb78e 100644 --- a/lib/ContextChat/ContentProvider.php +++ b/lib/ContextChat/ContentProvider.php @@ -30,7 +30,7 @@ public function handle(Event $event): void { return; } $event->registerContentProvider(Application::APP_ID, self::ID, self::class); -} + } /** * The ID of the provider @@ -91,7 +91,7 @@ public function importTicket($id) { } public function getContentOfTicket($id): string { - return array_reduce($this->zammadAPIService->getArticlesByTicket($this->userId, (int)$id), fn($agg, array $article) => $agg . $article['from'] . ":\n\n" . $article['body'] . "\n\n", ''); + return array_reduce($this->zammadAPIService->getArticlesByTicket($this->userId, (int)$id), fn ($agg, array $article) => $agg . $article['from'] . ":\n\n" . $article['body'] . "\n\n", ''); } -} \ No newline at end of file +} diff --git a/lib/Controller/ZammadAPIController.php b/lib/Controller/ZammadAPIController.php index f36aed3..3a43b06 100644 --- a/lib/Controller/ZammadAPIController.php +++ b/lib/Controller/ZammadAPIController.php @@ -99,7 +99,7 @@ private function importTicketsToContextChat(array $notifications): void { return; } $contentProvider = \OCP\Server::get('OCA\Zammad\ContextChat\ContentProvider'); - foreach($notifications as $notification) { + foreach ($notifications as $notification) { $contentProvider->importTicket($notification['o_id']); } } From 774d2bdd48e1cd8ed40de296d1fe287d74130690 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 18 Dec 2024 10:47:39 +0100 Subject: [PATCH 3/3] fix: update baseline Signed-off-by: Marcel Klehr --- tests/psalm-baseline.xml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/psalm-baseline.xml b/tests/psalm-baseline.xml index 5d91a0b..3225a15 100644 --- a/tests/psalm-baseline.xml +++ b/tests/psalm-baseline.xml @@ -1,3 +1,16 @@ + + + + + + + + + + + + +