diff --git a/database/migrations/2025_04_28_090019_create_notifications_table.php b/database/migrations/2025_04_28_090019_create_notifications_table.php index d738032..2a97d4b 100644 --- a/database/migrations/2025_04_28_090019_create_notifications_table.php +++ b/database/migrations/2025_04_28_090019_create_notifications_table.php @@ -15,6 +15,11 @@ public function up(): void $table->uuid('id')->primary(); $table->string('type'); $table->morphs('notifiable'); + $table->foreignId('site_id') + ->nullable() + ->constrained('sites') + ->cascadeOnUpdate() + ->cascadeOnDelete(); $table->text('data'); $table->timestamp('read_at')->nullable(); $table->timestamps(); diff --git a/src/EclipseServiceProvider.php b/src/EclipseServiceProvider.php index f2b52cb..3e55adf 100644 --- a/src/EclipseServiceProvider.php +++ b/src/EclipseServiceProvider.php @@ -16,6 +16,7 @@ use Eclipse\Core\Models\User; use Eclipse\Core\Models\User\Permission; use Eclipse\Core\Models\User\Role; +use Eclipse\Core\Notifications\Channels\SiteDatabaseChannel; use Eclipse\Core\Policies\User\RolePolicy; use Eclipse\Core\Providers\AdminPanelProvider; use Eclipse\Core\Providers\HorizonServiceProvider; @@ -28,6 +29,7 @@ use Illuminate\Auth\Events\Login; use Illuminate\Database\Eloquent\Model; use Illuminate\Mail\Events\MessageSent; +use Illuminate\Notifications\Channels\DatabaseChannel; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Event; @@ -104,6 +106,8 @@ public function register(): self return new Registry; }); + $this->app->bind(DatabaseChannel::class, SiteDatabaseChannel::class); + return $this; } diff --git a/src/Models/DatabaseNotification.php b/src/Models/DatabaseNotification.php new file mode 100644 index 0000000..3f6f7ca --- /dev/null +++ b/src/Models/DatabaseNotification.php @@ -0,0 +1,34 @@ +where($builder->getModel()->getTable().'.site_id', $siteId); + } + }); + } +} diff --git a/src/Models/User.php b/src/Models/User.php index 6c85afb..087c712 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -15,6 +15,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -184,6 +185,15 @@ public function getSettings(string $settingsClass = UserSettings::class): Settin return $settingsClass::forUser($this->id); } + /** + * Override notifications relation to use site-aware notification model. + */ + public function notifications(): MorphMany + { + return $this->morphMany(DatabaseNotification::class, 'notifiable') + ->orderBy('created_at', 'desc'); + } + /** * The channels the user receives notification broadcasts on. */ diff --git a/src/Notifications/Channels/SiteDatabaseChannel.php b/src/Notifications/Channels/SiteDatabaseChannel.php new file mode 100644 index 0000000..035aa7a --- /dev/null +++ b/src/Notifications/Channels/SiteDatabaseChannel.php @@ -0,0 +1,37 @@ +resolveSiteIdFromNotification($notification); + $resolved = $fromNotification ?? Context::get('site'); + $payload['site_id'] = $resolved; + + return $payload; + } + + /** + * Prefer an explicit site id coming from the notification instance + * (useful when dispatching from queues) before resolving ambient context. + */ + protected function resolveSiteIdFromNotification(Notification $notification): ?int + { + if (method_exists($notification, 'getSiteId')) { + return $notification->getSiteId(); + } + + return null; + } +}