2023-07-19 06:03:05 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Activity\Notifications\Handlers;
|
|
|
|
|
2023-08-15 09:39:39 -04:00
|
|
|
use BookStack\Activity\Models\Activity;
|
2023-08-05 09:19:23 -04:00
|
|
|
use BookStack\Activity\Models\Comment;
|
2023-07-19 06:03:05 -04:00
|
|
|
use BookStack\Activity\Models\Loggable;
|
2023-08-05 09:19:23 -04:00
|
|
|
use BookStack\Activity\Notifications\Messages\CommentCreationNotification;
|
|
|
|
use BookStack\Activity\Tools\EntityWatchers;
|
|
|
|
use BookStack\Activity\WatchLevels;
|
2023-08-16 11:02:00 -04:00
|
|
|
use BookStack\Entities\Models\Page;
|
2023-08-05 09:19:23 -04:00
|
|
|
use BookStack\Settings\UserNotificationPreferences;
|
2023-08-04 07:27:29 -04:00
|
|
|
use BookStack\Users\Models\User;
|
2023-07-19 06:03:05 -04:00
|
|
|
|
2023-08-05 09:19:23 -04:00
|
|
|
class CommentCreationNotificationHandler extends BaseNotificationHandler
|
2023-07-19 06:03:05 -04:00
|
|
|
{
|
2023-08-15 09:39:39 -04:00
|
|
|
public function handle(Activity $activity, Loggable|string $detail, User $user): void
|
2023-07-19 06:03:05 -04:00
|
|
|
{
|
2023-08-05 09:19:23 -04:00
|
|
|
if (!($detail instanceof Comment)) {
|
|
|
|
throw new \InvalidArgumentException("Detail for comment creation notifications must be a comment");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main watchers
|
2023-08-16 11:02:00 -04:00
|
|
|
/** @var Page $page */
|
2023-08-05 09:19:23 -04:00
|
|
|
$page = $detail->entity;
|
|
|
|
$watchers = new EntityWatchers($page, WatchLevels::COMMENTS);
|
|
|
|
$watcherIds = $watchers->getWatcherUserIds();
|
|
|
|
|
|
|
|
// Page owner if user preferences allow
|
2023-08-16 11:02:00 -04:00
|
|
|
if (!$watchers->isUserIgnoring($page->owned_by) && $page->ownedBy) {
|
|
|
|
$userNotificationPrefs = new UserNotificationPreferences($page->ownedBy);
|
2023-08-05 09:19:23 -04:00
|
|
|
if ($userNotificationPrefs->notifyOnOwnPageComments()) {
|
2023-08-16 11:02:00 -04:00
|
|
|
$watcherIds[] = $page->owned_by;
|
2023-08-05 09:19:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-15 09:39:39 -04:00
|
|
|
$this->sendNotificationToUserIds(CommentCreationNotification::class, $watcherIds, $user, $detail, $page);
|
2023-07-19 06:03:05 -04:00
|
|
|
}
|
|
|
|
}
|