| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Task; |
| 6 |
use FluentBoards\App\Models\User; |
| 7 |
use FluentBoards\App\Models\Board; |
| 8 |
use FluentBoards\App\Models\Notification; |
| 9 |
use FluentBoards\App\Services\Constant; |
| 10 |
|
| 11 |
class NotificationHandler |
| 12 |
{ |
| 13 |
/** |
| 14 |
* Add a generic comment notification for watchers who were not explicitly mentioned. |
| 15 |
* |
| 16 |
* Mentioned users receive the higher-priority mention notification separately, so they |
| 17 |
* should not also receive a generic comment notification for the same comment. |
| 18 |
* |
| 19 |
* @param \FluentBoards\App\Models\Comment $comment |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public function addCommentNotification($comment) |
| 23 |
{ |
| 24 |
if($comment->task_id){ |
| 25 |
$task = Task::findOrFail($comment->task_id); |
| 26 |
$mentionedUserIds = $this->getMentionedUserIdsFromComment($comment); |
| 27 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 28 |
$userIdsWhoGetNotification = $this->excludeMentionedUsersFromNotificationRecipients($userIdsWhoGetNotification, $mentionedUserIds); |
| 29 |
if(count($userIdsWhoGetNotification) > 0){ |
| 30 |
$plainDescription = wp_strip_all_tags($comment->description); |
| 31 |
$action = $comment->parent_id ? 'task_reply_added' : 'comment_created'; |
| 32 |
$message = $plainDescription; |
| 33 |
$settings = [ 'comment_id' => $comment->parent_id ?? $comment->id ]; |
| 34 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message, $settings); |
| 35 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Get normalized mentioned user IDs from a comment's stored settings. |
| 42 |
* |
| 43 |
* @param \FluentBoards\App\Models\Comment $comment |
| 44 |
* @return array |
| 45 |
*/ |
| 46 |
private function getMentionedUserIdsFromComment($comment) |
| 47 |
{ |
| 48 |
$settings = $comment->settings; |
| 49 |
|
| 50 |
if (!is_array($settings) || empty($settings['mentioned_id']) || !is_array($settings['mentioned_id'])) { |
| 51 |
return []; |
| 52 |
} |
| 53 |
|
| 54 |
return array_values(array_filter(array_map('intval', $settings['mentioned_id']))); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Remove mentioned users from generic notification recipients. |
| 59 |
* |
| 60 |
* @param array $recipientIds |
| 61 |
* @param array $mentionedUserIds |
| 62 |
* @return array |
| 63 |
*/ |
| 64 |
private function excludeMentionedUsersFromNotificationRecipients($recipientIds, $mentionedUserIds) |
| 65 |
{ |
| 66 |
$mentionedUserIds = array_filter(array_map('intval', $mentionedUserIds)); |
| 67 |
|
| 68 |
if (!$mentionedUserIds) { |
| 69 |
return array_values($recipientIds); |
| 70 |
} |
| 71 |
|
| 72 |
return array_values(array_filter($recipientIds, function ($recipientId) use ($mentionedUserIds) { |
| 73 |
return !in_array((int) $recipientId, $mentionedUserIds, true); |
| 74 |
})); |
| 75 |
} |
| 76 |
|
| 77 |
public function mentionInCommentNotification($comment, $mentionIds) |
| 78 |
{ |
| 79 |
if($comment){ |
| 80 |
$task = Task::findOrFail($comment->task_id); |
| 81 |
$plainDescription = wp_strip_all_tags($comment->description); |
| 82 |
$action = 'task_comment_mentioned'; |
| 83 |
$message = $plainDescription; |
| 84 |
$settings = [ 'comment_id' => $comment->parent_id ?? $comment->id ]; |
| 85 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message, $settings); |
| 86 |
$notification->users()->attach($mentionIds); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
public function addSubtaskNotification($parentTask, $subTask) |
| 91 |
{ |
| 92 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($parentTask); |
| 93 |
if(count($userIdsWhoGetNotification) > 0){ |
| 94 |
$board_id = $subTask->board_id; |
| 95 |
$action = 'subtask_added'; |
| 96 |
$message = $subTask->title ; |
| 97 |
$notification = $this->createNotification($parentTask, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 98 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
public function changeDueDateNotification($task, $oldDate) |
| 103 |
{ |
| 104 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 105 |
if(count($userIdsWhoGetNotification) > 0){ |
| 106 |
$action = 'task_due_date_changed'; |
| 107 |
$message = date_i18n('F j, Y, g:i a', strtotime($task->due_at)); |
| 108 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 109 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public function changeStartDateNotification($task, $oldDate) |
| 114 |
{ |
| 115 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 116 |
if(count($userIdsWhoGetNotification) > 0){ |
| 117 |
$action = 'task_start_date_changed'; |
| 118 |
$message = date_i18n('F j, Y, g:i a', strtotime($task->started_at)); |
| 119 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 120 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
public function changeStageNotification($task, $oldStageId) |
| 125 |
{ |
| 126 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 127 |
if(count($userIdsWhoGetNotification) > 0){ |
| 128 |
$action = 'task_moved_to_new_stage'; |
| 129 |
$message = $task->stage->title; |
| 130 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 131 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
public function changePriorityNotification($task, $oldPriority) |
| 136 |
{ |
| 137 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 138 |
if(count($userIdsWhoGetNotification) > 0){ |
| 139 |
$action = 'task_priority_changed'; |
| 140 |
$message = $task->priority; |
| 141 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 142 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
public function taskArchiveNotification($task) |
| 147 |
{ |
| 148 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 149 |
if(count($userIdsWhoGetNotification) > 0){ |
| 150 |
if ($task->archived_at) { |
| 151 |
$action = 'board_task_archived'; |
| 152 |
$message = $task->title; |
| 153 |
} else { |
| 154 |
$action = 'board_task_restored'; |
| 155 |
$message = $task->title; |
| 156 |
} |
| 157 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 158 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
public function changeTitleNotification($task, $col, $oldTask) |
| 163 |
{ |
| 164 |
if ($col !== 'title') { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 169 |
if(count($userIdsWhoGetNotification) > 0){ |
| 170 |
$action = 'task_title_updated'; |
| 171 |
$message = $task->title; |
| 172 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 173 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
public function changeBoardNotification($task, $oldBoardId) |
| 178 |
{ |
| 179 |
$userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 180 |
if(count($userIdsWhoGetNotification) > 0){ |
| 181 |
$new_board_id = Task::findOrFail($task->board_id)->board_id; |
| 182 |
$new_board_title = Board::findOrFail($new_board_id)->title; |
| 183 |
// translators: %1$s is the task title, %2$s is the board title |
| 184 |
$message = sprintf(__('moved %1$s task to %2$s board.','fluent-boards'), $task->title, $new_board_title); |
| 185 |
$notification = $this->createNotification($oldBoardId, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $message); |
| 186 |
$notification->users()->attach($userIdsWhoGetNotification); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
public function assigneeAddedNotification($task, $newAssigneeId) |
| 191 |
{ |
| 192 |
if($newAssigneeId != get_current_user_id()){ |
| 193 |
$action = 'task_assignee_changed'; |
| 194 |
$message = __('has added you as an assignee.','fluent-boards'); |
| 195 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 196 |
$notification->users()->attach($newAssigneeId); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
public function assigneeRemovedNotification($task, $newAssigneeId) |
| 201 |
{ |
| 202 |
if($newAssigneeId != get_current_user_id()){ |
| 203 |
$action = 'task_assignee_changed'; |
| 204 |
$message = __('has removed you as an assignee.','fluent-boards'); |
| 205 |
$notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 206 |
$notification->users()->attach($newAssigneeId); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
public function createNotification($task, $objectType, $action, $description, $settings = null) |
| 211 |
{ |
| 212 |
$data = [ |
| 213 |
'object_id' => $task->board_id, |
| 214 |
'object_type' => $objectType, |
| 215 |
'task_id' => $task->id, |
| 216 |
'action' => $action, |
| 217 |
'description' => $description, |
| 218 |
'settings' => $settings |
| 219 |
]; |
| 220 |
$currentUser = get_current_user_id(); |
| 221 |
if($currentUser == 0) { |
| 222 |
$data['activity_by'] = $task->created_by; |
| 223 |
} |
| 224 |
return Notification::create($data); |
| 225 |
} |
| 226 |
|
| 227 |
public function findUsersWhoWillGetNotification($task) |
| 228 |
{ |
| 229 |
$currentUserId = get_current_user_id(); |
| 230 |
$watchersOfTask = $task->watchers; |
| 231 |
$watchersExceptCurrentUser = []; |
| 232 |
foreach($watchersOfTask as $watcher){ |
| 233 |
if($watcher->ID != $currentUserId){ |
| 234 |
$watchersExceptCurrentUser[] = $watcher->ID; |
| 235 |
} |
| 236 |
} |
| 237 |
return $watchersExceptCurrentUser; |
| 238 |
} |
| 239 |
} |
| 240 |
|