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.
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\F7Support\Service;
|
||||
|
||||
use OCP\IConfig;
|
||||
|
||||
class ConfigService {
|
||||
public const APP_ID = 'f7support';
|
||||
|
||||
public function __construct(
|
||||
private IConfig $config,
|
||||
) {
|
||||
}
|
||||
|
||||
public function isPushEnabled(): bool {
|
||||
return $this->config->getAppValue(self::APP_ID, 'push_enabled', 'yes') === 'yes';
|
||||
}
|
||||
|
||||
public function getPushWebhookSecret(): string {
|
||||
return trim($this->config->getAppValue(self::APP_ID, 'push_webhook_secret', ''));
|
||||
}
|
||||
|
||||
public function verifyPushWebhookSecret(?string $provided): bool {
|
||||
if ($provided === null || $provided === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$own = $this->getPushWebhookSecret();
|
||||
if ($own !== '' && hash_equals($own, $provided)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$f7pushSecret = trim($this->config->getAppValue('f7push', 'api_secret', ''));
|
||||
if ($f7pushSecret !== '' && hash_equals($f7pushSecret, $provided)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?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) . '…';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user