|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coyote\Notifications; |
|
4
|
|
|
|
|
5
|
|
|
use Coyote\User; |
|
6
|
|
|
use Coyote\Pm; |
|
7
|
|
|
use Coyote\Services\Notification\Notification; |
|
8
|
|
|
use Illuminate\Bus\Queueable; |
|
9
|
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; |
|
10
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
11
|
|
|
use Illuminate\Notifications\Messages\BroadcastMessage; |
|
12
|
|
|
use Illuminate\Notifications\Messages\MailMessage; |
|
13
|
|
|
use NotificationChannels\WebPush\WebPushMessage; |
|
14
|
|
|
|
|
15
|
|
|
class PmCreatedNotification extends Notification implements ShouldQueue, ShouldBroadcast |
|
16
|
|
|
{ |
|
17
|
|
|
use Queueable; |
|
18
|
|
|
|
|
19
|
|
|
const ID = \Coyote\Notification::PM; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Pm |
|
23
|
|
|
*/ |
|
24
|
|
|
private $pm; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $text; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Pm $pm |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(Pm $pm) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->pm = $pm; |
|
37
|
|
|
$this->text = app('parser.pm')->parse($this->pm->text->text); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get the mail representation of the notification. |
|
42
|
|
|
* |
|
43
|
|
|
* @return \Illuminate\Notifications\Messages\MailMessage |
|
44
|
|
|
*/ |
|
45
|
|
|
public function toMail() |
|
46
|
|
|
{ |
|
47
|
|
|
return (new MailMessage) |
|
48
|
|
|
->subject($this->getMailSubject()) |
|
49
|
|
|
->view( |
|
50
|
|
|
'emails.notifications.pm', |
|
51
|
|
|
[ |
|
52
|
|
|
'text' => $this->text, |
|
53
|
|
|
'sender' => $this->pm->author->name, |
|
54
|
|
|
'url' => route('user.pm.show', [$this->pm->id], false) |
|
55
|
|
|
] |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param User $user |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
|
|
public function toDatabase($user) |
|
64
|
|
|
{ |
|
65
|
|
|
$excerpt = excerpt($this->text); |
|
66
|
|
|
|
|
67
|
|
|
return [ |
|
68
|
|
|
'object_id' => $this->objectId(), |
|
69
|
|
|
'user_id' => $user->id, |
|
70
|
|
|
'type_id' => static::ID, |
|
71
|
|
|
'subject' => $excerpt, |
|
72
|
|
|
'excerpt' => $excerpt, |
|
73
|
|
|
'url' => route('user.pm.show', [$this->pm->id], false), |
|
74
|
|
|
'id' => $this->id, |
|
75
|
|
|
'content_id' => $this->pm->id, |
|
76
|
|
|
'content_type' => class_basename($this->pm) |
|
77
|
|
|
]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return BroadcastMessage |
|
82
|
|
|
*/ |
|
83
|
|
|
public function toBroadcast() |
|
84
|
|
|
{ |
|
85
|
|
|
return new BroadcastMessage([ |
|
86
|
|
|
'headline' => $this->getMailSubject(), |
|
87
|
|
|
'subject' => excerpt($this->text), |
|
88
|
|
|
'url' => $this->notificationUrl() |
|
89
|
|
|
]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function toWebPush(): WebPushMessage |
|
93
|
|
|
{ |
|
94
|
|
|
return (new WebPushMessage()) |
|
95
|
|
|
->title($this->getMailSubject()) |
|
96
|
|
|
->icon('/apple-touch.png') |
|
97
|
|
|
->body(excerpt($this->text)) |
|
98
|
|
|
->data(['url' => $this->notificationUrl()]) |
|
99
|
|
|
->options(['TTL' => 1000]); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Unikalne ID okreslajace dano powiadomienie. To ID posluzy do grupowania powiadomien tego samego typu |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public function objectId() |
|
108
|
|
|
{ |
|
109
|
|
|
return substr(md5(class_basename($this) . $this->pm->user_id), 16); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
public function sender() |
|
116
|
|
|
{ |
|
117
|
|
|
return [ |
|
118
|
|
|
'user_id' => $this->pm->author_id, |
|
119
|
|
|
'name' => $this->pm->author->name |
|
120
|
|
|
]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
private function getMailSubject(): string |
|
124
|
|
|
{ |
|
125
|
|
|
return sprintf('Masz nową wiadomość od: %s', $this->pm->author->name); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|