|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BookStack\Activity\Notifications\Handlers; |
| 4 | + |
| 5 | +use BookStack\Activity\ActivityType; |
| 6 | +use BookStack\Activity\Models\Activity; |
| 7 | +use BookStack\Activity\Models\Comment; |
| 8 | +use BookStack\Activity\Models\Loggable; |
| 9 | +use BookStack\Activity\Models\MentionHistory; |
| 10 | +use BookStack\Activity\Notifications\Messages\CommentMentionNotification; |
| 11 | +use BookStack\Activity\Tools\MentionParser; |
| 12 | +use BookStack\Entities\Models\Page; |
| 13 | +use BookStack\Settings\UserNotificationPreferences; |
| 14 | +use BookStack\Users\Models\User; |
| 15 | +use Illuminate\Database\Eloquent\Collection; |
| 16 | +use Illuminate\Support\Carbon; |
| 17 | + |
| 18 | +class CommentMentionNotificationHandler extends BaseNotificationHandler |
| 19 | +{ |
| 20 | + public function handle(Activity $activity, Loggable|string $detail, User $user): void |
| 21 | + { |
| 22 | + if (!($detail instanceof Comment) || !($detail->entity instanceof Page)) { |
| 23 | + throw new \InvalidArgumentException("Detail for comment mention notifications must be a comment on a page"); |
| 24 | + } |
| 25 | + |
| 26 | + /** @var Page $page */ |
| 27 | + $page = $detail->entity; |
| 28 | + |
| 29 | + $parser = new MentionParser(); |
| 30 | + $mentionedUserIds = $parser->parseUserIdsFromHtml($detail->html); |
| 31 | + $realMentionedUsers = User::whereIn('id', $mentionedUserIds)->get(); |
| 32 | + |
| 33 | + $receivingNotifications = $realMentionedUsers->filter(function (User $user) { |
| 34 | + $prefs = new UserNotificationPreferences($user); |
| 35 | + return $prefs->notifyOnCommentMentions(); |
| 36 | + }); |
| 37 | + $receivingNotificationsUserIds = $receivingNotifications->pluck('id')->toArray(); |
| 38 | + |
| 39 | + $userMentionsToLog = $realMentionedUsers; |
| 40 | + |
| 41 | + // When an edit, we check our history to see if we've already notified the user about this comment before |
| 42 | + // so that we can filter them out to avoid double notifications. |
| 43 | + if ($activity->type === ActivityType::COMMENT_UPDATE) { |
| 44 | + $previouslyNotifiedUserIds = $this->getPreviouslyNotifiedUserIds($detail); |
| 45 | + $receivingNotificationsUserIds = array_values(array_diff($receivingNotificationsUserIds, $previouslyNotifiedUserIds)); |
| 46 | + $userMentionsToLog = $userMentionsToLog->filter(function (User $user) use ($previouslyNotifiedUserIds) { |
| 47 | + return !in_array($user->id, $previouslyNotifiedUserIds); |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + $this->logMentions($userMentionsToLog, $detail, $user); |
| 52 | + $this->sendNotificationToUserIds(CommentMentionNotification::class, $receivingNotificationsUserIds, $user, $detail, $page); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param Collection<User> $mentionedUsers |
| 57 | + */ |
| 58 | + protected function logMentions(Collection $mentionedUsers, Comment $comment, User $fromUser): void |
| 59 | + { |
| 60 | + $mentions = []; |
| 61 | + $now = Carbon::now(); |
| 62 | + |
| 63 | + foreach ($mentionedUsers as $mentionedUser) { |
| 64 | + $mentions[] = [ |
| 65 | + 'mentionable_type' => $comment->getMorphClass(), |
| 66 | + 'mentionable_id' => $comment->id, |
| 67 | + 'from_user_id' => $fromUser->id, |
| 68 | + 'to_user_id' => $mentionedUser->id, |
| 69 | + 'created_at' => $now, |
| 70 | + 'updated_at' => $now, |
| 71 | + ]; |
| 72 | + } |
| 73 | + |
| 74 | + MentionHistory::query()->insert($mentions); |
| 75 | + } |
| 76 | + |
| 77 | + protected function getPreviouslyNotifiedUserIds(Comment $comment): array |
| 78 | + { |
| 79 | + return MentionHistory::query() |
| 80 | + ->where('mentionable_id', $comment->id) |
| 81 | + ->where('mentionable_type', $comment->getMorphClass()) |
| 82 | + ->pluck('to_user_id') |
| 83 | + ->toArray(); |
| 84 | + } |
| 85 | +} |
0 commit comments