|
|
|
|
@ -209,6 +209,86 @@ namespace App\Services\Cloud {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace App\Services\Ticket {
|
|
|
|
|
final readonly class ReplyTicketCommand
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
|
|
|
|
public int $ticketId,
|
|
|
|
|
public int $actorId,
|
|
|
|
|
public string $content,
|
|
|
|
|
public int $status,
|
|
|
|
|
public mixed $request,
|
|
|
|
|
public ?int $ownerId = null,
|
|
|
|
|
public bool $actorIsAdmin = false,
|
|
|
|
|
public string $requestKey = '',
|
|
|
|
|
public ?int $requiredCurrentStatus = null
|
|
|
|
|
) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final class TicketReplyStateConflict extends \RuntimeException
|
|
|
|
|
{
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
parent::__construct('工单已关闭,无法通过 Telegram 回复', 409);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final class TicketIdempotencyConflict extends \RuntimeException {}
|
|
|
|
|
|
|
|
|
|
final class TicketThreadService
|
|
|
|
|
{
|
|
|
|
|
public static array $commands = [];
|
|
|
|
|
public static array $results = [];
|
|
|
|
|
public static bool $closed = false;
|
|
|
|
|
public static bool $fail = false;
|
|
|
|
|
|
|
|
|
|
public function reply(ReplyTicketCommand $command): ?TicketWriteResult
|
|
|
|
|
{
|
|
|
|
|
if (self::$fail) {
|
|
|
|
|
throw new \RuntimeException('synthetic ticket database failure');
|
|
|
|
|
}
|
|
|
|
|
if (self::$closed) {
|
|
|
|
|
throw new TicketReplyStateConflict();
|
|
|
|
|
}
|
|
|
|
|
self::$commands[] = $command;
|
|
|
|
|
if (isset(self::$results[$command->requestKey])) {
|
|
|
|
|
$previous = self::$results[$command->requestKey];
|
|
|
|
|
return new TicketWriteResult(
|
|
|
|
|
$previous->root,
|
|
|
|
|
$previous->message,
|
|
|
|
|
$previous->event,
|
|
|
|
|
$previous->attachments,
|
|
|
|
|
false
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$root = new \App\Models\Ticket();
|
|
|
|
|
$root->id = $command->ticketId;
|
|
|
|
|
$root->userid = 7;
|
|
|
|
|
$root->title = 'Example';
|
|
|
|
|
$root->rootid = 0;
|
|
|
|
|
$root->status = 1;
|
|
|
|
|
$message = clone $root;
|
|
|
|
|
$message->id = 900 + count(self::$results);
|
|
|
|
|
$message->rootid = $root->id;
|
|
|
|
|
$message->content = $command->content;
|
|
|
|
|
$result = new TicketWriteResult(
|
|
|
|
|
$root, $message, '管理员回复工单', [], true
|
|
|
|
|
);
|
|
|
|
|
self::$results[$command->requestKey] = $result;
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final class TicketNotificationDispatcher
|
|
|
|
|
{
|
|
|
|
|
public static array $events = [];
|
|
|
|
|
public function dispatch(TicketEvent $event): void
|
|
|
|
|
{
|
|
|
|
|
self::$events[] = $event;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers {
|
|
|
|
|
class BaseController {}
|
|
|
|
|
class AdminController extends BaseController {}
|
|
|
|
|
@ -246,6 +326,8 @@ namespace {
|
|
|
|
|
use App\Services\TicketTelegramCredentialCipher;
|
|
|
|
|
use App\Services\TicketTelegramNotifier;
|
|
|
|
|
use App\Services\Ticket\TicketEvent;
|
|
|
|
|
use App\Services\Ticket\TicketNotificationDispatcher;
|
|
|
|
|
use App\Services\Ticket\TicketThreadService;
|
|
|
|
|
use App\Services\Ticket\TicketWriteResult;
|
|
|
|
|
use App\Controllers\Admin\TelegramController;
|
|
|
|
|
use App\Controllers\TelegramWebhookController;
|
|
|
|
|
@ -389,6 +471,23 @@ namespace {
|
|
|
|
|
$result = new TicketWriteResult($root, $message, '新工单', [new \stdClass(), new \stdClass()]);
|
|
|
|
|
return $kind === 'created' ? TicketEvent::created($result) : TicketEvent::userReply($result);
|
|
|
|
|
};
|
|
|
|
|
$ticketReply = function (
|
|
|
|
|
int $updateId,
|
|
|
|
|
string $text = 'Telegram administrator reply',
|
|
|
|
|
string $chatId = '123456',
|
|
|
|
|
int $replyToMessageId = 1000
|
|
|
|
|
): array {
|
|
|
|
|
return [
|
|
|
|
|
'update_id' => $updateId,
|
|
|
|
|
'message' => [
|
|
|
|
|
'message_id' => 2000 + $updateId,
|
|
|
|
|
'chat' => ['id' => $chatId, 'type' => 'private'],
|
|
|
|
|
'from' => ['id' => $chatId, 'is_bot' => false],
|
|
|
|
|
'text' => $text,
|
|
|
|
|
'reply_to_message' => ['message_id' => $replyToMessageId],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
$System_Config = ['enable_telegram' => true, 'telegram_token' => '123456789:' . str_repeat('A', 35),
|
|
|
|
|
'telegram_request_token' => 'synthetic-webhook-secret-123', 'super_admin' => '1,2',
|
|
|
|
|
'baseUrl' => 'https://example.invalid'];
|
|
|
|
|
@ -538,6 +637,75 @@ namespace {
|
|
|
|
|
&& !TicketTelegramNotifier::notify($ticketEvent('user_reply', 43))
|
|
|
|
|
&& count(BotApi::$calls) === $callsAfterUnknown,
|
|
|
|
|
'An uncertain Telegram delivery was not fenced against replay.');
|
|
|
|
|
|
|
|
|
|
TicketThreadService::$commands = [];
|
|
|
|
|
TicketThreadService::$results = [];
|
|
|
|
|
TicketThreadService::$closed = false;
|
|
|
|
|
TicketThreadService::$fail = false;
|
|
|
|
|
TicketNotificationDispatcher::$events = [];
|
|
|
|
|
$reply = $ticketReply(501);
|
|
|
|
|
$assert(TicketTelegramNotifier::handleUpdate($reply),
|
|
|
|
|
'A reply to a mapped Telegram ticket notification was rejected.');
|
|
|
|
|
$replyCommand = TicketThreadService::$commands[0] ?? null;
|
|
|
|
|
$replyReceipt = BotApi::$calls[count(BotApi::$calls) - 1] ?? null;
|
|
|
|
|
$assert($replyCommand !== null
|
|
|
|
|
&& $replyCommand->ticketId === 42
|
|
|
|
|
&& $replyCommand->actorId === 1
|
|
|
|
|
&& $replyCommand->actorIsAdmin
|
|
|
|
|
&& $replyCommand->requiredCurrentStatus === 1
|
|
|
|
|
&& $replyCommand->requestKey === 'telegram.update.501'
|
|
|
|
|
&& count(TicketNotificationDispatcher::$events) === 1
|
|
|
|
|
&& $replyReceipt[0] === 'sendMessage'
|
|
|
|
|
&& str_contains($replyReceipt[1]['text'], '已回复工单 #42'),
|
|
|
|
|
'Mapped Telegram reply did not reuse the guarded ticket workflow.');
|
|
|
|
|
$receiptCount = count(BotApi::$calls);
|
|
|
|
|
$dispatchCount = count(TicketNotificationDispatcher::$events);
|
|
|
|
|
$assert(TicketTelegramNotifier::handleUpdate($reply)
|
|
|
|
|
&& count(TicketNotificationDispatcher::$events) === $dispatchCount
|
|
|
|
|
&& count(BotApi::$calls) === $receiptCount,
|
|
|
|
|
'A replayed Telegram update duplicated the reply notification or receipt.');
|
|
|
|
|
|
|
|
|
|
$groupReply = $ticketReply(502);
|
|
|
|
|
$groupReply['message']['chat']['type'] = 'group';
|
|
|
|
|
$forwardedReply = $ticketReply(503);
|
|
|
|
|
$forwardedReply['message']['forward_origin'] = ['type' => 'user'];
|
|
|
|
|
$wrongChatReply = $ticketReply(504, 'wrong chat', '654321');
|
|
|
|
|
foreach ([$groupReply, $forwardedReply, $wrongChatReply] as $unsafeReply) {
|
|
|
|
|
$assert(!TicketTelegramNotifier::handleUpdate($unsafeReply),
|
|
|
|
|
'A group, forwarded or wrong-chat Telegram reply reached the ticket workflow.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TicketThreadService::$closed = true;
|
|
|
|
|
$closedReply = $ticketReply(505);
|
|
|
|
|
$assert(TicketTelegramNotifier::handleUpdate($closedReply),
|
|
|
|
|
'A closed-ticket Telegram reply was not handled terminally.');
|
|
|
|
|
$closedReceipt = BotApi::$calls[count(BotApi::$calls) - 1] ?? null;
|
|
|
|
|
$assert($closedReceipt[0] === 'sendMessage'
|
|
|
|
|
&& str_contains($closedReceipt[1]['text'], '工单已关闭'),
|
|
|
|
|
'Closed-ticket Telegram reply did not receive a safe explanation.');
|
|
|
|
|
TicketThreadService::$closed = false;
|
|
|
|
|
|
|
|
|
|
BotApi::$fail = true;
|
|
|
|
|
$dispatchBeforeFailedReceipt = count(TicketNotificationDispatcher::$events);
|
|
|
|
|
$assert(TicketTelegramNotifier::handleUpdate($ticketReply(506)),
|
|
|
|
|
'A failed post-commit Telegram receipt changed a saved ticket reply into failure.');
|
|
|
|
|
BotApi::$fail = false;
|
|
|
|
|
$assert(count(TicketNotificationDispatcher::$events)
|
|
|
|
|
=== $dispatchBeforeFailedReceipt + 1,
|
|
|
|
|
'A failed Telegram receipt rolled back or suppressed the committed ticket reply.');
|
|
|
|
|
|
|
|
|
|
TicketThreadService::$fail = true;
|
|
|
|
|
$webhookForReplyFailure = new TelegramWebhookController();
|
|
|
|
|
$replyFailureRequest = new TelegramTestRequest(
|
|
|
|
|
TicketTelegramConfig::runtime(true)['webhook_secret'],
|
|
|
|
|
new TelegramTestStream((string) json_encode($ticketReply(507)))
|
|
|
|
|
);
|
|
|
|
|
$replyFailureResponse = $webhookForReplyFailure->handle(
|
|
|
|
|
$replyFailureRequest, new TelegramTestResponse(), []
|
|
|
|
|
);
|
|
|
|
|
TicketThreadService::$fail = false;
|
|
|
|
|
$assert($replyFailureResponse->status === 503,
|
|
|
|
|
'A pre-commit Telegram ticket database failure was acknowledged as success.');
|
|
|
|
|
$calls = count(BotApi::$calls);
|
|
|
|
|
foreach ([['bound_admin_id' => 1, 'chat_id' => '-100123456'],
|
|
|
|
|
['bound_admin_id' => 2, 'chat_id' => '123456'],
|
|
|
|
|
|