Skip to content

Commit

Permalink
Update notifications with socket
Browse files Browse the repository at this point in the history
  • Loading branch information
3x1io committed Nov 22, 2022
1 parent ed36af6 commit 009ddba
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
"name" => __('Database'),
"id" => "database"
],
[
"name" => "Socket",
"id" => "socket"
],
[
"name" => __('Email'),
"id" => "email"
Expand Down
47 changes: 47 additions & 0 deletions Events/FireEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Modules\Notifications\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class FireEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(
public ?string $title,
public ?string $message,
public ?string $icon,
public ?string $image,
public ?string $url,
public ?string $type,
public ?string $privacy,
public ?string $provider,
public ?string $model,
public ?string $model_id
){}

public function broadcastAs(): string
{
return 'notification';
}

/**
* Get the channels the event should broadcast on.
*
* @return Channel
*/
public function broadcastOn() :Channel
{
return new Channel('private.' . $this->model_id);
}
}
4 changes: 4 additions & 0 deletions Routes/web.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Support\Facades\Broadcast;
use Illuminate\Support\Facades\Route;
use Modules\Notifications\Http\Controllers\NotificationsController;

Expand All @@ -14,6 +15,9 @@
|
*/

Broadcast::channel('private.{userId}', function ($user, $userId) {
return true;
});

Route::middleware([
'auth:sanctum',
Expand Down
55 changes: 52 additions & 3 deletions Services/Actions/SendToJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,62 @@ public function sendToJob():void
}
if (count($collectRoles)) {
if ($this->user->hasRole($collectRoles)) {
NotificationJop::dispatch($arrgs);
if($provider === 'socket'){
\Modules\Notifications\Events\FireEvent::dispatch(
$this->title,
$this->message,
$this->icon,
$this->image,
$this->url,
$this->type,
$this->privacy,
$provider,
$this->model,
$this->user->id
);
}
else {
NotificationJop::dispatch($arrgs);
}

}
} else {
NotificationJop::dispatch($arrgs);
if($provider === 'socket'){
\Modules\Notifications\Events\FireEvent::dispatch(
$this->title,
$this->message,
$this->icon,
$this->image,
$this->url,
$this->type,
$this->privacy,
$provider,
$this->model,
$this->user->id
);
}
else {
NotificationJop::dispatch($arrgs);
}
}
} else {
NotificationJop::dispatch($arrgs);
if($provider === 'socket'){
\Modules\Notifications\Events\FireEvent::dispatch(
$this->title,
$this->message,
$this->icon,
$this->image,
$this->url,
$this->type,
$this->privacy,
$provider,
$this->model,
$this->user->id
);
}
else {
NotificationJop::dispatch($arrgs);
}
}
}
}
Expand Down

0 comments on commit 009ddba

Please sign in to comment.