Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

feat: use TokenExpired state instead of Disconnected on first token failure

Paulo Castellano8ca846a86529
diff --git a/app/Jobs/PublishToSocialPlatform.php b/app/Jobs/PublishToSocialPlatform.php
index a8cd99252..20fd03d5f 100644
--- a/app/Jobs/PublishToSocialPlatform.php
+++ b/app/Jobs/PublishToSocialPlatform.php
@@ -126,7 +126,7 @@ class PublishToSocialPlatform implements ShouldQueue
'platform_error_code' => $e->platformErrorCode,
'failed_at' => now()->toIso8601String(),
]);
- $this->postPlatform->socialAccount->markAsDisconnected($e->getMessage());
+ $this->postPlatform->socialAccount->markAsTokenExpired($e->getMessage());
break;
} catch (SocialPublishException $e) {
Log::error('Social publish failed: '.$e->userMessage);
diff --git a/app/Jobs/VerifyWorkspaceConnections.php b/app/Jobs/VerifyWorkspaceConnections.php
index cabca26ff..28fa67b56 100644
--- a/app/Jobs/VerifyWorkspaceConnections.php
+++ b/app/Jobs/VerifyWorkspaceConnections.php
@@ -30,7 +30,7 @@ class VerifyWorkspaceConnections implements ShouldQueue
public function handle(ConnectionVerifier $verifier): void
{
$connectedAccounts = $this->workspace->socialAccounts()
- ->where('status', Status::Connected)
+ ->whereIn('status', [Status::Connected, Status::TokenExpired])
->get();
if ($connectedAccounts->isEmpty()) {
diff --git a/app/Models/SocialAccount.php b/app/Models/SocialAccount.php
index 48b53099d..365f7eb3c 100644
--- a/app/Models/SocialAccount.php
+++ b/app/Models/SocialAccount.php
@@ -131,6 +131,42 @@ class SocialAccount extends Model
}
}
+ public function markAsTokenExpired(string $errorMessage): void
+ {
+ $lock = Cache::lock("social_account_disconnect:{$this->id}", 10);
+
+ if ($lock->get()) {
+ try {
+ $this->refresh();
+ $wasConnected = $this->status === Status::Connected;
+
+ $this->update([
+ 'status' => Status::TokenExpired,
+ 'error_message' => $errorMessage,
+ 'disconnected_at' => now(),
+ ]);
+
+ if ($wasConnected && $this->workspace->owner) {
+ $platformName = $this->platform->label();
+ $accountName = $this->username ?? $this->display_name;
+
+ SendNotification::dispatch(
+ user: $this->workspace->owner,
+ workspaceId: $this->workspace_id,
+ type: Type::AccountDisconnected,
+ channel: Channel::Both,
+ title: "{$platformName} token expired",
+ body: "@{$accountName} needs to be reconnected",
+ data: ['social_account_id' => $this->id],
+ mailable: new AccountDisconnected($this),
+ );
+ }
+ } finally {
+ $lock->release();
+ }
+ }
+ }
+
public function markAsConnected(): void
{
$this->update([
diff --git a/tests/Feature/Jobs/PublishToSocialPlatformTest.php b/tests/Feature/Jobs/PublishToSocialPlatformTest.php
index 72a1492b1..4dcb4f989 100644
--- a/tests/Feature/Jobs/PublishToSocialPlatformTest.php
+++ b/tests/Feature/Jobs/PublishToSocialPlatformTest.php
@@ -91,7 +91,7 @@ test('publish to social platform marks platform as failed on error', function ()
expect($this->postPlatform->error_message)->toBe('API Error');
});
-test('publish to social platform disconnects account on token expired', function () {
+test('publish to social platform marks account as token expired on auth failure', function () {
Event::fake();
Mail::fake();
@@ -106,7 +106,7 @@ test('publish to social platform disconnects account on token expired', function
$this->socialAccount->refresh();
expect($this->postPlatform->status)->toBe(PlatformStatus::Failed);
- expect($this->socialAccount->status)->toBe(AccountStatus::Disconnected);
+ expect($this->socialAccount->status)->toBe(AccountStatus::TokenExpired);
});
test('publish to social platform updates post status when all platforms finished', function () {
@@ -294,7 +294,7 @@ test('it retries with token refresh when token expires during publish', function
expect($this->socialAccount->status)->not->toBe(AccountStatus::Disconnected);
});
-test('it disconnects account when token refresh fails during publish retry', function () {
+test('it marks account as token expired when refresh fails during publish retry', function () {
Event::fake();
$publisher = Mockery::mock(LinkedInPublisher::class);
@@ -315,7 +315,7 @@ test('it disconnects account when token refresh fails during publish retry', fun
$this->socialAccount->refresh();
expect($this->postPlatform->status)->toBe(PlatformStatus::Failed);
- expect($this->socialAccount->status)->toBe(AccountStatus::Disconnected);
+ expect($this->socialAccount->status)->toBe(AccountStatus::TokenExpired);
});
test('publish to social platform skips if already published (idempotency)', function () {