|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\Event; |
|
3
|
|
|
|
|
4
|
|
|
use Cake\Event\Event; |
|
5
|
|
|
use Cake\Event\EventListenerInterface; |
|
6
|
|
|
use Cake\ORM\TableRegistry; |
|
7
|
|
|
|
|
8
|
|
|
class Notifications implements EventListenerInterface |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* ImplementedEvents method. |
|
13
|
|
|
* |
|
14
|
|
|
* @return array |
|
15
|
|
|
*/ |
|
16
|
|
|
public function implementedEvents() |
|
17
|
|
|
{ |
|
18
|
|
|
return [ |
|
19
|
|
|
'Model.Notifications.new' => 'newNotification', |
|
20
|
|
|
'Model.Notifications.dispatch' => 'dispatchParticipants' |
|
21
|
|
|
]; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* We send a new notification to an user. |
|
26
|
|
|
* |
|
27
|
|
|
* @param \Cake\Event\Event $event The event that was fired. |
|
28
|
|
|
* |
|
29
|
|
|
* @return false |
|
30
|
|
|
*/ |
|
31
|
|
|
public function newNotification(Event $event) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->Notifications = TableRegistry::get('Notifications'); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
if (!isset($event->data['type'])) { |
|
36
|
|
|
return false; |
|
37
|
|
|
} |
|
38
|
|
|
$event->data['type'] = strtolower($event->data['type']); |
|
39
|
|
|
|
|
40
|
|
|
switch ($event->data['type']) { |
|
41
|
|
|
case 'conversation.reply': |
|
42
|
|
|
$result = $this->_conversationReply($event); |
|
43
|
|
|
break; |
|
44
|
|
|
|
|
45
|
|
|
case 'bot': |
|
46
|
|
|
$result = $this->_bot($event); |
|
47
|
|
|
break; |
|
48
|
|
|
|
|
49
|
|
|
case 'badge': |
|
50
|
|
|
$result = $this->_badge($event); |
|
51
|
|
|
break; |
|
52
|
|
|
default: |
|
53
|
|
|
$result = false; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $result; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Dispatch notification for the participants of a conversation. |
|
61
|
|
|
* |
|
62
|
|
|
* @param \Cake\Event\Event $event The event that was fired. |
|
63
|
|
|
* |
|
64
|
|
|
* @return false |
|
65
|
|
|
*/ |
|
66
|
|
|
public function dispatchParticipants(Event $event) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->ConversationsUsers = TableRegistry::get('ConversationsUsers'); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
$participants = $this->ConversationsUsers |
|
71
|
|
|
->find() |
|
72
|
|
|
->where([ |
|
73
|
|
|
'ConversationsUsers.conversation_id' => $event->data['conversation_id'] |
|
74
|
|
|
]) |
|
75
|
|
|
->contain([ |
|
76
|
|
|
'Users' => function ($q) { |
|
77
|
|
|
return $q->select([ |
|
78
|
|
|
'id' |
|
79
|
|
|
]); |
|
80
|
|
|
} |
|
81
|
|
|
]) |
|
82
|
|
|
->select([ |
|
83
|
|
|
'ConversationsUsers.id', |
|
84
|
|
|
'ConversationsUsers.conversation_id', |
|
85
|
|
|
'ConversationsUsers.user_id' |
|
86
|
|
|
]) |
|
87
|
|
|
->toArray(); |
|
88
|
|
|
|
|
89
|
|
|
if (empty($participants)) { |
|
90
|
|
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
foreach ($participants as $participant) { |
|
94
|
|
|
if ($participant->user_id != $event->data['sender_id']) { |
|
95
|
|
|
$event->data['participant'] = $participant; |
|
96
|
|
|
|
|
97
|
|
|
$this->newNotification($event); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* A user has replied to a conversation. |
|
107
|
|
|
* |
|
108
|
|
|
* @param \Cake\Event\Event $event The event that was fired. |
|
109
|
|
|
* |
|
110
|
|
|
* @return bool |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function _conversationReply(Event $event) |
|
113
|
|
|
{ |
|
114
|
|
|
if (!is_integer($event->data['conversation_id'])) { |
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$this->Conversations = TableRegistry::get('Conversations'); |
|
|
|
|
|
|
119
|
|
|
$this->Users = TableRegistry::get('Users'); |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
$conversation = $this->Conversations |
|
|
|
|
|
|
122
|
|
|
->find() |
|
123
|
|
|
->where([ |
|
124
|
|
|
'Conversations.id' => $event->data['conversation_id'] |
|
125
|
|
|
]) |
|
126
|
|
|
->select([ |
|
127
|
|
|
'id', 'user_id', 'title', 'last_message_id' |
|
128
|
|
|
]) |
|
129
|
|
|
->first(); |
|
130
|
|
|
|
|
131
|
|
|
$sender = $this->Users |
|
132
|
|
|
->find('medium') |
|
133
|
|
|
->where([ |
|
134
|
|
|
'Users.id' => $event->data['sender_id'] |
|
135
|
|
|
]) |
|
136
|
|
|
->first(); |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
//Check if this user hasn't already a notification. (Prevent for spam) |
|
140
|
|
|
$hasReplied = $this->Notifications |
|
141
|
|
|
->find() |
|
142
|
|
|
->where([ |
|
143
|
|
|
'Notifications.foreign_key' => $conversation->id, |
|
144
|
|
|
'Notifications.type' => $event->data['type'], |
|
145
|
|
|
'Notifications.user_id' => $event->data['participant']->user->id |
|
146
|
|
|
]) |
|
147
|
|
|
->first(); |
|
148
|
|
|
|
|
149
|
|
|
if (!is_null($hasReplied)) { |
|
150
|
|
|
$hasReplied->data = serialize(['sender' => $sender, 'conversation' => $conversation]); |
|
151
|
|
|
$hasReplied->is_read = 0; |
|
152
|
|
|
|
|
153
|
|
|
$this->Notifications->save($hasReplied); |
|
154
|
|
|
} else { |
|
155
|
|
|
$data = []; |
|
156
|
|
|
$data['user_id'] = $event->data['participant']->user->id; |
|
157
|
|
|
$data['type'] = $event->data['type']; |
|
158
|
|
|
$data['data'] = serialize(['sender' => $sender, 'conversation' => $conversation]); |
|
159
|
|
|
$data['foreign_key'] = $conversation->id; |
|
160
|
|
|
|
|
161
|
|
|
$entity = $this->Notifications->newEntity($data); |
|
162
|
|
|
$this->Notifications->save($entity); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return true; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* A user has sign up on the website. |
|
170
|
|
|
* |
|
171
|
|
|
* @param \Cake\Event\Event $event The event that was fired. |
|
172
|
|
|
* |
|
173
|
|
|
* @return bool |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function _bot(Event $event) |
|
176
|
|
|
{ |
|
177
|
|
|
$this->Users = TableRegistry::get('Users'); |
|
178
|
|
|
|
|
179
|
|
|
$user = $this->Users |
|
180
|
|
|
->find() |
|
181
|
|
|
->where(['id' => $event->data['user_id']]) |
|
182
|
|
|
->first(); |
|
183
|
|
|
|
|
184
|
|
|
if (is_null($user)) { |
|
185
|
|
|
return false; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$data = []; |
|
189
|
|
|
$data['user_id'] = $user->id; |
|
190
|
|
|
$data['type'] = $event->data['type']; |
|
191
|
|
|
$data['data'] = serialize(['icon' => '../img/notifications/welcome.png']); |
|
192
|
|
|
|
|
193
|
|
|
$entity = $this->Notifications->newEntity($data); |
|
194
|
|
|
$this->Notifications->save($entity); |
|
195
|
|
|
|
|
196
|
|
|
return true; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* A user has unlock a badge. |
|
201
|
|
|
* |
|
202
|
|
|
* @param \Cake\Event\Event $event The event that was fired. |
|
203
|
|
|
* |
|
204
|
|
|
* @return bool |
|
205
|
|
|
*/ |
|
206
|
|
|
protected function _badge(Event $event) |
|
207
|
|
|
{ |
|
208
|
|
|
$this->Badges = TableRegistry::get('Badges'); |
|
|
|
|
|
|
209
|
|
|
$this->Notifications = TableRegistry::get('Notifications'); |
|
210
|
|
|
|
|
211
|
|
|
$badge = $this->Badges |
|
212
|
|
|
->find() |
|
213
|
|
|
->where(['Badges.id' => $event->data['badge']->badge_id]) |
|
214
|
|
|
->first(); |
|
215
|
|
|
|
|
216
|
|
|
if (is_null($badge)) { |
|
217
|
|
|
return false; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
$this->Users = TableRegistry::get('Users'); |
|
221
|
|
|
|
|
222
|
|
|
$user = $this->Users |
|
223
|
|
|
->find() |
|
224
|
|
|
->where(['id' => $event->data['badge']->user_id]) |
|
225
|
|
|
->select([ |
|
226
|
|
|
'id' |
|
227
|
|
|
]) |
|
228
|
|
|
->first(); |
|
229
|
|
|
|
|
230
|
|
|
if (is_null($user)) { |
|
231
|
|
|
return false; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
$data = []; |
|
235
|
|
|
$data['user_id'] = $event->data['badge']->user_id; |
|
236
|
|
|
$data['type'] = $event->data['type']; |
|
237
|
|
|
$data['data'] = serialize(['badge' => $badge, 'user' => $user]); |
|
238
|
|
|
|
|
239
|
|
|
$entity = $this->Notifications->newEntity($data); |
|
240
|
|
|
$this->Notifications->save($entity); |
|
241
|
|
|
|
|
242
|
|
|
return true; |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: