trypost/app/Http/Controllers/App/PostController.php

374 lines
13 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace App\Http\Controllers\App;
use App\Actions\Post\CreatePost;
use App\Actions\Post\DeletePost;
use App\Actions\Post\SyncPostPlatforms;
use App\Actions\Post\UpdatePost;
use App\Enums\Post\Action as PostAction;
use App\Enums\Post\Status as PostStatus;
use App\Enums\SocialAccount\Platform;
use App\Http\Requests\App\Post\UpdatePostRequest;
2026-04-23 16:23:24 +00:00
use App\Http\Resources\App\PlatformConfigResource;
use App\Http\Resources\App\SocialAccountResource;
use App\Models\Post;
use App\Models\PostPlatform;
use App\Services\Social\BlueskyAnalytics;
use App\Services\Social\FacebookAnalytics;
use App\Services\Social\InstagramAnalytics;
use App\Services\Social\LinkedInPageAnalytics;
use App\Services\Social\MastodonAnalytics;
use App\Services\Social\PinterestAnalytics;
use App\Services\Social\PinterestPublisher;
use App\Services\Social\ThreadsAnalytics;
2026-04-23 16:23:24 +00:00
use App\Services\Social\TikTokCreatorInfo;
use App\Services\Social\XAnalytics;
use App\Services\Social\YouTubeAnalytics;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Inertia\Inertia;
use Inertia\Response;
class PostController extends Controller
{
public function index(Request $request, ?string $status = null): Response|RedirectResponse
{
$workspace = $request->user()->currentWorkspace;
if (! $workspace) {
return redirect()->route('app.workspaces.create');
}
$this->authorize('view', $workspace);
$query = $workspace->posts()
->with(['postPlatforms' => fn ($query) => $query->where('enabled', true)->with('socialAccount'), 'user', 'labels']);
if ($status) {
$query = match ($status) {
PostStatus::Draft->value => $query->draft(),
PostStatus::Scheduled->value => $query->scheduled(),
PostStatus::Published->value => $query->published(),
default => $query,
};
}
2026-03-31 00:18:07 +00:00
if ($search = $request->input('search')) {
$query->where('content', 'ilike', "%{$search}%");
2026-03-31 00:18:07 +00:00
}
return Inertia::render('posts/Index', [
'workspace' => $workspace,
2026-03-31 00:18:07 +00:00
'posts' => Inertia::scroll(fn () => $query->latest('scheduled_at')->paginate(config('app.pagination.default'))),
'currentStatus' => $status,
2026-03-31 00:18:07 +00:00
'filters' => [
'search' => $request->input('search', ''),
],
]);
}
public function calendar(Request $request): Response|RedirectResponse
{
$workspace = $request->user()->currentWorkspace;
if (! $workspace) {
return redirect()->route('app.workspaces.create');
}
$this->authorize('view', $workspace);
$tz = 'UTC';
$view = $request->input('view', 'week');
$currentDay = $request->input('day')
? Carbon::parse($request->input('day'), $tz)->startOfDay()
: Carbon::now($tz)->startOfDay();
$weekStart = $request->input('week')
? Carbon::parse($request->input('week'), $tz)->startOfWeek()
: Carbon::now($tz)->startOfWeek();
$weekEnd = $weekStart->copy()->endOfWeek();
$monthDate = $request->input('month')
? Carbon::parse($request->input('month'), $tz)->startOfMonth()
: Carbon::now($tz)->startOfMonth();
$monthStart = $monthDate->copy()->startOfMonth()->startOfWeek();
$monthEnd = $monthDate->copy()->endOfMonth()->endOfWeek();
$rangeStart = match ($view) {
'day' => $currentDay,
'month' => $monthStart,
default => $weekStart,
};
$rangeEnd = match ($view) {
'day' => $currentDay->copy()->endOfDay(),
'month' => $monthEnd,
default => $weekEnd,
};
$posts = $workspace->posts()
->with(['postPlatforms' => fn ($query) => $query->where('enabled', true)->with('socialAccount')])
->whereBetween('scheduled_at', [$rangeStart->copy()->utc(), $rangeEnd->copy()->utc()])
->orderBy('scheduled_at')
->get()
->groupBy(fn ($post) => $post->scheduled_at?->setTimezone($tz)->format('Y-m-d'));
return Inertia::render('posts/Calendar', [
'workspace' => $workspace,
'posts' => $posts,
'currentDay' => $currentDay->format('Y-m-d'),
'currentWeekStart' => $weekStart->format('Y-m-d'),
'currentMonth' => $monthDate->format('Y-m-d'),
'view' => $view,
]);
}
public function create(Request $request): Response
{
$workspace = $request->user()->currentWorkspace;
$this->authorize('createPost', $workspace);
return Inertia::render('posts/Create', [
'date' => $request->query('date'),
'socialAccounts' => SocialAccountResource::collection(
$workspace->socialAccounts()->active()->get()
),
]);
}
public function store(Request $request): RedirectResponse|\Symfony\Component\HttpFoundation\Response
{
$workspace = $request->user()->currentWorkspace;
if (! $workspace) {
return redirect()->route('app.workspaces.create');
}
$this->authorize('createPost', $workspace);
2026-03-31 00:18:07 +00:00
$socialAccounts = $workspace->socialAccounts()->active()->get();
if ($socialAccounts->isEmpty()) {
session()->flash('flash.banner', __('posts.flash.connect_first'));
session()->flash('flash.bannerStyle', 'danger');
return redirect()->route('app.accounts');
}
$post = CreatePost::execute($workspace, $request->user(), [
'date' => $request->input('date'),
'media' => $request->input('media', []),
]);
return Inertia::location(route('app.posts.edit', $post));
}
public function platformMetrics(Request $request, Post $post, PostPlatform $postPlatform): JsonResponse
{
$workspace = $request->user()->currentWorkspace;
if (! $workspace || $post->workspace_id !== $workspace->id) {
abort(404);
}
$this->authorize('view', $workspace);
if ($postPlatform->post_id !== $post->id) {
abort(404);
}
if ($postPlatform->status->value !== 'published' || ! $postPlatform->platform_post_id) {
return response()->json(['unsupported' => true, 'reason' => 'not_published']);
}
$cacheKey = "post_metrics:{$postPlatform->id}";
$metrics = Cache::remember($cacheKey, 300, fn () => match ($postPlatform->platform) {
Platform::X => app(XAnalytics::class)->fetchPostMetrics($postPlatform),
Platform::Bluesky => app(BlueskyAnalytics::class)->fetchPostMetrics($postPlatform),
Platform::Mastodon => app(MastodonAnalytics::class)->fetchPostMetrics($postPlatform),
Platform::Instagram, Platform::InstagramFacebook => app(InstagramAnalytics::class)->fetchPostMetrics($postPlatform),
Platform::Facebook => app(FacebookAnalytics::class)->fetchPostMetrics($postPlatform),
Platform::Threads => app(ThreadsAnalytics::class)->fetchPostMetrics($postPlatform),
Platform::LinkedInPage => app(LinkedInPageAnalytics::class)->fetchPostMetrics($postPlatform),
Platform::YouTube => app(YouTubeAnalytics::class)->fetchPostMetrics($postPlatform),
Platform::Pinterest => app(PinterestAnalytics::class)->fetchPostMetrics($postPlatform),
default => ['unsupported' => true, 'reason' => 'platform_not_supported'],
});
return response()->json($metrics);
}
public function show(Request $request, Post $post): Response|RedirectResponse
{
$workspace = $request->user()->currentWorkspace;
if (! $workspace) {
return redirect()->route('app.workspaces.create');
}
$this->authorize('view', $workspace);
if ($post->workspace_id !== $workspace->id) {
abort(404);
}
if (in_array($post->status, [PostStatus::Draft, PostStatus::Scheduled, PostStatus::Failed], true)) {
return redirect()->route('app.posts.edit', $post);
}
$post->load(['postPlatforms.socialAccount', 'labels']);
return Inertia::render('posts/Show', [
'workspace' => $workspace,
'post' => $post,
]);
}
public function edit(Request $request, Post $post): Response|RedirectResponse
{
$workspace = $request->user()->currentWorkspace;
if (! $workspace) {
return redirect()->route('app.workspaces.create');
}
$this->authorize('view', $workspace);
if ($post->workspace_id !== $workspace->id) {
abort(404);
}
if (in_array($post->status, [PostStatus::Publishing, PostStatus::Published, PostStatus::PartiallyPublished], true)) {
return redirect()->route('app.posts.show', $post);
}
SyncPostPlatforms::execute($post);
$post->load(['postPlatforms.socialAccount', 'labels']);
2026-03-31 00:18:07 +00:00
$socialAccounts = $workspace->socialAccounts()->active()->get();
$labels = $workspace->labels;
$hashtags = $workspace->hashtags;
$platformConfigs = $socialAccounts->mapWithKeys(fn ($account) => [
2026-04-23 16:23:24 +00:00
$account->id => new PlatformConfigResource($account),
]);
$pinterestBoards = [];
$pinterestAccount = $socialAccounts->firstWhere('platform', Platform::Pinterest);
if ($pinterestAccount) {
try {
$pinterestBoards = app(PinterestPublisher::class)->getBoards($pinterestAccount);
} catch (\Exception $e) {
// Silently fail - boards will be empty
}
}
2026-04-23 16:23:24 +00:00
$tiktokAccounts = $socialAccounts->where('platform', Platform::TikTok);
return Inertia::render('posts/Edit', [
'workspace' => $workspace,
'post' => $post,
'socialAccounts' => $socialAccounts,
'platformConfigs' => $platformConfigs,
'pinterestBoards' => $pinterestBoards,
2026-04-23 16:23:24 +00:00
'tiktokCreatorInfos' => Inertia::defer(fn () => $tiktokAccounts->mapWithKeys(fn ($account) => [
$account->id => rescue(
fn () => app(TikTokCreatorInfo::class)->fetch($account),
null,
report: false,
),
])->filter()),
'labels' => $labels,
'hashtags' => $hashtags,
'authUserId' => $request->user()->id,
]);
}
public function update(UpdatePostRequest $request, Post $post): RedirectResponse
{
$workspace = $request->user()->currentWorkspace;
if (! $workspace) {
return redirect()->route('app.workspaces.create');
}
$this->authorize('createPost', $workspace);
if ($post->workspace_id !== $workspace->id) {
abort(404);
}
$result = UpdatePost::execute($workspace, $post, $request->validated());
$action = data_get($result, 'action');
if ($action === PostAction::AlreadyPublished) {
session()->flash('flash.banner', __('posts.flash.cannot_edit_published'));
session()->flash('flash.bannerStyle', 'danger');
return back();
}
if ($action === PostAction::Publishing) {
session()->flash('flash.banner', __('posts.flash.publishing'));
session()->flash('flash.bannerStyle', 'success');
return redirect()->route('app.posts.show', $post);
}
if ($action === PostAction::Scheduled) {
session()->flash('flash.banner', __('posts.flash.scheduled'));
session()->flash('flash.bannerStyle', 'success');
return redirect()->route('app.posts.show', $post);
}
return back();
}
public function destroy(Request $request, Post $post): RedirectResponse
{
$workspace = $request->user()->currentWorkspace;
if (! $workspace) {
return redirect()->route('app.workspaces.create');
}
$this->authorize('createPost', $workspace);
if ($post->workspace_id !== $workspace->id) {
abort(404);
}
if (in_array($post->status, [PostStatus::Publishing, PostStatus::Published, PostStatus::PartiallyPublished], true)) {
session()->flash('flash.banner', __('posts.flash.cannot_delete_published'));
session()->flash('flash.bannerStyle', 'danger');
return back();
}
DeletePost::execute($post);
session()->flash('flash.banner', __('posts.flash.deleted'));
session()->flash('flash.bannerStyle', 'success');
$allowedRedirects = ['app.posts.index', 'app.calendar'];
if ($redirect = $request->input('redirect')) {
if (in_array($redirect, $allowedRedirects)) {
return redirect()->route($redirect);
}
}
return redirect()->route('app.posts.index');
}
}