From 2f314c9a8c0e24d962ec54dd47390c3483b3e751 Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Mon, 22 Jun 2026 09:55:12 -0300 Subject: [PATCH] feat(telegram): surface connect failure in the dialog instead of silent expiry When a Telegram channel connection is rejected (e.g. the workspace already has a channel for that network), broadcast TelegramConnectFailed so the connect dialog shows a clear error message instead of waiting until the code silently expires. --- .../SocialAccount/ConnectTelegramChannel.php | 3 ++ app/Events/TelegramConnectFailed.php | 53 +++++++++++++++++++ lang/en/accounts.php | 1 + lang/es/accounts.php | 1 + lang/pt-BR/accounts.php | 1 + .../accounts/TelegramConnectDialog.vue | 16 ++++++ .../Feature/Social/TelegramConnectionTest.php | 8 +++ 7 files changed, 83 insertions(+) create mode 100644 app/Events/TelegramConnectFailed.php diff --git a/app/Actions/SocialAccount/ConnectTelegramChannel.php b/app/Actions/SocialAccount/ConnectTelegramChannel.php index c4d5e4b3..63d19f73 100644 --- a/app/Actions/SocialAccount/ConnectTelegramChannel.php +++ b/app/Actions/SocialAccount/ConnectTelegramChannel.php @@ -7,6 +7,7 @@ use App\Enums\SocialAccount\Platform; use App\Enums\SocialAccount\Status; use App\Events\TelegramChannelConnected; +use App\Events\TelegramConnectFailed; use App\Models\SocialAccount; use App\Models\Workspace; use App\Services\Social\Telegram\TelegramApi; @@ -34,6 +35,8 @@ public static function execute(Workspace $workspace, array $chat, string $nonce) ->exists(); if ($isNewAccount && self::networkAlreadyConnected($workspace, $chatId)) { + TelegramConnectFailed::dispatch($workspace->id, $nonce, 'network_taken'); + return null; } diff --git a/app/Events/TelegramConnectFailed.php b/app/Events/TelegramConnectFailed.php new file mode 100644 index 00000000..9210f7ce --- /dev/null +++ b/app/Events/TelegramConnectFailed.php @@ -0,0 +1,53 @@ + + */ + public function broadcastOn(): array + { + return [ + new PrivateChannel("workspace.{$this->workspaceId}"), + ]; + } + + /** + * @return array + */ + public function broadcastWith(): array + { + return [ + 'nonce' => $this->nonce, + 'reason' => $this->reason, + ]; + } + + public function broadcastQueue(): string + { + return 'broadcasts'; + } +} diff --git a/lang/en/accounts.php b/lang/en/accounts.php index 85bc7392..44876d8e 100644 --- a/lang/en/accounts.php +++ b/lang/en/accounts.php @@ -96,6 +96,7 @@ 'new_code' => 'Generate a new code', 'retry' => 'Try again', 'error_generic' => 'Could not start the connection. Please try again.', + 'network_taken' => 'This workspace already has a Telegram channel connected. Disconnect it first.', ], 'facebook' => [ diff --git a/lang/es/accounts.php b/lang/es/accounts.php index 1596a02b..2f04bd40 100644 --- a/lang/es/accounts.php +++ b/lang/es/accounts.php @@ -96,6 +96,7 @@ 'new_code' => 'Generar un nuevo código', 'retry' => 'Reintentar', 'error_generic' => 'No se pudo iniciar la conexión. Inténtalo de nuevo.', + 'network_taken' => 'Este workspace ya tiene un canal de Telegram conectado. Desconéctalo primero.', ], 'facebook' => [ diff --git a/lang/pt-BR/accounts.php b/lang/pt-BR/accounts.php index d9cecdc4..8701d934 100644 --- a/lang/pt-BR/accounts.php +++ b/lang/pt-BR/accounts.php @@ -96,6 +96,7 @@ 'new_code' => 'Gerar um novo código', 'retry' => 'Tentar novamente', 'error_generic' => 'Não foi possível iniciar a conexão. Tente novamente.', + 'network_taken' => 'Este workspace já tem um canal de Telegram conectado. Desconecte-o primeiro.', ], 'facebook' => [ diff --git a/resources/js/components/accounts/TelegramConnectDialog.vue b/resources/js/components/accounts/TelegramConnectDialog.vue index 7ec1425a..5cec83e6 100644 --- a/resources/js/components/accounts/TelegramConnectDialog.vue +++ b/resources/js/components/accounts/TelegramConnectDialog.vue @@ -72,6 +72,22 @@ useWorkspaceEcho<{ nonce: string }>( }, ); +useWorkspaceEcho<{ nonce: string; reason: string }>( + '.telegram.connect.failed', + (payload) => { + if (phase.value !== 'ready' || payload.nonce !== nonce.value) { + return; + } + + phase.value = 'error'; + clearExpiry(); + errorMessage.value = + payload.reason === 'network_taken' + ? trans('accounts.telegram.network_taken') + : trans('accounts.telegram.error_generic'); + }, +); + const start = async () => { phase.value = 'loading'; errorMessage.value = ''; diff --git a/tests/Feature/Social/TelegramConnectionTest.php b/tests/Feature/Social/TelegramConnectionTest.php index b0509299..f30e90f9 100644 --- a/tests/Feature/Social/TelegramConnectionTest.php +++ b/tests/Feature/Social/TelegramConnectionTest.php @@ -5,6 +5,7 @@ use App\Enums\SocialAccount\Platform; use App\Enums\UserWorkspace\Role; use App\Events\TelegramChannelConnected; +use App\Events\TelegramConnectFailed; use App\Models\Post; use App\Models\PostPlatform; use App\Models\SocialAccount; @@ -119,6 +120,7 @@ function telegramUpdate(string $code, array $chat = []): array it('does not connect a second telegram channel when one is already connected', function () { config(['trypost.self_hosted' => false]); + Event::fake([TelegramConnectFailed::class]); SocialAccount::factory()->telegram()->create([ 'workspace_id' => $this->workspace->id, @@ -135,6 +137,12 @@ function telegramUpdate(string $code, array $chat = []): array expect( SocialAccount::where('platform', Platform::Telegram)->where('platform_user_id', '-1001234567890')->exists() )->toBeFalse(); + + Event::assertDispatched( + TelegramConnectFailed::class, + fn (TelegramConnectFailed $event): bool => $event->workspaceId === $this->workspace->id + && $event->reason === 'network_taken', + ); }); it('connects a second telegram channel in self-hosted mode', function () {