|
|
|
|
@ -327,6 +327,7 @@ namespace {
|
|
|
|
|
use App\Services\TicketTelegramNotifier;
|
|
|
|
|
use App\Services\Ticket\TicketEvent;
|
|
|
|
|
use App\Services\Ticket\TicketNotificationDispatcher;
|
|
|
|
|
use App\Services\Ticket\TicketTelegramNotificationResult;
|
|
|
|
|
use App\Services\Ticket\TicketThreadService;
|
|
|
|
|
use App\Services\Ticket\TicketWriteResult;
|
|
|
|
|
use App\Controllers\Admin\TelegramController;
|
|
|
|
|
@ -377,7 +378,11 @@ namespace {
|
|
|
|
|
Configurations::$locks++;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
public function first() { return Configurations::$rows[$this->name] ?? null; }
|
|
|
|
|
public function first()
|
|
|
|
|
{
|
|
|
|
|
$row = Configurations::$rows[$this->name] ?? null;
|
|
|
|
|
return $row === null ? null : clone $row;
|
|
|
|
|
}
|
|
|
|
|
public function firstOrFail() { return $this->first() ?? throw new RuntimeException('Missing fixture.'); }
|
|
|
|
|
public function delete() { unset(Configurations::$rows[$this->name]); }
|
|
|
|
|
}
|
|
|
|
|
@ -427,6 +432,7 @@ namespace {
|
|
|
|
|
require dirname(__DIR__) . '/app/Services/TicketTelegramConfig.php';
|
|
|
|
|
require dirname(__DIR__) . '/app/Services/Ticket/TicketWriteResult.php';
|
|
|
|
|
require dirname(__DIR__) . '/app/Services/Ticket/TicketEvent.php';
|
|
|
|
|
require dirname(__DIR__) . '/app/Services/Ticket/TicketTelegramNotificationResult.php';
|
|
|
|
|
require dirname(__DIR__) . '/app/Services/TicketTelegramNotifier.php';
|
|
|
|
|
require dirname(__DIR__) . '/app/Controllers/Admin/TelegramController.php';
|
|
|
|
|
require dirname(__DIR__) . '/app/Controllers/TelegramWebhookController.php';
|
|
|
|
|
@ -537,6 +543,13 @@ namespace {
|
|
|
|
|
$assert($state()['pending_hash'] === hash('sha256', $binding['code'])
|
|
|
|
|
&& !str_contains(Configurations::$rows['ticket_telegram_binding']->value, $binding['code']),
|
|
|
|
|
'Plain binding code is persisted.');
|
|
|
|
|
$pendingState = $state();
|
|
|
|
|
Configurations::$failWrites = true;
|
|
|
|
|
$expectRejected(fn () => TicketTelegramNotifier::createBinding(1),
|
|
|
|
|
'A failed binding-code state write still returned an unusable code.');
|
|
|
|
|
Configurations::$failWrites = false;
|
|
|
|
|
$assert($state() === $pendingState,
|
|
|
|
|
'A failed binding-code state write changed the active pending code.');
|
|
|
|
|
foreach (['group', 'supergroup', 'channel'] as $type) {
|
|
|
|
|
$group = $update($binding['code']);
|
|
|
|
|
$group['message']['chat']['type'] = $type;
|
|
|
|
|
@ -602,7 +615,9 @@ namespace {
|
|
|
|
|
TicketTelegramDelivery::$rows = [];
|
|
|
|
|
TicketTelegramDelivery::$nextId = 1;
|
|
|
|
|
$setState(['bound_admin_id' => 1, 'chat_id' => '123456']);
|
|
|
|
|
$assert(TicketTelegramNotifier::notify($ticketEvent()), 'Eligible administrator was not notified.');
|
|
|
|
|
$assert(TicketTelegramNotifier::notify($ticketEvent())
|
|
|
|
|
=== TicketTelegramNotificationResult::Sent,
|
|
|
|
|
'Eligible administrator was not notified.');
|
|
|
|
|
$alertText = BotApi::$calls[0][1]['text'];
|
|
|
|
|
$assert(BotApi::$calls[0][1]['chat_id'] === '123456'
|
|
|
|
|
&& str_starts_with($alertText, '🆕 新工单 #42')
|
|
|
|
|
@ -623,18 +638,21 @@ namespace {
|
|
|
|
|
&& preg_match('/^[a-f0-9]{32}$/D', $delivery->binding_generation) === 1,
|
|
|
|
|
'Successful Telegram notification did not persist a reply mapping.');
|
|
|
|
|
$assert(TicketTelegramNotifier::notify($ticketEvent())
|
|
|
|
|
=== TicketTelegramNotificationResult::Sent
|
|
|
|
|
&& count(BotApi::$calls) === 1
|
|
|
|
|
&& count(TicketTelegramDelivery::$rows) === 1,
|
|
|
|
|
'A repeated ticket event was sent or persisted more than once.');
|
|
|
|
|
BotApi::$fail = true;
|
|
|
|
|
$assert(!TicketTelegramNotifier::notify($ticketEvent('user_reply', 43)),
|
|
|
|
|
$assert(TicketTelegramNotifier::notify($ticketEvent('user_reply', 43))
|
|
|
|
|
=== TicketTelegramNotificationResult::Failed,
|
|
|
|
|
'An uncertain Telegram send was reported as successful.');
|
|
|
|
|
BotApi::$fail = false;
|
|
|
|
|
$uncertain = TicketTelegramDelivery::$rows[2] ?? null;
|
|
|
|
|
$callsAfterUnknown = count(BotApi::$calls);
|
|
|
|
|
$assert($uncertain !== null && $uncertain->state === 'unknown'
|
|
|
|
|
&& $uncertain->telegram_message_id === null
|
|
|
|
|
&& !TicketTelegramNotifier::notify($ticketEvent('user_reply', 43))
|
|
|
|
|
&& TicketTelegramNotifier::notify($ticketEvent('user_reply', 43))
|
|
|
|
|
=== TicketTelegramNotificationResult::Failed
|
|
|
|
|
&& count(BotApi::$calls) === $callsAfterUnknown,
|
|
|
|
|
'An uncertain Telegram delivery was not fenced against replay.');
|
|
|
|
|
|
|
|
|
|
@ -653,7 +671,8 @@ namespace {
|
|
|
|
|
&& $replyCommand->actorId === 1
|
|
|
|
|
&& $replyCommand->actorIsAdmin
|
|
|
|
|
&& $replyCommand->requiredCurrentStatus === 1
|
|
|
|
|
&& $replyCommand->requestKey === 'telegram.update.501'
|
|
|
|
|
&& $replyCommand->requestKey === 'telegram.' . $delivery->binding_generation
|
|
|
|
|
. '.update.501'
|
|
|
|
|
&& count(TicketNotificationDispatcher::$events) === 1
|
|
|
|
|
&& $replyReceipt[0] === 'sendMessage'
|
|
|
|
|
&& str_contains($replyReceipt[1]['text'], '已回复工单 #42'),
|
|
|
|
|
@ -706,22 +725,126 @@ namespace {
|
|
|
|
|
TicketThreadService::$fail = false;
|
|
|
|
|
$assert($replyFailureResponse->status === 503,
|
|
|
|
|
'A pre-commit Telegram ticket database failure was acknowledged as success.');
|
|
|
|
|
|
|
|
|
|
$originalBindingState = $state();
|
|
|
|
|
$newGeneration = bin2hex(random_bytes(16));
|
|
|
|
|
$setState([
|
|
|
|
|
'bound_admin_id' => 1,
|
|
|
|
|
'chat_id' => '123456',
|
|
|
|
|
'binding_generation' => $newGeneration,
|
|
|
|
|
]);
|
|
|
|
|
$newBindingDelivery = new TicketTelegramDelivery();
|
|
|
|
|
$newBindingDelivery->binding_generation = $newGeneration;
|
|
|
|
|
$newBindingDelivery->bound_admin_id = 1;
|
|
|
|
|
$newBindingDelivery->chat_id = '123456';
|
|
|
|
|
$newBindingDelivery->ticket_id = 42;
|
|
|
|
|
$newBindingDelivery->ticket_message_id = 99;
|
|
|
|
|
$newBindingDelivery->event_kind = 'user_reply';
|
|
|
|
|
$newBindingDelivery->telegram_message_id = 1100;
|
|
|
|
|
$newBindingDelivery->state = 'sent';
|
|
|
|
|
$newBindingDelivery->content_hash = str_repeat('a', 64);
|
|
|
|
|
$newBindingDelivery->created_at = time();
|
|
|
|
|
$newBindingDelivery->sent_at = time();
|
|
|
|
|
$newBindingDelivery->updated_at = time();
|
|
|
|
|
$newBindingDelivery->save();
|
|
|
|
|
$commandsBeforeRebind = count(TicketThreadService::$commands);
|
|
|
|
|
$assert(TicketTelegramNotifier::handleUpdate($ticketReply(508, 'old binding', '123456', 1000))
|
|
|
|
|
&& count(TicketThreadService::$commands) === $commandsBeforeRebind,
|
|
|
|
|
'An old binding generation still authorized its Telegram notification.');
|
|
|
|
|
$assert(TicketTelegramNotifier::handleUpdate($ticketReply(501, 'new binding', '123456', 1100)),
|
|
|
|
|
'A new binding could not reuse a Telegram update identifier.');
|
|
|
|
|
$newBindingCommand = TicketThreadService::$commands[count(TicketThreadService::$commands) - 1] ?? null;
|
|
|
|
|
$assert($newBindingCommand !== null
|
|
|
|
|
&& $newBindingCommand->requestKey === 'telegram.' . $newGeneration . '.update.501',
|
|
|
|
|
'Telegram idempotency did not include the active binding generation.');
|
|
|
|
|
$setState($originalBindingState);
|
|
|
|
|
|
|
|
|
|
$commandsBeforeUnsafeReplies = count(TicketThreadService::$commands);
|
|
|
|
|
$unknownReply = $ticketReply(509, 'unknown notification', '123456', 999999);
|
|
|
|
|
$viaBotReply = $ticketReply(510);
|
|
|
|
|
$viaBotReply['message']['via_bot'] = ['id' => 99];
|
|
|
|
|
$mediaReply = $ticketReply(511);
|
|
|
|
|
$mediaReply['message']['photo'] = [['file_id' => 'not-accepted']];
|
|
|
|
|
$overflowReply = $ticketReply(512);
|
|
|
|
|
$overflowReply['update_id'] = (string) PHP_INT_MAX . '0';
|
|
|
|
|
$assert(TicketTelegramNotifier::handleUpdate($unknownReply)
|
|
|
|
|
&& !TicketTelegramNotifier::handleUpdate($viaBotReply)
|
|
|
|
|
&& !TicketTelegramNotifier::handleUpdate($mediaReply)
|
|
|
|
|
&& !TicketTelegramNotifier::handleUpdate($overflowReply)
|
|
|
|
|
&& count(TicketThreadService::$commands) === $commandsBeforeUnsafeReplies,
|
|
|
|
|
'Unknown, via-bot, media or overflowing Telegram updates reached the ticket workflow.');
|
|
|
|
|
User::$users[1]->is_admin = 0;
|
|
|
|
|
$assert(!TicketTelegramNotifier::handleUpdate($ticketReply(513)),
|
|
|
|
|
'A downgraded administrator replied through Telegram.');
|
|
|
|
|
User::$users[1]->is_admin = 1;
|
|
|
|
|
|
|
|
|
|
$fallbackEvent = $ticketEvent('user_reply', 44);
|
|
|
|
|
$fallbackEvent->result->root->userid = 999;
|
|
|
|
|
$fallbackEvent->result->root->instance_id = 999;
|
|
|
|
|
$fallbackEvent->result->message->content = str_repeat('测', 5000);
|
|
|
|
|
$assert(TicketTelegramNotifier::notify($fallbackEvent)
|
|
|
|
|
=== TicketTelegramNotificationResult::Sent,
|
|
|
|
|
'A missing user/instance projection suppressed the Telegram notification.');
|
|
|
|
|
$fallbackText = BotApi::$calls[count(BotApi::$calls) - 1][1]['text'] ?? '';
|
|
|
|
|
$assert(str_contains($fallbackText, '用户:UID 999')
|
|
|
|
|
&& !str_contains($fallbackText, '关联实例:')
|
|
|
|
|
&& mb_strlen($fallbackText, 'UTF-8') <= 4096,
|
|
|
|
|
'Telegram notification fallback or length limit is invalid.');
|
|
|
|
|
|
|
|
|
|
$closedEvent = $ticketEvent('user_reply', 45);
|
|
|
|
|
$closedEvent->result->root->status = 0;
|
|
|
|
|
$assert(TicketTelegramNotifier::notify($closedEvent)
|
|
|
|
|
=== TicketTelegramNotificationResult::Sent,
|
|
|
|
|
'Closed ticket update notification was not sent.');
|
|
|
|
|
$closedNotification = BotApi::$calls[count(BotApi::$calls) - 1][1]['text'] ?? '';
|
|
|
|
|
$assert(str_contains($closedNotification, '工单已关闭,请在管理后台查看')
|
|
|
|
|
&& !str_contains($closedNotification, '直接回复本消息'),
|
|
|
|
|
'Closed ticket notification still advertises direct Telegram replies.');
|
|
|
|
|
|
|
|
|
|
$stale = new TicketTelegramDelivery();
|
|
|
|
|
$stale->binding_generation = $originalBindingState['binding_generation'];
|
|
|
|
|
$stale->bound_admin_id = 1;
|
|
|
|
|
$stale->chat_id = '123456';
|
|
|
|
|
$stale->ticket_id = 42;
|
|
|
|
|
$stale->ticket_message_id = 46;
|
|
|
|
|
$stale->event_kind = 'user_reply';
|
|
|
|
|
$stale->telegram_message_id = null;
|
|
|
|
|
$stale->state = 'dispatching';
|
|
|
|
|
$stale->content_hash = str_repeat('b', 64);
|
|
|
|
|
$stale->created_at = time() - 60;
|
|
|
|
|
$stale->sent_at = null;
|
|
|
|
|
$stale->updated_at = time() - 60;
|
|
|
|
|
$stale->save();
|
|
|
|
|
$callsBeforeStale = count(BotApi::$calls);
|
|
|
|
|
$assert(TicketTelegramNotifier::notify($ticketEvent('user_reply', 46))
|
|
|
|
|
=== TicketTelegramNotificationResult::Failed
|
|
|
|
|
&& $stale->state === 'unknown'
|
|
|
|
|
&& count(BotApi::$calls) === $callsBeforeStale,
|
|
|
|
|
'A stale dispatching delivery was replayed or left permanently unresolved.');
|
|
|
|
|
$calls = count(BotApi::$calls);
|
|
|
|
|
foreach ([['bound_admin_id' => 1, 'chat_id' => '-100123456'],
|
|
|
|
|
['bound_admin_id' => 2, 'chat_id' => '123456'],
|
|
|
|
|
['bound_admin_id' => 1, 'chat_id' => ['unexpected']]] as $unsafe) {
|
|
|
|
|
$setState($unsafe);
|
|
|
|
|
$assert(!TicketTelegramNotifier::notify($ticketEvent('user_reply')), 'Group/ordinary user/malformed recipient was notified.');
|
|
|
|
|
$assert(TicketTelegramNotifier::notify($ticketEvent('user_reply'))
|
|
|
|
|
=== TicketTelegramNotificationResult::Skipped,
|
|
|
|
|
'Group/ordinary user/malformed recipient was notified.');
|
|
|
|
|
}
|
|
|
|
|
$setState(['bound_admin_id' => 1, 'chat_id' => '123456']);
|
|
|
|
|
User::$users[1]->is_admin = 0;
|
|
|
|
|
$assert(!TicketTelegramNotifier::notify($ticketEvent('user_reply')), 'Downgraded administrator received alerts.');
|
|
|
|
|
$assert(TicketTelegramNotifier::notify($ticketEvent('user_reply'))
|
|
|
|
|
=== TicketTelegramNotificationResult::Skipped,
|
|
|
|
|
'Downgraded administrator received alerts.');
|
|
|
|
|
User::$users[1]->is_admin = 1;
|
|
|
|
|
User::$users[1]->enable = 0;
|
|
|
|
|
$assert(!TicketTelegramNotifier::notify($ticketEvent('user_reply')), 'Disabled administrator received alerts.');
|
|
|
|
|
$assert(TicketTelegramNotifier::notify($ticketEvent('user_reply'))
|
|
|
|
|
=== TicketTelegramNotificationResult::Skipped,
|
|
|
|
|
'Disabled administrator received alerts.');
|
|
|
|
|
User::$users[1]->enable = 1;
|
|
|
|
|
$System_Config['super_admin'] = '2';
|
|
|
|
|
$assert(!TicketTelegramNotifier::notify($ticketEvent('user_reply')), 'Removed super-admin received alerts.');
|
|
|
|
|
$assert(TicketTelegramNotifier::notify($ticketEvent('user_reply'))
|
|
|
|
|
=== TicketTelegramNotificationResult::Skipped,
|
|
|
|
|
'Removed super-admin received alerts.');
|
|
|
|
|
$System_Config['super_admin'] = '1,2';
|
|
|
|
|
$assert(count(BotApi::$calls) === $calls, 'Rejected notification still invoked Telegram.');
|
|
|
|
|
|
|
|
|
|
|