| @@ -9,13 +9,24 @@ | ||
| 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 | 30 | $plainDescription = wp_strip_all_tags($comment->description); |
| 20 | 31 | $action = $comment->parent_id ? 'task_reply_added' : 'comment_created'; |
| 21 | 32 | $message = $plainDescription; |
| @@ -25,8 +36,45 @@ | ||
| 25 | 36 | } |
| 26 | 37 | } |
| 27 | 38 | } |
| 28 | 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 | + | |
| 29 | 77 | public function mentionInCommentNotification($comment, $mentionIds) |
| 30 | 78 | { |
| 31 | 79 | if($comment){ |
| 32 | 80 | $task = Task::findOrFail($comment->task_id); |
| @@ -110,19 +158,18 @@ | ||
| 110 | 158 | $notification->users()->attach($userIdsWhoGetNotification); |
| 111 | 159 | } |
| 112 | 160 | } |
| 113 | 161 | |
| 114 | - public function changeTitleOrDescriptionNotification($task, $col, $oldTask) | |
| 162 | + public function changeTitleNotification($task, $col, $oldTask) | |
| 115 | 163 | { |
| 164 | + if ($col !== 'title') { | |
| 165 | + return; | |
| 166 | + } | |
| 167 | + | |
| 116 | 168 | $userIdsWhoGetNotification = $this->findUsersWhoWillGetNotification($task); |
| 117 | 169 | if(count($userIdsWhoGetNotification) > 0){ |
| 118 | - if($col == 'title'){ | |
| 119 | - $action = 'task_title_updated'; | |
| 120 | - $message = $task->title; | |
| 121 | - }else{ | |
| 122 | - $action = 'task_description_updated'; | |
| 123 | - $message = $task->description; | |
| 124 | - } | |
| 170 | + $action = 'task_title_updated'; | |
| 171 | + $message = $task->title; | |
| 125 | 172 | $notification = $this->createNotification($task, Constant::OBJECT_TYPE_BOARD_NOTIFICATION, $action, $message); |
| 126 | 173 | $notification->users()->attach($userIdsWhoGetNotification); |
| 127 | 174 | } |
| 128 | 175 | } |