1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coyote\Notifications\Topic; |
4
|
|
|
|
5
|
|
|
use Coyote\Services\Notification\Notification; |
6
|
|
|
use Coyote\Services\UrlBuilder; |
7
|
|
|
use Coyote\Topic; |
8
|
|
|
use Coyote\User; |
9
|
|
|
use Illuminate\Bus\Queueable; |
10
|
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; |
11
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
12
|
|
|
use Illuminate\Notifications\Messages\BroadcastMessage; |
13
|
|
|
use NotificationChannels\WebPush\WebPushMessage; |
14
|
|
|
|
15
|
|
|
abstract class AbstractNotification extends Notification implements ShouldQueue, ShouldBroadcast |
16
|
|
|
{ |
17
|
|
|
use Queueable; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Topic |
21
|
|
|
*/ |
22
|
|
|
protected $topic; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var User|null |
26
|
|
|
*/ |
27
|
|
|
protected $notifier; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $reasonName; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $reasonText; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param User|null $notifier |
41
|
|
|
* @param Topic $topic |
42
|
|
|
*/ |
43
|
|
|
public function __construct(?User $notifier, Topic $topic) |
44
|
|
|
{ |
45
|
|
|
$this->notifier = $notifier; |
46
|
|
|
$this->topic = $topic; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getReasonName(): string |
53
|
|
|
{ |
54
|
|
|
return $this->reasonName ?: '(nie podano powodu)'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string|null $reasonName |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function setReasonName(?string $reasonName) |
62
|
|
|
{ |
63
|
|
|
$this->reasonName = $reasonName; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function getReasonText(): string |
72
|
|
|
{ |
73
|
|
|
return $this->reasonText ?: '(nie podano powodu)'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string|null $reasonText |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function setReasonText(?string $reasonText) |
81
|
|
|
{ |
82
|
|
|
$this->reasonText = $reasonText; |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Unikalne ID okreslajace dano powiadomienie. To ID posluzy do grupowania powiadomien tego samego typu |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function objectId() |
93
|
|
|
{ |
94
|
|
|
return substr(md5(class_basename($this) . $this->topic->title . $this->topic->id), 16); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
abstract protected function getMailSubject(): string; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param User $user |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function via(User $user) |
107
|
|
|
{ |
108
|
|
|
if (!$user->can('access', $this->topic->forum)) { |
109
|
|
|
return []; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return parent::channels($user); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param User $user |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public function toDatabase($user) |
120
|
|
|
{ |
121
|
|
|
return [ |
122
|
|
|
'object_id' => $this->objectId(), |
123
|
|
|
'user_id' => $user->id, |
124
|
|
|
'type_id' => static::ID, |
|
|
|
|
125
|
|
|
'subject' => $this->topic->title, |
126
|
|
|
'excerpt' => $this->getReasonName(), |
127
|
|
|
'url' => UrlBuilder::topic($this->topic), |
128
|
|
|
'id' => $this->id |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function sender() |
136
|
|
|
{ |
137
|
|
|
return [ |
138
|
|
|
'name' => $this->notifier->name, |
139
|
|
|
'user_id' => $this->notifier->id |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return BroadcastMessage |
145
|
|
|
*/ |
146
|
|
|
public function toBroadcast() |
147
|
|
|
{ |
148
|
|
|
return new BroadcastMessage([ |
149
|
|
|
'headline' => $this->getMailSubject(), |
150
|
|
|
'subject' => $this->topic->title, |
151
|
|
|
'url' => $this->notificationUrl() |
152
|
|
|
]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function toWebPush() |
156
|
|
|
{ |
157
|
|
|
return (new WebPushMessage()) |
158
|
|
|
->title($this->getMailSubject()) |
159
|
|
|
->icon('/apple-touch.png') |
160
|
|
|
->body($this->topic->title) |
161
|
|
|
->data(['url' => $this->notificationUrl()]) |
162
|
|
|
->options(['TTL' => 1000]); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|