| @@ -6,8 +6,9 @@ | ||
| 6 | 6 | use FluentCommunity\App\Models\Feed; |
| 7 | 7 | use FluentCommunity\App\Models\Notification; |
| 8 | 8 | use FluentCommunity\App\Models\NotificationSubscriber; |
| 9 | 9 | use FluentCommunity\App\Models\SpaceUserPivot; |
| 10 | +use FluentCommunity\App\Services\FeedsHelper; | |
| 10 | 11 | use FluentCommunity\Framework\Support\Arr; |
| 11 | 12 | |
| 12 | 13 | class NotificationEventHandler |
| 13 | 14 | { |
| @@ -59,9 +60,9 @@ | ||
| 59 | 60 | |
| 60 | 61 | $notificationContent = \sprintf( |
| 61 | 62 | /* translators: %1$s is the user name, %2$s is the feed title and %3$3s is the space title */ |
| 62 | 63 | __('%1$s posted %2$s in %3$s', 'fluent-community'), |
| 63 | - '<b class="fcom_nudn">' . esc_html($user->display_name) . '</b>', | |
| 64 | + '<b class="fcom_nudn">' . esc_html($user->getPublicDisplayName()) . '</b>', | |
| 64 | 65 | '<span class="fcom_nft">' . $feedTitle . '</span>', |
| 65 | 66 | '<b class="fcom_nst">' . esc_html($space->title) . '</b>' |
| 66 | 67 | ); |
| 67 | 68 | |
| @@ -99,9 +100,9 @@ | ||
| 99 | 100 | if ($existingNotification) { |
| 100 | 101 | $notificationContent = \sprintf( |
| 101 | 102 | /* translators: %1$s is the user name, %2$s is the like count & %3$s is the feed title */ |
| 102 | 103 | __('%1$s and %2$s other people reacted to your post %3$s', 'fluent-community'), |
| 103 | - '<b class="fcom_nudn">' . esc_html($user->display_name) . '</b>', | |
| 104 | + '<b class="fcom_nudn">' . esc_html($user->getPublicDisplayName()) . '</b>', | |
| 104 | 105 | '<b class="fcom_nrc">' . ($feed->reactions_count - 1) . '</b>', |
| 105 | 106 | '<span class="fcom_nft">' . $feedTitle . '</span>' |
| 106 | 107 | ); |
| 107 | 108 | |
| @@ -120,9 +121,9 @@ | ||
| 120 | 121 | |
| 121 | 122 | $notificationContent = \sprintf( |
| 122 | 123 | /* translators: %1$s is the user name, %2$s is the feed title */ |
| 123 | 124 | __('%1$s reacted to your post %2$s', 'fluent-community'), |
| 124 | - '<b class="fcom_nudn">' . esc_html($user->display_name) . '</b>', | |
| 125 | + '<b class="fcom_nudn">' . esc_html($user->getPublicDisplayName()) . '</b>', | |
| 125 | 126 | '<span class="fcom_nft">' . $feedTitle . '</span>' |
| 126 | 127 | ); |
| 127 | 128 | |
| 128 | 129 | $route = $feed->getJsRoute(); |
| @@ -153,9 +154,9 @@ | ||
| 153 | 154 | if ($comment->reactions_count > 1) { |
| 154 | 155 | $notificationContent = \sprintf( |
| 155 | 156 | /* translators: %1$s is the user name, %2$s is the like count & %3$s is the comment excerpt */ |
| 156 | 157 | __('%1$s and %2$s other people reacted to your comment %3$s', 'fluent-community'), |
| 157 | - '<b class="fcom_nudn">' . esc_html($user->display_name) . '</b>', | |
| 158 | + '<b class="fcom_nudn">' . esc_html($user->getPublicDisplayName()) . '</b>', | |
| 158 | 159 | '<b class="fcom_nrc">' . ($comment->reactions_count - 1) . '</b>', |
| 159 | 160 | '<span class="fcom_nft">' . $commentExcerpt . '</span>' |
| 160 | 161 | ); |
| 161 | 162 | } else { |
| @@ -161,9 +162,9 @@ | ||
| 161 | 162 | } else { |
| 162 | 163 | $notificationContent = \sprintf( |
| 163 | 164 | /* translators: %1$s is the user name, %2$s is the comment excerpt */ |
| 164 | 165 | __('%1$s reacted to your comment %2$s', 'fluent-community'), |
| 165 | - '<b class="fcom_nudn">' . esc_html($user->display_name) . '</b>', | |
| 166 | + '<b class="fcom_nudn">' . esc_html($user->getPublicDisplayName()) . '</b>', | |
| 166 | 167 | '<span class="fcom_nft">' . $commentExcerpt . '</span>' |
| 167 | 168 | ); |
| 168 | 169 | } |
| 169 | 170 | |
| @@ -201,10 +202,10 @@ | ||
| 201 | 202 | |
| 202 | 203 | $notificationContent = \sprintf( |
| 203 | 204 | /* translators: %1$s is the role name, %2$s is the space title */ |
| 204 | 205 | __('You have been added as %1$s in %2$s', 'fluent-community'), |
| 205 | - '<b>' . $pivot->role . '</b>', | |
| 206 | - '<b>' . $space->title . '</b>' | |
| 206 | + '<b>' . esc_html($pivot->role) . '</b>', | |
| 207 | + '<b>' . esc_html($space->title) . '</b>' | |
| 207 | 208 | ); |
| 208 | 209 | |
| 209 | 210 | $route = [ |
| 210 | 211 | 'name' => 'space_feeds', |
| @@ -226,18 +227,21 @@ | ||
| 226 | 227 | } |
| 227 | 228 | |
| 228 | 229 | protected function commentNotificationToAuthorFeed(Comment $comment, Feed $feed) |
| 229 | 230 | { |
| 230 | - if ($comment->user_id == $feed->user_id) { | |
| 231 | + $authorId = FeedsHelper::getNotificationAuthorId($feed); | |
| 232 | + | |
| 233 | + if ($comment->user_id == $authorId) { | |
| 231 | 234 | return; |
| 232 | 235 | } |
| 233 | 236 | |
| 234 | - $mentionedUserIds = Arr::get($feed->meta, 'mentioned_user_ids', []); | |
| 235 | - if (in_array($feed->user_id, $mentionedUserIds)) { | |
| 237 | + // Skip a mentioned author here; notifyMentionedUsers() notifies them instead. | |
| 238 | + $mentionedUserIds = Arr::get($comment->meta, 'mentioned_user_ids', []); | |
| 239 | + if (in_array($authorId, $mentionedUserIds)) { | |
| 236 | 240 | return; |
| 237 | 241 | } |
| 238 | 242 | |
| 239 | - $commenter = '<b class="fcom_nudn">' . esc_html($comment->user->display_name) . '</b>'; | |
| 243 | + $commenter = '<b class="fcom_nudn">' . esc_html($comment->user->getPublicDisplayName()) . '</b>'; | |
| 240 | 244 | $feedTitle = $feed->getHumanExcerpt(60); |
| 241 | 245 | |
| 242 | 246 | $exist = null; |
| 243 | 247 | if ($feed->comments_count > 1) { |
| @@ -243,18 +247,17 @@ | ||
| 243 | 247 | if ($feed->comments_count > 1) { |
| 244 | 248 | // check if we have existing notification for this feed and user |
| 245 | 249 | $exist = Notification::where('feed_id', $feed->id) |
| 246 | 250 | ->where('action', 'comment_added') |
| 247 | - ->whereHas('subscribers', function ($q) use ($feed) { | |
| 248 | - $q->where('user_id', $feed->user_id); | |
| 251 | + ->whereHas('subscribers', function ($q) use ($authorId) { | |
| 252 | + $q->where('user_id', $authorId); | |
| 249 | 253 | }) |
| 250 | 254 | ->first(); |
| 251 | 255 | |
| 252 | - $totalUsers = $feed->comments | |
| 253 | - ->where('user_id', '!=', $feed->user_id) | |
| 254 | - ->pluck('user_id') | |
| 255 | - ->unique() | |
| 256 | - ->count(); | |
| 256 | + $totalUsers = Comment::where('post_id', $feed->id) | |
| 257 | + ->where('user_id', '!=', $authorId) | |
| 258 | + ->distinct() | |
| 259 | + ->count('user_id'); | |
| 257 | 260 | |
| 258 | 261 | if ($feed->space_id) { |
| 259 | 262 | if ($totalUsers > 1) { |
| 260 | 263 | $notificationContent = \sprintf( |
| @@ -262,9 +265,9 @@ | ||
| 262 | 265 | __('%1$s and %2$s other people commented on your post %3$s in %4$s', 'fluent-community'), |
| 263 | 266 | $commenter, |
| 264 | 267 | '<b class="fcom_nrc">' . ($totalUsers - 1) . '</b>', |
| 265 | 268 | '<span class="fcom_nft">' . $feedTitle . '</span>', |
| 266 | - '<b class="fcom_nst">' . $feed->space->title . '</b>' | |
| 269 | + '<b class="fcom_nst">' . esc_html($feed->space->title) . '</b>' | |
| 267 | 270 | ); |
| 268 | 271 | } else { |
| 269 | 272 | $notificationContent = \sprintf( |
| 270 | 273 | /* translators: %1$s is the user name, %2$s is the feed title and %3$s space title*/ |
| @@ -270,9 +273,9 @@ | ||
| 270 | 273 | /* translators: %1$s is the user name, %2$s is the feed title and %3$s space title*/ |
| 271 | 274 | __('%1$s commented on your post: %2$s in %3$s', 'fluent-community'), |
| 272 | 275 | $commenter, |
| 273 | 276 | '<span class="fcom_nft">' . $feedTitle . '</span>', |
| 274 | - '<b class="fcom_nst">' . $feed->space->title . '</b>' | |
| 277 | + '<b class="fcom_nst">' . esc_html($feed->space->title) . '</b>' | |
| 275 | 278 | ); |
| 276 | 279 | } |
| 277 | 280 | } else { |
| 278 | 281 | |
| @@ -299,9 +302,9 @@ | ||
| 299 | 302 | /* translators: %1$s is the commenter name, %2$s is the feed title & %3$s is the space title */ |
| 300 | 303 | __('%1$s commented on your post: %2$s in %3$s', 'fluent-community'), |
| 301 | 304 | $commenter, |
| 302 | 305 | '<span class="fcom_nft">' . $feedTitle . '</span>', |
| 303 | - '<b class="fcom_nst">' . $feed->space->title . '</b>' | |
| 306 | + '<b class="fcom_nst">' . esc_html($feed->space->title) . '</b>' | |
| 304 | 307 | ); |
| 305 | 308 | } else { |
| 306 | 309 | $notificationContent = \sprintf( |
| 307 | 310 | /* translators: %1$s is the commenter name & %2$s is the feed title */ |
| @@ -321,9 +324,9 @@ | ||
| 321 | 324 | $exist->object_id = $comment->id; |
| 322 | 325 | $exist->save(); |
| 323 | 326 | |
| 324 | 327 | NotificationSubscriber::where('object_id', $exist->id) |
| 325 | - ->where('user_id', $feed->user_id) | |
| 328 | + ->where('user_id', $authorId) | |
| 326 | 329 | ->update([ |
| 327 | 330 | 'is_read' => 0, |
| 328 | 331 | 'updated_at' => current_time('mysql') |
| 329 | 332 | ]); |
| @@ -328,9 +331,9 @@ | ||
| 328 | 331 | 'updated_at' => current_time('mysql') |
| 329 | 332 | ]); |
| 330 | 333 | |
| 331 | 334 | do_action('fluent_community/notification/comment/notifed_to_author', [ |
| 332 | - 'user_ids' => [$feed->user_id], | |
| 335 | + 'user_ids' => [$authorId], | |
| 333 | 336 | 'notification' => $exist, |
| 334 | 337 | 'key' => 'notifed_to_author', |
| 335 | 338 | 'comment' => $comment, |
| 336 | 339 | 'feed' => $feed, |
| @@ -351,12 +354,12 @@ | ||
| 351 | 354 | ]; |
| 352 | 355 | |
| 353 | 356 | $notification = Notification::create($notification); |
| 354 | 357 | |
| 355 | - $notification->subscribe([$feed->user_id]); | |
| 358 | + $notification->subscribe([$authorId]); | |
| 356 | 359 | |
| 357 | 360 | do_action('fluent_community/notification/comment/notifed_to_author', [ |
| 358 | - 'user_ids' => [$feed->user_id], | |
| 361 | + 'user_ids' => [$authorId], | |
| 359 | 362 | 'notification' => $notification, |
| 360 | 363 | 'comment' => $comment, |
| 361 | 364 | 'key' => 'notifed_to_author', |
| 362 | 365 | 'feed' => $feed, |
| @@ -365,10 +368,17 @@ | ||
| 365 | 368 | } |
| 366 | 369 | |
| 367 | 370 | protected function commentNotificationToFeedCommenters($comment, $feed) |
| 368 | 371 | { |
| 372 | + $mentionedUserIds = Arr::get($comment->meta, 'mentioned_user_ids', []); | |
| 373 | + | |
| 374 | + // Notify mentioned users for both top-level comments and replies. | |
| 375 | + if ($mentionedUserIds) { | |
| 376 | + $this->notifyMentionedUsers($comment, $feed, $mentionedUserIds); | |
| 377 | + } | |
| 378 | + | |
| 369 | 379 | if ($comment->parent_id) { |
| 370 | - return $this->notifyForChildCommentReply($comment, $feed); | |
| 380 | + return $this->notifyForChildCommentReply($comment, $feed, $mentionedUserIds); | |
| 371 | 381 | } |
| 372 | 382 | |
| 373 | 383 | $userIds = Comment::whereNotIn('user_id', [$feed->user_id, $comment->user_id]) |
| 374 | 384 | ->where('post_id', $feed->id) |
| @@ -376,15 +386,13 @@ | ||
| 376 | 386 | ->distinct() |
| 377 | 387 | ->pluck('user_id') |
| 378 | 388 | ->toArray(); |
| 379 | 389 | |
| 380 | - $mentionedUserIds = Arr::get($comment->meta, 'mentioned_user_ids', []); | |
| 381 | - | |
| 382 | 390 | if (!$userIds && !$mentionedUserIds) { |
| 383 | 391 | return; |
| 384 | 392 | } |
| 385 | 393 | |
| 386 | - $commenter = '<b class="fcom_nudn">' . esc_html($comment->user->display_name) . '</b>'; | |
| 394 | + $commenter = '<b class="fcom_nudn">' . esc_html($comment->user->getPublicDisplayName()) . '</b>'; | |
| 387 | 395 | $feedTitle = '<span class="fcom_nft">' . $feed->getHumanExcerpt(60) . '</span>'; |
| 388 | 396 | |
| 389 | 397 | $route = $feed->getJsRoute(); |
| 390 | 398 | $space = $feed->space; |
| @@ -389,13 +397,12 @@ | ||
| 389 | 397 | $route = $feed->getJsRoute(); |
| 390 | 398 | $space = $feed->space; |
| 391 | 399 | |
| 392 | 400 | if ($feed->comments_count > 1) { |
| 393 | - $totalUsers = $feed->comments | |
| 401 | + $totalUsers = Comment::where('post_id', $feed->id) | |
| 394 | 402 | ->where('user_id', '!=', $comment->user_id) |
| 395 | - ->pluck('user_id') | |
| 396 | - ->unique() | |
| 397 | - ->count(); | |
| 403 | + ->distinct() | |
| 404 | + ->count('user_id'); | |
| 398 | 405 | |
| 399 | 406 | if ($feed->space_id) { |
| 400 | 407 | if ($totalUsers > 1) { |
| 401 | 408 | $notificationContent = \sprintf( |
| @@ -403,9 +410,9 @@ | ||
| 403 | 410 | __('%1$s and %2$s other people also commented on %3$s in %4$s', 'fluent-community'), |
| 404 | 411 | $commenter, |
| 405 | 412 | '<b class="fcom_nrc">' . ($totalUsers - 1) . '</b>', |
| 406 | 413 | $feedTitle, |
| 407 | - '<b class="fcom_nst">' . $feed->space->title . '</b>' | |
| 414 | + '<b class="fcom_nst">' . esc_html($feed->space->title) . '</b>' | |
| 408 | 415 | ); |
| 409 | 416 | } else { |
| 410 | 417 | $notificationContent = \sprintf( |
| 411 | 418 | /* translators: %1$s is the user name, %2$s is the feed title and %3$s space title*/ |
| @@ -411,9 +418,9 @@ | ||
| 411 | 418 | /* translators: %1$s is the user name, %2$s is the feed title and %3$s space title*/ |
| 412 | 419 | __('%1$s also commented on %2$s in %3$s', 'fluent-community'), |
| 413 | 420 | $commenter, |
| 414 | 421 | $feedTitle, |
| 415 | - '<b class="fcom_nst">' . $feed->space->title . '</b>' | |
| 422 | + '<b class="fcom_nst">' . esc_html($feed->space->title) . '</b>' | |
| 416 | 423 | ); |
| 417 | 424 | } |
| 418 | 425 | } else { |
| 419 | 426 | if ($totalUsers > 1) { |
| @@ -439,9 +446,9 @@ | ||
| 439 | 446 | /* translators: %1$s is the commenter name, %2$s is the feed title & %3$s is the space title */ |
| 440 | 447 | __('%1$s also commented on %2$s in %3$s', 'fluent-community'), |
| 441 | 448 | $commenter, |
| 442 | 449 | $feedTitle, |
| 443 | - '<b class="fcom_nst">' . $space->title . '</b>' | |
| 450 | + '<b class="fcom_nst">' . esc_html($space->title) . '</b>' | |
| 444 | 451 | ); |
| 445 | 452 | } else { |
| 446 | 453 | $notificationContent = \sprintf( |
| 447 | 454 | /* translators: %1$s is the commenter name & %2$s is the feed title */ |
| @@ -452,35 +459,8 @@ | ||
| 452 | 459 | } |
| 453 | 460 | } |
| 454 | 461 | |
| 455 | 462 | if ($mentionedUserIds) { |
| 456 | - $mentionNotification = Notification::create([ | |
| 457 | - 'feed_id' => $feed->id, | |
| 458 | - 'object_id' => $comment->id, | |
| 459 | - 'src_user_id' => $comment->user_id, | |
| 460 | - 'src_object_type' => 'comment', | |
| 461 | - 'action' => 'mention_added', | |
| 462 | - 'content' => \sprintf( | |
| 463 | - /* translators: %1$s is the commenter name & %2$s is the feed title */ | |
| 464 | - __('%1$s mentioned you in a comment at %2$s', 'fluent-community'), | |
| 465 | - $commenter, | |
| 466 | - $feedTitle, | |
| 467 | - ), | |
| 468 | - 'route' => $route, | |
| 469 | - ]); | |
| 470 | - | |
| 471 | - $mentionNotification->subscribe($mentionedUserIds); | |
| 472 | - | |
| 473 | - do_action('fluent_community/notification/comment/notifed_to_mentions', [ | |
| 474 | - 'user_ids' => $mentionedUserIds, | |
| 475 | - 'notification' => $mentionNotification, | |
| 476 | - 'key' => 'notifed_to_mentions', | |
| 477 | - 'comment' => $comment, | |
| 478 | - 'feed' => $feed | |
| 479 | - ]); | |
| 480 | - } | |
| 481 | - | |
| 482 | - if ($mentionedUserIds) { | |
| 483 | 463 | $userIds = array_values(array_diff($userIds, $mentionedUserIds)); |
| 484 | 464 | } |
| 485 | 465 | |
| 486 | 466 | if (!$userIds) { |
| @@ -546,10 +526,47 @@ | ||
| 546 | 526 | 'feed' => $feed |
| 547 | 527 | ]); |
| 548 | 528 | } |
| 549 | 529 | |
| 550 | - protected function notifyForChildCommentReply($comment, $feed) | |
| 530 | + protected function notifyMentionedUsers($comment, $feed, $mentionedUserIds) | |
| 551 | 531 | { |
| 532 | + if (!$mentionedUserIds) { | |
| 533 | + return; | |
| 534 | + } | |
| 535 | + | |
| 536 | + $commenter = '<b class="fcom_nudn">' . esc_html($comment->user->getPublicDisplayName()) . '</b>'; | |
| 537 | + $feedTitle = '<span class="fcom_nft">' . $feed->getHumanExcerpt(60) . '</span>'; | |
| 538 | + | |
| 539 | + $mentionNotification = Notification::create([ | |
| 540 | + 'feed_id' => $feed->id, | |
| 541 | + 'object_id' => $comment->id, | |
| 542 | + 'src_user_id' => $comment->user_id, | |
| 543 | + 'src_object_type' => 'comment', | |
| 544 | + 'action' => 'mention_added', | |
| 545 | + 'content' => \sprintf( | |
| 546 | + /* translators: %1$s is the commenter name & %2$s is the feed title */ | |
| 547 | + __('%1$s mentioned you in a comment at %2$s', 'fluent-community'), | |
| 548 | + $commenter, | |
| 549 | + $feedTitle, | |
| 550 | + ), | |
| 551 | + 'route' => $feed->getJsRoute(), | |
| 552 | + ]); | |
| 553 | + | |
| 554 | + $mentionNotification->subscribe($mentionedUserIds); | |
| 555 | + | |
| 556 | + do_action('fluent_community/notification/comment/notifed_to_mentions', [ | |
| 557 | + 'user_ids' => $mentionedUserIds, | |
| 558 | + 'notification' => $mentionNotification, | |
| 559 | + 'key' => 'notifed_to_mentions', | |
| 560 | + 'comment' => $comment, | |
| 561 | + 'feed' => $feed | |
| 562 | + ]); | |
| 563 | + | |
| 564 | + return $mentionNotification; | |
| 565 | + } | |
| 566 | + | |
| 567 | + protected function notifyForChildCommentReply($comment, $feed, $mentionedUserIds = []) | |
| 568 | + { | |
| 552 | 569 | // This is a parent comment, so we need to notify the parent comment author & all child comment authors |
| 553 | 570 | $childCommentUserIds = Comment::where(function ($q) use ($comment) { |
| 554 | 571 | $q->where('parent_id', $comment->parent_id) |
| 555 | 572 | ->orWhere('id', $comment->parent_id); |
| @@ -560,13 +577,18 @@ | ||
| 560 | 577 | ->get() |
| 561 | 578 | ->pluck('user_id') |
| 562 | 579 | ->toArray(); |
| 563 | 580 | |
| 581 | + // Mentioned users get a mention notification instead, so skip them here. | |
| 582 | + if ($mentionedUserIds) { | |
| 583 | + $childCommentUserIds = array_values(array_diff($childCommentUserIds, $mentionedUserIds)); | |
| 584 | + } | |
| 585 | + | |
| 564 | 586 | if (!$childCommentUserIds) { |
| 565 | 587 | return false; |
| 566 | 588 | } |
| 567 | 589 | |
| 568 | - $commenter = '<b class="fcom_nudn">' . esc_html($comment->user->display_name) . '</b>'; | |
| 590 | + $commenter = '<b class="fcom_nudn">' . esc_html($comment->user->getPublicDisplayName()) . '</b>'; | |
| 569 | 591 | |
| 570 | 592 | $existingNotification = Notification::where('object_id', $comment->parent_id) |
| 571 | 593 | ->where('action', 'child_comment_added') |
| 572 | 594 | ->whereHas('subscribers', function ($q) use ($childCommentUserIds) { |
| @@ -663,9 +685,9 @@ | ||
| 663 | 685 | |
| 664 | 686 | $notificationContent = \sprintf( |
| 665 | 687 | /* translators: %1$s is the user name, %2$s is the feed title */ |
| 666 | 688 | __('%1$s mentioned you in a post: %2$s', 'fluent-community'), |
| 667 | - '<b class="fcom_nudn">' . esc_html($user->display_name) . '</b>', | |
| 689 | + '<b class="fcom_nudn">' . esc_html($user->getPublicDisplayName()) . '</b>', | |
| 668 | 690 | '<b class="fcom_nft">' . $feedTitle . '</b>' |
| 669 | 691 | ); |
| 670 | 692 | |
| 671 | 693 | $route = $feed->getJsRoute(); |