trypost/app/Http/Controllers/Auth/SocialController.php
Paulo Castellano cbf8fb283c feat: per-workspace pricing, onboarding, and billing overhaul
Pricing
- Bill per workspace ($12/mo or $120/yr each); Stripe quantity tracks the
  workspace count and syncs on workspace create/delete.
- 2,500 AI credits per workspace, pooled at the account level; monthly reset
  on the billing anniversary, annual granted upfront (no rollover).
- One social account per network per workspace; remove all count-based limits
  (workspace/social/member) and the legacy plan tiers (single Workspace plan).

Onboarding (cloud only: SELF_HOSTED=false + PostHog)
- Replace the /subscribe plan picker with /onboarding persona selection
  (Creator/Freelancer/Startup/Agency/Small business/Other), saved on the user
  (users.persona) and mirrored to PostHog, then Stripe Checkout on the monthly
  price. 8-day trial so Stripe displays 7.

Billing screen
- Remove the Change Plan dialog (dead with a single plan); add an annual-upgrade
  banner for monthly subscribers (swapToYearly).
- Current-plan card shows the workspace count instead of the plan name.

System AI
- Brand analyzer / workspace autofill is always allowed and never debits credits
  (system feature, not the user's usage).

Self-hosted (SELF_HOSTED=true) bypasses all billing, credit, limit, network,
and onboarding logic.
2026-06-21 20:40:03 -03:00

223 lines
7.6 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('view', $workspace);
$accounts = $workspace->socialAccounts()
->when(
$request->input('search'),
fn ($query, $search) => $query->where(function ($q) use ($search): void {
$q->where('display_name', 'ilike', "%{$search}%")
->orWhere('username', 'ilike', "%{$search}%")
->orWhere('platform', 'ilike', "%{$search}%");
}),
)
->orderBy('id')
->paginate(config('app.pagination.default'));
$platforms = collect(SocialPlatform::enabled())->map(fn ($platform) => [
'value' => $platform->value,
'label' => $platform->label(),
'color' => $platform->color(),
])->values();
return Inertia::render('accounts/Index', [
'workspace' => $workspace,
'accounts' => Inertia::scroll(fn () => SocialAccountResource::collection($accounts)),
'platforms' => $platforms,
'filters' => [
'search' => $request->input('search', ''),
],
'openDialog' => $request->boolean('openDialog'),
]);
}
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]);
session(['social_connect_onboarding' => $request->boolean('onboarding')]);
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', 'social_connect_onboarding']);
}
protected function getRedirectRoute(): string
{
return session('social_connect_onboarding', false) ? 'onboarding.connect' : 'accounts';
}
/**
* 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,
]);
}
}