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