2026-05-04 01:37:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Actions\Post;
|
|
|
|
|
|
2026-07-24 14:30:15 +00:00
|
|
|
use App\Enums\Post\CreatedVia;
|
2026-05-04 01:37:51 +00:00
|
|
|
use App\Enums\Post\Status as PostStatus;
|
|
|
|
|
use App\Enums\PostPlatform\Status as PostPlatformStatus;
|
|
|
|
|
use App\Models\Post;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-08-03 16:04:42 +00:00
|
|
|
* Clones a Post (and its still-connected platform rows + label associations)
|
|
|
|
|
* into a fresh Draft. The new post is owned by the actor and unscheduled —
|
|
|
|
|
* the user picks a new date in the editor.
|
|
|
|
|
*
|
|
|
|
|
* Only platform rows with a still-existing social account are copied
|
|
|
|
|
* (`whereHas('socialAccount')`), so orphan history rows left after disconnect
|
|
|
|
|
* never land on the new draft.
|
2026-05-04 01:37:51 +00:00
|
|
|
*/
|
|
|
|
|
class DuplicatePost
|
|
|
|
|
{
|
|
|
|
|
public static function execute(Post $original, User $user): Post
|
|
|
|
|
{
|
2026-07-24 15:01:01 +00:00
|
|
|
return DB::transaction(function () use ($original, $user): Post {
|
2026-05-04 01:37:51 +00:00
|
|
|
$copy = $original->workspace->posts()->create([
|
|
|
|
|
'user_id' => $user->id,
|
|
|
|
|
'content' => $original->content,
|
|
|
|
|
'media' => $original->media,
|
|
|
|
|
'status' => PostStatus::Draft,
|
2026-07-24 14:30:15 +00:00
|
|
|
'created_via' => CreatedVia::Web,
|
2026-05-04 01:37:51 +00:00
|
|
|
'scheduled_at' => null,
|
|
|
|
|
'published_at' => null,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-08-03 16:04:42 +00:00
|
|
|
$platforms = $original->postPlatforms()
|
|
|
|
|
->whereHas('socialAccount')
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
foreach ($platforms as $platform) {
|
2026-05-04 01:37:51 +00:00
|
|
|
$copy->postPlatforms()->create([
|
|
|
|
|
'social_account_id' => $platform->social_account_id,
|
|
|
|
|
'platform' => $platform->platform,
|
|
|
|
|
'platform_name' => $platform->platform_name,
|
|
|
|
|
'platform_username' => $platform->platform_username,
|
|
|
|
|
'platform_avatar' => $platform->getRawOriginal('platform_avatar'),
|
|
|
|
|
'content_type' => $platform->content_type,
|
|
|
|
|
'enabled' => $platform->enabled,
|
|
|
|
|
'meta' => $platform->meta,
|
|
|
|
|
// Always reset platform-level status — never carry
|
|
|
|
|
// published/failed/publishing into the new draft.
|
|
|
|
|
'status' => PostPlatformStatus::Pending,
|
|
|
|
|
'platform_post_id' => null,
|
|
|
|
|
'platform_url' => null,
|
|
|
|
|
'error_message' => null,
|
|
|
|
|
'error_context' => null,
|
|
|
|
|
'published_at' => null,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$copy->labels()->attach($original->labels->pluck('id'));
|
|
|
|
|
|
|
|
|
|
return $copy;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|