Publishing improvements: - Create PostPlatformStatus enum (Pending, Publishing, Published, Failed) - Update PostPlatform model, jobs, factories to use enum - Add PostPublishFailed email notification when post fails to publish - Maizzle template + blade for failure email with platform details - PublishPost job: add $tries=3, $backoff=30, failed() method - Fix broadcast event to serialize enum status value Security: - Add rate limiting (throttle:6,1) on social connect endpoints - Fix MediaController::reorder IDOR vulnerability - Fix Connect.vue broken import (storeStep2 -> storeConnect) - Fix UpdatePost data_get() consistency Database: - Add composite index on post_platforms (post_id, enabled) - Add index on post_platforms (social_account_id) Tests: - Add 3 tests for profile photo upload/delete - Add 2 tests for media reorder (including IDOR check) - Fix publish tests for PostPlatformStatus enum - Add Mail::fake() to publish tests Cleanup: - Remove unused AppHeader.vue and AppHeaderLayout.vue - Remove dead BillingController methods All 733 tests passing.
141 lines
5.1 KiB
PHP
141 lines
5.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Enums\PostPlatform\Status as PostPlatformStatus;
|
|
use App\Enums\SocialAccount\Platform as SocialPlatform;
|
|
use App\Events\PostPlatformStatusUpdated;
|
|
use App\Exceptions\TokenExpiredException;
|
|
use App\Mail\PostPublishFailed;
|
|
use App\Models\Post;
|
|
use App\Models\PostPlatform;
|
|
use App\Services\Social\BlueskyPublisher;
|
|
use App\Services\Social\FacebookPublisher;
|
|
use App\Services\Social\InstagramPublisher;
|
|
use App\Services\Social\LinkedInPagePublisher;
|
|
use App\Services\Social\LinkedInPublisher;
|
|
use App\Services\Social\MastodonPublisher;
|
|
use App\Services\Social\PinterestPublisher;
|
|
use App\Services\Social\ThreadsPublisher;
|
|
use App\Services\Social\TikTokPublisher;
|
|
use App\Services\Social\XPublisher;
|
|
use App\Services\Social\YouTubePublisher;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
class PublishToSocialPlatform implements ShouldQueue
|
|
{
|
|
use Queueable;
|
|
|
|
public int $tries = 3;
|
|
|
|
public int $backoff = 60;
|
|
|
|
public function __construct(public PostPlatform $postPlatform) {}
|
|
|
|
public function handle(): void
|
|
{
|
|
if ($this->postPlatform->socialAccount->isDisconnected()) {
|
|
$this->postPlatform->markAsFailed(__('posts.errors.account_disconnected'));
|
|
$this->updatePostStatus();
|
|
$this->broadcastStatus();
|
|
|
|
return;
|
|
}
|
|
|
|
$this->postPlatform->markAsPublishing();
|
|
$this->broadcastStatus();
|
|
|
|
try {
|
|
$publisher = $this->getPublisher();
|
|
$result = $publisher->publish($this->postPlatform);
|
|
|
|
$this->postPlatform->markAsPublished($result['id'], $result['url'] ?? null);
|
|
} catch (TokenExpiredException $e) {
|
|
Log::error('Token expired while publishing to social platform', [
|
|
'post_platform_id' => $this->postPlatform->id,
|
|
'platform' => $this->postPlatform->platform->value,
|
|
'error' => $e->getMessage(),
|
|
'platform_error_code' => $e->platformErrorCode,
|
|
]);
|
|
|
|
$this->postPlatform->markAsFailed($e->getMessage());
|
|
$this->postPlatform->socialAccount->markAsDisconnected($e->getMessage());
|
|
} catch (\Throwable $e) {
|
|
Log::error('Failed to publish to social platform', [
|
|
'post_platform_id' => $this->postPlatform->id,
|
|
'platform' => $this->postPlatform->platform->value,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
|
|
$this->postPlatform->markAsFailed($e->getMessage());
|
|
}
|
|
|
|
// Always check and update post status after each platform finishes
|
|
$this->updatePostStatus();
|
|
|
|
// Broadcast final status
|
|
$this->broadcastStatus();
|
|
}
|
|
|
|
private function broadcastStatus(): void
|
|
{
|
|
PostPlatformStatusUpdated::dispatch($this->postPlatform->fresh());
|
|
}
|
|
|
|
private function getPublisher(): LinkedInPublisher|LinkedInPagePublisher|XPublisher|TikTokPublisher|YouTubePublisher|FacebookPublisher|InstagramPublisher|ThreadsPublisher|PinterestPublisher|BlueskyPublisher|MastodonPublisher
|
|
{
|
|
return match ($this->postPlatform->platform) {
|
|
SocialPlatform::LinkedIn => app(LinkedInPublisher::class),
|
|
SocialPlatform::LinkedInPage => app(LinkedInPagePublisher::class),
|
|
SocialPlatform::X => app(XPublisher::class),
|
|
SocialPlatform::TikTok => app(TikTokPublisher::class),
|
|
SocialPlatform::YouTube => app(YouTubePublisher::class),
|
|
SocialPlatform::Facebook => app(FacebookPublisher::class),
|
|
SocialPlatform::Instagram => app(InstagramPublisher::class),
|
|
SocialPlatform::Threads => app(ThreadsPublisher::class),
|
|
SocialPlatform::Pinterest => app(PinterestPublisher::class),
|
|
SocialPlatform::Bluesky => app(BlueskyPublisher::class),
|
|
SocialPlatform::Mastodon => app(MastodonPublisher::class),
|
|
};
|
|
}
|
|
|
|
private function updatePostStatus(): void
|
|
{
|
|
$post = $this->postPlatform->post->fresh();
|
|
$enabledPlatforms = $post->postPlatforms->where('enabled', true);
|
|
|
|
$total = $enabledPlatforms->count();
|
|
$publishedCount = $enabledPlatforms->where('status', PostPlatformStatus::Published)->count();
|
|
$failedCount = $enabledPlatforms->where('status', PostPlatformStatus::Failed)->count();
|
|
$finishedCount = $publishedCount + $failedCount;
|
|
|
|
// Only update post status when all platforms have finished
|
|
if ($finishedCount < $total) {
|
|
return;
|
|
}
|
|
|
|
if ($publishedCount === $total) {
|
|
$post->markAsPublished();
|
|
} elseif ($publishedCount > 0) {
|
|
$post->markAsPartiallyPublished();
|
|
$this->notifyOwner($post);
|
|
} else {
|
|
$post->markAsFailed();
|
|
$this->notifyOwner($post);
|
|
}
|
|
}
|
|
|
|
private function notifyOwner(Post $post): void
|
|
{
|
|
$owner = $post->workspace->owner;
|
|
|
|
if ($owner) {
|
|
Mail::to($owner)->send(new PostPublishFailed($post));
|
|
}
|
|
}
|
|
}
|