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.
This commit is contained in:
Paulo Castellano 2026-06-22 09:55:12 -03:00
parent aacc4390a2
commit 2f314c9a8c
7 changed files with 83 additions and 0 deletions

View file

@ -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;
}

View file

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace App\Events;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class TelegramConnectFailed implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct(
public string $workspaceId,
public string $nonce,
public string $reason,
) {}
public function broadcastAs(): string
{
return 'telegram.connect.failed';
}
/**
* @return array<int, PrivateChannel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel("workspace.{$this->workspaceId}"),
];
}
/**
* @return array<string, string>
*/
public function broadcastWith(): array
{
return [
'nonce' => $this->nonce,
'reason' => $this->reason,
];
}
public function broadcastQueue(): string
{
return 'broadcasts';
}
}

View file

@ -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' => [

View file

@ -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' => [

View file

@ -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' => [

View file

@ -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 = '';

View file

@ -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 () {