1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Jobs\Notifications;
7
8use App\Exceptions\InvalidNotificationException;
9use App\Models\Comment;
10use App\Models\Follow;
11use App\Models\Notification;
12use App\Models\User;
13use App\Models\UserNotificationOption;
14
15class CommentNew extends BroadcastNotificationBase
16{
17 const NOTIFICATION_OPTION_NAME = Notification::COMMENT_NEW;
18
19 protected $comment;
20
21 public static function getMailLink(Notification $notification): string
22 {
23 // TODO: actual item commented on.
24 return route('comments.show', $notification->details['comment_id']);
25 }
26
27 public function __construct(Comment $comment, User $source)
28 {
29 parent::__construct($source);
30
31 $this->comment = $comment;
32
33 if ($this->comment->commentable === null) {
34 throw new InvalidNotificationException("{$this->name}: comment #{$this->comment->getKey()} missing commentable");
35 }
36 }
37
38 public function getDetails(): array
39 {
40 $details = [
41 'comment_id' => $this->comment->getKey(),
42 'title' => $this->comment->commentable->commentableTitle(),
43 'content' => truncate($this->comment->message, static::CONTENT_TRUNCATE),
44 'cover_url' => $this->comment->commentable->notificationCover(),
45 ];
46
47 if ($this->comment->parent !== null) {
48 $details['reply_to'] = [
49 'user_id' => $this->comment->parent->user_id,
50 ];
51 }
52
53 return $details;
54 }
55
56 public function getListeningUserIds(): array
57 {
58 $userIds = Follow::whereNotifiable($this->comment->commentable)
59 ->where(['subtype' => 'comment'])
60 ->pluck('user_id');
61
62 if ($this->comment->parent !== null) {
63 // also notify parent if option is enabled.
64 $user = $this->comment->parent->user;
65 if ($user !== null) {
66 $notificationOption = $user->notificationOptions()->where('name', Notification::COMMENT_NEW)->first();
67
68 if ($notificationOption->details[UserNotificationOption::COMMENT_REPLY] ?? true) {
69 $userIds->push($user->getKey());
70 }
71 }
72 }
73
74 return $userIds->all();
75 }
76
77 public function getNotifiable()
78 {
79 return $this->comment->commentable;
80 }
81}