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
Match an older Instagram card when linking it through Facebook (#421)
Paulo Castellanoe6b943582fa7
diff --git a/app/Http/Controllers/Auth/InstagramFacebookController.php b/app/Http/Controllers/Auth/InstagramFacebookController.phpindex 222b23f11..4a8a4262a 100644--- a/app/Http/Controllers/Auth/InstagramFacebookController.php+++ b/app/Http/Controllers/Auth/InstagramFacebookController.php@@ -5,6 +5,8 @@ declare(strict_types=1);namespace App\Http\Controllers\Auth;use App\Enums\SocialAccount\Platform as SocialPlatform;+use App\Models\SocialAccount;+use App\Services\Social\ConnectionVerifier;use App\Services\Social\Meta\ManagedPages;use App\Support\Social\PendingConnection;use Exception;@@ -73,6 +75,8 @@ class InstagramFacebookController extends MetaController$workspace = $this->connectWorkspace($request);$reconnect = $this->reconnectAccount($workspace);+ $this->fetchMissingInstagramAccountId($reconnect);+try {$socialUser = Socialite::driver($this->driver)->usingGraphVersion($this->graphVersion())@@ -124,6 +128,28 @@ class InstagramFacebookController extends MetaController}}+ /**+ * An Instagram Login card connected before its professional account id was+ * stored has nothing to match the Facebook listing against, so moving it to+ * Facebook asks Instagram for the id first. A failed lookup leaves the card+ * as it was, and the reconnect reports the missing page as before.+ */+ private function fetchMissingInstagramAccountId(?SocialAccount $reconnect): void+ {+ if ($reconnect?->platform !== SocialPlatform::Instagram || filled(data_get($reconnect->meta, 'ig_user_id'))) {+ return;+ }++ try {+ app(ConnectionVerifier::class)->verify($reconnect);+ } catch (Exception $e) {+ Log::warning('Instagram account id lookup failed before reconnecting through Facebook', [+ 'social_account_id' => $reconnect->id,+ 'error' => $e->getMessage(),+ ]);+ }+ }+/*** A lookup we never made says nothing about the handle or avatar a reconnect* already has, so an undescribed account leaves both as they are.diff --git a/tests/Feature/Social/InstagramFacebookControllerTest.php b/tests/Feature/Social/InstagramFacebookControllerTest.phpindex bb513a655..a416801c1 100644--- a/tests/Feature/Social/InstagramFacebookControllerTest.php+++ b/tests/Feature/Social/InstagramFacebookControllerTest.php@@ -929,3 +929,57 @@ test('a facebook callback reconnecting a standalone instagram card that finds an->and($account->fresh()->platform)->toBe(Platform::Instagram)->and($account->fresh()->platform_user_id)->toBe('app-scoped-1');});++test('a facebook callback reconnecting an instagram card connected before its account id was stored asks instagram for it', function () {+ $account = SocialAccount::factory()->create([+ 'workspace_id' => $this->workspace->id,+ 'platform' => Platform::Instagram,+ 'platform_user_id' => 'app-scoped-1',+ 'access_token' => 'instagram-token',+ 'token_expires_at' => now()->addMonth(),+ 'meta' => ['account_type' => 'BUSINESS'],+ ]);++ startSocialConnect($this->workspace, Platform::InstagramFacebook, $account);++ $socialiteUser = Mockery::mock(SocialiteUser::class);+ $socialiteUser->token = 'test-user-token';++ Socialite::shouldReceive('driver')+ ->with('facebook')+ ->andReturn(Mockery::mock()+ ->shouldReceive('usingGraphVersion')->andReturnSelf()+ ->shouldReceive('redirectUrl')->andReturnSelf()+ ->shouldReceive('user')->andReturn($socialiteUser)+ ->getMock());++ $graphApi = config('trypost.platforms.instagram-facebook.graph_api');+ $instagramApi = config('trypost.platforms.instagram.graph_api');++ Http::fake([+ "{$instagramApi}/me?*" => Http::response(['id' => 'app-scoped-1', 'username' => 'mybiz', 'user_id' => 'ig-pro-1'], 200),+ "{$graphApi}/me?*" => Http::response(['id' => 'facebook_user_123', 'name' => 'User'], 200),+ "{$graphApi}/me/permissions*" => Http::response(['data' => [['permission' => 'pages_show_list', 'status' => 'granted']]], 200),+ "{$graphApi}/me/businesses*" => Http::response(['data' => []], 200),+ "{$graphApi}/me/accounts*" => Http::response(['data' => [[+ 'id' => 'page-1',+ 'name' => 'My Page',+ 'access_token' => 'page-token',+ 'instagram_business_account' => ['id' => 'ig-pro-1'],+ ]]], 200),+ "{$graphApi}/ig-pro-1*" => Http::response(['id' => 'ig-pro-1', 'username' => 'mybiz', 'name' => 'My Biz'], 200),+ ]);++ $this->actingAs($this->user)->get(route('app.social.instagram-facebook.callback'));++ expect(socialConnectFailure())->toBeNull();++ finishSocialConnect(Platform::InstagramFacebook)->assertInertiaFlash('connectedChannel.created', false);++ $account->refresh();++ expect($this->workspace->socialAccounts()->count())->toBe(1)+ ->and($account->platform)->toBe(Platform::InstagramFacebook)+ ->and($account->platform_user_id)->toBe('ig-pro-1')+ ->and($account->access_token)->toBe('page-token');+});