2023-07-19 18:03:05 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Activity\Notifications\Handlers;
|
|
|
|
|
2023-08-05 21:19:23 +08:00
|
|
|
use BookStack\Activity\Models\Comment;
|
2023-07-19 18:03:05 +08:00
|
|
|
use BookStack\Activity\Models\Loggable;
|
2023-08-05 21:19:23 +08:00
|
|
|
use BookStack\Activity\Notifications\Messages\CommentCreationNotification;
|
|
|
|
use BookStack\Activity\Tools\EntityWatchers;
|
|
|
|
use BookStack\Activity\WatchLevels;
|
|
|
|
use BookStack\Settings\UserNotificationPreferences;
|
2023-08-04 19:27:29 +08:00
|
|
|
use BookStack\Users\Models\User;
|
2023-07-19 18:03:05 +08:00
|
|
|
|
2023-08-05 21:19:23 +08:00
|
|
|
class CommentCreationNotificationHandler extends BaseNotificationHandler
|
2023-07-19 18:03:05 +08:00
|
|
|
{
|
2023-08-04 19:27:29 +08:00
|
|
|
public function handle(string $activityType, Loggable|string $detail, User $user): void
|
2023-07-19 18:03:05 +08:00
|
|
|
{
|
2023-08-05 21:19:23 +08:00
|
|
|
if (!($detail instanceof Comment)) {
|
|
|
|
throw new \InvalidArgumentException("Detail for comment creation notifications must be a comment");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main watchers
|
|
|
|
$page = $detail->entity;
|
|
|
|
$watchers = new EntityWatchers($page, WatchLevels::COMMENTS);
|
|
|
|
$watcherIds = $watchers->getWatcherUserIds();
|
|
|
|
|
|
|
|
// Page owner if user preferences allow
|
|
|
|
if (!$watchers->isUserIgnoring($detail->owned_by) && $detail->ownedBy) {
|
|
|
|
$userNotificationPrefs = new UserNotificationPreferences($detail->ownedBy);
|
|
|
|
if ($userNotificationPrefs->notifyOnOwnPageComments()) {
|
|
|
|
$watcherIds[] = $detail->owned_by;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent comment creator if preferences allow
|
|
|
|
$parentComment = $detail->parent()->first();
|
|
|
|
if ($parentComment && !$watchers->isUserIgnoring($parentComment->created_by) && $parentComment->createdBy) {
|
|
|
|
$parentCommenterNotificationsPrefs = new UserNotificationPreferences($parentComment->createdBy);
|
|
|
|
if ($parentCommenterNotificationsPrefs->notifyOnCommentReplies()) {
|
|
|
|
$watcherIds[] = $parentComment->created_by;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->sendNotificationToUserIds(CommentCreationNotification::class, $watcherIds, $user, $page);
|
2023-07-19 18:03:05 +08:00
|
|
|
}
|
|
|
|
}
|