Skip to content

Commit 8b741ba

Browse files
committed
Allow users to receive notifications for replies
1 parent 0109863 commit 8b741ba

File tree

13 files changed

+244
-0
lines changed

13 files changed

+244
-0
lines changed

app/Events/ReplyWasCreated.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\Reply;
6+
use Illuminate\Queue\SerializesModels;
7+
8+
class ReplyWasCreated
9+
{
10+
use SerializesModels;
11+
12+
/**
13+
* @var \App\Models\Reply
14+
*/
15+
public $reply;
16+
17+
public function __construct(Reply $reply)
18+
{
19+
$this->reply = $reply;
20+
}
21+
}

app/Helpers/ReceivesReplies.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Helpers;
44

55
use App\Models\Reply;
6+
use App\Models\Subscription;
67
use Illuminate\Database\Eloquent\Relations\MorphMany;
78

89
trait ReceivesReplies
@@ -32,4 +33,17 @@ public function repliesRelation(): MorphMany
3233
{
3334
return $this->morphMany(Reply::class, 'replyable');
3435
}
36+
37+
/**
38+
* @return \App\Models\Subscription[]
39+
*/
40+
public function subscriptions()
41+
{
42+
return $this->subscriptionsRelation;
43+
}
44+
45+
public function subscriptionsRelation(): MorphMany
46+
{
47+
return $this->morphMany(Subscription::class, 'subscriptionable');
48+
}
3549
}

app/Jobs/CreateReply.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\User;
66
use App\Models\Reply;
77
use App\Models\ReplyAble;
8+
use App\Events\ReplyWasCreated;
89
use App\Http\Requests\CreateReplyRequest;
910

1011
class CreateReply
@@ -49,6 +50,8 @@ public function handle(): Reply
4950
$reply->to($this->replyAble);
5051
$reply->save();
5152

53+
event(new ReplyWasCreated($reply));
54+
5255
return $reply;
5356
}
5457
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\ReplyWasCreated;
6+
use App\Notifications\NewReply;
7+
8+
class SendNewReplyNotification
9+
{
10+
public function handle(ReplyWasCreated $event)
11+
{
12+
foreach ($event->reply->replyAble()->subscriptions() as $subscription) {
13+
$subscription->user()->notify(new NewReply());
14+
}
15+
}
16+
}

app/Models/ReplyAble.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@ public function latestReplies(int $amount = 5);
2222
public function deleteReplies();
2323

2424
public function repliesRelation(): MorphMany;
25+
26+
/**
27+
* @return \App\Models\Subscription[]
28+
*/
29+
public function subscriptions();
30+
31+
public function subscriptionsRelation(): MorphMany;
2532
}

app/Models/Subscription.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use App\User;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
9+
class Subscription extends Model
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
protected $table = 'subscriptions';
15+
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
protected $fillable = [
20+
'subscriptionable_id',
21+
'subscriptionable_type',
22+
];
23+
24+
public function user(): User
25+
{
26+
return $this->userRelation;
27+
}
28+
29+
public function userRelation(): BelongsTo
30+
{
31+
return $this->belongsTo(User::class, 'user_id');
32+
}
33+
}

app/Notifications/NewReply.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace App\Notifications;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Notifications\Notification;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Notifications\Messages\MailMessage;
9+
10+
class NewReply extends Notification implements ShouldQueue
11+
{
12+
use Queueable;
13+
14+
public function __construct()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Get the notification's delivery channels.
21+
*
22+
* @param mixed $notifiable
23+
* @return array
24+
*/
25+
public function via($notifiable)
26+
{
27+
return ['mail'];
28+
}
29+
30+
/**
31+
* Get the mail representation of the notification.
32+
*
33+
* @param mixed $notifiable
34+
* @return \Illuminate\Notifications\Messages\MailMessage
35+
*/
36+
public function toMail($notifiable)
37+
{
38+
return (new MailMessage)
39+
->line('A new reply was posted to this thread.');
40+
}
41+
42+
/**
43+
* Get the array representation of the notification.
44+
*
45+
* @param mixed $notifiable
46+
* @return array
47+
*/
48+
public function toArray($notifiable)
49+
{
50+
return [
51+
//
52+
];
53+
}
54+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use App\Events\ReplyWasCreated;
6+
use App\Listeners\SendNewReplyNotification;
7+
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
8+
9+
class EventServiceProvider extends ServiceProvider
10+
{
11+
/**
12+
* The event listener mappings for the application.
13+
*
14+
* @var array
15+
*/
16+
protected $listen = [
17+
ReplyWasCreated::class => [
18+
SendNewReplyNotification::class,
19+
],
20+
];
21+
}

config/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
*/
174174
App\Providers\AppServiceProvider::class,
175175
App\Providers\AuthServiceProvider::class,
176+
App\Providers\EventServiceProvider::class,
176177
App\Providers\RouteServiceProvider::class,
177178
App\Markdown\MarkdownServiceProvider::class,
178179
App\Spam\SpamServiceProvider::class,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use App\User;
4+
use App\Models\Thread;
5+
use App\Models\Subscription;
6+
7+
$factory->define(Subscription::class, function (Faker\Generator $faker) {
8+
return [
9+
'user_id' => factory(User::class)->create()->id(),
10+
'subscriptionable_id' => factory(Thread::class)->create()->id(),
11+
'subscriptionable_type' => Thread::TABLE,
12+
];
13+
});

0 commit comments

Comments
 (0)