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.
PHP ai-agentsblueskyfacebookinstagramlaravellinkedinmastodonmcpmcp-serveropen-sourcerest-apiself-hostedsocial-mediasocial-media-managementsocial-media-schedulerthreadstiktokvuejsxyoutube
Replace generic token-TTL constant with per-platform Platform::defaultTokenTtlSeconds()
Paulo Castellanoc8f875550a51
diff --git a/app/Enums/SocialAccount/Platform.php b/app/Enums/SocialAccount/Platform.phpindex 808f865b9..afcd02f33 100644--- a/app/Enums/SocialAccount/Platform.php+++ b/app/Enums/SocialAccount/Platform.php@@ -23,13 +23,6 @@ enum Platform: stringcase Telegram = 'telegram';case Discord = 'discord';- /**- * Fallback lifetime, in seconds, for Meta long-lived tokens (Instagram and- * Threads — see extendsAccessTokenOnRefresh) when the provider response- * omits expires_in. Meta issues these tokens for 60 days.- */- public const LONG_LIVED_TOKEN_TTL_SECONDS = 5184000;-/*** The social network this platform belongs to. Variants that represent the* same network (LinkedIn profile vs. company page, Instagram standalone vs.@@ -300,6 +293,28 @@ enum Platform: string));}+ /**+ * The token lifetime, in seconds, to assume when the provider's OAuth+ * response omits expires_in. Each value is that network's own documented+ * default:+ *+ * - X: a 2-hour access token.+ * - Instagram / Threads: Meta's 60-day long-lived token.+ *+ * Networks that always return expires_in (LinkedIn, TikTok, YouTube,+ * Pinterest), whose refresh sets a fixed lifetime directly (Bluesky), or+ * whose tokens never expire (Facebook, Mastodon, Telegram, Discord) have no+ * fallback here and return null.+ */+ public function defaultTokenTtlSeconds(): ?int+ {+ return match ($this) {+ self::X => 7200,+ self::Instagram, self::Threads => 5184000,+ default => null,+ };+ }+public function queue(): string{return 'social-'.$this->value;diff --git a/app/Http/Controllers/Auth/InstagramController.php b/app/Http/Controllers/Auth/InstagramController.phpindex af48218c5..f8a0c0546 100644--- a/app/Http/Controllers/Auth/InstagramController.php+++ b/app/Http/Controllers/Auth/InstagramController.php@@ -69,7 +69,7 @@ class InstagramController extends SocialController$avatarPath = $socialUser->getAvatar() ? uploadFromUrl($socialUser->getAvatar()) : null;// Calculate token expiration (long-lived tokens last 60 days)- $expiresIn = $socialUser->expiresIn ?? SocialPlatform::LONG_LIVED_TOKEN_TTL_SECONDS;+ $expiresIn = $socialUser->expiresIn ?? $this->platform->defaultTokenTtlSeconds();$tokenExpiresAt = now()->addSeconds($expiresIn);$workspace->socialAccounts()->updateOrCreate(diff --git a/app/Http/Controllers/Auth/ThreadsController.php b/app/Http/Controllers/Auth/ThreadsController.phpindex 059b173e2..955009ea0 100644--- a/app/Http/Controllers/Auth/ThreadsController.php+++ b/app/Http/Controllers/Auth/ThreadsController.php@@ -117,7 +117,7 @@ class ThreadsController extends SocialController$longLivedData = $longLivedResponse->json();$longLivedToken = $longLivedData['access_token'] ?? $shortLivedToken;- $expiresIn = $longLivedData['expires_in'] ?? SocialPlatform::LONG_LIVED_TOKEN_TTL_SECONDS;+ $expiresIn = $longLivedData['expires_in'] ?? $this->platform->defaultTokenTtlSeconds();// Fetch user profile$profileResponse = Http::get(config('trypost.platforms.threads.graph_api')."/{$userId}", [diff --git a/app/Services/Social/ConnectionVerifier.php b/app/Services/Social/ConnectionVerifier.phpindex 547b78d65..071d554e1 100644--- a/app/Services/Social/ConnectionVerifier.php+++ b/app/Services/Social/ConnectionVerifier.php@@ -181,7 +181,7 @@ class ConnectionVerifier$account->update(['access_token' => data_get($data, 'access_token'),'refresh_token' => data_get($data, 'refresh_token', $account->refresh_token),- 'token_expires_at' => now()->addSeconds(data_get($data, 'expires_in', 7200)),+ 'token_expires_at' => now()->addSeconds(data_get($data, 'expires_in', $account->platform->defaultTokenTtlSeconds())),]);$account->refresh();@@ -328,7 +328,7 @@ class ConnectionVerifier$account->update(['access_token' => $newToken,'refresh_token' => $newToken,- 'token_expires_at' => now()->addSeconds(data_get($data, 'expires_in', Platform::LONG_LIVED_TOKEN_TTL_SECONDS)),+ 'token_expires_at' => now()->addSeconds(data_get($data, 'expires_in', $account->platform->defaultTokenTtlSeconds())),]);$account->refresh();@@ -350,7 +350,7 @@ class ConnectionVerifier$account->update(['access_token' => $newToken,'refresh_token' => $newToken,- 'token_expires_at' => now()->addSeconds(data_get($data, 'expires_in', Platform::LONG_LIVED_TOKEN_TTL_SECONDS)),+ 'token_expires_at' => now()->addSeconds(data_get($data, 'expires_in', $account->platform->defaultTokenTtlSeconds())),]);$account->refresh();diff --git a/tests/Unit/Enums/PlatformTest.php b/tests/Unit/Enums/PlatformTest.phpindex 5c4f94d1a..340d302f6 100644--- a/tests/Unit/Enums/PlatformTest.php+++ b/tests/Unit/Enums/PlatformTest.php@@ -85,6 +85,24 @@ test('platform supports text only correctly', function () {expect(Platform::Pinterest->supportsTextOnly())->toBeFalse();});+test('platform exposes the correct default token TTL fallback', function () {+ // X access tokens live 2 hours.+ expect(Platform::X->defaultTokenTtlSeconds())->toBe(7200);++ // Instagram and Threads use Meta's 60-day long-lived token.+ expect(Platform::Instagram->defaultTokenTtlSeconds())->toBe(5184000);+ expect(Platform::Threads->defaultTokenTtlSeconds())->toBe(5184000);++ // Networks that always return expires_in, set a fixed lifetime directly, or+ // never expire have no fallback here.+ expect(Platform::LinkedIn->defaultTokenTtlSeconds())->toBeNull();+ expect(Platform::TikTok->defaultTokenTtlSeconds())->toBeNull();+ expect(Platform::YouTube->defaultTokenTtlSeconds())->toBeNull();+ expect(Platform::Pinterest->defaultTokenTtlSeconds())->toBeNull();+ expect(Platform::Bluesky->defaultTokenTtlSeconds())->toBeNull();+ expect(Platform::Facebook->defaultTokenTtlSeconds())->toBeNull();+});+test('platform is enabled by default', function () {expect(Platform::LinkedIn->isEnabled())->toBeTrue();expect(Platform::Instagram->isEnabled())->toBeTrue();