trypost/app/Http/Controllers/Auth/BlueskyController.php
Paulo Castellano 12a1baa757 refactor(social): address PR review follow-ups
- DeleteWorkspace now reassigns affected users to another workspace they
  belong to (instead of nulling current_workspace_id), so deleting the
  current workspace while owning others no longer bounces to "create".
- Remove the dead LINKEDIN_PAGE_CLIENT_REDIRECT env (the unified flow uses
  a single callback; config no longer reads a page redirect).
- useOAuthPopup uses the Wayfinder connect helpers per platform instead of
  hardcoding /connect/{platform}.
- Unify the Inertia\Response alias in Mastodon/Bluesky controllers to
  InertiaResponse, matching the other connect controllers.
- Add tests: workspace reassignment on delete, and independent refresh of
  a linkedin row vs its linkedin-page sibling.
2026-06-25 15:01:23 -03:00

118 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Auth;
use App\Enums\SocialAccount\Platform as SocialPlatform;
use App\Enums\SocialAccount\Status;
use App\Services\Social\BlueskyLexicon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
use Inertia\Inertia;
use Inertia\Response as InertiaResponse;
class BlueskyController extends SocialController
{
protected SocialPlatform $platform = SocialPlatform::Bluesky;
public function connect(Request $request): InertiaResponse
{
$this->ensurePlatformEnabled();
$workspace = $request->user()->currentWorkspace;
$this->authorize('manageAccounts', $workspace);
return Inertia::render('accounts/BlueskyConnect', [
'errors' => session('errors')?->getBag('default')?->toArray() ?? [],
]);
}
public function store(Request $request): InertiaResponse
{
$this->ensurePlatformEnabled();
$request->validate([
'identifier' => 'required|string',
'password' => 'required|string|min:3',
]);
$workspace = $request->user()->currentWorkspace;
$this->authorize('manageAccounts', $workspace);
$service = config('trypost.platforms.bluesky.default_service');
try {
// Authenticate with Bluesky
$response = Http::post("{$service}/xrpc/".BlueskyLexicon::CREATE_SESSION, [
'identifier' => $request->identifier,
'password' => $request->password,
]);
if ($response->failed()) {
Log::error('Bluesky authentication failed', [
'status' => $response->status(),
'body' => $response->body(),
]);
$errorMessage = 'Invalid credentials';
$body = $response->json();
if (isset($body['message'])) {
$errorMessage = $body['message'];
}
throw ValidationException::withMessages(['password' => $errorMessage]);
}
$data = $response->json();
// Get profile
$profileResponse = Http::withToken(data_get($data, 'accessJwt'))
->get("{$service}/xrpc/".BlueskyLexicon::GET_PROFILE, [
'actor' => data_get($data, 'did'),
]);
$profile = $profileResponse->successful() ? $profileResponse->json() : [];
$avatarPath = data_get($profile, 'avatar') ? uploadFromUrl(data_get($profile, 'avatar')) : null;
$workspace->socialAccounts()->updateOrCreate(
[
'platform' => $this->platform->value,
'platform_user_id' => data_get($data, 'did'),
],
[
'username' => data_get($data, 'handle'),
'display_name' => data_get($profile, 'displayName', data_get($data, 'handle')),
'avatar_url' => $avatarPath,
'access_token' => data_get($data, 'accessJwt'),
'refresh_token' => data_get($data, 'refreshJwt'),
'token_expires_at' => now()->addHours(2),
'status' => Status::Connected,
'error_message' => null,
'disconnected_at' => null,
'meta' => [
'service' => $service,
'identifier' => $request->identifier,
'password' => encrypt($request->password),
],
],
);
return $this->popupCallback(true, __('accounts.popup_callback.connected'), $this->platform->value);
} catch (ValidationException $e) {
throw $e;
} catch (\Exception $e) {
Log::error('Bluesky connection error', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
throw ValidationException::withMessages(['password' => 'Error connecting to Bluesky. Please try again.']);
}
}
}