123 lines
3.6 KiB
PHP
123 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace OCA\NuqloudScrum\Service;
|
|
|
|
use OCP\Http\Client\IClientService;
|
|
use OCP\IURLGenerator;
|
|
|
|
class TalkApiClient {
|
|
public function __construct(
|
|
private IClientService $clientService,
|
|
private IURLGenerator $urlGenerator,
|
|
private ConfigService $configService,
|
|
) {
|
|
}
|
|
|
|
/** @return array<int, array<string, mixed>> */
|
|
public function listConversations(): array {
|
|
$credentials = $this->configService->getAutomationCredentials();
|
|
$username = trim($credentials['user']);
|
|
$appPassword = trim($credentials['password']);
|
|
if ($username === '' || $appPassword === '') {
|
|
throw new \RuntimeException('Talk automation credentials are not configured.');
|
|
}
|
|
|
|
$url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/spreed/api/v4/room?format=json&noStatusUpdate=1');
|
|
$client = $this->clientService->newClient();
|
|
|
|
try {
|
|
$response = $client->get($url, [
|
|
'timeout' => 20,
|
|
'auth' => [$username, $appPassword],
|
|
'headers' => [
|
|
'Accept' => 'application/json',
|
|
'OCS-APIRequest' => 'true',
|
|
],
|
|
]);
|
|
$decoded = json_decode((string)$response->getBody(), true, 512, JSON_THROW_ON_ERROR);
|
|
} catch (\Throwable $e) {
|
|
throw new \RuntimeException('Failed to fetch Talk conversations.', 0, $e);
|
|
}
|
|
|
|
if (!is_array($decoded)) {
|
|
throw new \RuntimeException('Talk conversation response was not valid JSON.');
|
|
}
|
|
|
|
$meta = $decoded['ocs']['meta'] ?? null;
|
|
$data = $decoded['ocs']['data'] ?? null;
|
|
if (!is_array($meta) || strtolower(trim((string)($meta['status'] ?? ''))) !== 'ok') {
|
|
throw new \RuntimeException('Talk conversation response was not successful.');
|
|
}
|
|
if (!is_array($data)) {
|
|
return [];
|
|
}
|
|
|
|
$conversations = [];
|
|
foreach ($data as $entry) {
|
|
if (is_array($entry)) {
|
|
$conversations[] = $entry;
|
|
}
|
|
}
|
|
return $conversations;
|
|
}
|
|
|
|
/** @return array<int, array<string, mixed>> */
|
|
public function listMessages(string $talkToken, int $lastKnownMessageId = 0): array {
|
|
$credentials = $this->configService->getAutomationCredentials();
|
|
$username = trim($credentials['user']);
|
|
$appPassword = trim($credentials['password']);
|
|
if ($username === '' || $appPassword === '') {
|
|
throw new \RuntimeException('Talk automation credentials are not configured.');
|
|
}
|
|
|
|
$query = http_build_query([
|
|
'format' => 'json',
|
|
'lookIntoFuture' => 0,
|
|
'limit' => 200,
|
|
'lastKnownMessageId' => max(0, $lastKnownMessageId),
|
|
'setReadMarker' => 0,
|
|
'noStatusUpdate' => 1,
|
|
'markNotificationsAsRead' => 0,
|
|
], '', '&', PHP_QUERY_RFC3986);
|
|
$url = $this->urlGenerator->getAbsoluteURL('/ocs/v2.php/apps/spreed/api/v1/chat/' . rawurlencode($talkToken) . '?' . $query);
|
|
$client = $this->clientService->newClient();
|
|
|
|
try {
|
|
$response = $client->get($url, [
|
|
'timeout' => 20,
|
|
'auth' => [$username, $appPassword],
|
|
'headers' => [
|
|
'Accept' => 'application/json',
|
|
'OCS-APIRequest' => 'true',
|
|
],
|
|
]);
|
|
$decoded = json_decode((string)$response->getBody(), true, 512, JSON_THROW_ON_ERROR);
|
|
} catch (\Throwable $e) {
|
|
throw new \RuntimeException('Failed to fetch Talk messages.', 0, $e);
|
|
}
|
|
|
|
if (!is_array($decoded)) {
|
|
throw new \RuntimeException('Talk message response was not valid JSON.');
|
|
}
|
|
|
|
$meta = $decoded['ocs']['meta'] ?? null;
|
|
$data = $decoded['ocs']['data'] ?? null;
|
|
if (!is_array($meta) || strtolower(trim((string)($meta['status'] ?? ''))) !== 'ok') {
|
|
throw new \RuntimeException('Talk message response was not successful.');
|
|
}
|
|
if (!is_array($data)) {
|
|
return [];
|
|
}
|
|
|
|
$messages = [];
|
|
foreach ($data as $entry) {
|
|
if (is_array($entry)) {
|
|
$messages[] = $entry;
|
|
}
|
|
}
|
|
return $messages;
|
|
}
|
|
}
|