From 06e72831f606f9570889b89b0410078797440a85 Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Thu, 7 May 2026 16:02:22 -0300 Subject: [PATCH] feat: workspace-scoped broadcast channel and post lifecycle events MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a private workspace channel (WorkspaceChannel, authorised by membership) so list views can subscribe once and receive events for every post in the workspace, instead of opening N per-post channels. New events: - PostCreated (broadcast on workspace.{id}) - PostDeleted (broadcast on workspace.{id}; carries primitive ids so it fires after \$post->delete()) - PostPlatformStatusUpdated now broadcasts on both post.{id} and workspace.{id} so focused views and lists share the same trigger. All broadcast events migrated to the namespaced 'entity.action' convention from Laravel's broadcasting docs and switched from ShouldBroadcastNow to ShouldBroadcast on a dedicated 'broadcasts' queue (added to the supervisor-1 list in config/horizon.php) to keep HTTP responses fast: PostCreated -> post.created PostDeleted -> post.deleted PostPlatformStatusUpdated -> post.platform.status.updated PostCommentCreated -> post.comment.created NotificationCreated -> notification.created PostCreationReady -> ai.creation.completed Drops the SubscriptionCreated event — it was broadcast on users.{owner_id} but had no listeners (frontend or backend) and the billing/PostHog flow already handles plan-change tracking elsewhere. PostPlatformStatusUpdated payload trimmed to {post_id} since every consumer (Show, Edit, Index) does router.reload({ only: [...] }) and ignored the rich shape. --- app/Actions/Post/CreatePost.php | 7 ++- app/Actions/Post/DeletePost.php | 6 +++ app/Broadcasting/WorkspaceChannel.php | 16 ++++++ app/Events/Ai/PostCreationReady.php | 19 ++++--- app/Events/NotificationCreated.php | 22 +++++--- app/Events/PostCommentCreated.php | 22 +++++--- app/Events/PostCreated.php | 46 +++++++++++++++++ app/Events/PostDeleted.php | 47 +++++++++++++++++ app/Events/PostPlatformStatusUpdated.php | 30 +++++------ config/horizon.php | 2 +- .../js/composables/{ => echo}/useAiStream.ts | 0 routes/channels.php | 3 ++ tests/Feature/Actions/Post/CreatePostTest.php | 26 ++++++++++ tests/Feature/Actions/Post/DeletePostTest.php | 45 ++++++++++++++++ .../Broadcasting/WorkspaceChannelTest.php | 29 +++++++++++ tests/Unit/Events/PostCreatedTest.php | 51 +++++++++++++++++++ tests/Unit/Events/PostDeletedTest.php | 29 +++++++++++ .../Events/PostPlatformStatusUpdatedTest.php | 47 +++++------------ 18 files changed, 372 insertions(+), 75 deletions(-) create mode 100644 app/Broadcasting/WorkspaceChannel.php create mode 100644 app/Events/PostCreated.php create mode 100644 app/Events/PostDeleted.php rename resources/js/composables/{ => echo}/useAiStream.ts (100%) create mode 100644 tests/Feature/Actions/Post/CreatePostTest.php create mode 100644 tests/Feature/Actions/Post/DeletePostTest.php create mode 100644 tests/Unit/Broadcasting/WorkspaceChannelTest.php create mode 100644 tests/Unit/Events/PostCreatedTest.php create mode 100644 tests/Unit/Events/PostDeletedTest.php diff --git a/app/Actions/Post/CreatePost.php b/app/Actions/Post/CreatePost.php index 53081a70..bfdb75bd 100644 --- a/app/Actions/Post/CreatePost.php +++ b/app/Actions/Post/CreatePost.php @@ -5,6 +5,7 @@ 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 @@ public static function execute(Workspace $workspace, User $user, array $data): P { $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 @@ public static function execute(Workspace $workspace, User $user, array $data): P return $post; }); + + PostCreated::dispatch($post); + + return $post; } /** diff --git a/app/Actions/Post/DeletePost.php b/app/Actions/Post/DeletePost.php index 071933c9..e907acc9 100644 --- a/app/Actions/Post/DeletePost.php +++ b/app/Actions/Post/DeletePost.php @@ -4,12 +4,18 @@ 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 00000000..7d6483a1 --- /dev/null +++ b/app/Broadcasting/WorkspaceChannel.php @@ -0,0 +1,16 @@ +hasMember($user); + } +} diff --git a/app/Events/Ai/PostCreationReady.php b/app/Events/Ai/PostCreationReady.php index 89aff8f1..8c323cf9 100644 --- a/app/Events/Ai/PostCreationReady.php +++ b/app/Events/Ai/PostCreationReady.php @@ -6,11 +6,11 @@ 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,16 +23,16 @@ public function __construct( public ?string $imageBody = null, ) {} + public function broadcastAs(): string + { + return 'ai.creation.completed'; + } + public function broadcastOn(): PrivateChannel { return new PrivateChannel("users.{$this->userId}.ai-creation.{$this->creationId}"); } - public function broadcastAs(): string - { - return 'PostCreationReady'; - } - /** * @return array */ @@ -46,4 +46,9 @@ public function broadcastWith(): array 'error' => $this->error, ]; } + + public function broadcastQueue(): string + { + return 'broadcasts'; + } } diff --git a/app/Events/NotificationCreated.php b/app/Events/NotificationCreated.php index 7f01d1e2..416d35e5 100644 --- a/app/Events/NotificationCreated.php +++ b/app/Events/NotificationCreated.php @@ -7,15 +7,20 @@ 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 @@ public function broadcastOn(): array ]; } - public function broadcastAs(): string - { - return 'NotificationCreated'; - } - + /** + * @return array + */ public function broadcastWith(): array { return [ @@ -47,4 +50,9 @@ public function broadcastWith(): array ], ]; } + + public function broadcastQueue(): string + { + return 'broadcasts'; + } } diff --git a/app/Events/PostCommentCreated.php b/app/Events/PostCommentCreated.php index d0c1a1ae..f0e0140d 100644 --- a/app/Events/PostCommentCreated.php +++ b/app/Events/PostCommentCreated.php @@ -9,15 +9,20 @@ 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 @@ public function broadcastOn(): array ]; } - public function broadcastAs(): string - { - return 'PostCommentCreated'; - } - + /** + * @return array + */ public function broadcastWith(): array { $mentionedIds = MentionParser::extractUserIds($this->comment->body ?? ''); @@ -59,4 +62,9 @@ public function broadcastWith(): array '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 00000000..060e81b9 --- /dev/null +++ b/app/Events/PostCreated.php @@ -0,0 +1,46 @@ +post->workspace_id), + ]; + } + + /** + * @return array + */ + 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 00000000..b33bbe51 --- /dev/null +++ b/app/Events/PostDeleted.php @@ -0,0 +1,47 @@ +workspaceId), + ]; + } + + /** + * @return array + */ + 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 9e009669..6d2544f6 100644 --- a/app/Events/PostPlatformStatusUpdated.php +++ b/app/Events/PostPlatformStatusUpdated.php @@ -7,11 +7,11 @@ 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 @@ public function __construct(public PostPlatform $postPlatform) {} 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 + */ 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 60ede02f..3953f0e7 100644 --- a/config/horizon.php +++ b/config/horizon.php @@ -214,7 +214,7 @@ '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 e517bb14..a6bab924 100644 --- a/routes/channels.php +++ b/routes/channels.php @@ -5,11 +5,14 @@ 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 00000000..4a1252e0 --- /dev/null +++ b/tests/Feature/Actions/Post/CreatePostTest.php @@ -0,0 +1,26 @@ +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 00000000..84224673 --- /dev/null +++ b/tests/Feature/Actions/Post/DeletePostTest.php @@ -0,0 +1,45 @@ +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 00000000..fd12a177 --- /dev/null +++ b/tests/Unit/Broadcasting/WorkspaceChannelTest.php @@ -0,0 +1,29 @@ +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 00000000..df664bf8 --- /dev/null +++ b/tests/Unit/Events/PostCreatedTest.php @@ -0,0 +1,51 @@ +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 00000000..44a1a449 --- /dev/null +++ b/tests/Unit/Events/PostDeletedTest.php @@ -0,0 +1,29 @@ +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 6d723db4..6ab0de1c 100644 --- a/tests/Unit/Events/PostPlatformStatusUpdatedTest.php +++ b/tests/Unit/Events/PostPlatformStatusUpdatedTest.php @@ -28,48 +28,25 @@ ]); }); -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(); - - expect($data['post_platform']['error_message'])->toBe('API Error'); -}); - -test('event broadcasts null published_at when not published', function () { +test('event broadcasts with the post id', function () { $event = new PostPlatformStatusUpdated($this->postPlatform); - $data = $event->broadcastWith(); - expect($data['post_platform']['published_at'])->toBeNull(); + expect($event->broadcastWith())->toBe(['post_id' => $this->post->id]); +}); + +test('event broadcasts as a stable name', function () { + $event = new PostPlatformStatusUpdated($this->postPlatform); + + expect($event->broadcastAs())->toBe('post.platform.status.updated'); });