Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

Fire PostCreated on duplicate and drop unused PostHog eager load.

Paulo Castellano8b1ce057c313
diff --git a/app/Actions/Post/DuplicatePost.php b/app/Actions/Post/DuplicatePost.php
index 1e40854e2..53b5d1342 100644
--- a/app/Actions/Post/DuplicatePost.php
+++ b/app/Actions/Post/DuplicatePost.php
@@ -7,6 +7,7 @@ namespace App\Actions\Post;
use App\Enums\Post\CreatedVia;
use App\Enums\Post\Status as PostStatus;
use App\Enums\PostPlatform\Status as PostPlatformStatus;
+use App\Events\PostCreated;
use App\Models\Post;
use App\Models\User;
use Illuminate\Support\Facades\DB;
@@ -20,7 +21,7 @@ class DuplicatePost
{
public static function execute(Post $original, User $user): Post
{
- return DB::transaction(function () use ($original, $user): Post {
+ $copy = DB::transaction(function () use ($original, $user): Post {
$copy = $original->workspace->posts()->create([
'user_id' => $user->id,
'content' => $original->content,
@@ -56,5 +57,9 @@ class DuplicatePost
return $copy;
});
+
+ PostCreated::dispatch($copy);
+
+ return $copy;
}
}
diff --git a/app/Jobs/PostHog/TrackPost.php b/app/Jobs/PostHog/TrackPost.php
index 15137b0b5..bd31ff04d 100644
--- a/app/Jobs/PostHog/TrackPost.php
+++ b/app/Jobs/PostHog/TrackPost.php
@@ -33,7 +33,7 @@ class TrackPost implements ShouldQueue
}
$post = Post::query()
- ->with(['workspace.account.plan', 'user'])
+ ->with(['workspace.account.plan'])
->find($this->postId);
if (! $post) {
diff --git a/tests/Feature/Actions/Post/DuplicatePostTest.php b/tests/Feature/Actions/Post/DuplicatePostTest.php
index 628c69439..93f6705c2 100644
--- a/tests/Feature/Actions/Post/DuplicatePostTest.php
+++ b/tests/Feature/Actions/Post/DuplicatePostTest.php
@@ -5,9 +5,11 @@ declare(strict_types=1);
use App\Actions\Post\DuplicatePost;
use App\Enums\Post\CreatedVia;
use App\Enums\Post\Status as PostStatus;
+use App\Events\PostCreated;
use App\Models\Post;
use App\Models\User;
use App\Models\Workspace;
+use Illuminate\Support\Facades\Event;
test('execute clones the post as a draft created via web', function () {
$user = User::factory()->create();
@@ -29,3 +31,21 @@ test('execute clones the post as a draft created via web', function () {
->and($copy->scheduled_at)->toBeNull()
->and($copy->published_at)->toBeNull();
});
+
+test('execute dispatches PostCreated for the duplicated post', function () {
+ Event::fake([PostCreated::class]);
+
+ $user = User::factory()->create();
+ $workspace = Workspace::factory()->create(['user_id' => $user->id]);
+ $original = Post::factory()->create([
+ 'workspace_id' => $workspace->id,
+ 'user_id' => $user->id,
+ ]);
+
+ $copy = DuplicatePost::execute($original, $user);
+
+ Event::assertDispatched(
+ PostCreated::class,
+ fn (PostCreated $event) => $event->post->id === $copy->id,
+ );
+});