trypost/app/Services/Social/MastodonAnalytics.php
Paulo Castellano 1660084227 refactor(social): use config for OAuth hosts everywhere
Earlier in the PR the new configs (linkedin.oauth_api, youtube.oauth_api,
bluesky.default_service, mastodon.default_instance) were only read by
ConnectionVerifier. The same URLs were still hardcoded in the publishers,
analytics and the Bluesky auth controller — meaning a self-hosted user
setting BLUESKY_DEFAULT_SERVICE or MASTODON_DEFAULT_INSTANCE in env would
get split behavior: refresh/verify honor the override, publish/analytics
don't.

Routes all 10 remaining call sites through the same config values so the
overrides actually work end-to-end.
2026-05-19 08:34:35 -03:00

47 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Social;
use App\Models\PostPlatform;
use App\Services\Social\Concerns\HasSocialHttpClient;
use Illuminate\Support\Facades\Log;
class MastodonAnalytics
{
use HasSocialHttpClient;
public function fetchPostMetrics(PostPlatform $postPlatform): array
{
$account = $postPlatform->socialAccount;
if (! $account || ! $postPlatform->platform_post_id) {
return ['unsupported' => true, 'reason' => 'missing_post_id'];
}
$instance = data_get($account->meta, 'instance', config('trypost.platforms.mastodon.default_instance'));
// Public posts: no auth needed. Our token only requests write scopes
// (read:accounts + write:statuses + write:media), so attaching the
// Bearer would 403 with "outside the authorized scopes".
$response = $this->socialHttp()
->get("{$instance}/api/v1/statuses/{$postPlatform->platform_post_id}");
if ($response->failed()) {
Log::warning('Mastodon post metrics fetch failed', [
'body' => $this->redactResponseBody($response->body()),
]);
return ['unsupported' => true, 'reason' => 'api_error'];
}
$status = $response->json();
return [
['label' => __('analytics.metrics.favourites'), 'value' => data_get($status, 'favourites_count', 0)],
['label' => __('analytics.metrics.reblogs'), 'value' => data_get($status, 'reblogs_count', 0)],
['label' => __('analytics.metrics.replies'), 'value' => data_get($status, 'replies_count', 0)],
];
}
}