Skip to content

Commit 27b0743

Browse files
committed
Updated event name, updated docs
1 parent bb27cc8 commit 27b0743

File tree

5 files changed

+23
-35
lines changed

5 files changed

+23
-35
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ class User extends Model implements Commenter
168168
}
169169
```
170170

171+
### Events
172+
173+
Two events are dispatched when a comment is created or reacted to:
174+
175+
- `Kirschbaum\Commentions\Events\UserWasMentionedEvent`
176+
- `Kirschbaum\Commentions\Events\CommentWasReactedEvent`
177+
171178
### Sending notifications when a user is mentioned
172179

173180
Every time a user is mentioned, the `Kirschbaum\Commentions\Events\UserWasMentionedEvent` is dispatched. You can listen to this event and send notifications to the mentioned user.

src/Actions/ToggleCommentReaction.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Kirschbaum\Commentions\Comment;
77
use Kirschbaum\Commentions\CommentReaction;
88
use Kirschbaum\Commentions\Contracts\Commenter;
9-
use Kirschbaum\Commentions\Events\CommentReactionToggledEvent;
9+
use Kirschbaum\Commentions\Events\CommentWasReactedEvent;
1010

1111
class ToggleCommentReaction
1212
{
@@ -30,27 +30,16 @@ public static function run(Comment $comment, string $reaction, ?Commenter $user
3030

3131
if ($existingReaction) {
3232
$existingReaction->delete();
33-
34-
event(new CommentReactionToggledEvent(
35-
comment: $comment,
36-
reaction: $existingReaction,
37-
user: $user,
38-
reactionType: $reaction,
39-
wasCreated: false
40-
));
4133
} else {
42-
$newReaction = $comment->reactions()->create([
34+
$reaction = $comment->reactions()->create([
4335
'reactor_id' => $user->getKey(),
4436
'reactor_type' => $user->getMorphClass(),
4537
'reaction' => $reaction,
4638
]);
4739

48-
event(new CommentReactionToggledEvent(
40+
event(new CommentWasReactedEvent(
4941
comment: $comment,
50-
reaction: $newReaction,
51-
user: $user,
52-
reactionType: $reaction,
53-
wasCreated: true
42+
reaction: $reaction,
5443
));
5544
}
5645
}

src/CommentReaction.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
namespace Kirschbaum\Commentions;
44

55
use Illuminate\Database\Eloquent\Model;
6-
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6+
use Kirschbaum\Commentions\Contracts\Commenter;
77
use Illuminate\Database\Eloquent\Relations\MorphTo;
8+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
89

10+
/**
11+
* @property-read Comment $comment
12+
* @property-read Commenter $reactor
13+
*/
914
class CommentReaction extends Model
1015
{
1116
protected $fillable = [
@@ -15,11 +20,13 @@ class CommentReaction extends Model
1520
'reaction',
1621
];
1722

23+
/** @return BelongsTo<Comment> */
1824
public function comment(): BelongsTo
1925
{
2026
return $this->belongsTo(Comment::class);
2127
}
2228

29+
/** @return MorphTo<Commenter> */
2330
public function reactor(): MorphTo
2431
{
2532
return $this->morphTo();

src/Events/CommentReactionToggledEvent.php renamed to src/Events/CommentWasReactedEvent.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88
use Kirschbaum\Commentions\CommentReaction;
99
use Kirschbaum\Commentions\Contracts\Commenter;
1010

11-
class CommentReactionToggledEvent
11+
class CommentWasReactedEvent
1212
{
1313
use Dispatchable, SerializesModels;
1414

1515
public function __construct(
1616
public Comment $comment,
17-
public ?CommentReaction $reaction,
18-
public Commenter $user,
19-
public string $reactionType,
20-
public bool $wasCreated
17+
public CommentReaction $reaction,
2118
) {
2219
}
2320
}

tests/Livewire/CommentReactionTest.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Kirschbaum\Commentions\Comment as CommentModel;
66
use Kirschbaum\Commentions\Config;
77
use Kirschbaum\Commentions\Database\Factories\CommentFactory;
8-
use Kirschbaum\Commentions\Events\CommentReactionToggledEvent;
8+
use Kirschbaum\Commentions\Events\CommentWasReactedEvent;
99
use Kirschbaum\Commentions\Livewire\Comment as CommentComponent;
1010
use Kirschbaum\Commentions\Livewire\Reactions;
1111
use Tests\Database\Factories\PostFactory;
@@ -32,11 +32,8 @@
3232
livewire(CommentComponent::class, ['comment' => $comment])
3333
->call('toggleReaction', $reactionEmoji);
3434

35-
Event::assertDispatched(CommentReactionToggledEvent::class, function (CommentReactionToggledEvent $event) use ($comment, $user, $reactionEmoji) {
35+
Event::assertDispatched(CommentWasReactedEvent::class, function (CommentWasReactedEvent $event) use ($comment, $user, $reactionEmoji) {
3636
return $event->comment->is($comment)
37-
&& $event->user->is($user)
38-
&& $event->reactionType === $reactionEmoji
39-
&& $event->wasCreated === true
4037
&& $event->reaction !== null
4138
&& $event->reaction->reaction === $reactionEmoji;
4239
});
@@ -77,15 +74,6 @@
7774
livewire(CommentComponent::class, ['comment' => $comment])
7875
->call('toggleReaction', $reactionEmoji);
7976

80-
Event::assertDispatched(CommentReactionToggledEvent::class, function (CommentReactionToggledEvent $event) use ($comment, $user, $reactionEmoji, $reaction) {
81-
return $event->comment->is($comment)
82-
&& $event->user->is($user)
83-
&& $event->reactionType === $reactionEmoji
84-
&& $event->wasCreated === false
85-
&& $event->reaction !== null // The deleted reaction model is passed
86-
&& $event->reaction->is($reaction);
87-
});
88-
8977
$this->assertDatabaseMissing('comment_reactions', [
9078
'comment_id' => $comment->id,
9179
'reactor_id' => $user->id,

0 commit comments

Comments
 (0)