|
|
|
|
@ -62,6 +62,87 @@ namespace App\Models {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final class TicketTelegramDelivery
|
|
|
|
|
{
|
|
|
|
|
public static array $rows = [];
|
|
|
|
|
public static int $nextId = 1;
|
|
|
|
|
public ?int $id = null;
|
|
|
|
|
public string $binding_generation = '';
|
|
|
|
|
public int $bound_admin_id = 0;
|
|
|
|
|
public string $chat_id = '';
|
|
|
|
|
public int $ticket_id = 0;
|
|
|
|
|
public int $ticket_message_id = 0;
|
|
|
|
|
public string $event_kind = '';
|
|
|
|
|
public ?int $telegram_message_id = null;
|
|
|
|
|
public string $state = '';
|
|
|
|
|
public string $content_hash = '';
|
|
|
|
|
public int $created_at = 0;
|
|
|
|
|
public ?int $sent_at = null;
|
|
|
|
|
public int $updated_at = 0;
|
|
|
|
|
public static function where($key, $value)
|
|
|
|
|
{
|
|
|
|
|
return (new TicketTelegramDeliveryQuery())->where($key, $value);
|
|
|
|
|
}
|
|
|
|
|
public function save()
|
|
|
|
|
{
|
|
|
|
|
if ($this->id === null) {
|
|
|
|
|
foreach (self::$rows as $row) {
|
|
|
|
|
if ($row->binding_generation === $this->binding_generation
|
|
|
|
|
&& $row->ticket_message_id === $this->ticket_message_id
|
|
|
|
|
&& $row->event_kind === $this->event_kind) {
|
|
|
|
|
throw new \RuntimeException('synthetic duplicate delivery');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->id = self::$nextId++;
|
|
|
|
|
}
|
|
|
|
|
self::$rows[$this->id] = $this;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final class TicketTelegramDeliveryQuery
|
|
|
|
|
{
|
|
|
|
|
private array $filters = [];
|
|
|
|
|
public function where($key, $value)
|
|
|
|
|
{
|
|
|
|
|
$this->filters[$key] = $value;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
public function first()
|
|
|
|
|
{
|
|
|
|
|
foreach (TicketTelegramDelivery::$rows as $row) {
|
|
|
|
|
if ($this->matches($row)) {
|
|
|
|
|
return $row;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
public function update(array $values)
|
|
|
|
|
{
|
|
|
|
|
$updated = 0;
|
|
|
|
|
foreach (TicketTelegramDelivery::$rows as $row) {
|
|
|
|
|
if (!$this->matches($row)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
foreach ($values as $key => $value) {
|
|
|
|
|
$row->$key = $value;
|
|
|
|
|
}
|
|
|
|
|
TicketTelegramDelivery::$rows[$row->id] = $row;
|
|
|
|
|
$updated++;
|
|
|
|
|
}
|
|
|
|
|
return $updated;
|
|
|
|
|
}
|
|
|
|
|
private function matches(TicketTelegramDelivery $row): bool
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->filters as $key => $value) {
|
|
|
|
|
if (($row->$key ?? null) != $value) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final class Configurations
|
|
|
|
|
{
|
|
|
|
|
public static array $rows = [];
|
|
|
|
|
@ -138,6 +219,7 @@ namespace TelegramBot\Api {
|
|
|
|
|
{
|
|
|
|
|
public static array $calls = [];
|
|
|
|
|
public static bool $fail = false;
|
|
|
|
|
public static int $nextMessageId = 1000;
|
|
|
|
|
public function __construct(private string $token) {}
|
|
|
|
|
public function call($method, $parameters, $timeout)
|
|
|
|
|
{
|
|
|
|
|
@ -145,7 +227,8 @@ namespace TelegramBot\Api {
|
|
|
|
|
if (self::$fail) {
|
|
|
|
|
throw new \RuntimeException('synthetic SDK failure with token=' . $this->token);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
return $method === 'sendMessage'
|
|
|
|
|
? ['message_id' => self::$nextMessageId++] : true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -154,6 +237,7 @@ namespace {
|
|
|
|
|
use App\Models\Configurations;
|
|
|
|
|
use App\Models\CloudInstance;
|
|
|
|
|
use App\Models\Ticket;
|
|
|
|
|
use App\Models\TicketTelegramDelivery;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use App\Services\Auth;
|
|
|
|
|
use App\Services\Cloud\CloudSecrets;
|
|
|
|
|
@ -287,7 +371,7 @@ namespace {
|
|
|
|
|
return ['message' => ['chat' => ['id' => $chatId, 'type' => 'private'],
|
|
|
|
|
'from' => ['id' => $chatId, 'is_bot' => false], 'text' => '/start ' . $code]];
|
|
|
|
|
};
|
|
|
|
|
$ticketEvent = function (string $kind = 'created'): TicketEvent {
|
|
|
|
|
$ticketEvent = function (string $kind = 'created', int $messageId = 42): TicketEvent {
|
|
|
|
|
$root = new Ticket();
|
|
|
|
|
$root->id = 42;
|
|
|
|
|
$root->userid = 7;
|
|
|
|
|
@ -300,6 +384,7 @@ namespace {
|
|
|
|
|
$root->priority = 'high';
|
|
|
|
|
$root->instance_id = 55;
|
|
|
|
|
$message = clone $root;
|
|
|
|
|
$message->id = $messageId;
|
|
|
|
|
$message->content = "<i>Body</i>\nSecond line";
|
|
|
|
|
$result = new TicketWriteResult($root, $message, '新工单', [new \stdClass(), new \stdClass()]);
|
|
|
|
|
return $kind === 'created' ? TicketEvent::created($result) : TicketEvent::userReply($result);
|
|
|
|
|
@ -374,7 +459,11 @@ namespace {
|
|
|
|
|
$assert(BotApi::$calls === [], 'A failed binding state write still sent a success confirmation.');
|
|
|
|
|
$binding = TicketTelegramNotifier::createBinding(1);
|
|
|
|
|
$assert(TicketTelegramNotifier::handleUpdate($update($binding['code'])), 'Valid private binding failed.');
|
|
|
|
|
$assert($state() === ['bound_admin_id' => 1, 'chat_id' => '123456'], 'Consumed token or recipient state is incorrect.');
|
|
|
|
|
$boundState = $state();
|
|
|
|
|
$assert(($boundState['bound_admin_id'] ?? 0) === 1
|
|
|
|
|
&& ($boundState['chat_id'] ?? '') === '123456'
|
|
|
|
|
&& preg_match('/^[a-f0-9]{32}$/D', (string) ($boundState['binding_generation'] ?? '')) === 1,
|
|
|
|
|
'Consumed token, recipient or binding generation state is incorrect.');
|
|
|
|
|
$assert(count(BotApi::$calls) === 1
|
|
|
|
|
&& BotApi::$calls[0][0] === 'sendMessage'
|
|
|
|
|
&& BotApi::$calls[0][1]['chat_id'] === '123456'
|
|
|
|
|
@ -400,13 +489,19 @@ namespace {
|
|
|
|
|
$assert(TicketTelegramNotifier::handleUpdate($update($binding['code'])),
|
|
|
|
|
'A failed Telegram confirmation changed a committed binding into a webhook failure.');
|
|
|
|
|
BotApi::$fail = false;
|
|
|
|
|
$assert($state() === ['bound_admin_id' => 1, 'chat_id' => '123456'],
|
|
|
|
|
$failedConfirmationState = $state();
|
|
|
|
|
$assert(($failedConfirmationState['bound_admin_id'] ?? 0) === 1
|
|
|
|
|
&& ($failedConfirmationState['chat_id'] ?? '') === '123456'
|
|
|
|
|
&& preg_match('/^[a-f0-9]{32}$/D', (string) ($failedConfirmationState['binding_generation'] ?? '')) === 1,
|
|
|
|
|
'A failed Telegram confirmation rolled back the committed binding.');
|
|
|
|
|
$callsAfterFailedConfirmation = count(BotApi::$calls);
|
|
|
|
|
$assert(!TicketTelegramNotifier::handleUpdate($update($binding['code']))
|
|
|
|
|
&& count(BotApi::$calls) === $callsAfterFailedConfirmation,
|
|
|
|
|
'A replay after a failed confirmation resent or re-consumed the binding code.');
|
|
|
|
|
BotApi::$calls = [];
|
|
|
|
|
BotApi::$nextMessageId = 1000;
|
|
|
|
|
TicketTelegramDelivery::$rows = [];
|
|
|
|
|
TicketTelegramDelivery::$nextId = 1;
|
|
|
|
|
$setState(['bound_admin_id' => 1, 'chat_id' => '123456']);
|
|
|
|
|
$assert(TicketTelegramNotifier::notify($ticketEvent()), 'Eligible administrator was not notified.');
|
|
|
|
|
$alertText = BotApi::$calls[0][1]['text'];
|
|
|
|
|
@ -423,6 +518,26 @@ namespace {
|
|
|
|
|
&& !str_contains($alertText, 'owner-secret@example.test')
|
|
|
|
|
&& !str_contains($alertText, '+15550001111')
|
|
|
|
|
&& !isset(BotApi::$calls[0][1]['parse_mode']), 'Rich alert content or plain-text privacy boundary is wrong.');
|
|
|
|
|
$delivery = TicketTelegramDelivery::$rows[1] ?? null;
|
|
|
|
|
$assert($delivery !== null && $delivery->state === 'sent'
|
|
|
|
|
&& $delivery->telegram_message_id === 1000
|
|
|
|
|
&& preg_match('/^[a-f0-9]{32}$/D', $delivery->binding_generation) === 1,
|
|
|
|
|
'Successful Telegram notification did not persist a reply mapping.');
|
|
|
|
|
$assert(TicketTelegramNotifier::notify($ticketEvent())
|
|
|
|
|
&& 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)),
|
|
|
|
|
'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))
|
|
|
|
|
&& count(BotApi::$calls) === $callsAfterUnknown,
|
|
|
|
|
'An uncertain Telegram delivery was not fenced against replay.');
|
|
|
|
|
$calls = count(BotApi::$calls);
|
|
|
|
|
foreach ([['bound_admin_id' => 1, 'chat_id' => '-100123456'],
|
|
|
|
|
['bound_admin_id' => 2, 'chat_id' => '123456'],
|
|
|
|
|
|