1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coyote\Services\Notification; |
4
|
|
|
|
5
|
|
|
use Coyote\User; |
6
|
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; |
7
|
|
|
use Illuminate\Notifications\Notification as BaseNotification; |
8
|
|
|
use Coyote\Notification as Model; |
9
|
|
|
use NotificationChannels\WebPush\WebPushChannel; |
10
|
|
|
|
11
|
|
|
abstract class Notification extends BaseNotification implements NotificationInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string|null |
15
|
|
|
*/ |
16
|
|
|
public $broadcastChannel; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get the notification's delivery channels. |
20
|
|
|
* |
21
|
|
|
* @param User $user |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public function via(User $user) |
25
|
|
|
{ |
26
|
|
|
return $this->channels($user); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the channels the event should be broadcast on. |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
public function broadcastOn() |
35
|
|
|
{ |
36
|
|
|
return [$this->broadcastChannel]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param User $user |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
protected function channels(User $user): array |
44
|
|
|
{ |
45
|
|
|
$this->broadcastChannel = $user->receivesBroadcastNotificationsOn(); |
46
|
|
|
|
47
|
|
|
/** @var \Illuminate\Support\Collection $channels */ |
48
|
|
|
$channels = $user->notificationSettings()->select('channel')->where('type_id', static::ID)->where('is_enabled', true)->pluck('channel'); |
|
|
|
|
49
|
|
|
|
50
|
|
|
if (empty($channels)) { |
51
|
|
|
return []; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($channels->contains(Model::DB) && $this instanceof ShouldBroadcast) { |
55
|
|
|
$channels->push('broadcast'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($channels->contains(Model::MAIL) && !$user->canReceiveEmail()) { |
59
|
|
|
$channels->forget(Model::MAIL); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// do not send another notification if previous was not yet read |
63
|
|
|
if (!empty($user->getUnreadNotification($this->objectId()))) { |
64
|
|
|
$channels->forget([Model::MAIL, Model::PUSH]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->resolveChannels($channels); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param \Illuminate\Support\Collection $channels |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
private function resolveChannels($channels): array |
75
|
|
|
{ |
76
|
|
|
$replacement = [Model::DB => DatabaseChannel::class, Model::PUSH => WebPushChannel::class]; |
77
|
|
|
|
78
|
|
|
foreach ($replacement as $channel => $class) { |
79
|
|
|
if ($channels->contains($channel)) { |
80
|
|
|
$channels = $channels->reject(fn ($value) => $value === $channel)->push($class); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $channels->toArray(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
protected function notificationUrl() |
91
|
|
|
{ |
92
|
|
|
return route('user.notifications.url', [$this->id]); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|