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 array $channels */ |
48
|
|
|
$channels = $user->notificationSettings()->select('channel')->where('type_id', static::ID)->where('is_enabled', true)->pluck('channel')->toArray(); |
|
|
|
|
49
|
|
|
|
50
|
|
|
if (empty($channels)) { |
51
|
|
|
return []; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// extra channel: broadcast notification via web socket |
55
|
|
|
if (in_array(Model::DB, $channels) && $this instanceof ShouldBroadcast) { |
56
|
|
|
$channels[] = Model::BROADCAST; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// remove mail channel from the list if user can't get emails (email address is not verified) |
60
|
|
|
if (in_array(Model::MAIL, $channels) && !$user->canReceiveEmail()) { |
61
|
|
|
$channels = $this->forget($channels, Model::MAIL); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// do not send another notification if previous was not yet read |
65
|
|
|
if (!empty($user->getUnreadNotification($this->objectId()))) { |
66
|
|
|
$channels = $this->forget($channels, Model::MAIL, Model::PUSH, Model::BROADCAST); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->resolveChannels($channels); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $channels |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
private function resolveChannels(array $channels): array |
77
|
|
|
{ |
78
|
|
|
$replacement = [Model::DB => DatabaseChannel::class, Model::PUSH => WebPushChannel::class]; |
79
|
|
|
|
80
|
|
|
foreach ($replacement as $channel => $class) { |
81
|
|
|
if (in_array($channel, $channels)) { |
82
|
|
|
$channels = array_prepend($this->forget($channels, $channel), $class); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $channels; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
protected function redirectionUrl(): string |
93
|
|
|
{ |
94
|
|
|
return route('user.notifications.url', [$this->id]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function forget($channels, ...$values) |
98
|
|
|
{ |
99
|
|
|
foreach ($values as $value) { |
100
|
|
|
if (($key = array_search($value, $channels)) !== false) { |
101
|
|
|
unset($channels[$key]); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $channels; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|