From 01c5ce3e388d8eff79fcab575e0081c4380ea736 Mon Sep 17 00:00:00 2001 From: mahdihty Date: Mon, 8 Mar 2021 16:39:28 +0330 Subject: [PATCH] add some event, listener and fsm channel --- app/Channels/FcmChannel.php | 23 +++ app/Channels/Messages/FcmMessage.php | 132 ++++++++++++++++++ app/Enums/cruds.php | 6 +- app/Events/BusinessUserCreate.php | 38 +++++ app/Listeners/BusinessUserCreateNotif.php | 46 ++++++ app/Models/ReportableRelation.php | 32 +++-- app/Models/User.php | 10 ++ app/Notifications/DBNotification.php | 63 +++++++++ app/Notifications/FcmNotification.php | 48 +++++++ app/Notifications/MailNotification.php | 2 +- app/Providers/AppServiceProvider.php | 9 +- app/Providers/EventServiceProvider.php | 7 +- ...3_08_114700_create_notifications_table.php | 35 +++++ resources/lang/fa/notification.php | 4 + 14 files changed, 434 insertions(+), 21 deletions(-) create mode 100644 app/Channels/FcmChannel.php create mode 100644 app/Channels/Messages/FcmMessage.php create mode 100644 app/Events/BusinessUserCreate.php create mode 100644 app/Listeners/BusinessUserCreateNotif.php create mode 100644 app/Notifications/DBNotification.php create mode 100644 app/Notifications/FcmNotification.php create mode 100644 database/migrations/2021_03_08_114700_create_notifications_table.php diff --git a/app/Channels/FcmChannel.php b/app/Channels/FcmChannel.php new file mode 100644 index 0000000..e141bb4 --- /dev/null +++ b/app/Channels/FcmChannel.php @@ -0,0 +1,23 @@ +toFcm($notifiable); + + $recipients = $notifiable->routeNotificationFor('fcm', $notification); + } +} diff --git a/app/Channels/Messages/FcmMessage.php b/app/Channels/Messages/FcmMessage.php new file mode 100644 index 0000000..dc81a8a --- /dev/null +++ b/app/Channels/Messages/FcmMessage.php @@ -0,0 +1,132 @@ +to = $to[0]; + } else { + $this->to = $to; + } + + return $this; + } + + /** + * Set the topic of the FCM message. + * + * @param string $topic + * @return $this + */ + public function topic(string $topic) + { + $this->topic = $topic; + + return $this; + } + + /** + * Set the data of the FCM message. + * + * @param array $data + * @return $this + */ + public function data(array $data) + { + $this->data = $data; + + return $this; + } + + /** + * Set the notification of the FCM message. + * + * @param array $notification + * @return $this + */ + public function notification(array $notification) + { + $this->notification = $notification; + + return $this; + } + + /** + * Set the condition for receive the FCM message. + * + * @param string $condition + * @return $this + */ + public function condition(string $condition) + { + $this->condition = $condition; + + return $this; + } + + /** + * Set the priority of the FCM message. + * + * @param string $priority + * @return $this + */ + public function priority(string $priority) + { + $this->priority = $priority; + + return $this; + } +} diff --git a/app/Enums/cruds.php b/app/Enums/cruds.php index 1980e2e..283ea3b 100644 --- a/app/Enums/cruds.php +++ b/app/Enums/cruds.php @@ -3,9 +3,9 @@ return [ 'inverse' => [ - 10 => ['name' => 'Create'], - 20 => ['name' => 'Update'], - 30 => ['name' => 'Delete'], + 10 => ['name' => 'Create', 'singular_name' => 'create'], + 20 => ['name' => 'Update', 'singular_name' => 'update'], + 30 => ['name' => 'Delete', 'singular_name' => 'delete'], ], ]; diff --git a/app/Events/BusinessUserCreate.php b/app/Events/BusinessUserCreate.php new file mode 100644 index 0000000..b150a8d --- /dev/null +++ b/app/Events/BusinessUserCreate.php @@ -0,0 +1,38 @@ +message = $message; + } + + /** + * Get the channels the event should broadcast on. + * + * @return \Illuminate\Broadcasting\Channel|array + */ + public function broadcastOn() + { + return new PrivateChannel('channel-name'); + } +} diff --git a/app/Listeners/BusinessUserCreateNotif.php b/app/Listeners/BusinessUserCreateNotif.php new file mode 100644 index 0000000..caf33f0 --- /dev/null +++ b/app/Listeners/BusinessUserCreateNotif.php @@ -0,0 +1,46 @@ +message; + $new_user = User::findOrFail($payload->data->original->user_id); + $owners = Business::findOrFail($payload->business)->owners()->where('id', '!=', $new_user->id)->get(); + + $notif = [ + 'body' => __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name'), ['business' => request('_business_info')['name'], 'user' => $new_user->name]) + ]; + + $users = $owners->prepend($new_user); + Notification::send($users, new MailNotification($notif)); + Notification::send($users, new DBNotification($notif)); + } +} diff --git a/app/Models/ReportableRelation.php b/app/Models/ReportableRelation.php index b8b5d07..f525bf0 100644 --- a/app/Models/ReportableRelation.php +++ b/app/Models/ReportableRelation.php @@ -4,6 +4,7 @@ namespace App\Models; use Anik\Amqp\Exchange; use Anik\Amqp\Facades\Amqp; +use App\Events\ModelSaved; use PhpAmqpLib\Wire\AMQPTable; use Anik\Amqp\PublishableMessage; use Illuminate\Support\Facades\Auth; @@ -56,21 +57,22 @@ class ReportableRelation extends Pivot 'from' => env('CONTAINER_NAME'), ]; - $message = new PublishableMessage(json_encode($payload)); - - $routers = [ - "activity_exchange" => ["name" => "activity",], - "notif_exchange" => ["name" => "notif",], - "socket_exchange" => ["name" => "socket",], - ]; - - foreach ($routers as $exchange => $properties) { - $message->setProperties(["application_headers" => new AMQPTable($properties)]); - - $message->setExchange(new Exchange($exchange)); - - Amqp::publish($message, ""); - } + ModelSaved::dispatch(json_encode($payload)); +// $message = new PublishableMessage(json_encode($payload)); +// +// $routers = [ +// "activity_exchange" => ["name" => "activity",], +// "notif_exchange" => ["name" => "notif",], +// "socket_exchange" => ["name" => "socket",], +// ]; +// +// foreach ($routers as $exchange => $properties) { +// $message->setProperties(["application_headers" => new AMQPTable($properties)]); +// +// $message->setExchange(new Exchange($exchange)); +// +// Amqp::publish($message, ""); +// } } public function properties() diff --git a/app/Models/User.php b/app/Models/User.php index b1549d3..f6154ff 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -44,6 +44,16 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac public $detach_relation = false; + /** + * Specifies the user's FCM token + * + * @return string + */ + public function routeNotificationForFcm() + { + return $this->fingerprints()->fcm_token; + } + public function updateRelations() { // projects relations diff --git a/app/Notifications/DBNotification.php b/app/Notifications/DBNotification.php new file mode 100644 index 0000000..9c76954 --- /dev/null +++ b/app/Notifications/DBNotification.php @@ -0,0 +1,63 @@ +message = $message; + } + + /** + * Get the notification's delivery channels. + * + * @param mixed $notifiable + * @return array + */ + public function via($notifiable) + { + return ['database']; + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $notifiable + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($notifiable) + { + 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. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + 'data' => $this->message['body'] + ]; + } +} diff --git a/app/Notifications/FcmNotification.php b/app/Notifications/FcmNotification.php new file mode 100644 index 0000000..c302a64 --- /dev/null +++ b/app/Notifications/FcmNotification.php @@ -0,0 +1,48 @@ +to([]) + } + +} diff --git a/app/Notifications/MailNotification.php b/app/Notifications/MailNotification.php index 4122952..abe937b 100644 --- a/app/Notifications/MailNotification.php +++ b/app/Notifications/MailNotification.php @@ -67,7 +67,7 @@ class MailNotification extends Notification //implements ShouldQueue public function toMail($notifiable) { return (new MailMessage) - ->line(__('notification.'.$this->message)) + ->line($this->message['body']) ->action('Notification Action', url('/')) ->line('Thank you for using our application!'); } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index ee8ca5b..7b917de 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,9 @@ namespace App\Providers; +use App\Channels\FcmChannel; +use Illuminate\Notifications\ChannelManager; +use Illuminate\Support\Facades\Notification; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -13,7 +16,11 @@ class AppServiceProvider extends ServiceProvider */ public function register() { - // + Notification::resolved(function (ChannelManager $service) { + $service->extend('fcm', function ($app) { + return new FcmChannel(new HttpClient, config('fcm.key')); + }); + }); } /** diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index c627395..c3a0983 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -2,9 +2,11 @@ namespace App\Providers; +use App\Events\BusinessUserCreate; use App\Events\ModelSaved; use App\Events\TagCreate; use App\Listeners\ActivityRegistration; +use App\Listeners\BusinessUserCreateNotif; use App\Listeners\NotifHandler; use App\Listeners\TagCreateNotif; use Illuminate\Auth\Events\Registered; @@ -29,7 +31,10 @@ class EventServiceProvider extends ServiceProvider ], TagCreate::class => [ TagCreateNotif::class, - ] + ], + BusinessUserCreate::class => [ + BusinessUserCreateNotif::class, + ], ]; /** diff --git a/database/migrations/2021_03_08_114700_create_notifications_table.php b/database/migrations/2021_03_08_114700_create_notifications_table.php new file mode 100644 index 0000000..9797596 --- /dev/null +++ b/database/migrations/2021_03_08_114700_create_notifications_table.php @@ -0,0 +1,35 @@ +uuid('id')->primary(); + $table->string('type'); + $table->morphs('notifiable'); + $table->text('data'); + $table->timestamp('read_at')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('notifications'); + } +} diff --git a/resources/lang/fa/notification.php b/resources/lang/fa/notification.php index e885bcc..d6e95b4 100644 --- a/resources/lang/fa/notification.php +++ b/resources/lang/fa/notification.php @@ -15,6 +15,10 @@ return [ 'tags' => [ 'create' => 'یک تگ ساخته شد.' + ], + + 'business_user' => [ + 'create' => 'کاربر :user به کسب و کار :business اضافه شد.' ] ];