Skip to content

Commit

Permalink
enh: Integrate with ContextChat
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelklehr committed Dec 18, 2024
1 parent 9aa6409 commit 179d594
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Check failure on line 57 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedClass

lib/AppInfo/Application.php:57:36: UndefinedClass: Class, interface or enum named OCA\ContextChat\Event\ContentProviderRegisterEvent does not exist (see https://psalm.dev/019)

Check failure on line 57 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

MissingDependency

lib/AppInfo/Application.php:57:96: MissingDependency: OCA\Zammad\ContextChat\ContentProvider depends on class or interface oca\contextchat\public\icontentprovider that does not exist (see https://psalm.dev/157)
}
}

public function boot(IBootContext $context): void {
Expand Down
97 changes: 97 additions & 0 deletions lib/ContextChat/ContentProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace OCA\Zammad\ContextChat;

use OCA\ContextChat\Event\ContentProviderRegisterEvent;
use OCA\ContextChat\Public\ContentItem;
use OCA\ContextChat\Public\ContentManager;
use OCA\ContextChat\Public\IContentProvider;
use OCA\ContextChat\Public\UpdateAccessOp;
use OCA\Zammad\AppInfo\Application;
use OCA\Zammad\Service\ZammadAPIService;
use OCP\EventDispatcher\Event;
use OCP\IConfig;

class ContentProvider implements IContentProvider {

Check failure on line 15 in lib/ContextChat/ContentProvider.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

UndefinedClass

lib/ContextChat/ContentProvider.php:15:34: UndefinedClass: Class, interface or enum named OCA\ContextChat\Public\IContentProvider does not exist (see https://psalm.dev/019)

public function __construct(
private IConfig $config,
private ZammadAPIService $zammadAPIService,
private ?string $userId,
private ContentManager $contentManager,
) {

}

public const ID = 'integration_zammad:tickets';

public function handle(Event $event): void {
if (!$event instanceof ContentProviderRegisterEvent) {
return;
}
$event->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", '');
}

}
11 changes: 11 additions & 0 deletions lib/Controller/ZammadAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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']);
}
}

}
10 changes: 10 additions & 0 deletions lib/Service/ZammadAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 179d594

Please sign in to comment.