Skip to content

Commit bc0d1f7

Browse files
author
Paulo Castellano
committed
refactor: centralize outbound User-Agent in config('app.user_agent')
1 parent 2dee606 commit bc0d1f7

17 files changed

Lines changed: 30 additions & 36 deletions

‎.env.example‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ REDDIT_ENABLED=true
174174
REDDIT_CLIENT_ID=
175175
REDDIT_CLIENT_SECRET=
176176
REDDIT_CLIENT_REDIRECT="${APP_URL}/accounts/reddit/callback"
177-
# Optional — defaults to TRYPOST_USER_AGENT. Reddit recommends "platform:appid:version (by /u/username)".
178-
# REDDIT_USER_AGENT=
179177
REDDIT_SCOPES="identity,read,submit,flair,mysubreddits"
180178

181179
# AI Services

‎CLAUDE.md‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,7 @@ Vue components must have a single root element.
338338

339339
## HTTP User-Agent
340340

341-
- NEVER hardcode a custom `User-Agent` string for outbound HTTP. There is ONE shared default: `config('trypost.user_agent')` (`env('TRYPOST_USER_AGENT', ...)`). Use it for any outbound request that needs a UA.
342-
- If a provider REQUIRES its own UA (e.g. Reddit's API mandates a unique descriptive User-Agent), add a per-platform key under `config/trypost.php` `platforms.<name>.user_agent` that DEFAULTS to the shared one — `env('<PLATFORM>_USER_AGENT', $userAgent)` where `$userAgent` is the hoisted shared value — never a fresh hardcoded literal.
341+
- NEVER hardcode a `User-Agent` string for outbound HTTP, and NEVER add a per-platform or per-feature user-agent config key. There is ONE source of truth: `config('app.user_agent')` (backed by `env('TRYPOST_USER_AGENT', ...)` in `config/app.php`). Every outbound request that needs a UA — automation webhook/http nodes, social provider APIs (Reddit, etc.), anything — reads `config('app.user_agent')`.
343342

344343
## TryPost.it Documentation
345344

‎app/Actions/Automation/Node/RunHttpRequestNode.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ private function buildRequest(array $config, array $context): PendingRequest
364364
$request = $request->withHeaders($headers);
365365
}
366366

367-
return $request->withUserAgent(config('trypost.user_agent'));
367+
return $request->withUserAgent(config('app.user_agent'));
368368
}
369369

