trypost/app/Http/Controllers/Auth/SocialController.php
Paulo Castellano d84666360a refactor(linkedin): infer post format from media + unify account connection
Collapse LinkedIn to one content type per account kind (linkedin_post, linkedin_page_post). Publishers infer the publish format from the attached media — text, single image/video, multi-image carousel, or PDF document — matching how facebook_post/x_post already work; PDF is exclusive of any other attachment. Removes the editor variant picker, keeping only the PDF document title field. Includes a data migration collapsing the retired carousel/document content types.

Replace the two LinkedIn account cards with a single Connect LinkedIn button: one unified OAuth grant (linkedin-openid driver, union of scopes) then a post-callback identity picker to post as the personal profile (linkedin) or a company page the member administers (linkedin-page). The chosen organization is validated against the admin-verified list from the OAuth grant. Per-capability gating via LINKEDIN_ENABLED / LINKEDIN_PAGE_ENABLED supports profile-only or org-only self-hosting. Removes LinkedInPageController, LinkedInTokenSynchronizer, the standalone linkedin-page connect routes, and the unused redirect_page config.
2026-06-24 21:05:09 -03:00

206 lines
6.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Auth;
use App\Actions\SocialAccount\ToggleSocialAccount;
use App\Enums\PostPlatform\Status as PostPlatformStatus;
use App\Enums\SocialAccount\Platform as SocialPlatform;
use App\Enums\SocialAccount\Status;
use App\Exceptions\SocialAccount\NetworkAlreadyConnectedException;
use App\Http\Controllers\Controller;
use App\Http\Resources\App\SocialAccountResource;
use App\Models\SocialAccount;
use App\Models\Workspace;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;
use Inertia\Inertia;
use Inertia\Response;
use Laravel\Socialite\Facades\Socialite;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
class SocialController extends Controller
{
protected SocialPlatform $platform;
protected function ensurePlatformEnabled(): void
{
if (isset($this->platform) && ! $this->platform->isEnabled()) {
abort(SymfonyResponse::HTTP_FORBIDDEN, 'This platform is currently unavailable.');
}
}
public function index(Request $request): Response|RedirectResponse
{
$workspace = $request->user()->currentWorkspace;
if (! $workspace) {
return redirect()->route('app.workspaces.create');
}
$this->authorize('manageAccounts', $workspace);
$platforms = collect(SocialPlatform::cases())
->filter(fn ($platform) => $platform->isConnectable())
->map(fn ($platform) => [
'value' => $platform->value,
'label' => $platform->label(),
'color' => $platform->color(),
'network' => $platform->network(),
])->values();
return Inertia::render('accounts/Index', [
'workspace' => $workspace,
'platforms' => $platforms,
'connectedAccounts' => SocialAccountResource::collection(
$workspace->socialAccounts()->orderBy('id')->get(),
)->resolve(),
]);
}
public function disconnect(Request $request, SocialAccount $account): RedirectResponse
{
$workspace = $request->user()->currentWorkspace;
if (! $workspace) {
return redirect()->route('app.workspaces.create');
}
$this->authorize('manageAccounts', $workspace);
if ($account->workspace_id !== $workspace->id) {
abort(403);
}
// Drop pending platform rows from drafts/scheduled posts so the account
// disappears cleanly from their UI. Published/failed rows survive via the
// FK's nullOnDelete cascade and keep their snapshot fields for history.
$account->postPlatforms()
->where('status', PostPlatformStatus::Pending->value)
->delete();
$account->delete();
session()->flash('flash.banner', __('accounts.flash.disconnected'));
session()->flash('flash.bannerStyle', 'success');
return back();
}
public function toggleActive(Request $request, SocialAccount $account): RedirectResponse
{
$workspace = $request->user()->currentWorkspace;
if (! $workspace) {
return redirect()->route('app.workspaces.create');
}
$this->authorize('manageAccounts', $workspace);
if ($account->workspace_id !== $workspace->id) {
abort(403);
}
ToggleSocialAccount::execute($account);
$status = $account->is_active ? 'activated' : 'deactivated';
session()->flash('flash.banner', __("accounts.flash.{$status}"));
session()->flash('flash.bannerStyle', 'success');
return back();
}
protected function redirectToProvider(Request $request, string $driver, array $scopes): SymfonyResponse
{
$workspace = $request->user()->currentWorkspace;
if (! $workspace) {
return redirect()->route('app.workspaces.create');
}
session(['social_connect_workspace' => $workspace->id]);
return Inertia::location(
Socialite::driver($driver)
->scopes($scopes)
->redirect()
->getTargetUrl()
);
}
protected function handleCallback(
Request $request,
SocialPlatform $platform,
string $driver
): View {
$workspaceId = session('social_connect_workspace');
if (! $workspaceId) {
return $this->popupCallback(false, __('accounts.popup_callback.session_expired'), $platform->value);
}
$workspace = Workspace::find($workspaceId);
if (! $workspace || ! $request->user()->can('manageAccounts', $workspace)) {
return $this->popupCallback(false, __('accounts.popup_callback.workspace_not_found'), $platform->value);
}
try {
$socialUser = Socialite::driver($driver)->user();
$avatarPath = uploadFromUrl($socialUser->getAvatar());
$workspace->socialAccounts()->updateOrCreate(
[
'platform' => $platform->value,
'platform_user_id' => $socialUser->getId(),
],
[
'username' => $socialUser->getNickname(),
'display_name' => $socialUser->getName(),
'avatar_url' => $avatarPath,
'access_token' => $socialUser->token,
'refresh_token' => $socialUser->refreshToken,
'token_expires_at' => $socialUser->expiresIn ? now()->addSeconds($socialUser->expiresIn) : null,
'scopes' => $socialUser->approvedScopes ?? null,
'status' => Status::Connected,
'error_message' => null,
'disconnected_at' => null,
],
);
return $this->popupCallback(true, __('accounts.popup_callback.connected'), $platform->value);
} catch (NetworkAlreadyConnectedException) {
return $this->popupCallback(false, __('accounts.popup_callback.network_taken'), $platform->value);
} catch (\Exception $e) {
Log::error('Social OAuth Error', [
'platform' => $platform->value,
'error' => $e->getMessage(),
]);
return $this->popupCallback(false, __('accounts.popup_callback.error_connecting'), $platform->value);
}
}
protected function forgetSocialConnectSession(): void
{
session()->forget('social_connect_workspace');
}
/**
* Return a view that closes the popup and notifies the parent window.
*/
protected function popupCallback(bool $success, string $message, ?string $platform = null): View
{
$this->forgetSocialConnectSession();
return view('auth.social-callback', [
'success' => $success,
'message' => $message,
'platform' => $platform,
]);
}
}