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.

feat: workspace-scoped broadcast channel and post lifecycle events

Paulo Castellano06e72831f606
diff --git a/app/Actions/Post/CreatePost.php b/app/Actions/Post/CreatePost.php
index 53081a700..bfdb75bdb 100644
--- a/app/Actions/Post/CreatePost.php
+++ b/app/Actions/Post/CreatePost.php
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Actions\Post;
use App\Enums\Post\Status as PostStatus;
+use App\Events\PostCreated;
use App\Models\Post;
use App\Models\User;
use App\Models\Workspace;
@@ -38,7 +39,7 @@ class CreatePost
{
$scheduledAt = self::resolveScheduledAt($data);
- return DB::transaction(function () use ($workspace, $user, $data, $scheduledAt): Post {
+ $post = DB::transaction(function () use ($workspace, $user, $data, $scheduledAt): Post {
$post = $workspace->posts()->create([
'user_id' => $user->id,
'content' => data_get($data, 'content', ''),
@@ -72,6 +73,10 @@ class CreatePost
return $post;
});
+
+ PostCreated::dispatch($post);
+
+ return $post;
}
/**
diff --git a/app/Actions/Post/DeletePost.php b/app/Actions/Post/DeletePost.php
index 071933c97..e907acc9b 100644
--- a/app/Actions/Post/DeletePost.php
+++ b/app/Actions/Post/DeletePost.php
@@ -4,12 +4,18 @@ declare(strict_types=1);
namespace App\Actions\Post;
+use App\Events\PostDeleted;
use App\Models\Post;
class DeletePost
{
public static function execute(Post $post): void
{
+ $postId = $post->id;
+ $workspaceId = $post->workspace_id;
+
$post->delete();
+
+ PostDeleted::dispatch($postId, $workspaceId);
}
}
diff --git a/app/Broadcasting/WorkspaceChannel.php b/app/Broadcasting/WorkspaceChannel.php
new file mode 100644
index 000000000..7d6483a14
--- /dev/null
+++ b/app/Broadcasting/WorkspaceChannel.php
@@ -0,0 +1,16 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Broadcasting;
+
+use App\Models\User;
+use App\Models\Workspace;
+
+class WorkspaceChannel
+{
+ public function join(User $user, Workspace $workspace): bool
+ {
+ return $workspace->hasMember($user);
+ }
+}
diff --git a/app/Events/Ai/PostCreationReady.php b/app/Events/Ai/PostCreationReady.php
index 89aff8f12..8c323cf9e 100644
--- a/app/Events/Ai/PostCreationReady.php
+++ b/app/Events/Ai/PostCreationReady.php
@@ -6,11 +6,11 @@ namespace App\Events\Ai;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
-use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
-class PostCreationReady implements ShouldBroadcastNow
+class PostCreationReady implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
@@ -23,14 +23,14 @@ class PostCreationReady implements ShouldBroadcastNow
public ?string $imageBody = null,
) {}
- public function broadcastOn(): PrivateChannel
+ public function broadcastAs(): string
{
- return new PrivateChannel("users.{$this->userId}.ai-creation.{$this->creationId}");
+ return 'ai.creation.completed';
}
- public function broadcastAs(): string
+ public function broadcastOn(): PrivateChannel
{
- return 'PostCreationReady';
+ return new PrivateChannel("users.{$this->userId}.ai-creation.{$this->creationId}");
}
/**
@@ -46,4 +46,9 @@ class PostCreationReady implements ShouldBroadcastNow
'error' => $this->error,
];
}
+
+ public function broadcastQueue(): string
+ {
+ return 'broadcasts';
+ }
}
diff --git a/app/Events/NotificationCreated.php b/app/Events/NotificationCreated.php
index 7f01d1e21..416d35e51 100644
--- a/app/Events/NotificationCreated.php
+++ b/app/Events/NotificationCreated.php
@@ -7,15 +7,20 @@ namespace App\Events;
use App\Models\Notification;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
-use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
-class NotificationCreated implements ShouldBroadcastNow
+class NotificationCreated implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets;
public function __construct(public Notification $notification) {}
+ public function broadcastAs(): string
+ {
+ return 'notification.created';
+ }
+
public function broadcastOn(): array
{
return [
@@ -23,11 +28,9 @@ class NotificationCreated implements ShouldBroadcastNow
];
}
- public function broadcastAs(): string
- {
- return 'NotificationCreated';
- }
-
+ /**
+ * @return array<string, mixed>
+ */
public function broadcastWith(): array
{
return [
@@ -47,4 +50,9 @@ class NotificationCreated implements ShouldBroadcastNow
],
];
}
+
+ public function broadcastQueue(): string
+ {
+ return 'broadcasts';
+ }
}
diff --git a/app/Events/PostCommentCreated.php b/app/Events/PostCommentCreated.php
index d0c1a1ae6..f0e0140d2 100644
--- a/app/Events/PostCommentCreated.php
+++ b/app/Events/PostCommentCreated.php
@@ -9,15 +9,20 @@ use App\Models\User;
use App\Support\MentionParser;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
-use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
-class PostCommentCreated implements ShouldBroadcastNow
+class PostCommentCreated implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets;
public function __construct(public PostComment $comment) {}
+ public function broadcastAs(): string
+ {
+ return 'post.comment.created';
+ }
+
public function broadcastOn(): array
{
return [
@@ -25,11 +30,9 @@ class PostCommentCreated implements ShouldBroadcastNow
];
}
- public function broadcastAs(): string
- {
- return 'PostCommentCreated';
- }
-
+ /**
+ * @return array<string, mixed>
+ */
public function broadcastWith(): array
{
$mentionedIds = MentionParser::extractUserIds($this->comment->body ?? '');
@@ -59,4 +62,9 @@ class PostCommentCreated implements ShouldBroadcastNow
'mentioned_users' => $mentionedUsers,
];
}
+
+ public function broadcastQueue(): string
+ {
+ return 'broadcasts';
+ }
}
diff --git a/app/Events/PostCreated.php b/app/Events/PostCreated.php
new file mode 100644
index 000000000..060e81b97
--- /dev/null
+++ b/app/Events/PostCreated.php
@@ -0,0 +1,46 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Events;
+
+use App\Models\Post;
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+
+class PostCreated implements ShouldBroadcast
+{
+ use Dispatchable, InteractsWithSockets, SerializesModels;
+
+ public function __construct(public Post $post) {}
+
+ public function broadcastAs(): string
+ {
+ return 'post.created';
+ }
+
+ public function broadcastOn(): array
+ {
+ return [
+ new PrivateChannel('workspace.'.$this->post->workspace_id),
+ ];
+ }
+
+ /**
+ * @return array<string, string>
+ */
+ public function broadcastWith(): array
+ {
+ return [
+ 'post_id' => $this->post->id,
+ ];
+ }
+
+ public function broadcastQueue(): string
+ {
+ return 'broadcasts';
+ }
+}
diff --git a/app/Events/PostDeleted.php b/app/Events/PostDeleted.php
new file mode 100644
index 000000000..b33bbe51a
--- /dev/null
+++ b/app/Events/PostDeleted.php
@@ -0,0 +1,47 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Events;
+
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
+use Illuminate\Foundation\Events\Dispatchable;
+
+class PostDeleted implements ShouldBroadcast
+{
+ use Dispatchable, InteractsWithSockets;
+
+ public function __construct(
+ public string $postId,
+ public string $workspaceId,
+ ) {}
+
+ public function broadcastAs(): string
+ {
+ return 'post.deleted';
+ }
+
+ public function broadcastOn(): array
+ {
+ return [
+ new PrivateChannel('workspace.'.$this->workspaceId),
+ ];
+ }
+
+ /**
+ * @return array<string, string>
+ */
+ public function broadcastWith(): array
+ {
+ return [
+ 'post_id' => $this->postId,
+ ];
+ }
+
+ public function broadcastQueue(): string
+ {
+ return 'broadcasts';
+ }
+}
diff --git a/app/Events/PostPlatformStatusUpdated.php b/app/Events/PostPlatformStatusUpdated.php
index 9e0096694..6d2544f6e 100644
--- a/app/Events/PostPlatformStatusUpdated.php
+++ b/app/Events/PostPlatformStatusUpdated.php
@@ -7,11 +7,11 @@ namespace App\Events;
use App\Models\PostPlatform;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
-use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
+use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
-class PostPlatformStatusUpdated implements ShouldBroadcastNow
+class PostPlatformStatusUpdated implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
@@ -19,33 +19,29 @@ class PostPlatformStatusUpdated implements ShouldBroadcastNow
public function broadcastAs(): string
{
- return 'PostPlatformStatusUpdated';
+ return 'post.platform.status.updated';
}
public function broadcastOn(): array
{
return [
new PrivateChannel('post.'.$this->postPlatform->post_id),
+ new PrivateChannel('workspace.'.$this->postPlatform->post->workspace_id),
];
}
+ /**
+ * @return array<string, string>
+ */
public function broadcastWith(): array
{
- $post = $this->postPlatform->post->fresh();
-
return [
- 'post_platform' => [
- 'id' => $this->postPlatform->id,
- 'status' => $this->postPlatform->status->value,
- 'platform_url' => $this->postPlatform->platform_url,
- 'error_message' => $this->postPlatform->error_message,
- 'published_at' => $this->postPlatform->published_at?->toISOString(),
- ],
- 'post' => [
- 'id' => $post->id,
- 'status' => $post->status->value,
- 'published_at' => $post->published_at?->toISOString(),
- ],
+ 'post_id' => $this->postPlatform->post_id,
];
}
+
+ public function broadcastQueue(): string
+ {
+ return 'broadcasts';
+ }
}
diff --git a/config/horizon.php b/config/horizon.php
index 60ede02f5..3953f0e7f 100644
--- a/config/horizon.php
+++ b/config/horizon.php
@@ -214,7 +214,7 @@ return [
'defaults' => [
'supervisor-1' => [
'connection' => 'redis',
- 'queue' => ['default', 'posthog'],
+ 'queue' => ['default', 'posthog', 'broadcasts'],
'balance' => 'auto',
'autoScalingStrategy' => 'time',
'maxProcesses' => 1,
diff --git a/resources/js/composables/useAiStream.ts b/resources/js/composables/echo/useAiStream.ts
similarity index 100%
rename from resources/js/composables/useAiStream.ts
rename to resources/js/composables/echo/useAiStream.ts
diff --git a/routes/channels.php b/routes/channels.php
index e517bb14b..a6bab9249 100644
--- a/routes/channels.php
+++ b/routes/channels.php
@@ -5,11 +5,14 @@ declare(strict_types=1);
use App\Broadcasting\PostChannel;
use App\Broadcasting\UserAiCreationChannel;
use App\Broadcasting\UserAiGenerationChannel;
+use App\Broadcasting\WorkspaceChannel;
use App\Broadcasting\WorkspaceUserChannel;
use Illuminate\Support\Facades\Broadcast;
Broadcast::channel('post.{post}', PostChannel::class);
+Broadcast::channel('workspace.{workspace}', WorkspaceChannel::class);
+
Broadcast::channel('workspace.{workspace}.user.{owner}', WorkspaceUserChannel::class);
Broadcast::channel('users.{userId}.ai-gen.{generationId}', UserAiGenerationChannel::class);
diff --git a/tests/Feature/Actions/Post/CreatePostTest.php b/tests/Feature/Actions/Post/CreatePostTest.php
new file mode 100644
index 000000000..4a1252e0b
--- /dev/null
+++ b/tests/Feature/Actions/Post/CreatePostTest.php
@@ -0,0 +1,26 @@
+<?php
+
+declare(strict_types=1);
+
+use App\Actions\Post\CreatePost;
+use App\Events\PostCreated;
+use App\Models\User;
+use App\Models\Workspace;
+use Illuminate\Support\Facades\Event;
+
+test('execute dispatches PostCreated with the persisted post', function () {
+ Event::fake([PostCreated::class]);
+
+ $user = User::factory()->create();
+ $workspace = Workspace::factory()->create(['user_id' => $user->id]);
+
+ $post = CreatePost::execute($workspace, $user, [
+ 'content' => 'Hello world',
+ ]);
+
+ Event::assertDispatched(
+ PostCreated::class,
+ fn (PostCreated $event) => $event->post->id === $post->id
+ && $event->post->workspace_id === $workspace->id,
+ );
+});
diff --git a/tests/Feature/Actions/Post/DeletePostTest.php b/tests/Feature/Actions/Post/DeletePostTest.php
new file mode 100644
index 000000000..842246730
--- /dev/null
+++ b/tests/Feature/Actions/Post/DeletePostTest.php
@@ -0,0 +1,45 @@
+<?php
+
+declare(strict_types=1);
+
+use App\Actions\Post\DeletePost;
+use App\Events\PostDeleted;
+use App\Models\Post;
+use App\Models\User;
+use App\Models\Workspace;
+use Illuminate\Support\Facades\Event;
+
+test('execute deletes the post', function () {
+ $user = User::factory()->create();
+ $workspace = Workspace::factory()->create(['user_id' => $user->id]);
+ $post = Post::factory()->create([
+ 'workspace_id' => $workspace->id,
+ 'user_id' => $user->id,
+ ]);
+
+ DeletePost::execute($post);
+
+ expect(Post::find($post->id))->toBeNull();
+});
+
+test('execute dispatches PostDeleted with the post and workspace ids', function () {
+ Event::fake([PostDeleted::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,
+ ]);
+
+ $postId = $post->id;
+ $workspaceId = $post->workspace_id;
+
+ DeletePost::execute($post);
+
+ Event::assertDispatched(
+ PostDeleted::class,
+ fn (PostDeleted $event) => $event->postId === $postId
+ && $event->workspaceId === $workspaceId,
+ );
+});
diff --git a/tests/Unit/Broadcasting/WorkspaceChannelTest.php b/tests/Unit/Broadcasting/WorkspaceChannelTest.php
new file mode 100644
index 000000000..fd12a1778
--- /dev/null
+++ b/tests/Unit/Broadcasting/WorkspaceChannelTest.php
@@ -0,0 +1,29 @@
+<?php
+
+declare(strict_types=1);
+
+use App\Broadcasting\WorkspaceChannel;
+use App\Enums\UserWorkspace\Role;
+use App\Models\User;
+use App\Models\Workspace;
+
+test('workspace channel allows a member to join', function () {
+ $user = User::factory()->create();
+ $workspace = Workspace::factory()->create(['user_id' => $user->id]);
+ $workspace->members()->attach($user->id, ['role' => Role::Member->value]);
+
+ $channel = new WorkspaceChannel;
+
+ expect($channel->join($user, $workspace))->toBeTrue();
+});
+
+test('workspace channel denies a non-member', function () {
+ $owner = User::factory()->create();
+ $workspace = Workspace::factory()->create(['user_id' => $owner->id]);
+
+ $outsider = User::factory()->create();
+
+ $channel = new WorkspaceChannel;
+
+ expect($channel->join($outsider, $workspace))->toBeFalse();
+});
diff --git a/tests/Unit/Events/PostCreatedTest.php b/tests/Unit/Events/PostCreatedTest.php
new file mode 100644
index 000000000..df664bf8e
--- /dev/null
+++ b/tests/Unit/Events/PostCreatedTest.php
@@ -0,0 +1,51 @@
+<?php
+
+declare(strict_types=1);
+
+use App\Events\PostCreated;
+use App\Models\Post;
+use App\Models\User;
+use App\Models\Workspace;
+use Illuminate\Broadcasting\PrivateChannel;
+
+test('event broadcasts on the workspace channel', function () {
+ $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 = new PostCreated($post);
+ $channels = $event->broadcastOn();
+
+ expect($channels)->toHaveCount(1);
+ expect($channels[0])->toBeInstanceOf(PrivateChannel::class);
+ expect($channels[0]->name)->toBe('private-workspace.'.$workspace->id);
+});
+
+test('event broadcasts with the post id', function () {
+ $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 = new PostCreated($post);
+
+ expect($event->broadcastWith())->toBe(['post_id' => $post->id]);
+});
+
+test('event broadcasts as a stable name', function () {
+ $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 = new PostCreated($post);
+
+ expect($event->broadcastAs())->toBe('post.created');
+});
diff --git a/tests/Unit/Events/PostDeletedTest.php b/tests/Unit/Events/PostDeletedTest.php
new file mode 100644
index 000000000..44a1a449f
--- /dev/null
+++ b/tests/Unit/Events/PostDeletedTest.php
@@ -0,0 +1,29 @@
+<?php
+
+declare(strict_types=1);
+
+use App\Events\PostDeleted;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Support\Str;
+
+test('event broadcasts on the workspace channel only', function () {
+ $event = new PostDeleted(Str::uuid()->toString(), 'workspace-123');
+ $channels = $event->broadcastOn();
+
+ expect($channels)->toHaveCount(1);
+ expect($channels[0])->toBeInstanceOf(PrivateChannel::class);
+ expect($channels[0]->name)->toBe('private-workspace.workspace-123');
+});
+
+test('event broadcasts with the post id', function () {
+ $postId = (string) Str::uuid();
+ $event = new PostDeleted($postId, 'workspace-123');
+
+ expect($event->broadcastWith())->toBe(['post_id' => $postId]);
+});
+
+test('event broadcasts as a stable name', function () {
+ $event = new PostDeleted(Str::uuid()->toString(), 'workspace-123');
+
+ expect($event->broadcastAs())->toBe('post.deleted');
+});
diff --git a/tests/Unit/Events/PostPlatformStatusUpdatedTest.php b/tests/Unit/Events/PostPlatformStatusUpdatedTest.php
index 6d723db42..6ab0de1c0 100644
--- a/tests/Unit/Events/PostPlatformStatusUpdatedTest.php
+++ b/tests/Unit/Events/PostPlatformStatusUpdatedTest.php
@@ -28,48 +28,25 @@ beforeEach(function () {
]);
});
-test('event broadcasts on correct channel', function () {
+test('event broadcasts on the per-post and per-workspace channels', function () {
$event = new PostPlatformStatusUpdated($this->postPlatform);
$channels = $event->broadcastOn();
- expect($channels)->toHaveCount(1);
+ expect($channels)->toHaveCount(2);
expect($channels[0])->toBeInstanceOf(PrivateChannel::class);
expect($channels[0]->name)->toBe('private-post.'.$this->post->id);
+ expect($channels[1])->toBeInstanceOf(PrivateChannel::class);
+ expect($channels[1]->name)->toBe('private-workspace.'.$this->workspace->id);
});
-test('event broadcasts with correct data', function () {
- $this->postPlatform->update([
- 'status' => 'published',
- 'platform_url' => 'https://linkedin.com/post/123',
- 'published_at' => now(),
- ]);
-
- $event = new PostPlatformStatusUpdated($this->postPlatform->fresh());
- $data = $event->broadcastWith();
-
- expect($data)->toHaveKey('post_platform');
- expect($data)->toHaveKey('post');
- expect($data['post_platform']['id'])->toBe($this->postPlatform->id);
- expect($data['post_platform']['status'])->toBe('published');
- expect($data['post_platform']['platform_url'])->toBe('https://linkedin.com/post/123');
- expect($data['post']['id'])->toBe($this->post->id);
-});
-
-test('event broadcasts error message when failed', function () {
- $this->postPlatform->update([
- 'status' => 'failed',
- 'error_message' => 'API Error',
- ]);
-
- $event = new PostPlatformStatusUpdated($this->postPlatform->fresh());
- $data = $event->broadcastWith();
+test('event broadcasts with the post id', function () {
+ $event = new PostPlatformStatusUpdated($this->postPlatform);
- expect($data['post_platform']['error_message'])->toBe('API Error');
+ expect($event->broadcastWith())->toBe(['post_id' => $this->post->id]);
});
-test('event broadcasts null published_at when not published', function () {
+test('event broadcasts as a stable name', function () {
$event = new PostPlatformStatusUpdated($this->postPlatform);
- $data = $event->broadcastWith();
- expect($data['post_platform']['published_at'])->toBeNull();
+ expect($event->broadcastAs())->toBe('post.platform.status.updated');
});