trypost/app/Events/NotificationCreated.php
Paulo Castellano 06e72831f6 feat: workspace-scoped broadcast channel and post lifecycle events
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.
2026-05-07 16:02:22 -03:00

58 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Events;
use App\Models\Notification;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
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 [
new PrivateChannel('workspace.'.$this->notification->workspace_id.'.user.'.$this->notification->user_id),
];
}
/**
* @return array<string, mixed>
*/
public function broadcastWith(): array
{
return [
'notification' => [
'id' => $this->notification->id,
'user_id' => $this->notification->user_id,
'workspace_id' => $this->notification->workspace_id,
'type' => $this->notification->type->value,
'channel' => $this->notification->channel->value,
'title' => $this->notification->title,
'body' => $this->notification->body,
'data' => $this->notification->data,
'read_at' => $this->notification->read_at?->toISOString(),
'archived_at' => $this->notification->archived_at?->toISOString(),
'created_at' => $this->notification->created_at->toISOString(),
'updated_at' => $this->notification->updated_at->toISOString(),
],
];
}
public function broadcastQueue(): string
{
return 'broadcasts';
}
}