Skip to content

Commit 009ddba

Browse files
committed
Update notifications with socket
1 parent ed36af6 commit 009ddba

4 files changed

Lines changed: 107 additions & 3 deletions

File tree

Config/config.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
"name" => __('Database'),
5252
"id" => "database"
5353
],
54+
[
55+
"name" => "Socket",
56+
"id" => "socket"
57+
],
5458
[
5559
"name" => __('Email'),
5660
"id" => "email"

Events/FireEvent.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Modules\Notifications\Events;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class FireEvent implements ShouldBroadcast
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
/**
16+
* Create a new event instance.
17+
*
18+
* @return void
19+
*/
20+
public function __construct(
21+
public ?string $title,
22+
public ?string $message,
23+
public ?string $icon,
24+
public ?string $image,
25+
public ?string $url,
26+
public ?string $type,
27+
public ?string $privacy,
28+
public ?string $provider,
29+
public ?string $model,
30+
public ?string $model_id
31+
){}
32+
33+
public function broadcastAs(): string
34+
{
35+
return 'notification';
36+
}
37+
38+
/**
39+
* Get the channels the event should broadcast on.
40+
*
41+
* @return Channel
42+
*/
43+
public function broadcastOn() :Channel
44+
{
45+
return new Channel('private.' . $this->model_id);
46+
}
47+
}

Routes/web.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Illuminate\Support\Facades\Broadcast;
34
use Illuminate\Support\Facades\Route;
45
use Modules\Notifications\Http\Controllers\NotificationsController;
56

@@ -14,6 +15,9 @@
1415
|
1516
*/
1617

18+
Broadcast::channel('private.{userId}', function ($user, $userId) {
19+
return true;
20+
});
1721

1822
Route::middleware([
1923
'auth:sanctum',

Services/Actions/SendToJob.php

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,62 @@ public function sendToJob():void
3636
}
3737
if (count($collectRoles)) {
3838
if ($this->user->hasRole($collectRoles)) {
39-
NotificationJop::dispatch($arrgs);
39+
if($provider === 'socket'){
40+
\Modules\Notifications\Events\FireEvent::dispatch(
41+
$this->title,
42+
$this->message,
43+
$this->icon,
44+
$this->image,
45+
$this->url,
46+
$this->type,
47+
$this->privacy,
48+
$provider,
49+
$this->model,
50+
$this->user->id
51+
);
52+
}
53+
else {
54+
NotificationJop::dispatch($arrgs);
55+
}
56+
4057
}
4158
} else {
42-
NotificationJop::dispatch($arrgs);
59+
if($provider === 'socket'){
60+
\Modules\Notifications\Events\FireEvent::dispatch(
61+
$this->title,
62+
$this->message,
63+
$this->icon,
64+
$this->image,
65+
$this->url,
66+
$this->type,
67+
$this->privacy,
68+
$provider,
69+
$this->model,
70+
$this->user->id
71+
);
72+
}
73+
else {
74+
NotificationJop::dispatch($arrgs);
75+
}
4376
}
4477
} else {
45-
NotificationJop::dispatch($arrgs);
78+
if($provider === 'socket'){
79+
\Modules\Notifications\Events\FireEvent::dispatch(
80+
$this->title,
81+
$this->message,
82+
$this->icon,
83+
$this->image,
84+
$this->url,
85+
$this->type,
86+
$this->privacy,
87+
$provider,
88+
$this->model,
89+
$this->user->id
90+
);
91+
}
92+
else {
93+
NotificationJop::dispatch($arrgs);
94+
}
4695
}
4796
}
4897
}

0 commit comments

Comments
 (0)