Files
f7support/lib/Service/PushBridgeService.php
root dc299709f7 Add f7push webhook for support replies and ticket deep links.
OCS endpoint for support.f7cloud.ru, notification notifier, and ?ticket= URL opening in the client UI.
2026-05-24 12:30:02 +03:00

84 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\F7Support\Service;
use OCP\IURLGenerator;
use OCP\Notification\IManager as INotificationManager;
use Psr\Log\LoggerInterface;
class PushBridgeService {
private const PREVIEW_MAX = 240;
public function __construct(
private INotificationManager $notificationManager,
private IURLGenerator $urlGenerator,
private ConfigService $config,
private LoggerInterface $logger,
) {
}
public function notifyNewSupportMessage(
string $userId,
string $ticketNumber,
string $messagePreview,
?string $ticketSubject = null,
): bool {
if (!$this->config->isPushEnabled()) {
return false;
}
$preview = $this->truncate($messagePreview);
if ($preview === '') {
$preview = 'Новый ответ от поддержки';
}
$subject = trim((string)$ticketSubject);
$link = $this->urlGenerator->linkToRouteAbsolute('f7support.page.index')
. '?ticket=' . rawurlencode($ticketNumber);
$notification = $this->notificationManager->createNotification();
$notification
->setApp('f7support')
->setUser($userId)
->setDateTime(new \DateTime())
->setObject('ticket', $ticketNumber)
->setSubject('support_reply', [
'ticketNumber' => $ticketNumber,
'ticketSubject' => $subject,
'preview' => $preview,
])
->setMessage('support_reply', [
'preview' => $preview,
])
->setLink($link);
try {
$this->notificationManager->notify($notification);
$this->logger->debug('f7support: push notification queued', [
'app' => 'f7support',
'user' => $userId,
'ticket' => $ticketNumber,
]);
return true;
} catch (\Throwable $e) {
$this->logger->warning('f7support: failed to queue push notification', [
'app' => 'f7support',
'user' => $userId,
'ticket' => $ticketNumber,
'exception' => $e,
]);
return false;
}
}
private function truncate(string $text): string {
$text = trim(preg_replace('/\s+/u', ' ', $text) ?? $text);
if ($text === '' || mb_strlen($text) <= self::PREVIEW_MAX) {
return $text;
}
return mb_substr($text, 0, self::PREVIEW_MAX - 1) . '…';
}
}