370370
/**

‎app/Actions/Automation/Node/RunWebhookNode.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function __invoke(AutomationRun $run, array $config): NodeRunResult
7474

7575
try {
7676
$response = Http::withHeaders($headers)
77-
->withUserAgent(config('trypost.user_agent'))
77+
->withUserAgent(config('app.user_agent'))
7878
->send($method, $url, ['json' => $payload]);
7979
} catch (Throwable $e) {
8080
return NodeRunResult::failed(__('automations.errors.webhook_request_failed'), [

‎app/Services/Social/ConnectionVerifier.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ private function verifyMastodon(SocialAccount $account): bool
543543
private function verifyReddit(SocialAccount $account): bool
544544
{
545545
$response = Http::withToken((string) $account->access_token)
546-
->withHeaders(['User-Agent' => (string) config('trypost.platforms.reddit.user_agent')])
546+
->withHeaders(['User-Agent' => (string) config('app.user_agent')])
547547
->get(config('trypost.platforms.reddit.api').'/api/v1/me');
548548

549549
if (in_array($response->status(), [401, 403], true)) {
@@ -561,7 +561,7 @@ private function refreshRedditToken(SocialAccount $account): void
561561

562562
$response = TokenRefreshClient::for(Platform::Reddit)->send(fn () => Http::asForm()
563563
->withBasicAuth((string) config('services.reddit.client_id'), (string) config('services.reddit.client_secret'))
564-
->withHeaders(['User-Agent' => (string) config('trypost.platforms.reddit.user_agent')])
564+
->withHeaders(['User-Agent' => (string) config('app.user_agent')])
565565
->post(config('trypost.platforms.reddit.oauth_api').'/access_token', [
566566
'grant_type' => 'refresh_token',
567567
'refresh_token' => $account->refresh_token,

‎app/Services/Social/Reddit/RedditClient.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,6 @@ private function reddit(SocialAccount $account): PendingRequest
135135
{
136136
return $this->socialHttp()
137137
->withToken((string) $account->access_token)
138-
->withHeaders(['User-Agent' => (string) config('trypost.platforms.reddit.user_agent')]);
138+
->withHeaders(['User-Agent' => (string) config('app.user_agent')]);
139139
}
140140
}

‎app/Services/Social/Reddit/RedditPublisher.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,6 @@ private function reddit(SocialAccount $account): PendingRequest
312312
{
313313
return $this->socialHttp()
314314
->withToken((string) $account->access_token)
315-
->withHeaders(['User-Agent' => (string) config('trypost.platforms.reddit.user_agent')]);
315+
->withHeaders(['User-Agent' => (string) config('app.user_agent')]);
316316
}
317317
}

‎app/Socialite/RedditProvider.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function getAccessTokenResponse($code): array
5050
'auth' => [config('services.reddit.client_id'), config('services.reddit.client_secret')],
5151
'headers' => [
5252
'Accept' => 'application/json',
53-
'User-Agent' => config('trypost.platforms.reddit.user_agent'),
53+
'User-Agent' => config('app.user_agent'),
5454
],
5555
'form_params' => [
5656
'grant_type' => 'authorization_code',
@@ -70,7 +70,7 @@ protected function getUserByToken($token): array
7070
$response = $this->getHttpClient()->get(config('trypost.platforms.reddit.api').'/api/v1/me', [
7171
'headers' => [
7272
'Authorization' => "Bearer {$token}",
73-
'User-Agent' => config('trypost.platforms.reddit.user_agent'),
73+
'User-Agent' => config('app.user_agent'),
7474
],
7575
]);
7676

‎config/app.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@
6969

7070
'webhook_url' => env('WEBHOOK_URL', env('APP_URL', 'https://app.trypost.it')),
7171

72+
/*
73+
|--------------------------------------------------------------------------
74+
| Outbound User-Agent
75+
|--------------------------------------------------------------------------
76+
|
77+
| Single, branded User-Agent for every outbound HTTP request the app makes
78+
| (automation webhook/http nodes, social provider APIs, etc.). Centralized
79+
| here so there is one source of truth; self-hosters can override it.
80+
|
81+
*/
82+
83+
'user_agent' => env('TRYPOST_USER_AGENT', 'TryPost.it/1.0 (+https://trypost.it)'),
84+
7285
/*
7386
|--------------------------------------------------------------------------
7487
| Application Timezone

‎config/trypost.php‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
declare(strict_types=1);
44

5-
$userAgent = env('TRYPOST_USER_AGENT', 'TryPost.it/1.0 (+https://trypost.it)');
6-
75
return [
86

97
/*
@@ -61,19 +59,6 @@
6159
|
6260
*/
6361

64-
/*
65-
|--------------------------------------------------------------------------
66-
| Outbound User-Agent
67-
|--------------------------------------------------------------------------
68-
|
69-
| Branded User-Agent applied to outbound HTTP from automation nodes
70-
| (webhook + http_request) so recipients know the request came from
71-
| TryPost.it. Self-hosters can override it.
72-
|
73-
*/
74-
75-
'user_agent' => $userAgent,
76-
7762
'google_auth_enabled' => env('GOOGLE_AUTH_ENABLED', false),
7863

7964
'github_auth_enabled' => env('GITHUB_AUTH_ENABLED', false),
@@ -182,7 +167,6 @@
182167
'enabled' => env('REDDIT_ENABLED', true),
183168
'api' => env('REDDIT_API', 'https://oauth.reddit.com'),
184169
'oauth_api' => env('REDDIT_OAUTH_API', 'https://www.reddit.com/api/v1'),
185-
'user_agent' => env('REDDIT_USER_AGENT', $userAgent),
186170
'scopes' => array_values(array_filter(array_map('trim', explode(',', (string) env('REDDIT_SCOPES', 'identity,read,submit,flair,mysubreddits'))))),
187171
],
188172
],

0 commit comments

Comments
 (0)