From 179d594b131e461e87ea2129ac6e3adbcbf7ef8f Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 18 Dec 2024 10:34:39 +0100 Subject: [PATCH] enh: Integrate with ContextChat --- 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