| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentCommunity\App\Models\Comment; |
| 6 |
use FluentCommunity\App\Models\Feed; |
| 7 |
use FluentCommunity\App\Models\Notification; |
| 8 |
use FluentCommunity\App\Models\NotificationSubscriber; |
| 9 |
use FluentCommunity\App\Models\SpaceUserPivot; |
| 10 |
use FluentCommunity\App\Services\FeedsHelper; |
| 11 |
use FluentCommunity\Framework\Support\Arr; |
| 12 |
|
| 13 |
class NotificationEventHandler |
| 14 |
{ |
| 15 |
public function register() |
| 16 |
{ |
| 17 |
add_action('fluent_community/comment_added', [$this, 'handleNewCommentEvent'], 10, 2); |
| 18 |
add_action('fluent_community/space_feed/created', [$this, 'handleNewSpaceFeed'], 10); |
| 19 |
add_action('fluent_community/feed/react_added', [$this, 'handleNewFeedReact'], 10, 2); |
| 20 |
add_action('fluent_community/comment/react_added', [$this, 'handleNewCommentReact'], 10, 3); |
| 21 |
|
| 22 |
add_action('fluent_community/space/member/role_updated', [$this, 'handleSpaceMemberRoleUpdated'], 10, 2); |
| 23 |
|
| 24 |
/* |
| 25 |
* Mentions handler |
| 26 |
*/ |
| 27 |
add_action('fluent_community/feed/created', [$this, 'maybeHandleMentionedUserIds'], 10, 1); |
| 28 |
} |
| 29 |
|
| 30 |
public function handleNewCommentEvent($comment, $feed) |
| 31 |
{ |
| 32 |
$this->commentNotificationToAuthorFeed($comment, $feed); |
| 33 |
|
| 34 |
// now notify all users who commented on this feed |
| 35 |
$this->commentNotificationToFeedCommenters($comment, $feed); |
| 36 |
} |
| 37 |
|
| 38 |
public function handleNewSpaceFeed($feed) |
| 39 |
{ |
| 40 |
if (!$this->willCreateFeedCreatedNotification($feed)) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
$user = $feed->user; |
| 45 |
$space = $feed->space; |
| 46 |
|
| 47 |
$this->maybeHasEveryoneTag($feed); |
| 48 |
|
| 49 |
$userIds = SpaceUserPivot::where('space_id', $space->id) |
| 50 |
->where('user_id', '!=', $user->ID) |
| 51 |
->where('status', 'active') |
| 52 |
->pluck('user_id') |
| 53 |
->toArray(); |
| 54 |
|
| 55 |
if (!$userIds) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$feedTitle = $feed->getHumanExcerpt(60); |
| 60 |
|
| 61 |
$notificationContent = \sprintf( |
| 62 |
/* translators: %1$s is the user name, %2$s is the feed title and %3$3s is the space title */ |
| 63 |
__('%1$s posted %2$s in %3$s', 'fluent-community'), |
| 64 |
'<b class="fcom_nudn">' . esc_html($user->getPublicDisplayName()) . '</b>', |
| 65 |
'<span class="fcom_nft">' . $feedTitle . '</span>', |
| 66 |
'<b class="fcom_nst">' . esc_html($space->title) . '</b>' |
| 67 |
); |
| 68 |
|
| 69 |
$route = $feed->getJsRoute(); |
| 70 |
|
| 71 |
$notification = [ |
| 72 |
'feed_id' => $feed->id, |
| 73 |
'src_user_id' => $user->ID, |
| 74 |
'src_object_type' => 'feed', |
| 75 |
'action' => 'space_feed/created', |
| 76 |
'content' => $notificationContent, |
| 77 |
'route' => $route, |
| 78 |
]; |
| 79 |
|
| 80 |
$notification = Notification::create($notification); |
| 81 |
|
| 82 |
$notification->subscribe($userIds); |
| 83 |
} |
| 84 |
|
| 85 |
public function handleNewFeedReact($react, $feed) |
| 86 |
{ |
| 87 |
if ($react->user_id == $feed->user_id) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
$feedTitle = $feed->getHumanExcerpt(40); |
| 92 |
$user = $react->user; |
| 93 |
|
| 94 |
if ($feed->reactions_count > 1) { |
| 95 |
// check if we have existing notification for this feed and user |
| 96 |
$existingNotification = Notification::where('feed_id', $feed->id) |
| 97 |
->where('action', 'feed/react_added') |
| 98 |
->first(); |
| 99 |
|
| 100 |
if ($existingNotification) { |
| 101 |
$notificationContent = \sprintf( |
| 102 |
/* translators: %1$s is the user name, %2$s is the like count & %3$s is the feed title */ |
| 103 |
__('%1$s and %2$s other people reacted to your post %3$s', 'fluent-community'), |
| 104 |
'<b class="fcom_nudn">' . esc_html($user->getPublicDisplayName()) . '</b>', |
| 105 |
'<b class="fcom_nrc">' . ($feed->reactions_count - 1) . '</b>', |
| 106 |
'<span class="fcom_nft">' . $feedTitle . '</span>' |
| 107 |
); |
| 108 |
|
| 109 |
$existingNotification->content = $notificationContent; |
| 110 |
$existingNotification->src_user_id = $user->ID; |
| 111 |
$existingNotification->save(); |
| 112 |
NotificationSubscriber::where('object_id', $existingNotification->id) |
| 113 |
->where('user_id', $feed->user_id) |
| 114 |
->update([ |
| 115 |
'is_read' => 0, |
| 116 |
'updated_at' => current_time('mysql') |
| 117 |
]); |
| 118 |
return; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
$notificationContent = \sprintf( |
| 123 |
/* translators: %1$s is the user name, %2$s is the feed title */ |
| 124 |
__('%1$s reacted to your post %2$s', 'fluent-community'), |
| 125 |
'<b class="fcom_nudn">' . esc_html($user->getPublicDisplayName()) . '</b>', |
| 126 |
'<span class="fcom_nft">' . $feedTitle . '</span>' |
| 127 |
); |
| 128 |
|
| 129 |
$route = $feed->getJsRoute(); |
| 130 |
|
| 131 |
$notification = [ |
| 132 |
'feed_id' => $feed->id, |
| 133 |
'src_user_id' => $user->ID, |
| 134 |
'src_object_type' => 'feed', |
| 135 |
'action' => 'feed/react_added', |
| 136 |
'content' => $notificationContent, |
| 137 |
'route' => $route, |
| 138 |
]; |
| 139 |
|
| 140 |
$notification = Notification::create($notification); |
| 141 |
|
| 142 |
$notification->subscribe([$feed->user_id]); |
| 143 |
} |
| 144 |
|
| 145 |
public function handleNewCommentReact($react, $comment, $feed) |
| 146 |
{ |
| 147 |
if ($react->user_id == $comment->user_id) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
$commentExcerpt = $comment->getHumanExcerpt(40); |
| 152 |
$user = $react->user; |
| 153 |
|
| 154 |
if ($comment->reactions_count > 1) { |
| 155 |
$notificationContent = \sprintf( |
| 156 |
/* translators: %1$s is the user name, %2$s is the like count & %3$s is the comment excerpt */ |
| 157 |
__('%1$s and %2$s other people reacted to your comment %3$s', 'fluent-community'), |
| 158 |
'<b class="fcom_nudn">' . esc_html($user->getPublicDisplayName()) . '</b>', |
| 159 |
'<b class="fcom_nrc">' . ($comment->reactions_count - 1) . '</b>', |
| 160 |
'<span class="fcom_nft">' . $commentExcerpt . '</span>' |
| 161 |
); |
| 162 |
} else { |
| 163 |
$notificationContent = \sprintf( |
| 164 |
/* translators: %1$s is the user name, %2$s is the comment excerpt */ |
| 165 |
__('%1$s reacted to your comment %2$s', 'fluent-community'), |
| 166 |
'<b class="fcom_nudn">' . esc_html($user->getPublicDisplayName()) . '</b>', |
| 167 |
'<span class="fcom_nft">' . $commentExcerpt . '</span>' |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
$existingNotification = Notification::where('feed_id', $feed->id) |
| 172 |
->where('object_id', $comment->id) |
| 173 |
->where('action', 'comment/react_added') |
| 174 |
->first(); |
| 175 |
|
| 176 |
if ($existingNotification) { |
| 177 |
$existingNotification->content = $notificationContent; |
| 178 |
$existingNotification->src_user_id = $user->ID; |
| 179 |
$existingNotification->save(); |
| 180 |
NotificationSubscriber::where('object_id', $existingNotification->id) |
| 181 |
->where('user_id', $comment->user_id) |
| 182 |
->update(['is_read' => 0, 'updated_at' => current_time('mysql')]); |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
$notification = Notification::create([ |
| 187 |
'feed_id' => $feed->id, |
| 188 |
'object_id' => $comment->id, |
| 189 |
'src_user_id' => $user->ID, |
| 190 |
'src_object_type' => 'comment', |
| 191 |
'action' => 'comment/react_added', |
| 192 |
'content' => $notificationContent, |
| 193 |
'route' => $feed->getJsRoute(), |
| 194 |
]); |
| 195 |
|
| 196 |
$notification->subscribe([$comment->user_id]); |
| 197 |
} |
| 198 |
|
| 199 |
public function handleSpaceMemberRoleUpdated($space, $pivot) |
| 200 |
{ |
| 201 |
$user = $pivot->user; |
| 202 |
|
| 203 |
$notificationContent = \sprintf( |
| 204 |
/* translators: %1$s is the role name, %2$s is the space title */ |
| 205 |
__('You have been added as %1$s in %2$s', 'fluent-community'), |
| 206 |
'<b>' . esc_html($pivot->role) . '</b>', |
| 207 |
'<b>' . esc_html($space->title) . '</b>' |
| 208 |
); |
| 209 |
|
| 210 |
$route = [ |
| 211 |
'name' => 'space_feeds', |
| 212 |
'params' => [ |
| 213 |
'space' => $space->slug |
| 214 |
] |
| 215 |
]; |
| 216 |
|
| 217 |
$notification = [ |
| 218 |
'object_id' => $space->id, |
| 219 |
'src_user_id' => $user->ID, |
| 220 |
'src_object_type' => 'space', |
| 221 |
'action' => 'space/member/role_updated', |
| 222 |
'content' => $notificationContent, |
| 223 |
'route' => $route, |
| 224 |
]; |
| 225 |
$notification = Notification::create($notification); |
| 226 |
$notification->subscribe([$pivot->user_id]); |
| 227 |
} |
| 228 |
|
| 229 |
protected function commentNotificationToAuthorFeed(Comment $comment, Feed $feed) |
| 230 |
{ |
| 231 |
$authorId = FeedsHelper::getNotificationAuthorId($feed); |
| 232 |
|
| 233 |
if ($comment->user_id == $authorId) { |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 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)) { |
| 240 |
return; |
| 241 |
} |
| 242 |
|
| 243 |
$commenter = '<b class="fcom_nudn">' . esc_html($comment->user->getPublicDisplayName()) . '</b>'; |
| 244 |
$feedTitle = $feed->getHumanExcerpt(60); |
| 245 |
|
| 246 |
$exist = null; |
| 247 |
if ($feed->comments_count > 1) { |
| 248 |
// check if we have existing notification for this feed and user |
| 249 |
$exist = Notification::where('feed_id', $feed->id) |
| 250 |
->where('action', 'comment_added') |
| 251 |
->whereHas('subscribers', function ($q) use ($authorId) { |
| 252 |
$q->where('user_id', $authorId); |
| 253 |
}) |
| 254 |
->first(); |
| 255 |
|
| 256 |
$totalUsers = Comment::where('post_id', $feed->id) |
| 257 |
->where('user_id', '!=', $authorId) |
| 258 |
->distinct() |
| 259 |
->count('user_id'); |
| 260 |
|
| 261 |
if ($feed->space_id) { |
| 262 |
if ($totalUsers > 1) { |
| 263 |
$notificationContent = \sprintf( |
| 264 |
/* translators: %1$s is the user name, %2$s is the people count, %3$s is the feed title and %4$s space title*/ |
| 265 |
__('%1$s and %2$s other people commented on your post %3$s in %4$s', 'fluent-community'), |
| 266 |
$commenter, |
| 267 |
'<b class="fcom_nrc">' . ($totalUsers - 1) . '</b>', |
| 268 |
'<span class="fcom_nft">' . $feedTitle . '</span>', |
| 269 |
'<b class="fcom_nst">' . esc_html($feed->space->title) . '</b>' |
| 270 |
); |
| 271 |
} else { |
| 272 |
$notificationContent = \sprintf( |
| 273 |
/* translators: %1$s is the user name, %2$s is the feed title and %3$s space title*/ |
| 274 |
__('%1$s commented on your post: %2$s in %3$s', 'fluent-community'), |
| 275 |
$commenter, |
| 276 |
'<span class="fcom_nft">' . $feedTitle . '</span>', |
| 277 |
'<b class="fcom_nst">' . esc_html($feed->space->title) . '</b>' |
| 278 |
); |
| 279 |
} |
| 280 |
} else { |
| 281 |
|
| 282 |
if ($totalUsers > 1) { |
| 283 |
$notificationContent = \sprintf( |
| 284 |
/* translators: %1$s is the user name, %2$s is the like count & %3$s is the feed title */ |
| 285 |
__('%1$s and %2$s other people commented on your post %3$s', 'fluent-community'), |
| 286 |
$commenter, |
| 287 |
'<b class="fcom_nrc">' . ($totalUsers - 1) . '</b>', |
| 288 |
'<span class="fcom_nft">' . $feedTitle . '</span>' |
| 289 |
); |
| 290 |
} else { |
| 291 |
$notificationContent = \sprintf( |
| 292 |
/* translators: %1$s is the user name & %2$s is the feed title */ |
| 293 |
__('%1$s commented on your post: %2$s', 'fluent-community'), |
| 294 |
$commenter, |
| 295 |
'<span class="fcom_nft">' . $feedTitle . '</span>' |
| 296 |
); |
| 297 |
} |
| 298 |
} |
| 299 |
} else { |
| 300 |
if ($feed->space_id) { |
| 301 |
$notificationContent = \sprintf( |
| 302 |
/* translators: %1$s is the commenter name, %2$s is the feed title & %3$s is the space title */ |
| 303 |
__('%1$s commented on your post: %2$s in %3$s', 'fluent-community'), |
| 304 |
$commenter, |
| 305 |
'<span class="fcom_nft">' . $feedTitle . '</span>', |
| 306 |
'<b class="fcom_nst">' . esc_html($feed->space->title) . '</b>' |
| 307 |
); |
| 308 |
} else { |
| 309 |
$notificationContent = \sprintf( |
| 310 |
/* translators: %1$s is the commenter name & %2$s is the feed title */ |
| 311 |
__('%1$s commented on your post: %2$s', 'fluent-community'), |
| 312 |
$commenter, |
| 313 |
'<span class="fcom_nft">' . $feedTitle . '</span>' |
| 314 |
); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
$route = $feed->getJsRoute(); |
| 319 |
|
| 320 |
if ($exist) { |
| 321 |
$exist->content = $notificationContent; |
| 322 |
$exist->updated_at = current_time('mysql'); |
| 323 |
$exist->src_user_id = $comment->user->ID; |
| 324 |
$exist->object_id = $comment->id; |
| 325 |
$exist->save(); |
| 326 |
|
| 327 |
NotificationSubscriber::where('object_id', $exist->id) |
| 328 |
->where('user_id', $authorId) |
| 329 |
->update([ |
| 330 |
'is_read' => 0, |
| 331 |
'updated_at' => current_time('mysql') |
| 332 |
]); |
| 333 |
|
| 334 |
do_action('fluent_community/notification/comment/notifed_to_author', [ |
| 335 |
'user_ids' => [$authorId], |
| 336 |
'notification' => $exist, |
| 337 |
'key' => 'notifed_to_author', |
| 338 |
'comment' => $comment, |
| 339 |
'feed' => $feed, |
| 340 |
'created' => false |
| 341 |
]); |
| 342 |
|
| 343 |
return; |
| 344 |
} |
| 345 |
|
| 346 |
$notification = [ |
| 347 |
'feed_id' => $feed->id, |
| 348 |
'object_id' => $comment->id, |
| 349 |
'src_user_id' => $comment->user_id, |
| 350 |
'src_object_type' => 'comment', |
| 351 |
'action' => 'comment_added', |
| 352 |
'content' => $notificationContent, |
| 353 |
'route' => $route, |
| 354 |
]; |
| 355 |
|
| 356 |
$notification = Notification::create($notification); |
| 357 |
|
| 358 |
$notification->subscribe([$authorId]); |
| 359 |
|
| 360 |
do_action('fluent_community/notification/comment/notifed_to_author', [ |
| 361 |
'user_ids' => [$authorId], |
| 362 |
'notification' => $notification, |
| 363 |
'comment' => $comment, |
| 364 |
'key' => 'notifed_to_author', |
| 365 |
'feed' => $feed, |
| 366 |
'created' => true |
| 367 |
]); |
| 368 |
} |
| 369 |
|
| 370 |
protected function commentNotificationToFeedCommenters($comment, $feed) |
| 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 |
|
| 379 |
if ($comment->parent_id) { |
| 380 |
return $this->notifyForChildCommentReply($comment, $feed, $mentionedUserIds); |
| 381 |
} |
| 382 |
|
| 383 |
$userIds = Comment::whereNotIn('user_id', [$feed->user_id, $comment->user_id]) |
| 384 |
->where('post_id', $feed->id) |
| 385 |
->whereNull('parent_id') |
| 386 |
->distinct() |
| 387 |
->pluck('user_id') |
| 388 |
->toArray(); |
| 389 |
|
| 390 |
if (!$userIds && !$mentionedUserIds) { |
| 391 |
return; |
| 392 |
} |
| 393 |
|
| 394 |
$commenter = '<b class="fcom_nudn">' . esc_html($comment->user->getPublicDisplayName()) . '</b>'; |
| 395 |
$feedTitle = '<span class="fcom_nft">' . $feed->getHumanExcerpt(60) . '</span>'; |
| 396 |
|
| 397 |
$route = $feed->getJsRoute(); |
| 398 |
$space = $feed->space; |
| 399 |
|
| 400 |
if ($feed->comments_count > 1) { |
| 401 |
$totalUsers = Comment::where('post_id', $feed->id) |
| 402 |
->where('user_id', '!=', $comment->user_id) |
| 403 |
->distinct() |
| 404 |
->count('user_id'); |
| 405 |
|
| 406 |
if ($feed->space_id) { |
| 407 |
if ($totalUsers > 1) { |
| 408 |
$notificationContent = \sprintf( |
| 409 |
/* translators: %1$s is the user name, %2$s is the people count, %3$s is the feed title and %4$s space title*/ |
| 410 |
__('%1$s and %2$s other people also commented on %3$s in %4$s', 'fluent-community'), |
| 411 |
$commenter, |
| 412 |
'<b class="fcom_nrc">' . ($totalUsers - 1) . '</b>', |
| 413 |
$feedTitle, |
| 414 |
'<b class="fcom_nst">' . esc_html($feed->space->title) . '</b>' |
| 415 |
); |
| 416 |
} else { |
| 417 |
$notificationContent = \sprintf( |
| 418 |
/* translators: %1$s is the user name, %2$s is the feed title and %3$s space title*/ |
| 419 |
__('%1$s also commented on %2$s in %3$s', 'fluent-community'), |
| 420 |
$commenter, |
| 421 |
$feedTitle, |
| 422 |
'<b class="fcom_nst">' . esc_html($feed->space->title) . '</b>' |
| 423 |
); |
| 424 |
} |
| 425 |
} else { |
| 426 |
if ($totalUsers > 1) { |
| 427 |
$notificationContent = \sprintf( |
| 428 |
/* translators: %1$s is the user name, %2$s is the like count & %3$s is the feed title */ |
| 429 |
__('%1$s and %2$s other people also commented on %3$s', 'fluent-community'), |
| 430 |
$commenter, |
| 431 |
'<b class="fcom_nrc">' . ($totalUsers - 1) . '</b>', |
| 432 |
$feedTitle, |
| 433 |
); |
| 434 |
} else { |
| 435 |
$notificationContent = \sprintf( |
| 436 |
/* translators: %1$s is the user name & %2$s is the feed title */ |
| 437 |
__('%1$s also commented on %2$s', 'fluent-community'), |
| 438 |
$commenter, |
| 439 |
$feedTitle, |
| 440 |
); |
| 441 |
} |
| 442 |
} |
| 443 |
} else { |
| 444 |
if ($feed->space_id) { |
| 445 |
$notificationContent = \sprintf( |
| 446 |
/* translators: %1$s is the commenter name, %2$s is the feed title & %3$s is the space title */ |
| 447 |
__('%1$s also commented on %2$s in %3$s', 'fluent-community'), |
| 448 |
$commenter, |
| 449 |
$feedTitle, |
| 450 |
'<b class="fcom_nst">' . esc_html($space->title) . '</b>' |
| 451 |
); |
| 452 |
} else { |
| 453 |
$notificationContent = \sprintf( |
| 454 |
/* translators: %1$s is the commenter name & %2$s is the feed title */ |
| 455 |
__('%1$s also commented on %2$s', 'fluent-community'), |
| 456 |
$commenter, |
| 457 |
$feedTitle, |
| 458 |
); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
if ($mentionedUserIds) { |
| 463 |
$userIds = array_values(array_diff($userIds, $mentionedUserIds)); |
| 464 |
} |
| 465 |
|
| 466 |
if (!$userIds) { |
| 467 |
return; |
| 468 |
} |
| 469 |
|
| 470 |
$existIds = Notification::where('feed_id', $feed->id) |
| 471 |
->where('action', 'comment_added') |
| 472 |
->whereDoesntHave('subscribers', function ($query) use ($feed, $comment) { |
| 473 |
$query->whereIn('user_id', [$feed->user_id, $comment->user_id]); |
| 474 |
}) |
| 475 |
->pluck('id') |
| 476 |
->toArray(); |
| 477 |
|
| 478 |
$existingSubscriberUserIds = []; |
| 479 |
if (!empty($existIds)) { |
| 480 |
Notification::whereIn('id', $existIds)->update([ |
| 481 |
'content' => $notificationContent, |
| 482 |
'updated_at' => current_time('mysql'), |
| 483 |
'src_user_id' => $comment->user_id, |
| 484 |
'object_id' => $comment->id |
| 485 |
]); |
| 486 |
|
| 487 |
$existingSubscriberUserIds = NotificationSubscriber::whereIn('object_id', $existIds) |
| 488 |
->pluck('user_id') |
| 489 |
->unique() |
| 490 |
->toArray(); |
| 491 |
|
| 492 |
NotificationSubscriber::whereIn('object_id', $existIds) |
| 493 |
->whereIn('user_id', $existingSubscriberUserIds) |
| 494 |
->update([ |
| 495 |
'is_read' => 0, |
| 496 |
'updated_at' => current_time('mysql') |
| 497 |
]); |
| 498 |
} |
| 499 |
|
| 500 |
if (!empty($existingSubscriberUserIds)) { |
| 501 |
$userIds = array_values(array_diff($userIds, $existingSubscriberUserIds)); |
| 502 |
} |
| 503 |
|
| 504 |
$notification = [ |
| 505 |
'feed_id' => $feed->id, |
| 506 |
'object_id' => $comment->id, |
| 507 |
'src_user_id' => $comment->user_id, |
| 508 |
'src_object_type' => 'comment', |
| 509 |
'action' => 'comment_added', |
| 510 |
'content' => $notificationContent, |
| 511 |
'route' => $route, |
| 512 |
]; |
| 513 |
|
| 514 |
foreach ($userIds as $userId) { |
| 515 |
$createdNotification = Notification::create($notification); |
| 516 |
$createdNotification->subscribe([$userId]); |
| 517 |
} |
| 518 |
|
| 519 |
$sendingUserIds = array_merge($userIds, $existingSubscriberUserIds); |
| 520 |
|
| 521 |
do_action('fluent_community/notification/comment/notifed_to_other_users', [ |
| 522 |
'user_ids' => $sendingUserIds, |
| 523 |
'key' => 'notifed_to_other_users', |
| 524 |
'notification' => $notification, |
| 525 |
'comment' => $comment, |
| 526 |
'feed' => $feed |
| 527 |
]); |
| 528 |
} |
| 529 |
|
| 530 |
protected function notifyMentionedUsers($comment, $feed, $mentionedUserIds) |
| 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 |
{ |
| 569 |
// This is a parent comment, so we need to notify the parent comment author & all child comment authors |
| 570 |
$childCommentUserIds = Comment::where(function ($q) use ($comment) { |
| 571 |
$q->where('parent_id', $comment->parent_id) |
| 572 |
->orWhere('id', $comment->parent_id); |
| 573 |
}) |
| 574 |
->whereNotIn('user_id', [$comment->user_id, $feed->user_id]) |
| 575 |
->select(['user_id']) |
| 576 |
->distinct('user_id') |
| 577 |
->get() |
| 578 |
->pluck('user_id') |
| 579 |
->toArray(); |
| 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 |
|
| 586 |
if (!$childCommentUserIds) { |
| 587 |
return false; |
| 588 |
} |
| 589 |
|
| 590 |
$commenter = '<b class="fcom_nudn">' . esc_html($comment->user->getPublicDisplayName()) . '</b>'; |
| 591 |
|
| 592 |
$existingNotification = Notification::where('object_id', $comment->parent_id) |
| 593 |
->where('action', 'child_comment_added') |
| 594 |
->whereHas('subscribers', function ($q) use ($childCommentUserIds) { |
| 595 |
$q->whereIn('user_id', $childCommentUserIds); |
| 596 |
}) |
| 597 |
->first(); |
| 598 |
|
| 599 |
if ($existingNotification) { |
| 600 |
$newContent = \sprintf( |
| 601 |
/* translators: %1$s is the commenter name, %2$s is the comment user count & %3$s feed excerpt */ |
| 602 |
__('%1$s and %2$s other people replied to your comment at %3$s', 'fluent-community'), |
| 603 |
$commenter, |
| 604 |
'<b class="fcom_nrc">' . count($childCommentUserIds) . '</b>', |
| 605 |
'<b class="fcom_nft">' . $feed->getHumanExcerpt(60) . '</b>' |
| 606 |
); |
| 607 |
|
| 608 |
$route = $existingNotification->route; |
| 609 |
$route['query'] = [ |
| 610 |
'comment_id' => $comment->id |
| 611 |
]; |
| 612 |
|
| 613 |
$existingNotification->content = $newContent; |
| 614 |
$existingNotification->updated_at = current_time('mysql'); |
| 615 |
$existingNotification->src_user_id = $comment->user_id; |
| 616 |
$existingNotification->route = $route; |
| 617 |
$existingNotification->save(); |
| 618 |
|
| 619 |
NotificationSubscriber::where('object_id', $existingNotification->id) |
| 620 |
->whereIn('user_id', $childCommentUserIds) |
| 621 |
->update([ |
| 622 |
'is_read' => 0, |
| 623 |
'updated_at' => current_time('mysql') |
| 624 |
]); |
| 625 |
|
| 626 |
do_action('fluent_community/notification/comment/notifed_to_thread_commetenter', [ |
| 627 |
'user_ids' => $childCommentUserIds, |
| 628 |
'notification' => $existingNotification, |
| 629 |
'key' => 'notifed_to_thread_commetenter', |
| 630 |
'comment' => $comment, |
| 631 |
'feed' => $feed |
| 632 |
]); |
| 633 |
|
| 634 |
return $existingNotification; |
| 635 |
} |
| 636 |
|
| 637 |
$notificationContent = \sprintf( |
| 638 |
/* translators: %1$s is the commenter name & %2$s is the feed excerpt */ |
| 639 |
__('%1$s replied to your comment at %2$s', 'fluent-community'), |
| 640 |
$commenter, |
| 641 |
'<b class="fcom_nft">' . $feed->getHumanExcerpt(60) . '</b>' |
| 642 |
); |
| 643 |
|
| 644 |
$route = $feed->getJsRoute(); |
| 645 |
|
| 646 |
$route['query'] = [ |
| 647 |
'comment_id' => $comment->id |
| 648 |
]; |
| 649 |
|
| 650 |
$notification = [ |
| 651 |
'feed_id' => $feed->id, |
| 652 |
'object_id' => $comment->parent_id, |
| 653 |
'src_user_id' => $comment->user_id, |
| 654 |
'src_object_type' => 'comment', |
| 655 |
'action' => 'child_comment_added', |
| 656 |
'content' => $notificationContent, |
| 657 |
'route' => $route, |
| 658 |
]; |
| 659 |
|
| 660 |
$notification = Notification::create($notification); |
| 661 |
$notification->subscribe($childCommentUserIds); |
| 662 |
|
| 663 |
do_action('fluent_community/notification/comment/notifed_to_thread_commetenter', [ |
| 664 |
'user_ids' => $childCommentUserIds, |
| 665 |
'notification' => $notification, |
| 666 |
'key' => 'notifed_to_thread_commetenter', |
| 667 |
'comment' => $comment, |
| 668 |
'feed' => $feed |
| 669 |
]); |
| 670 |
|
| 671 |
return $notification; |
| 672 |
} |
| 673 |
|
| 674 |
public function maybeHandleMentionedUserIds($feed) |
| 675 |
{ |
| 676 |
$mentionedUserIds = Arr::get($feed->meta, 'mentioned_user_ids', []); |
| 677 |
if (!$mentionedUserIds) { |
| 678 |
return; |
| 679 |
} |
| 680 |
|
| 681 |
do_action('fluent_community/feed_mentioned_user_ids', $feed, $mentionedUserIds); |
| 682 |
|
| 683 |
$feedTitle = $feed->getHumanExcerpt(60); |
| 684 |
$user = $feed->user; |
| 685 |
|
| 686 |
$notificationContent = \sprintf( |
| 687 |
/* translators: %1$s is the user name, %2$s is the feed title */ |
| 688 |
__('%1$s mentioned you in a post: %2$s', 'fluent-community'), |
| 689 |
'<b class="fcom_nudn">' . esc_html($user->getPublicDisplayName()) . '</b>', |
| 690 |
'<b class="fcom_nft">' . $feedTitle . '</b>' |
| 691 |
); |
| 692 |
|
| 693 |
$route = $feed->getJsRoute(); |
| 694 |
|
| 695 |
$notification = [ |
| 696 |
'feed_id' => $feed->id, |
| 697 |
'src_user_id' => $user->ID, |
| 698 |
'src_object_type' => 'feed', |
| 699 |
'action' => 'feed/mentioned', |
| 700 |
'content' => $notificationContent, |
| 701 |
'route' => $route, |
| 702 |
]; |
| 703 |
|
| 704 |
$notification = Notification::create($notification); |
| 705 |
|
| 706 |
$notification->subscribe($mentionedUserIds); |
| 707 |
} |
| 708 |
|
| 709 |
private function maybeHasEveryoneTag(Feed $feed) |
| 710 |
{ |
| 711 |
if (!$feed->space_id) { |
| 712 |
return; |
| 713 |
} |
| 714 |
|
| 715 |
if (!$feed->isEnabledForEveryoneTag()) { |
| 716 |
return; |
| 717 |
} |
| 718 |
|
| 719 |
// we have everyone |
| 720 |
// check if current user is a moderator or admin |
| 721 |
$user = $feed->user; |
| 722 |
$spaceRole = $user->getSpaceRole($feed->space); |
| 723 |
if (!in_array($spaceRole, ['admin', 'moderator'])) { |
| 724 |
return; |
| 725 |
} |
| 726 |
|
| 727 |
do_action('fluent_community/feed/scheduling_everyone_tag', $feed); |
| 728 |
// Let's schedule an email hook to send email to everyone for this post |
| 729 |
// We are scheduling this after 5 minutes of the post publish for performance |
| 730 |
as_schedule_single_action(time() + 300, 'fluent_community/email_notify_users_everyone_tag', [ |
| 731 |
$feed->id, |
| 732 |
0 |
| 733 |
], 'fluent-community'); |
| 734 |
} |
| 735 |
|
| 736 |
private function willCreateFeedCreatedNotification($feed) |
| 737 |
{ |
| 738 |
// validate if the user is a moderator |
| 739 |
if (!$feed->user) { |
| 740 |
return; |
| 741 |
} |
| 742 |
|
| 743 |
if ($feed->user->isCommunityModerator()) { |
| 744 |
return true; |
| 745 |
} |
| 746 |
|
| 747 |
if ($feed->space) { |
| 748 |
$role = $feed->user->getSpaceRole($feed->space); |
| 749 |
return in_array($role, ['admin', 'moderator']); |
| 750 |
} |
| 751 |
|
| 752 |
return false; |
| 753 |
} |
| 754 |
} |
| 755 |
|