From 6c6078fa6a4aa0336275fa6761171939629a46d9 Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Tue, 19 May 2026 09:52:49 -0300 Subject: [PATCH] refactor(social): drop redundant \$account->refresh() after CV refresh MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every refresh*Token method inside ConnectionVerifier already finishes with \$account->update(...) (mutates the model) + \$account->refresh() (reloads from DB to pick up sibling updates like LinkedInTokenSynchronizer). The lock-contention branch in refreshToken() also calls \$account->refresh() before returning. So a second \$account->refresh() in the caller was always a redundant SELECT — no scenario where it actually pulled a different value than what CV already left in memory. Removed from all 15 call sites, plus fixed the duplicated "Mastodon tokens don't expire" comment. --- app/Services/Social/BlueskyPublisher.php | 1 - app/Services/Social/ConnectionVerifier.php | 5 ++--- app/Services/Social/InstagramAnalytics.php | 2 -- app/Services/Social/InstagramPublisher.php | 1 - app/Services/Social/LinkedInPageAnalytics.php | 2 -- app/Services/Social/LinkedInPagePublisher.php | 2 -- app/Services/Social/LinkedInPublisher.php | 2 -- app/Services/Social/PinterestAnalytics.php | 2 -- app/Services/Social/PinterestPublisher.php | 2 -- app/Services/Social/ThreadsAnalytics.php | 2 -- app/Services/Social/ThreadsPublisher.php | 1 - app/Services/Social/TikTokAnalytics.php | 1 - app/Services/Social/TikTokCreatorInfo.php | 1 - app/Services/Social/TikTokPublisher.php | 1 - app/Services/Social/XAnalytics.php | 2 -- app/Services/Social/XPublisher.php | 1 - app/Services/Social/YouTubeAnalytics.php | 2 -- app/Services/Social/YouTubePublisher.php | 1 - 18 files changed, 2 insertions(+), 29 deletions(-) diff --git a/app/Services/Social/BlueskyPublisher.php b/app/Services/Social/BlueskyPublisher.php index bc7a5b65..38094fc6 100644 --- a/app/Services/Social/BlueskyPublisher.php +++ b/app/Services/Social/BlueskyPublisher.php @@ -30,7 +30,6 @@ public function publish(PostPlatform $postPlatform): array // Refresh token if needed if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $medias = $postPlatform->post->mediaItems; diff --git a/app/Services/Social/ConnectionVerifier.php b/app/Services/Social/ConnectionVerifier.php index a0b25dfb..7f25319f 100644 --- a/app/Services/Social/ConnectionVerifier.php +++ b/app/Services/Social/ConnectionVerifier.php @@ -99,9 +99,8 @@ public function refreshToken(SocialAccount $account): void Platform::Pinterest => $this->refreshPinterestToken($account), Platform::Threads => $this->refreshThreadsToken($account), Platform::Instagram => $this->refreshInstagramToken($account), - // InstagramFacebook uses page tokens that don't expire (like Facebook) - // Mastodon tokens don't expire - // Mastodon tokens don't expire + // Facebook / InstagramFacebook use Page tokens that don't expire. + // Mastodon tokens don't expire either. default => null, }; } finally { diff --git a/app/Services/Social/InstagramAnalytics.php b/app/Services/Social/InstagramAnalytics.php index de677310..50c20c88 100644 --- a/app/Services/Social/InstagramAnalytics.php +++ b/app/Services/Social/InstagramAnalytics.php @@ -46,7 +46,6 @@ public function fetchPostMetrics(PostPlatform $postPlatform): array if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $this->accessToken = $account->access_token; @@ -90,7 +89,6 @@ private function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $si if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $this->accessToken = $account->access_token; diff --git a/app/Services/Social/InstagramPublisher.php b/app/Services/Social/InstagramPublisher.php index d4da8274..c22bef71 100644 --- a/app/Services/Social/InstagramPublisher.php +++ b/app/Services/Social/InstagramPublisher.php @@ -31,7 +31,6 @@ public function publish(PostPlatform $postPlatform): array if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $instagramId = $account->platform_user_id; diff --git a/app/Services/Social/LinkedInPageAnalytics.php b/app/Services/Social/LinkedInPageAnalytics.php index 3fdba549..71fb4355 100644 --- a/app/Services/Social/LinkedInPageAnalytics.php +++ b/app/Services/Social/LinkedInPageAnalytics.php @@ -52,7 +52,6 @@ public function fetchPostMetrics(PostPlatform $postPlatform): array if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } // platform_post_id is the share URN (e.g., "urn:li:share:12345"). @@ -82,7 +81,6 @@ private function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $si { if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $this->accessToken = $account->access_token; diff --git a/app/Services/Social/LinkedInPagePublisher.php b/app/Services/Social/LinkedInPagePublisher.php index 8269a21b..c1a186f6 100644 --- a/app/Services/Social/LinkedInPagePublisher.php +++ b/app/Services/Social/LinkedInPagePublisher.php @@ -47,7 +47,6 @@ public function publish(PostPlatform $postPlatform): array if ($this->account->is_token_expired || $this->account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($this->account); - $this->account->refresh(); } $this->accessToken = $this->account->access_token; @@ -82,7 +81,6 @@ private function retryWithRefresh(PostPlatform $postPlatform, ?string $content, try { app(ConnectionVerifier::class)->refreshToken($this->account); - $this->account->refresh(); $this->accessToken = $this->account->access_token; $organizationId = $this->account->meta['organization_id'] ?? null; diff --git a/app/Services/Social/LinkedInPublisher.php b/app/Services/Social/LinkedInPublisher.php index 567bf1e0..8053d458 100644 --- a/app/Services/Social/LinkedInPublisher.php +++ b/app/Services/Social/LinkedInPublisher.php @@ -47,7 +47,6 @@ public function publish(PostPlatform $postPlatform): array if ($this->account->is_token_expired || $this->account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($this->account); - $this->account->refresh(); } $this->accessToken = $this->account->access_token; @@ -76,7 +75,6 @@ private function retryWithRefresh(PostPlatform $postPlatform, ?string $content, try { app(ConnectionVerifier::class)->refreshToken($this->account); - $this->account->refresh(); $this->accessToken = $this->account->access_token; $personUrn = "urn:li:person:{$this->account->platform_user_id}"; diff --git a/app/Services/Social/PinterestAnalytics.php b/app/Services/Social/PinterestAnalytics.php index d18391a4..082963a2 100644 --- a/app/Services/Social/PinterestAnalytics.php +++ b/app/Services/Social/PinterestAnalytics.php @@ -48,7 +48,6 @@ public function fetchPostMetrics(PostPlatform $postPlatform): array if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $start = now()->subDays(90)->format('Y-m-d'); @@ -93,7 +92,6 @@ private function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $si { if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $this->accessToken = $account->access_token; diff --git a/app/Services/Social/PinterestPublisher.php b/app/Services/Social/PinterestPublisher.php index 5fe1d7b3..994199fa 100644 --- a/app/Services/Social/PinterestPublisher.php +++ b/app/Services/Social/PinterestPublisher.php @@ -35,7 +35,6 @@ public function publish(PostPlatform $postPlatform): array if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $content = $postPlatform->post->content ? app(ContentSanitizer::class)->sanitize($postPlatform->post->content, $postPlatform->platform) : null; @@ -407,7 +406,6 @@ public function getBoards(SocialAccount $account): array { if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $response = $this->socialHttp()->withToken($account->access_token) diff --git a/app/Services/Social/ThreadsAnalytics.php b/app/Services/Social/ThreadsAnalytics.php index 26258979..ef196477 100644 --- a/app/Services/Social/ThreadsAnalytics.php +++ b/app/Services/Social/ThreadsAnalytics.php @@ -48,7 +48,6 @@ public function fetchPostMetrics(PostPlatform $postPlatform): array if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $response = $this->socialHttp() @@ -80,7 +79,6 @@ private function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $si { if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $this->accessToken = $account->access_token; diff --git a/app/Services/Social/ThreadsPublisher.php b/app/Services/Social/ThreadsPublisher.php index 28d3d19a..c37402f1 100644 --- a/app/Services/Social/ThreadsPublisher.php +++ b/app/Services/Social/ThreadsPublisher.php @@ -31,7 +31,6 @@ public function publish(PostPlatform $postPlatform): array if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $userId = $account->platform_user_id; diff --git a/app/Services/Social/TikTokAnalytics.php b/app/Services/Social/TikTokAnalytics.php index a1a80d63..37861978 100644 --- a/app/Services/Social/TikTokAnalytics.php +++ b/app/Services/Social/TikTokAnalytics.php @@ -37,7 +37,6 @@ private function fetchMetricsFromApi(SocialAccount $account): array { if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $this->accessToken = $account->access_token; diff --git a/app/Services/Social/TikTokCreatorInfo.php b/app/Services/Social/TikTokCreatorInfo.php index 418316bd..7571d067 100644 --- a/app/Services/Social/TikTokCreatorInfo.php +++ b/app/Services/Social/TikTokCreatorInfo.php @@ -60,7 +60,6 @@ private function fetchFresh(SocialAccount $account): array { if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $this->accessToken = $account->access_token; diff --git a/app/Services/Social/TikTokPublisher.php b/app/Services/Social/TikTokPublisher.php index 554e3402..d26a8a8a 100644 --- a/app/Services/Social/TikTokPublisher.php +++ b/app/Services/Social/TikTokPublisher.php @@ -37,7 +37,6 @@ public function publish(PostPlatform $postPlatform): array if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $this->accessToken = $account->access_token; diff --git a/app/Services/Social/XAnalytics.php b/app/Services/Social/XAnalytics.php index 1b22c7e9..c562b2a8 100644 --- a/app/Services/Social/XAnalytics.php +++ b/app/Services/Social/XAnalytics.php @@ -48,7 +48,6 @@ private function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $si { if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $this->accessToken = $account->access_token; @@ -164,7 +163,6 @@ public function fetchPostMetrics(PostPlatform $postPlatform): array if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $this->accessToken = $account->access_token; diff --git a/app/Services/Social/XPublisher.php b/app/Services/Social/XPublisher.php index fefdda53..625a840b 100644 --- a/app/Services/Social/XPublisher.php +++ b/app/Services/Social/XPublisher.php @@ -38,7 +38,6 @@ public function publish(PostPlatform $postPlatform): array // Refresh token if expired or expiring soon if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $this->accessToken = $account->access_token; diff --git a/app/Services/Social/YouTubeAnalytics.php b/app/Services/Social/YouTubeAnalytics.php index 43d01248..c4eb8342 100644 --- a/app/Services/Social/YouTubeAnalytics.php +++ b/app/Services/Social/YouTubeAnalytics.php @@ -48,7 +48,6 @@ public function fetchPostMetrics(PostPlatform $postPlatform): array if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $this->accessToken = $account->access_token; @@ -100,7 +99,6 @@ private function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $si { if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $this->accessToken = $account->access_token; diff --git a/app/Services/Social/YouTubePublisher.php b/app/Services/Social/YouTubePublisher.php index b3a68fd6..5b4467c5 100644 --- a/app/Services/Social/YouTubePublisher.php +++ b/app/Services/Social/YouTubePublisher.php @@ -35,7 +35,6 @@ public function publish(PostPlatform $postPlatform): array if ($account->is_token_expired || $account->is_token_expiring_soon) { app(ConnectionVerifier::class)->refreshToken($account); - $account->refresh(); } $media = $postPlatform->post->mediaItems;