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.

Dispatch PostCreated from PostObserver on every create.

Paulo Castellanod1d38865efbf
diff --git a/app/Actions/Post/CreatePost.php b/app/Actions/Post/CreatePost.php
index 11c5a1f43..26535cecf 100644
--- a/app/Actions/Post/CreatePost.php
+++ b/app/Actions/Post/CreatePost.php
@@ -6,7 +6,6 @@ namespace App\Actions\Post;
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;
@@ -91,8 +90,6 @@ class CreatePost
return $post;
});
- PostCreated::dispatch($post);
-
return $post;
}
diff --git a/app/Actions/Post/DuplicatePost.php b/app/Actions/Post/DuplicatePost.php
index 53b5d1342..1e40854e2 100644
--- a/app/Actions/Post/DuplicatePost.php
+++ b/app/Actions/Post/DuplicatePost.php
@@ -7,7 +7,6 @@ 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;
@@ -21,7 +20,7 @@ class DuplicatePost
{
public static function execute(Post $original, User $user): Post
{
- $copy = DB::transaction(function () use ($original, $user): Post {
+ return DB::transaction(function () use ($original, $user): Post {
$copy = $original->workspace->posts()->create([
'user_id' => $user->id,
'content' => $original->content,
@@ -57,9 +56,5 @@ class DuplicatePost
return $copy;
});
-
- PostCreated::dispatch($copy);
-
- return $copy;
}
}
diff --git a/app/Observers/PostObserver.php b/app/Observers/PostObserver.php
index 33456b275..c9cfd84a7 100644
--- a/app/Observers/PostObserver.php
+++ b/app/Observers/PostObserver.php
@@ -6,11 +6,18 @@ namespace App\Observers;
use App\Enums\Automation\Trigger\Type as TriggerType;
use App\Enums\Post\Status as PostStatus;
+use App\Events\PostCreated;
use App\Jobs\Automation\DispatchPostTriggerAutomationsJob;
use App\Models\Post;
+use Illuminate\Contracts\Events\ShouldHandleEventsAfterCommit;
-class PostObserver
+class PostObserver implements ShouldHandleEventsAfterCommit
{
+ public function created(Post $post): void
+ {
+ PostCreated::dispatch($post);
+ }
+
public function saved(Post $post): void
{
if (! $post->wasChanged('status')) {
@@ -27,6 +34,6 @@ class PostObserver
return;
}
- DispatchPostTriggerAutomationsJob::dispatch($post, $triggerType)->afterCommit();
+ DispatchPostTriggerAutomationsJob::dispatch($post, $triggerType);
}
}
diff --git a/tests/Feature/Actions/Post/CreatePostTest.php b/tests/Feature/Actions/Post/CreatePostTest.php
index 410257dd9..3a50dceee 100644
--- a/tests/Feature/Actions/Post/CreatePostTest.php
+++ b/tests/Feature/Actions/Post/CreatePostTest.php
@@ -9,7 +9,7 @@ use App\Models\User;
use App\Models\Workspace;
use Illuminate\Support\Facades\Event;
-test('execute dispatches PostCreated with the persisted post', function () {
+test('execute relies on the observer to dispatch PostCreated', function () {
Event::fake([PostCreated::class]);
$user = User::factory()->create();
diff --git a/tests/Feature/Actions/Post/DuplicatePostTest.php b/tests/Feature/Actions/Post/DuplicatePostTest.php
index 93f6705c2..13e43daf4 100644
--- a/tests/Feature/Actions/Post/DuplicatePostTest.php
+++ b/tests/Feature/Actions/Post/DuplicatePostTest.php
@@ -32,9 +32,7 @@ test('execute clones the post as a draft created via web', function () {
->and($copy->published_at)->toBeNull();
});
-test('execute dispatches PostCreated for the duplicated post', function () {
- Event::fake([PostCreated::class]);
-
+test('execute relies on the observer to dispatch PostCreated for the duplicate', function () {
$user = User::factory()->create();
$workspace = Workspace::factory()->create(['user_id' => $user->id]);
$original = Post::factory()->create([
@@ -42,6 +40,8 @@ test('execute dispatches PostCreated for the duplicated post', function () {
'user_id' => $user->id,
]);
+ Event::fake([PostCreated::class]);
+
$copy = DuplicatePost::execute($original, $user);
Event::assertDispatched(
diff --git a/tests/Feature/Observers/PostObserverTest.php b/tests/Feature/Observers/PostObserverTest.php
new file mode 100644
index 000000000..ebda452b2
--- /dev/null
+++ b/tests/Feature/Observers/PostObserverTest.php
@@ -0,0 +1,40 @@
+<?php
+
+declare(strict_types=1);
+
+use App\Events\PostCreated;
+use App\Models\Post;
+use App\Models\User;
+use App\Models\Workspace;
+use Illuminate\Support\Facades\Event;
+
+test('creating a post dispatches PostCreated via the observer', function () {
+ Event::fake([PostCreated::class]);
+
+ $user = User::factory()->create();
+ $workspace = Workspace::factory()->create(['user_id' => $user->id]);
+
+ $post = Post::factory()->create([
+ 'workspace_id' => $workspace->id,
+ 'user_id' => $user->id,
+ ]);
+
+ Event::assertDispatched(
+ PostCreated::class,
+ fn (PostCreated $event) => $event->post->id === $post->id,
+ );
+});
+
+test('creating a post quietly does not dispatch PostCreated', function () {
+ Event::fake([PostCreated::class]);
+
+ $user = User::factory()->create();
+ $workspace = Workspace::factory()->create(['user_id' => $user->id]);
+
+ Post::factory()->createQuietly([
+ 'workspace_id' => $workspace->id,
+ 'user_id' => $user->id,
+ ]);
+
+ Event::assertNotDispatched(PostCreated::class);
+});