Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}

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 {

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
13 changes: 13 additions & 0 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
<file src="lib/AppInfo/Application.php">
<MissingDependency>
<code><![CDATA[\OCA\Zammad\ContextChat\ContentProvider]]></code>
</MissingDependency>
<UndefinedClass>
<code><![CDATA[\OCA\ContextChat\Event\ContentProviderRegisterEvent]]></code>
</UndefinedClass>
</file>
<file src="lib/ContextChat/ContentProvider.php">
<UndefinedClass>
<code><![CDATA[IContentProvider]]></code>
</UndefinedClass>
</file>
</files>
Loading