|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Tinyissue package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Tinyissue\Model\Project\Issue; |
|
13
|
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
15
|
|
|
use Tinyissue\Model\Message\Queue; |
|
16
|
|
|
use Tinyissue\Model\Project; |
|
17
|
|
|
use Tinyissue\Model\Project\Issue; |
|
18
|
|
|
use Tinyissue\Model\Tag; |
|
19
|
|
|
use Tinyissue\Model\User; |
|
20
|
|
|
use Tinyissue\Services\SendMessagesAbstract; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* SendMessages is a class to manage & process of sending messages about issue changes. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class SendMessages extends SendMessagesAbstract |
|
28
|
|
|
{ |
|
29
|
|
|
protected $template = 'update_issue'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Collection of tags. |
|
33
|
|
|
* |
|
34
|
|
|
* @var Collection |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $tags; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Returns an instance of Issue. |
|
40
|
|
|
* |
|
41
|
|
|
* @return bool |
|
42
|
|
|
*/ |
|
43
|
3 |
|
protected function getIssue() |
|
44
|
|
|
{ |
|
45
|
3 |
|
if (null === $this->issue) { |
|
46
|
3 |
|
$this->issue = $this->getModel(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
3 |
|
return $this->issue; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Returns an instance of Project. |
|
54
|
|
|
* |
|
55
|
|
|
* @return Project |
|
56
|
|
|
*/ |
|
57
|
3 |
|
protected function getProject() |
|
58
|
|
|
{ |
|
59
|
3 |
|
return $this->getIssue()->project; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns the project id. |
|
64
|
|
|
* |
|
65
|
|
|
* @return int |
|
66
|
|
|
*/ |
|
67
|
3 |
|
protected function getProjectId() |
|
68
|
|
|
{ |
|
69
|
3 |
|
return $this->getIssue()->project_id; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns message data belongs to adding an issue. |
|
74
|
|
|
* |
|
75
|
|
|
* @param Queue $queue |
|
76
|
|
|
* |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
3 |
|
protected function getMessageDataForAddIssue(Queue $queue) |
|
80
|
|
|
{ |
|
81
|
3 |
|
$messageData = ['changes' => []]; |
|
82
|
3 |
|
$messageData['changeByHeading'] = $queue->changeBy->fullname . ' created a new issue'; |
|
|
|
|
|
|
83
|
3 |
|
if ($this->issue->assigned) { |
|
|
|
|
|
|
84
|
1 |
|
$messageData['changes']['assignee'] = $this->issue->assigned->fullname; |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
3 |
|
$tags = $this->issue->tags()->with('parent')->get(); |
|
88
|
3 |
|
foreach ($tags as $tag) { |
|
89
|
2 |
|
$tagArray = $tag->toShortArray(); |
|
90
|
2 |
|
$tagArray['now'] = $tagArray['name']; |
|
91
|
2 |
|
$messageData['changes'][$tag->parent->name] = $tagArray; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
3 |
|
if ($this->issue->time_quote) { |
|
95
|
|
|
$messageData['changes']['time_quote'] = \Html::duration($this->issue->time_quote); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
3 |
|
return $messageData; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns message data belongs to updating an issue. |
|
103
|
|
|
* |
|
104
|
|
|
* @param Queue $queue |
|
105
|
|
|
* |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
1 |
|
protected function getMessageDataForUpdateIssue(Queue $queue) |
|
109
|
|
|
{ |
|
110
|
1 |
|
$messageData = []; |
|
111
|
1 |
|
$messageData['changeByHeading'] = $queue->changeBy->fullname . ' updated an issue'; |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
1 |
|
foreach ($queue->payload['dirty'] as $field => $value) { |
|
|
|
|
|
|
114
|
|
|
// Skip fields that not part of the white list |
|
115
|
1 |
|
if (!in_array($field, |
|
116
|
1 |
|
['change_by', 'title', 'body', 'assignee', 'status', 'type', 'resolution', 'time_quote']) |
|
117
|
|
|
) { |
|
118
|
1 |
|
continue; |
|
119
|
|
|
} |
|
120
|
|
|
// Format quote to readable time |
|
121
|
1 |
|
$value = $field === 'time_quote' ? \Html::duration($value) : $value; |
|
122
|
1 |
|
$value = $field === 'body' ? \Html::format($value) : $value; |
|
123
|
1 |
|
$messageData['changes'][$field] = [ |
|
124
|
1 |
|
'now' => $value, |
|
125
|
1 |
|
'was' => $queue->getDataFromPayload('origin.' . $field), |
|
126
|
|
|
]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
1 |
|
return $messageData; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Returns message data belongs to reopening an issue. |
|
134
|
|
|
* |
|
135
|
|
|
* @param Queue $queue |
|
136
|
|
|
* |
|
137
|
|
|
* @return array |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function getMessageDataForReopenIssue(Queue $queue) |
|
140
|
|
|
{ |
|
141
|
|
|
$messageData = []; |
|
142
|
|
|
$messageData['changeByHeading'] = $queue->changeBy->fullname . ' reopened an issue'; |
|
|
|
|
|
|
143
|
|
|
$statusNow = $this->issue |
|
144
|
|
|
->tags()->with('parent')->get()->where('parent.name', Tag::GROUP_STATUS)->last(); |
|
145
|
|
|
$messageData['changes']['status'] = [ |
|
146
|
|
|
'was' => trans('tinyissue.closed'), |
|
147
|
|
|
'now' => ($statusNow ? $statusNow->fullname : ''), |
|
148
|
|
|
'id' => ($statusNow ? $statusNow->id : ''), |
|
149
|
|
|
]; |
|
150
|
|
|
|
|
151
|
|
|
return $messageData; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Returns message data belongs to closing an issue. |
|
156
|
|
|
* |
|
157
|
|
|
* @param Queue $queue |
|
158
|
|
|
* |
|
159
|
|
|
* @return array |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function getMessageDataForCloseIssue(Queue $queue) |
|
162
|
|
|
{ |
|
163
|
|
|
$messageData = []; |
|
164
|
|
|
$messageData['changeByHeading'] = $queue->changeBy->fullname . ' closed an issue'; |
|
|
|
|
|
|
165
|
|
|
$statusWas = $this->issue |
|
166
|
|
|
->tags()->with('parent')->get()->where('parent.name', Tag::GROUP_STATUS)->last(); |
|
167
|
|
|
$messageData['changes']['status'] = [ |
|
168
|
|
|
'was' => ($statusWas ? $statusWas->fullname : ''), |
|
169
|
|
|
'now' => trans('tinyissue.closed'), |
|
170
|
|
|
]; |
|
171
|
|
|
|
|
172
|
|
|
return $messageData; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Returns message data belongs to assigning an issue to a user. |
|
177
|
|
|
* |
|
178
|
|
|
* @param Queue $queue |
|
179
|
|
|
* @param array $extraData |
|
180
|
|
|
* |
|
181
|
|
|
* @return array |
|
182
|
|
|
*/ |
|
183
|
|
|
protected function getMessageDataForAssignIssue(Queue $queue, array $extraData) |
|
184
|
|
|
{ |
|
185
|
|
|
$messageData = []; |
|
186
|
|
|
if (!array_key_exists('now', $extraData)) { |
|
187
|
|
|
$assignTo = $this->getUserById($queue->getDataFromPayload('dirty.assigned_to')); |
|
188
|
|
|
$extraData = ['now' => $assignTo->fullname]; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$messageData['changes']['assignee'] = $extraData; |
|
192
|
|
|
$messageData['changeByHeading'] = $queue->changeBy->fullname . ' assigned an issue to ' . $extraData['now']; |
|
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
return $messageData; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Returns message data belongs to changing an issue tags. |
|
199
|
|
|
* |
|
200
|
|
|
* @param Queue $queue |
|
201
|
|
|
* |
|
202
|
|
|
* @return array |
|
203
|
|
|
*/ |
|
204
|
2 |
|
protected function getMessageDataForChangeTagIssue(Queue $queue) |
|
205
|
|
|
{ |
|
206
|
2 |
|
$messageData = []; |
|
207
|
2 |
|
$messageData['changeByHeading'] = $queue->changeBy->fullname . ' changed an issue tag'; |
|
|
|
|
|
|
208
|
|
|
|
|
209
|
2 |
|
foreach ($queue->payload['added'] as $tag) { |
|
|
|
|
|
|
210
|
2 |
|
$group = strtolower($tag['group']); |
|
211
|
2 |
|
$messageData['changes'][$group] = [ |
|
212
|
2 |
|
'now' => $tag['name'], |
|
213
|
2 |
|
'id' => $tag['id'], |
|
214
|
2 |
|
'message_limit' => $tag['message_limit'], |
|
215
|
|
|
]; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
2 |
|
foreach ($queue->payload['removed'] as $tag) { |
|
|
|
|
|
|
219
|
1 |
|
$group = strtolower($tag['group']); |
|
220
|
1 |
|
$messageData['changes'][$group]['was'] = $tag['name']; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
2 |
|
return $messageData; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Check that the issue is load. |
|
228
|
|
|
* |
|
229
|
|
|
* @return bool |
|
230
|
|
|
*/ |
|
231
|
3 |
|
protected function validateData() |
|
232
|
|
|
{ |
|
233
|
|
|
// if issue closed and last issue not closed, then something is wrong skip |
|
234
|
3 |
|
if (!$this->issue->isOpen() && $this->latestMessage->event !== Queue::CLOSE_ISSUE) { |
|
235
|
|
|
return false; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
3 |
|
return $this->issue; |
|
|
|
|
|
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Populate assigned relation in the current issue. |
|
243
|
|
|
* |
|
244
|
|
|
* @return void |
|
245
|
|
|
*/ |
|
246
|
3 |
View Code Duplication |
protected function populateData() |
|
|
|
|
|
|
247
|
|
|
{ |
|
248
|
3 |
|
$this->issue->setRelation('assigned', $this->getUserById($this->issue->assigned_to)); |
|
249
|
3 |
|
$creator = $this->getUserById($this->issue->created_by); |
|
250
|
3 |
|
if ($creator) { |
|
251
|
3 |
|
$this->issue->setRelation('user', $creator); |
|
252
|
|
|
} |
|
253
|
3 |
|
$this->loadIssueCreatorToProjectUsers(); |
|
254
|
3 |
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Check if the latest message is about closing or reopening an issue. |
|
258
|
|
|
* |
|
259
|
|
|
* @return bool |
|
260
|
|
|
*/ |
|
261
|
3 |
|
public function isStatusMessage() |
|
262
|
|
|
{ |
|
263
|
3 |
|
return ((!$this->issue->isOpen() && $this->latestMessage->event === Queue::CLOSE_ISSUE) |
|
264
|
3 |
|
|| ($this->issue->isOpen() && $this->latestMessage->event === Queue::REOPEN_ISSUE)); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Process assign to user message. Send direct message to new and previous users and full subscribers. |
|
269
|
|
|
* |
|
270
|
|
|
* @return void |
|
271
|
|
|
*/ |
|
272
|
3 |
|
protected function processDirectMessages() |
|
273
|
|
|
{ |
|
274
|
|
|
// Stop if issue is closed |
|
275
|
3 |
|
if (!$this->getIssue()->isOpen()) { |
|
276
|
|
|
return; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
// Fetch all of the assign issue changes |
|
280
|
3 |
|
$assignMessages = $this->allMessages->where('event', Queue::ASSIGN_ISSUE); |
|
281
|
|
|
|
|
282
|
|
|
// Skip if no changes |
|
283
|
3 |
|
if ($assignMessages->isEmpty()) { |
|
284
|
3 |
|
return; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
// Fetch the latest assignee |
|
288
|
|
|
/** @var Queue $assignMessage */ |
|
289
|
|
|
$assignMessage = $assignMessages->first(); |
|
290
|
|
|
|
|
291
|
|
|
// Fetch the user details of the new assignee & previous assignee if this isn't new issue |
|
292
|
|
|
$assigns = []; |
|
293
|
|
|
$assigns['new'] = (int) $assignMessage->getDataFromPayload('dirty.assigned_to'); |
|
294
|
|
|
if (!$this->addMessage) { |
|
295
|
|
|
$previousAssignMessage = $assignMessages->last(); |
|
296
|
|
|
$assigns['old'] = (int) $previousAssignMessage->getDataFromPayload('origin.assigned_to'); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
// Fetch users objects for old and new assignee |
|
300
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection $assignObjects */ |
|
301
|
|
|
$assignObjects = (new User())->whereIn('id', $assigns)->get(); |
|
|
|
|
|
|
302
|
|
|
|
|
303
|
|
|
// If for what ever reason the user does not exists, skip this change |
|
304
|
|
|
// or if there is only one user and is not matching the new assignee. |
|
305
|
|
|
// then skip this message |
|
306
|
|
|
if ($assignObjects->count() === 0 |
|
307
|
|
|
|| ($assignObjects->count() === 1 && (int) $assignObjects->first()->id !== $assigns['new']) |
|
308
|
|
|
) { |
|
309
|
|
|
return; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
// Get the object of the new assignee |
|
313
|
|
|
$assignTo = $assignObjects->where('id', $assigns['new'], false)->first(); |
|
314
|
|
|
$users = collect([$this->createProjectUserObject($assignTo)]); |
|
315
|
|
|
|
|
316
|
|
|
// Exclude the user from any other message for this issue |
|
317
|
|
|
$this->addToExcludeUsers($assignTo); |
|
318
|
|
|
|
|
319
|
|
|
// Data about new and previous assignee |
|
320
|
|
|
$extraMessageData = [ |
|
321
|
|
|
'now' => $assignTo->fullname, |
|
322
|
|
|
]; |
|
323
|
|
|
|
|
324
|
|
|
// Make sure that the previous assignee was not the same as the new user |
|
325
|
|
|
if (array_key_exists('old', $assigns) && $assigns['old'] > 0 && $assigns['new'] !== $assigns['old']) { |
|
326
|
|
|
$previousAssign = $assignObjects->where('id', $assigns['old'], false)->first(); |
|
327
|
|
|
if ($previousAssign) { |
|
328
|
|
|
$extraMessageData['was'] = $previousAssign->fullname; |
|
329
|
|
|
$users->push($this->createProjectUserObject($previousAssign)); |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
// Get message data needed for the message & send |
|
334
|
|
|
$messageData = $this->getMessageData($assignMessage, $extraMessageData); |
|
335
|
|
|
$this->sendMessages($users, $messageData); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* Create user project object for a user. |
|
340
|
|
|
* |
|
341
|
|
|
* @param User $user |
|
342
|
|
|
* |
|
343
|
|
|
* @return Project\User |
|
344
|
|
|
*/ |
|
345
|
|
|
protected function createProjectUserObject(User $user) |
|
346
|
|
|
{ |
|
347
|
|
|
$userProject = new Project\User([ |
|
348
|
|
|
'user_id' => $user->id, |
|
349
|
|
|
'project_id' => $this->getProjectId(), |
|
350
|
|
|
]); |
|
351
|
|
|
$userProject->setRelation('user', $user); |
|
352
|
|
|
$userProject->setRelation('project', $this->getProject()); |
|
353
|
|
|
|
|
354
|
|
|
return $userProject; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* Get collection of tags or one by ID. |
|
359
|
|
|
* |
|
360
|
|
|
* @param null|int $tagId |
|
361
|
|
|
* |
|
362
|
|
|
* @return \Illuminate\Support\Collection|Tag |
|
363
|
|
|
*/ |
|
364
|
2 |
|
protected function getTags($tagId = null) |
|
365
|
|
|
{ |
|
366
|
2 |
|
if (null === $this->tags) { |
|
367
|
2 |
|
$this->tags = collect([]); |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
// Load & extract tag by ID |
|
371
|
2 |
|
if (null !== $tagId) { |
|
372
|
2 |
|
$tag = $this->tags->where('id', $tagId, false)->first(); |
|
373
|
2 |
|
if (!$tag) { |
|
374
|
2 |
|
$tag = (new Tag())->find($tagId); |
|
|
|
|
|
|
375
|
2 |
|
$this->tags->push($tag); |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
2 |
|
return $tag; |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
return $this->tags; |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
/** |
|
385
|
|
|
* Whether or not the user wants to receive the message. |
|
386
|
|
|
* |
|
387
|
|
|
* @param Project\User $user |
|
388
|
|
|
* @param array $data |
|
389
|
|
|
* |
|
390
|
|
|
* @return bool |
|
391
|
|
|
*/ |
|
392
|
3 |
|
protected function wantToReceiveMessage(Project\User $user, array $data) |
|
393
|
|
|
{ |
|
394
|
3 |
|
$status = parent::wantToReceiveMessage($user, $data); |
|
395
|
|
|
|
|
396
|
3 |
|
if (!$status) { |
|
397
|
3 |
|
return false; |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
// Search for tag changes and verify if the user can receive messages |
|
401
|
3 |
|
foreach ($data['changes'] as $label => $change) { |
|
402
|
|
|
// Skip other changes that are not related to tag |
|
403
|
3 |
|
if (!in_array($label, Tag::getCoreGroups())) { |
|
404
|
3 |
|
continue; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
// If the change was only remove a tag |
|
408
|
2 |
|
if (!array_key_exists('now', $change) && array_key_exists('was', $change)) { |
|
409
|
|
|
return true; |
|
410
|
|
|
} |
|
411
|
|
|
|
|
412
|
|
|
// If tag id not found |
|
413
|
2 |
|
if (!array_key_exists('id', $change)) { |
|
414
|
|
|
return true; |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
// Fetch tag details |
|
418
|
2 |
|
$tag = $this->getTags($change['id']); |
|
419
|
|
|
|
|
420
|
|
|
// Check if user allowed to receive the message |
|
421
|
2 |
|
if (!$tag->allowMessagesToUser($user->user)) { |
|
|
|
|
|
|
422
|
2 |
|
return false; |
|
423
|
|
|
} |
|
424
|
|
|
} |
|
425
|
|
|
|
|
426
|
3 |
|
return true; |
|
427
|
|
|
} |
|
428
|
|
|
} |
|
429
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.