PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.11.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.11.0
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
← All changes | app/Hooks/Handlers/NotificationEventHandler.php +92 -64 2.5.02.11.0 View file →
@@ -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,15 +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')
251 + ->whereHas('subscribers', function ($q) use ($authorId) {
252 + $q->where('user_id', $authorId);
253 + })
247 254 ->first();
248 255
249 - $totalUsers = $feed->comments
250 - ->where('user_id', '!=', $feed->user_id)
251 - ->pluck('user_id')
252 - ->unique()
253 - ->count();
256 + $totalUsers = Comment::where('post_id', $feed->id)
257 + ->where('user_id', '!=', $authorId)
258 + ->distinct()
259 + ->count('user_id');
254 260
255 261 if ($feed->space_id) {
256 262 if ($totalUsers > 1) {
257 263 $notificationContent = \sprintf(
@@ -259,9 +265,9 @@
259 265 __('%1$s and %2$s other people commented on your post %3$s in %4$s', 'fluent-community'),
260 266 $commenter,
261 267 '<b class="fcom_nrc">' . ($totalUsers - 1) . '</b>',
262 268 '<span class="fcom_nft">' . $feedTitle . '</span>',
263 - '<b class="fcom_nst">' . $feed->space->title . '</b>'
269 + '<b class="fcom_nst">' . esc_html($feed->space->title) . '</b>'
264 270 );
265 271 } else {
266 272 $notificationContent = \sprintf(
267 273 /* translators: %1$s is the user name, %2$s is the feed title and %3$s space title*/
@@ -267,9 +273,9 @@
267 273 /* translators: %1$s is the user name, %2$s is the feed title and %3$s space title*/
268 274 __('%1$s commented on your post: %2$s in %3$s', 'fluent-community'),
269 275 $commenter,
270 276 '<span class="fcom_nft">' . $feedTitle . '</span>',
271 - '<b class="fcom_nst">' . $feed->space->title . '</b>'
277 + '<b class="fcom_nst">' . esc_html($feed->space->title) . '</b>'
272 278 );
273 279 }
274 280 } else {
275 281
@@ -296,9 +302,9 @@
296 302 /* translators: %1$s is the commenter name, %2$s is the feed title & %3$s is the space title */
297 303 __('%1$s commented on your post: %2$s in %3$s', 'fluent-community'),
298 304 $commenter,
299 305 '<span class="fcom_nft">' . $feedTitle . '</span>',
300 - '<b class="fcom_nst">' . $feed->space->title . '</b>'
306 + '<b class="fcom_nst">' . esc_html($feed->space->title) . '</b>'
301 307 );
302 308 } else {
303 309 $notificationContent = \sprintf(
304 310 /* translators: %1$s is the commenter name & %2$s is the feed title */
@@ -318,9 +324,9 @@
318 324 $exist->object_id = $comment->id;
319 325 $exist->save();
320 326
321 327 NotificationSubscriber::where('object_id', $exist->id)
322 - ->where('user_id', $feed->user_id)
328 + ->where('user_id', $authorId)
323 329 ->update([
324 330 'is_read' => 0,
325 331 'updated_at' => current_time('mysql')
326 332 ]);
@@ -325,9 +331,9 @@
325 331 'updated_at' => current_time('mysql')
326 332 ]);
327 333
328 334 do_action('fluent_community/notification/comment/notifed_to_author', [
329 - 'user_ids' => [$feed->user_id],
335 + 'user_ids' => [$authorId],
330 336 'notification' => $exist,
331 337 'key' => 'notifed_to_author',
332 338 'comment' => $comment,
333 339 'feed' => $feed,
@@ -348,12 +354,12 @@
348 354 ];
349 355
350 356 $notification = Notification::create($notification);
351 357
352 - $notification->subscribe([$feed->user_id]);
358 + $notification->subscribe([$authorId]);
353 359
354 360 do_action('fluent_community/notification/comment/notifed_to_author', [
355 - 'user_ids' => [$feed->user_id],
361 + 'user_ids' => [$authorId],
356 362 'notification' => $notification,
357 363 'comment' => $comment,
358 364 'key' => 'notifed_to_author',
359 365 'feed' => $feed,
@@ -362,10 +368,17 @@
362 368 }
363 369
364 370 protected function commentNotificationToFeedCommenters($comment, $feed)
365 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 +
366 379 if ($comment->parent_id) {
367 - return $this->notifyForChildCommentReply($comment, $feed);
380 + return $this->notifyForChildCommentReply($comment, $feed, $mentionedUserIds);
368 381 }
369 382
370 383 $userIds = Comment::whereNotIn('user_id', [$feed->user_id, $comment->user_id])
371 384 ->where('post_id', $feed->id)
@@ -373,15 +386,13 @@
373 386 ->distinct()
374 387 ->pluck('user_id')
375 388 ->toArray();
376 389
377 - $mentionedUserIds = Arr::get($comment->meta, 'mentioned_user_ids', []);
378 -
379 390 if (!$userIds && !$mentionedUserIds) {
380 391 return;
381 392 }
382 393
383 - $commenter = '<b class="fcom_nudn">' . esc_html($comment->user->display_name) . '</b>';
394 + $commenter = '<b class="fcom_nudn">' . esc_html($comment->user->getPublicDisplayName()) . '</b>';
384 395 $feedTitle = '<span class="fcom_nft">' . $feed->getHumanExcerpt(60) . '</span>';
385 396
386 397 $route = $feed->getJsRoute();
387 398 $space = $feed->space;
@@ -386,13 +397,12 @@
386 397 $route = $feed->getJsRoute();
387 398 $space = $feed->space;
388 399
389 400 if ($feed->comments_count > 1) {
390 - $totalUsers = $feed->comments
401 + $totalUsers = Comment::where('post_id', $feed->id)
391 402 ->where('user_id', '!=', $comment->user_id)
392 - ->pluck('user_id')
393 - ->unique()
394 - ->count();
403 + ->distinct()
404 + ->count('user_id');
395 405
396 406 if ($feed->space_id) {
397 407 if ($totalUsers > 1) {
398 408 $notificationContent = \sprintf(
@@ -400,9 +410,9 @@
400 410 __('%1$s and %2$s other people also commented on %3$s in %4$s', 'fluent-community'),
401 411 $commenter,
402 412 '<b class="fcom_nrc">' . ($totalUsers - 1) . '</b>',
403 413 $feedTitle,
404 - '<b class="fcom_nst">' . $feed->space->title . '</b>'
414 + '<b class="fcom_nst">' . esc_html($feed->space->title) . '</b>'
405 415 );
406 416 } else {
407 417 $notificationContent = \sprintf(
408 418 /* translators: %1$s is the user name, %2$s is the feed title and %3$s space title*/
@@ -408,9 +418,9 @@
408 418 /* translators: %1$s is the user name, %2$s is the feed title and %3$s space title*/
409 419 __('%1$s also commented on %2$s in %3$s', 'fluent-community'),
410 420 $commenter,
411 421 $feedTitle,
412 - '<b class="fcom_nst">' . $feed->space->title . '</b>'
422 + '<b class="fcom_nst">' . esc_html($feed->space->title) . '</b>'
413 423 );
414 424 }
415 425 } else {
416 426 if ($totalUsers > 1) {
@@ -436,9 +446,9 @@
436 446 /* translators: %1$s is the commenter name, %2$s is the feed title & %3$s is the space title */
437 447 __('%1$s also commented on %2$s in %3$s', 'fluent-community'),
438 448 $commenter,
439 449 $feedTitle,
440 - '<b class="fcom_nst">' . $space->title . '</b>'
450 + '<b class="fcom_nst">' . esc_html($space->title) . '</b>'
441 451 );
442 452 } else {
443 453 $notificationContent = \sprintf(
444 454 /* translators: %1$s is the commenter name & %2$s is the feed title */
@@ -449,35 +459,8 @@
449 459 }
450 460 }
451 461
452 462 if ($mentionedUserIds) {
453 - $mentionNotification = Notification::create([
454 - 'feed_id' => $feed->id,
455 - 'object_id' => $comment->id,
456 - 'src_user_id' => $comment->user_id,
457 - 'src_object_type' => 'comment',
458 - 'action' => 'mention_added',
459 - 'content' => \sprintf(
460 - /* translators: %1$s is the commenter name & %2$s is the feed title */
461 - __('%1$s mentioned you in a comment at %2$s', 'fluent-community'),
462 - $commenter,
463 - $feedTitle,
464 - ),
465 - 'route' => $route,
466 - ]);
467 -
468 - $mentionNotification->subscribe($mentionedUserIds);
469 -
470 - do_action('fluent_community/notification/comment/notifed_to_mentions', [
471 - 'user_ids' => $mentionedUserIds,
472 - 'notification' => $mentionNotification,
473 - 'key' => 'notifed_to_mentions',
474 - 'comment' => $comment,
475 - 'feed' => $feed
476 - ]);
477 - }
478 -
479 - if ($mentionedUserIds) {
480 463 $userIds = array_values(array_diff($userIds, $mentionedUserIds));
481 464 }
482 465
483 466 if (!$userIds) {
@@ -543,10 +526,47 @@
543 526 'feed' => $feed
544 527 ]);
545 528 }
546 529
547 - protected function notifyForChildCommentReply($comment, $feed)
530 + protected function notifyMentionedUsers($comment, $feed, $mentionedUserIds)
548 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 + {
549 569 // This is a parent comment, so we need to notify the parent comment author & all child comment authors
550 570 $childCommentUserIds = Comment::where(function ($q) use ($comment) {
551 571 $q->where('parent_id', $comment->parent_id)
552 572 ->orWhere('id', $comment->parent_id);
@@ -557,16 +577,24 @@
557 577 ->get()
558 578 ->pluck('user_id')
559 579 ->toArray();
560 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 +
561 586 if (!$childCommentUserIds) {
562 587 return false;
563 588 }
564 589
565 - $commenter = '<b class="fcom_nudn">' . esc_html($comment->user->display_name) . '</b>';
590 + $commenter = '<b class="fcom_nudn">' . esc_html($comment->user->getPublicDisplayName()) . '</b>';
566 591
567 592 $existingNotification = Notification::where('object_id', $comment->parent_id)
568 593 ->where('action', 'child_comment_added')
594 + ->whereHas('subscribers', function ($q) use ($childCommentUserIds) {
595 + $q->whereIn('user_id', $childCommentUserIds);
596 + })
569 597 ->first();
570 598
571 599 if ($existingNotification) {
572 600 $newContent = \sprintf(
@@ -657,9 +685,9 @@
657 685
658 686 $notificationContent = \sprintf(
659 687 /* translators: %1$s is the user name, %2$s is the feed title */
660 688 __('%1$s mentioned you in a post: %2$s', 'fluent-community'),
661 - '<b class="fcom_nudn">' . esc_html($user->display_name) . '</b>',
689 + '<b class="fcom_nudn">' . esc_html($user->getPublicDisplayName()) . '</b>',
662 690 '<b class="fcom_nft">' . $feedTitle . '</b>'
663 691 );
664 692
665 693 $route = $feed->getJsRoute();