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+ }
0 commit comments