refactor(events): broadcast team events to an explicit team

Move the shared team id and team channel logic of 15 broadcast events
into a BroadcastsToTeam trait. Events no longer fall back to the session
team: the session team can differ from the resource team, and queued
jobs have no session.

Fix StopDatabaseProxy, which dispatched DatabaseProxyStopped without a
team, so the event was never broadcast from queued jobs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Andras Bacsai
2026-09-29 10:46:16 +02:00
co-authored by Claude Opus 5.5
parent 65262e639e
commit cbee84353e
19 changed files with 192 additions and 322 deletions
+1 -2
View File
@@ -31,7 +31,6 @@ class StopDatabaseProxy
$database->save();
DatabaseProxyStopped::dispatch();
DatabaseProxyStopped::dispatch($server->team_id);
}
}
+2 -23
View File
@@ -2,34 +2,13 @@
namespace App\Events;
use App\Traits\BroadcastsToTeam;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ApplicationConfigurationChanged implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?int $teamId = null;
public function __construct($teamId = null)
{
if (is_null($teamId) && auth()->check() && auth()->user()->currentTeam()) {
$teamId = auth()->user()->currentTeam()->id;
}
$this->teamId = $teamId;
}
public function broadcastOn(): array
{
if (is_null($this->teamId)) {
return [];
}
return [
new PrivateChannel("team.{$this->teamId}"),
];
}
use BroadcastsToTeam, Dispatchable, InteractsWithSockets, SerializesModels;
}
+2 -23
View File
@@ -2,34 +2,13 @@
namespace App\Events;
use App\Traits\BroadcastsToTeam;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ApplicationStatusChanged implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?int $teamId = null;
public function __construct($teamId = null)
{
if (is_null($teamId) && auth()->check() && auth()->user()->currentTeam()) {
$teamId = auth()->user()->currentTeam()->id;
}
$this->teamId = $teamId;
}
public function broadcastOn(): array
{
if (is_null($this->teamId)) {
return [];
}
return [
new PrivateChannel("team.{$this->teamId}"),
];
}
use BroadcastsToTeam, Dispatchable, InteractsWithSockets, SerializesModels;
}
+2 -23
View File
@@ -2,8 +2,8 @@
namespace App\Events;
use App\Traits\BroadcastsToTeam;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
@@ -11,26 +11,5 @@ use Laravel\Horizon\Contracts\Silenced;
class BackupCreated implements ShouldBroadcast, Silenced
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?int $teamId = null;
public function __construct($teamId = null)
{
if (is_null($teamId) && auth()->check() && auth()->user()->currentTeam()) {
$teamId = auth()->user()->currentTeam()->id;
}
$this->teamId = $teamId;
}
public function broadcastOn(): array
{
if (is_null($this->teamId)) {
return [];
}
return [
new PrivateChannel("team.{$this->teamId}"),
];
}
use BroadcastsToTeam, Dispatchable, InteractsWithSockets, SerializesModels;
}
+2 -23
View File
@@ -2,34 +2,13 @@
namespace App\Events;
use App\Traits\BroadcastsToTeam;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class CloudflareTunnelConfigured implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?int $teamId = null;
public function __construct($teamId = null)
{
if (is_null($teamId) && auth()->check() && auth()->user()->currentTeam()) {
$teamId = auth()->user()->currentTeam()->id;
}
$this->teamId = $teamId;
}
public function broadcastOn(): array
{
if (is_null($this->teamId)) {
return [];
}
return [
new PrivateChannel("team.{$this->teamId}"),
];
}
use BroadcastsToTeam, Dispatchable, InteractsWithSockets, SerializesModels;
}
+2 -23
View File
@@ -2,34 +2,13 @@
namespace App\Events;
use App\Traits\BroadcastsToTeam;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class DatabaseProxyStopped implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?int $teamId = null;
public function __construct($teamId = null)
{
if (is_null($teamId) && auth()->check() && auth()->user()->currentTeam()) {
$teamId = auth()->user()->currentTeam()->id;
}
$this->teamId = $teamId;
}
public function broadcastOn(): array
{
if (is_null($this->teamId)) {
return [];
}
return [
new PrivateChannel("team.{$this->teamId}"),
];
}
use BroadcastsToTeam, Dispatchable, InteractsWithSockets, SerializesModels;
}
+2 -23
View File
@@ -2,34 +2,13 @@
namespace App\Events;
use App\Traits\BroadcastsToTeam;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class FileStorageChanged implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?int $teamId = null;
public function __construct($teamId = null)
{
if (is_null($teamId) && auth()->check() && auth()->user()->currentTeam()) {
$teamId = auth()->user()->currentTeam()->id;
}
$this->teamId = $teamId;
}
public function broadcastOn(): array
{
if (is_null($this->teamId)) {
return [];
}
return [
new PrivateChannel("team.{$this->teamId}"),
];
}
use BroadcastsToTeam, Dispatchable, InteractsWithSockets, SerializesModels;
}
+3 -19
View File
@@ -2,37 +2,21 @@
namespace App\Events;
use App\Traits\BroadcastsToTeam;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ProxyStatusChangedUI implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?int $teamId = null;
use BroadcastsToTeam, Dispatchable, InteractsWithSockets, SerializesModels;
public ?int $activityId = null;
public function __construct(?int $teamId = null, ?int $activityId = null)
public function __construct(?int $teamId, ?int $activityId = null)
{
if (is_null($teamId) && auth()->check() && auth()->user()->currentTeam()) {
$teamId = auth()->user()->currentTeam()->id;
}
$this->teamId = $teamId;
$this->activityId = $activityId;
}
public function broadcastOn(): array
{
if (is_null($this->teamId)) {
return [];
}
return [
new PrivateChannel("team.{$this->teamId}"),
];
}
}
+2 -23
View File
@@ -2,34 +2,13 @@
namespace App\Events;
use App\Traits\BroadcastsToTeam;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ScheduledTaskDone implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?int $teamId = null;
public function __construct($teamId = null)
{
if (is_null($teamId) && auth()->check() && auth()->user()->currentTeam()) {
$teamId = auth()->user()->currentTeam()->id;
}
$this->teamId = $teamId;
}
public function broadcastOn(): array
{
if (is_null($this->teamId)) {
return [];
}
return [
new PrivateChannel("team.{$this->teamId}"),
];
}
use BroadcastsToTeam, Dispatchable, InteractsWithSockets, SerializesModels;
}
+2 -15
View File
@@ -3,17 +3,15 @@
namespace App\Events;
use App\Models\Server;
use App\Traits\BroadcastsToTeam;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class SentinelRestarted implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?int $teamId = null;
use BroadcastsToTeam, Dispatchable, InteractsWithSockets, SerializesModels;
public ?string $version = null;
@@ -25,15 +23,4 @@ class SentinelRestarted implements ShouldBroadcast
$this->serverUuid = $server->uuid;
$this->version = $version;
}
public function broadcastOn(): array
{
if (is_null($this->teamId)) {
return [];
}
return [
new PrivateChannel("team.{$this->teamId}"),
];
}
}
+2 -15
View File
@@ -3,17 +3,15 @@
namespace App\Events;
use App\Models\Server;
use App\Traits\BroadcastsToTeam;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class SentinelSynchronized implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?int $teamId = null;
use BroadcastsToTeam, Dispatchable, InteractsWithSockets, SerializesModels;
public string $serverUuid;
@@ -22,15 +20,4 @@ class SentinelSynchronized implements ShouldBroadcast
$this->teamId = $server->team_id;
$this->serverUuid = $server->uuid;
}
public function broadcastOn(): array
{
if (is_null($this->teamId)) {
return [];
}
return [
new PrivateChannel("team.{$this->teamId}"),
];
}
}
+2 -23
View File
@@ -2,34 +2,13 @@
namespace App\Events;
use App\Traits\BroadcastsToTeam;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ServerPackageUpdated implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?int $teamId = null;
public function __construct($teamId = null)
{
if (is_null($teamId) && auth()->check() && auth()->user()->currentTeam()) {
$teamId = auth()->user()->currentTeam()->id;
}
$this->teamId = $teamId;
}
public function broadcastOn(): array
{
if (is_null($this->teamId)) {
return [];
}
return [
new PrivateChannel("team.{$this->teamId}"),
];
}
use BroadcastsToTeam, Dispatchable, InteractsWithSockets, SerializesModels;
}
+3 -19
View File
@@ -2,40 +2,24 @@
namespace App\Events;
use App\Traits\BroadcastsToTeam;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ServerValidated implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?int $teamId = null;
use BroadcastsToTeam, Dispatchable, InteractsWithSockets, SerializesModels;
public ?string $serverUuid = null;
public function __construct(?int $teamId = null, ?string $serverUuid = null)
public function __construct(?int $teamId, ?string $serverUuid = null)
{
if (is_null($teamId) && auth()->check() && auth()->user()->currentTeam()) {
$teamId = auth()->user()->currentTeam()->id;
}
$this->teamId = $teamId;
$this->serverUuid = $serverUuid;
}
public function broadcastOn(): array
{
if (is_null($this->teamId)) {
return [];
}
return [
new PrivateChannel("team.{$this->teamId}"),
];
}
public function broadcastAs(): string
{
return 'ServerValidated';
+2 -23
View File
@@ -2,8 +2,8 @@
namespace App\Events;
use App\Traits\BroadcastsToTeam;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
@@ -11,26 +11,5 @@ use Laravel\Horizon\Contracts\Silenced;
class ServiceChecked implements ShouldBroadcast, Silenced
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?int $teamId = null;
public function __construct($teamId = null)
{
if (is_null($teamId) && auth()->check() && auth()->user()->currentTeam()) {
$teamId = auth()->user()->currentTeam()->id;
}
$this->teamId = $teamId;
}
public function broadcastOn(): array
{
if (is_null($this->teamId)) {
return [];
}
return [
new PrivateChannel("team.{$this->teamId}"),
];
}
use BroadcastsToTeam, Dispatchable, InteractsWithSockets, SerializesModels;
}
+2 -22
View File
@@ -2,33 +2,13 @@
namespace App\Events;
use App\Traits\BroadcastsToTeam;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Auth;
class ServiceStatusChanged implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct(
public ?int $teamId = null
) {
if (is_null($this->teamId) && Auth::check() && Auth::user()->currentTeam()) {
$this->teamId = Auth::user()->currentTeam()->id;
}
}
public function broadcastOn(): array
{
if (is_null($this->teamId)) {
return [];
}
return [
new PrivateChannel("team.{$this->teamId}"),
];
}
use BroadcastsToTeam, Dispatchable, InteractsWithSockets, SerializesModels;
}
+2 -22
View File
@@ -2,33 +2,13 @@
namespace App\Events;
use App\Traits\BroadcastsToTeam;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class TestEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public ?int $teamId = null;
public function __construct()
{
if (auth()->check() && auth()->user()->currentTeam()) {
$this->teamId = auth()->user()->currentTeam()->id;
}
}
public function broadcastOn(): array
{
if (is_null($this->teamId)) {
return [];
}
return [
new PrivateChannel("team.{$this->teamId}"),
];
}
use BroadcastsToTeam, Dispatchable, InteractsWithSockets, SerializesModels;
}
+1 -1
View File
@@ -33,7 +33,7 @@ class Controller extends BaseController
if (auth()->user()?->currentTeam()->id !== 0) {
return redirect(RouteServiceProvider::HOME);
}
TestEvent::dispatch();
TestEvent::dispatch(currentTeam()->id);
return 'Look at your other tab.';
}
+34
View File
@@ -0,0 +1,34 @@
<?php
namespace App\Traits;
use Illuminate\Broadcasting\PrivateChannel;
/**
* Broadcasts an event on the private channel of an explicit team.
*
* The team must come from the resource the event is about, never from the session:
* the session team can differ from the resource team (e.g. after a team switch in
* another tab), and queued jobs have no session at all.
*/
trait BroadcastsToTeam
{
public ?int $teamId = null;
public function __construct(?int $teamId)
{
$this->teamId = $teamId;
}
/**
* @return array<int, PrivateChannel>
*/
public function broadcastOn(): array
{
if (is_null($this->teamId)) {
return [];
}
return [new PrivateChannel("team.{$this->teamId}")];
}
}
+124
View File
@@ -0,0 +1,124 @@
<?php
use App\Actions\Database\StopDatabaseProxy;
use App\Events\ApplicationConfigurationChanged;
use App\Events\ApplicationStatusChanged;
use App\Events\BackupCreated;
use App\Events\CloudflareTunnelConfigured;
use App\Events\DatabaseProxyStopped;
use App\Events\FileStorageChanged;
use App\Events\ProxyStatusChangedUI;
use App\Events\ScheduledTaskDone;
use App\Events\SentinelRestarted;
use App\Events\SentinelSynchronized;
use App\Events\ServerPackageUpdated;
use App\Events\ServerValidated;
use App\Events\ServiceChecked;
use App\Events\ServiceStatusChanged;
use App\Events\TestEvent;
use App\Models\Environment;
use App\Models\InstanceSettings;
use App\Models\Project;
use App\Models\Server;
use App\Models\StandaloneDocker;
use App\Models\StandalonePostgresql;
use App\Models\Team;
use App\Models\User;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Str;
uses(RefreshDatabase::class);
beforeEach(function () {
config(['app.maintenance.store' => 'array', 'cache.default' => 'array']);
InstanceSettings::unguarded(fn () => InstanceSettings::updateOrCreate(['id' => 0], ['id' => 0]));
$this->team = Team::factory()->create();
});
function teamBroadcastChannelNames(object $event): array
{
return array_map(fn (PrivateChannel $channel) => $channel->name, $event->broadcastOn());
}
dataset('team broadcast events', [
ApplicationConfigurationChanged::class,
ApplicationStatusChanged::class,
BackupCreated::class,
CloudflareTunnelConfigured::class,
DatabaseProxyStopped::class,
FileStorageChanged::class,
ProxyStatusChangedUI::class,
ScheduledTaskDone::class,
ServerPackageUpdated::class,
ServerValidated::class,
ServiceChecked::class,
ServiceStatusChanged::class,
TestEvent::class,
]);
it('broadcasts on the given team channel, not the session team', function (string $eventClass) {
$otherTeam = Team::factory()->create();
$user = User::factory()->create();
$user->teams()->attach($otherTeam, ['role' => 'owner']);
$this->actingAs($user);
session(['currentTeam' => $otherTeam]);
expect(teamBroadcastChannelNames(new $eventClass($this->team->id)))
->toBe(["private-team.{$this->team->id}"]);
})->with('team broadcast events');
it('does not broadcast without a team', function (string $eventClass) {
expect((new $eventClass(null))->broadcastOn())->toBe([]);
})->with('team broadcast events');
it('broadcasts sentinel events on the server team channel', function () {
$server = new Server(['team_id' => $this->team->id]);
$server->uuid = 'server-uuid';
expect(teamBroadcastChannelNames(new SentinelRestarted($server, '1.0.0')))
->toBe(["private-team.{$this->team->id}"])
->and(teamBroadcastChannelNames(new SentinelSynchronized($server)))
->toBe(["private-team.{$this->team->id}"]);
});
it('announces a stopped database proxy to the team that owns the server', function () {
Event::fake([DatabaseProxyStopped::class]);
Process::fake(fn () => Process::result(output: 'OK'));
$keyId = DB::table('private_keys')->insertGetId([
'uuid' => (string) Str::uuid(),
'name' => 'Test Key',
'private_key' => Crypt::encryptString('test-key'),
'team_id' => $this->team->id,
'created_at' => now(),
'updated_at' => now(),
]);
$server = Server::factory()->create(['team_id' => $this->team->id, 'private_key_id' => $keyId]);
$destination = StandaloneDocker::withoutEvents(fn () => StandaloneDocker::firstOrCreate(
['server_id' => $server->id, 'network' => 'coolify'],
['uuid' => (string) Str::uuid(), 'name' => 'test-docker']
));
$project = Project::create(['uuid' => (string) Str::uuid(), 'name' => 'Project', 'team_id' => $this->team->id]);
$environment = $project->environments()->first() ?? Environment::factory()->create(['project_id' => $project->id]);
$database = StandalonePostgresql::create([
'uuid' => (string) Str::uuid(),
'name' => 'db',
'postgres_user' => 'postgres',
'postgres_password' => 'password',
'postgres_db' => 'db',
'image' => 'postgres:17',
'environment_id' => $environment->id,
'destination_id' => $destination->id,
'destination_type' => $destination->getMorphClass(),
]);
// Runs as a queued job in production: there is no authenticated user.
StopDatabaseProxy::run($database);
Event::assertDispatched(DatabaseProxyStopped::class, fn (DatabaseProxyStopped $event) => $event->teamId === $this->team->id);
});