| @@ -9,43 +9,85 @@ | ||
| 9 | 9 | use FluentBoards\App\Services\Constant; |
| 10 | 10 | |
| 11 | 11 | class NotificationHandler |
| 12 | 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 | + */ | |
| 13 | 22 | public function addCommentNotification($comment) |
| 14 | 23 | { |
| 15 | 24 | if($comment->task_id){ |
| 16 | 25 | $task = Task::findOrFail($comment->task_id); |
| 26 | + $mentionedUserIds = $this->getMentionedUserIdsFromComment($comment); | |
| 17 | 27 | $userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 28 | + $userIdsWhoGetNotification = $this->excludeMentionedUsersFromNotificationRecipients($userIdsWhoGetNotification, $mentionedUserIds); | |
| 18 | 29 | if(count($userIdsWhoGetNotification) > 0){ |
| 19 | - $plainDescription = strip_tags($comment->description); | |
| 20 | - $action = 'task_comment_added'; | |
| 30 | + $plainDescription = wp_strip_all_tags($comment->description); | |
| 31 | + $action = $comment->parent_id ? 'task_reply_added' : 'comment_created'; | |
| 21 | 32 | $message = $plainDescription; |
| 22 | - $notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); | |
| 33 | + $settings = [ 'comment_id' => $comment->parent_id ?? $comment->id ]; | |
| 34 | + $notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message, $settings); | |
| 23 | 35 | $notification->users()->attach($userIdsWhoGetNotification); |
| 24 | 36 | } |
| 25 | 37 | } |
| 26 | 38 | } |
| 27 | 39 | |
| 28 | - public function mentionInCommentNotification($id, $comment_by, $mentionIds) | |
| 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) | |
| 29 | 47 | { |
| 30 | - $commenter_name = User::where('id', $comment_by)->first()->display_name; | |
| 31 | - $task_title = Task::findOrFail($id)->title; | |
| 32 | - $board_id = Task::findOrFail($id)->board_id; | |
| 33 | - $message = $commenter_name . ' mentioned you in a comment.'; | |
| 48 | + $settings = $comment->settings; | |
| 34 | 49 | |
| 35 | - foreach ($mentionIds as $id) { | |
| 36 | - if ($id != $comment_by) { | |
| 37 | - Notification::create([ | |
| 38 | - 'user_id' => (int) $id, | |
| 39 | - 'board_id' => (int) $board_id, | |
| 40 | - 'activity_by' => (int) $comment_by, | |
| 41 | - 'activity_type' => 'mention', | |
| 42 | - 'description' => $message, | |
| 43 | - ]); | |
| 44 | - } | |
| 50 | + if (!is_array($settings) || empty($settings['mentioned_id']) || !is_array($settings['mentioned_id'])) { | |
| 51 | + return []; | |
| 45 | 52 | } |
| 53 | + | |
| 54 | + return array_values(array_filter(array_map('intval', $settings['mentioned_id']))); | |
| 46 | 55 | } |
| 47 | 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 | + | |
| 48 | 90 | public function addSubtaskNotification($parentTask, $subTask) |
| 49 | 91 | { |
| 50 | 92 | $userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($parentTask); |
| 51 | 93 | if(count($userIdsWhoGetNotification) > 0){ |
| @@ -56,13 +98,13 @@ | ||
| 56 | 98 | $notification->users()->attach($userIdsWhoGetNotification); |
| 57 | 99 | } |
| 58 | 100 | } |
| 59 | 101 | |
| 60 | - public function changeDateNotification($task, $oldDate) | |
| 102 | + public function changeDueDateNotification($task, $oldDate) | |
| 61 | 103 | { |
| 62 | 104 | $userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 63 | 105 | if(count($userIdsWhoGetNotification) > 0){ |
| 64 | - $action = 'task_date_changed'; | |
| 106 | + $action = 'task_due_date_changed'; | |
| 65 | 107 | $message = date_i18n('F j, Y, g:i a', strtotime($task->due_at)); |
| 66 | 108 | $notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 67 | 109 | $notification->users()->attach($userIdsWhoGetNotification); |
| 68 | 110 | } |
| @@ -67,8 +109,19 @@ | ||
| 67 | 109 | $notification->users()->attach($userIdsWhoGetNotification); |
| 68 | 110 | } |
| 69 | 111 | } |
| 70 | 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 | + | |
| 71 | 124 | public function changeStageNotification($task, $oldStageId) |
| 72 | 125 | { |
| 73 | 126 | $userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 74 | 127 | if(count($userIdsWhoGetNotification) > 0){ |
| @@ -105,19 +158,18 @@ | ||
| 105 | 158 | $notification->users()->attach($userIdsWhoGetNotification); |
| 106 | 159 | } |
| 107 | 160 | } |
| 108 | 161 | |
| 109 | - public function changeTitleOrDescriptionNotification($task, $col, $oldTask) | |
| 162 | + public function changeTitleNotification($task, $col, $oldTask) | |
| 110 | 163 | { |
| 164 | + if ($col !== 'title') { | |
| 165 | + return; | |
| 166 | + } | |
| 167 | + | |
| 111 | 168 | $userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 112 | 169 | if(count($userIdsWhoGetNotification) > 0){ |
| 113 | - if($col == 'title'){ | |
| 114 | - $action = 'task_title_updated'; | |
| 115 | - $message = $task->title; | |
| 116 | - }else{ | |
| 117 | - $action = 'task_description_updated'; | |
| 118 | - $message = $task->description; | |
| 119 | - } | |
| 170 | + $action = 'task_title_updated'; | |
| 171 | + $message = $task->title; | |
| 120 | 172 | $notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 121 | 173 | $notification->users()->attach($userIdsWhoGetNotification); |
| 122 | 174 | } |
| 123 | 175 | } |
| @@ -127,33 +179,49 @@ | ||
| 127 | 179 | $userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 128 | 180 | if(count($userIdsWhoGetNotification) > 0){ |
| 129 | 181 | $new_board_id = Task::findOrFail($task->board_id)->board_id; |
| 130 | 182 | $new_board_title = Board::findOrFail($new_board_id)->title; |
| 131 | - $message = 'moved "' . $task->title . '" task to "' . $new_board_title . '" board.'; | |
| 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); | |
| 132 | 185 | $notification = $this->createNotification($oldBoardId, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $message); |
| 133 | 186 | $notification->users()->attach($userIdsWhoGetNotification); |
| 134 | 187 | } |
| 135 | 188 | } |
| 136 | 189 | |
| 137 | - public function assigneeAddedNotification($task, $newAssigneeId, $operation) | |
| 190 | + public function assigneeAddedNotification($task, $newAssigneeId) | |
| 138 | 191 | { |
| 139 | 192 | if($newAssigneeId != get_current_user_id()){ |
| 140 | 193 | $action = 'task_assignee_changed'; |
| 141 | - $message = 'has '.$operation.' you as an assignee.'; | |
| 194 | + $message = __('has added you as an assignee.','fluent-boards'); | |
| 142 | 195 | $notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 143 | 196 | $notification->users()->attach($newAssigneeId); |
| 144 | 197 | } |
| 145 | 198 | } |
| 146 | 199 | |
| 147 | - public function createNotification($task, $objectType, $action, $description) | |
| 200 | + public function assigneeRemovedNotification($task, $newAssigneeId) | |
| 148 | 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 | + { | |
| 149 | 212 | $data = [ |
| 150 | 213 | 'object_id' => $task->board_id, |
| 151 | 214 | 'object_type' => $objectType, |
| 152 | 215 | 'task_id' => $task->id, |
| 153 | 216 | 'action' => $action, |
| 154 | - 'description' => $description | |
| 217 | + 'description' => $description, | |
| 218 | + 'settings' => $settings | |
| 155 | 219 | ]; |
| 220 | + $currentUser = get_current_user_id(); | |
| 221 | + if($currentUser == 0) { | |
| 222 | + $data['activity_by'] = $task->created_by; | |
| 223 | + } | |
| 156 | 224 | return Notification::create($data); |
| 157 | 225 | } |
| 158 | 226 | |
| 159 | 227 | public function findUsersWhoWillGetNotification($task) |