Skip to content

Commit 179d594

Browse files
committed
enh: Integrate with ContextChat
1 parent 9aa6409 commit 179d594

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

lib/AppInfo/Application.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class Application extends App implements IBootstrap {
3434
public const APP_ID = 'integration_zammad';
3535
private IConfig $config;
3636

37+
public static $contextChatEnabled = false;
38+
3739
public function __construct(array $urlParams = []) {
3840
parent::__construct(self::APP_ID, $urlParams);
3941

@@ -50,6 +52,10 @@ public function register(IRegistrationContext $context): void {
5052

5153
$context->registerReferenceProvider(ZammadReferenceProvider::class);
5254
$context->registerEventListener(RenderReferenceEvent::class, ZammadReferenceListener::class);
55+
if (class_exists('\OCA\ContextChat\Public\IContentProvider')) {
56+
self::$contextChatEnabled = true;
57+
$context->registerEventListener(\OCA\ContextChat\Event\ContentProviderRegisterEvent::class, \OCA\Zammad\ContextChat\ContentProvider::class);
58+
}
5359
}
5460

5561
public function boot(IBootContext $context): void {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace OCA\Zammad\ContextChat;
4+
5+
use OCA\ContextChat\Event\ContentProviderRegisterEvent;
6+
use OCA\ContextChat\Public\ContentItem;
7+
use OCA\ContextChat\Public\ContentManager;
8+
use OCA\ContextChat\Public\IContentProvider;
9+
use OCA\ContextChat\Public\UpdateAccessOp;
10+
use OCA\Zammad\AppInfo\Application;
11+
use OCA\Zammad\Service\ZammadAPIService;
12+
use OCP\EventDispatcher\Event;
13+
use OCP\IConfig;
14+
15+
class ContentProvider implements IContentProvider {
16+
17+
public function __construct(
18+
private IConfig $config,
19+
private ZammadAPIService $zammadAPIService,
20+
private ?string $userId,
21+
private ContentManager $contentManager,
22+
) {
23+
24+
}
25+
26+
public const ID = 'integration_zammad:tickets';
27+
28+
public function handle(Event $event): void {
29+
if (!$event instanceof ContentProviderRegisterEvent) {
30+
return;
31+
}
32+
$event->registerContentProvider(Application::APP_ID, self::ID, self::class);
33+
}
34+
35+
/**
36+
* The ID of the provider
37+
*
38+
* @return string
39+
* @since 1.1.0
40+
*/
41+
public function getId(): string {
42+
return self::ID;
43+
}
44+
45+
/**
46+
* The ID of the app making the provider avaialble
47+
*
48+
* @return string
49+
* @since 1.1.0
50+
*/
51+
public function getAppId(): string {
52+
return Application::APP_ID;
53+
}
54+
55+
/**
56+
* The absolute URL to the content item
57+
*
58+
* @param string $id
59+
* @return string
60+
* @since 1.1.0
61+
*/
62+
public function getItemUrl(string $id): string {
63+
$adminZammadOauthUrl = $this->config->getAppValue(Application::APP_ID, 'oauth_instance_url');
64+
$zammadUrl = $this->config->getUserValue($this->userId, Application::APP_ID, 'url') ?: $adminZammadOauthUrl;
65+
return $zammadUrl . '/#ticket/zoom/' . $id;
66+
}
67+
68+
/**
69+
* Starts the initial import of content items into content chat
70+
*
71+
* @return void
72+
* @since 1.1.0
73+
*/
74+
public function triggerInitialImport(): void {
75+
}
76+
77+
public function importTicket($id) {
78+
$ticketInfo = $this->zammadAPIService->getTicketInfo($this->userId, (int)$id);
79+
$item = new ContentItem(
80+
(string)$id,
81+
$this->getId(),
82+
$ticketInfo['title'],
83+
$this->getContentOfTicket($id),
84+
'Ticket',
85+
new \DateTime($ticketInfo['updated_at']),
86+
[$this->userId]
87+
);
88+
$this->contentManager->updateAccess(Application::APP_ID, self::ID, $id, UpdateAccessOp::ALLOW, [$this->userId]);
89+
$this->contentManager->updateAccessProvider(Application::APP_ID, self::ID, UpdateAccessOp::ALLOW, [$this->userId]);
90+
$this->contentManager->submitContent(Application::APP_ID, [$item]);
91+
}
92+
93+
public function getContentOfTicket($id): string {
94+
return array_reduce($this->zammadAPIService->getArticlesByTicket($this->userId, (int)$id), fn($agg, array $article) => $agg . $article['from'] . ":\n\n" . $article['body'] . "\n\n", '');
95+
}
96+
97+
}

lib/Controller/ZammadAPIController.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function getNotifications(?string $since = null): DataResponse {
8585
return new DataResponse('', Http::STATUS_BAD_REQUEST);
8686
}
8787
$result = $this->zammadAPIService->getNotifications($this->userId, $since, 7);
88+
$this->importTicketsToContextChat($result);
8889
if (!isset($result['error'])) {
8990
$response = new DataResponse($result);
9091
} else {
@@ -93,4 +94,14 @@ public function getNotifications(?string $since = null): DataResponse {
9394
return $response;
9495
}
9596

97+
private function importTicketsToContextChat(array $notifications): void {
98+
if (!Application::$contextChatEnabled) {
99+
return;
100+
}
101+
$contentProvider = \OCP\Server::get('OCA\Zammad\ContextChat\ContentProvider');
102+
foreach($notifications as $notification) {
103+
$contentProvider->importTicket($notification['o_id']);
104+
}
105+
}
106+
96107
}

lib/Service/ZammadAPIService.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,16 @@ public function getOrganizationInfo(string $userId, int $zammadOrgId): array {
435435
return $this->request($userId, 'organizations/' . $zammadOrgId);
436436
}
437437

438+
/**
439+
* @param string|null $userId
440+
* @param int $ticketId
441+
* @return array
442+
* @throws Exception
443+
*/
444+
public function getArticlesByTicket(?string $userId, int $ticketId): array {
445+
return $this->request($userId, 'ticket_articles/by_ticket/' . $ticketId);
446+
}
447+
438448
/**
439449
* @param string $userId
440450
* @param string $endPoint

0 commit comments

Comments
 (0)