Browse Source

action 触发站内信息

moell 11 months ago
parent
commit
32d7c8aa9f

+ 29 - 0
app/Listeners/SendActionBrowserNotification.php

@@ -4,7 +4,10 @@ namespace App\Listeners;
 
 use App\Events\ObjectActionCreate;
 use App\Models\Enums\ActionObjectType;
+use App\Models\Enums\NotificationObjectType;
 use App\Models\Enums\ObjectAction;
+use App\Models\Notification;
+use App\Models\NotificationRecord;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Queue\InteractsWithQueue;
 
@@ -23,7 +26,33 @@ class SendActionBrowserNotification implements ShouldQueue
      */
     public function handle(ObjectActionCreate $event): void
     {
+        $actionObjectType = ActionObjectType::tryFrom($event->action->object_type);
+
+        $object = $actionObjectType->modelBuilder()->find($event->action->object_id);
+        if (! $object) {
+            return;
+        }
+
+        $userIds = match ($actionObjectType) {
+            ActionObjectType::TASK => $object->assign > 0 ? [$object->assign] : [],
+            default => [],
+        };
 
+        if (! $userIds) {
+            return;
+        }
+
+        $notification = Notification::query()->create([
+            'object_type' => NotificationObjectType::ACTION->value,
+            'object_id' => $event->action->id,
+        ]);
+
+        foreach ($userIds as $userId) {
+            NotificationRecord::query()->create([
+                'notification_id' => $notification->id,
+                'user_id' => $userId,
+            ]);
+        }
     }
 
     public function shouldQueue(ObjectActionCreate $event): bool

+ 10 - 0
app/Models/Enums/NotificationObjectType.php

@@ -0,0 +1,10 @@
+<?php
+
+namespace App\Models\Enums;
+
+enum NotificationObjectType: string
+{
+    case ANNOUNCEMENT = "announcement"; //公告
+
+    case ACTION = "action"; //Action 通知
+}

+ 13 - 0
app/Models/Notification.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class Notification extends Model
+{
+    use HasFactory;
+
+    protected $guarded = ['id'];
+}

+ 13 - 0
app/Models/NotificationRecord.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class NotificationRecord extends Model
+{
+    use HasFactory;
+
+    protected $guarded = ['id'];
+}

+ 0 - 54
app/Notifications/TaskAction.php

@@ -1,54 +0,0 @@
-<?php
-
-namespace App\Notifications;
-
-use Illuminate\Bus\Queueable;
-use Illuminate\Contracts\Queue\ShouldQueue;
-use Illuminate\Notifications\Messages\MailMessage;
-use Illuminate\Notifications\Notification;
-
-class TaskAction extends Notification
-{
-    use Queueable;
-
-    /**
-     * Create a new notification instance.
-     */
-    public function __construct()
-    {
-        //
-    }
-
-    /**
-     * Get the notification's delivery channels.
-     *
-     * @return array<int, string>
-     */
-    public function via(object $notifiable): array
-    {
-        return ['mail'];
-    }
-
-    /**
-     * Get the mail representation of the notification.
-     */
-    public function toMail(object $notifiable): MailMessage
-    {
-        return (new MailMessage)
-                    ->line('The introduction to the notification.')
-                    ->action('Notification Action', url('/'))
-                    ->line('Thank you for using our application!');
-    }
-
-    /**
-     * Get the array representation of the notification.
-     *
-     * @return array<string, mixed>
-     */
-    public function toArray(object $notifiable): array
-    {
-        return [
-            //
-        ];
-    }
-}

+ 6 - 4
app/Providers/EventServiceProvider.php

@@ -3,6 +3,7 @@
 namespace App\Providers;
 
 use App\Events\ObjectActionCreate;
+use App\Listeners\SendActionBrowserNotification;
 use App\Listeners\SendActionEmailNotification;
 use Illuminate\Auth\Events\Registered;
 use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
@@ -20,6 +21,10 @@ class EventServiceProvider extends ServiceProvider
         Registered::class => [
             SendEmailVerificationNotification::class,
         ],
+        ObjectActionCreate::class => [
+            SendActionBrowserNotification::class,
+            SendActionEmailNotification::class,
+        ]
     ];
 
     /**
@@ -27,10 +32,7 @@ class EventServiceProvider extends ServiceProvider
      */
     public function boot(): void
     {
-        Event::listen(
-            ObjectActionCreate::class,
-            [SendActionEmailNotification::class, 'handle']
-        );
+
     }
 
     /**

+ 2 - 2
database/migrations/2024_04_07_211410_create_notifications_table.php

@@ -15,11 +15,11 @@ return new class extends Migration
             $table->id();
             $table->string('object_type', 30)->comment("action;announcement");
             $table->integer('object_id')->default(0);
-            $table->text('content');
+            $table->text('content')->nullable();
             $table->timestamp("start_at")->nullable();
             $table->timestamp("end_at")->nullable();
             $table->integer("created_by")->nullable();
-            $table->string("announcement_status")->nullable();
+            $table->string("status", 30)->nullable();
             $table->timestamps();
         });
     }

+ 1 - 1
database/migrations/2024_04_08_221714_create_notification_records_table.php

@@ -15,7 +15,7 @@ return new class extends Migration
             $table->id();
             $table->integer("notification_id");
             $table->integer("user_id");
-            $table->timestamp("read_at");
+            $table->timestamp("read_at")->nullable()->default(null);
             $table->timestamps();
         });
     }