Skip to content

Commit ae2930a

Browse files
authored
Add comment replies to news feed (#623)
1 parent 7849958 commit ae2930a

23 files changed

+195
-70
lines changed

assets/styles/comment.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
.comment__reply {
3838
max-width: calc(100vw - var(--review-sidebar-width) - var(--review-sidebar-gap) - 75px);
3939
padding-left: 40px;
40+
41+
&.comment__reply--no-indent {
42+
padding-left: 5px;
43+
}
4044
}
4145

4246
.diff-side-by-side .comment__reply {

src/Controller/App/Review/Comment/AddCommentReactionController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __invoke(Request $request, #[MapEntity] Comment $comment): JsonR
3939
$this->replyRepository->save($reply, true);
4040

4141
$this->bus->dispatch(
42-
new CommentReplyAdded((int)$comment->getReview()->getId(), (int)$reply->getId(), $user->getId(), $message)
42+
new CommentReplyAdded((int)$comment->getReview()->getId(), (int)$reply->getId(), $user->getId(), $message, $comment->getFilePath())
4343
);
4444

4545
return $this->json(['success' => true]);

src/Controller/App/Review/Comment/AddCommentReplyController.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __invoke(Request $request, #[MapEntity] ?Comment $comment): Json
4545
/** @var array{message: string} $data */
4646
$data = $form->getData();
4747

48-
$user = $this->getUser();
48+
$user = $this->getUser();
4949
$reply = new CommentReply();
5050
$reply->setUser($user);
5151
$reply->setComment($comment);
@@ -56,7 +56,13 @@ public function __invoke(Request $request, #[MapEntity] ?Comment $comment): Json
5656
$this->replyRepository->save($reply, true);
5757

5858
$this->bus->dispatch(
59-
new CommentReplyAdded((int)$comment->getReview()->getId(), (int)$reply->getId(), $user->getId(), $data['message'])
59+
new CommentReplyAdded(
60+
(int)$comment->getReview()->getId(),
61+
(int)$reply->getId(),
62+
$user->getId(),
63+
$data['message'],
64+
$comment->getFilePath()
65+
)
6066
);
6167

6268
return $this->json(['success' => true, 'commentId' => $comment->getId()]);

src/Entity/Review/Comment.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ public function getFilePath(): string
8484
return $this->filePath;
8585
}
8686

87-
public function setFilePath(string $filePath): void
87+
public function setFilePath(string $filePath): self
8888
{
8989
$this->filePath = $filePath;
90+
91+
return $this;
9092
}
9193

9294
public function getLineReference(): LineReference
@@ -178,9 +180,11 @@ public function getReview(): CodeReview
178180
return $this->review;
179181
}
180182

181-
public function setReview(CodeReview $review): void
183+
public function setReview(CodeReview $review): self
182184
{
183185
$this->review = $review;
186+
187+
return $this;
184188
}
185189

186190
public function getUser(): User

src/Message/Comment/CommentReplyAdded.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public function __construct(
1414
public readonly int $reviewId,
1515
public readonly int $commentReplyId,
1616
public readonly int $byUserId,
17-
public readonly string $message
17+
public readonly string $message,
18+
public readonly string $file
1819
) {
1920
}
2021

@@ -43,6 +44,6 @@ public function getUserId(): int
4344
*/
4445
public function getPayload(): array
4546
{
46-
return ['commentId' => $this->commentReplyId, 'message' => $this->message];
47+
return ['commentId' => $this->commentReplyId, 'message' => $this->message, 'file' => $this->file];
4748
}
4849
}

src/Service/CodeReview/Activity/CodeReviewActivityFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private function addCustomParams(CodeReviewActivity $activity, array $params): a
109109

110110
// add message
111111
if ($activity->getEventName() === CommentReplyAdded::NAME) {
112-
$params[] = new Variable('message', (string)$activity->getDataValue('message'));
112+
$params[] = new Variable('file', basename((string)$activity->getDataValue('file')));
113113
}
114114

115115
return $params;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace DR\Review\Service\CodeReview\Comment;
5+
6+
use DR\Review\Entity\Review\CodeReviewActivity;
7+
use DR\Review\Entity\Review\Comment;
8+
use DR\Review\Entity\Review\CommentReply;
9+
use DR\Review\Message\Comment\CommentAdded;
10+
use DR\Review\Message\Comment\CommentReplyAdded;
11+
use DR\Review\Repository\Review\CommentReplyRepository;
12+
use DR\Review\Repository\Review\CommentRepository;
13+
14+
class ActivityCommentProvider
15+
{
16+
public function __construct(private readonly CommentRepository $commentRepository, private readonly CommentReplyRepository $replyRepository)
17+
{
18+
}
19+
20+
public function getCommentFor(CodeReviewActivity $activity): Comment|CommentReply|null
21+
{
22+
if ($activity->getEventName() === CommentAdded::NAME) {
23+
return $this->commentRepository->find((int)$activity->getDataValue('commentId'));
24+
}
25+
26+
if ($activity->getEventName() === CommentReplyAdded::NAME) {
27+
return $this->replyRepository->find((int)$activity->getDataValue('commentId'));
28+
}
29+
30+
return null;
31+
}
32+
}

src/ViewModel/App/Review/Timeline/TimelineEntryViewModel.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
use DR\Review\Entity\Review\CodeReviewActivity;
77
use DR\Review\Entity\Review\Comment;
8+
use DR\Review\Entity\Review\CommentReply;
89
use DR\Review\Entity\Revision\Revision;
910

1011
class TimelineEntryViewModel
1112
{
12-
private ?Comment $comment = null;
13-
private ?Revision $revision = null;
13+
private ?Comment $comment = null;
14+
private ?CommentReply $reply = null;
15+
private ?Revision $revision = null;
1416

1517
/**
1618
* @param non-empty-array<CodeReviewActivity> $activities
@@ -24,9 +26,18 @@ public function getComment(): ?Comment
2426
return $this->comment;
2527
}
2628

27-
public function setComment(?Comment $comment): self
29+
public function getReply(): ?CommentReply
2830
{
29-
$this->comment = $comment;
31+
return $this->reply;
32+
}
33+
34+
public function setCommentOrReply(Comment|CommentReply|null $comment): self
35+
{
36+
if ($comment instanceof Comment) {
37+
$this->comment = $comment;
38+
} elseif ($comment instanceof CommentReply) {
39+
$this->reply = $comment;
40+
}
3041

3142
return $this;
3243
}

src/ViewModelProvider/ProjectsViewModelProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Doctrine\DBAL\Exception;
77
use DR\Review\Entity\User\User;
88
use DR\Review\Message\Comment\CommentAdded;
9+
use DR\Review\Message\Comment\CommentReplyAdded;
910
use DR\Review\Message\Comment\CommentResolved;
1011
use DR\Review\Message\Review\ReviewAccepted;
1112
use DR\Review\Message\Review\ReviewOpened;
@@ -22,6 +23,7 @@ class ProjectsViewModelProvider
2223
ReviewOpened::NAME,
2324
CommentAdded::NAME,
2425
CommentResolved::NAME,
26+
CommentReplyAdded::NAME
2527
];
2628

2729
public function __construct(

src/ViewModelProvider/ReviewTimelineViewModelProvider.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
use DR\Review\Message\Comment\CommentReplyAdded;
1212
use DR\Review\Message\Revision\ReviewRevisionAdded;
1313
use DR\Review\Repository\Review\CodeReviewActivityRepository;
14-
use DR\Review\Repository\Review\CommentRepository;
1514
use DR\Review\Service\CodeReview\Activity\CodeReviewActivityFormatter;
1615
use DR\Review\Service\CodeReview\Activity\CodeReviewActivityUrlGenerator;
16+
use DR\Review\Service\CodeReview\Comment\ActivityCommentProvider;
1717
use DR\Review\ViewModel\App\Review\Timeline\TimelineEntryViewModel;
1818
use DR\Review\ViewModel\App\Review\Timeline\TimelineViewModel;
1919

@@ -22,7 +22,7 @@ class ReviewTimelineViewModelProvider
2222
public function __construct(
2323
private readonly CodeReviewActivityRepository $activityRepository,
2424
private readonly CodeReviewActivityFormatter $activityFormatter,
25-
private readonly CommentRepository $commentRepository,
25+
private readonly ActivityCommentProvider $commentProvider,
2626
private readonly CodeReviewActivityUrlGenerator $urlGenerator,
2727
private readonly User $user
2828
) {
@@ -45,7 +45,7 @@ public function getTimelineViewModel(CodeReview $review, array $revisions): Time
4545

4646
$timelineEntries[] = $entry = new TimelineEntryViewModel([$activity], $message, null);
4747
if ($activity->getEventName() === CommentAdded::NAME) {
48-
$entry->setComment($review->getComments()->get((int)$activity->getDataValue('commentId')));
48+
$entry->setCommentOrReply($review->getComments()->get((int)$activity->getDataValue('commentId')));
4949
} elseif ($activity->getEventName() === ReviewRevisionAdded::NAME) {
5050
$entry->setRevision($revisions[(int)$activity->getDataValue('revisionId')] ?? null);
5151
}
@@ -68,14 +68,11 @@ public function getTimelineViewModelForFeed(User $user, array $events, ?Reposito
6868
if ($message === null) {
6969
continue;
7070
}
71-
$comment = null;
72-
if ($activity->getEventName() === CommentAdded::NAME) {
73-
$comment = $this->commentRepository->find((int)$activity->getDataValue('commentId'));
74-
if ($comment === null) {
75-
continue;
76-
}
77-
}
78-
$timelineEntries[] = (new TimelineEntryViewModel([$activity], $message, $this->urlGenerator->generate($activity)))->setComment($comment);
71+
72+
$url = $this->urlGenerator->generate($activity);
73+
$entry = (new TimelineEntryViewModel([$activity], $message, $url));
74+
$entry->setCommentOrReply($this->commentProvider->getCommentFor($activity));
75+
$timelineEntries[] = $entry;
7976
}
8077

8178
return new TimelineViewModel($timelineEntries);

0 commit comments

Comments
 (0)