Skip to content

Commit b77eaab

Browse files
committed
wip
1 parent 4eab294 commit b77eaab

File tree

12 files changed

+140
-15
lines changed

12 files changed

+140
-15
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Jobs\UnsubscribeFromSubscriptionAble;
6+
use App\Models\Subscription;
7+
8+
class SubscriptionController extends Controller
9+
{
10+
public function unsubscribe(Subscription $subscription)
11+
{
12+
$thread = $subscription->subscriptionAble();
13+
14+
$this->dispatch(new UnsubscribeFromSubscriptionAble($subscription->user(), $thread));
15+
16+
$this->success("You're now unsubscribed from this thread.");
17+
18+
return redirect()->route('thread', $thread->slug());
19+
}
20+
}

app/Listeners/SendNewReplyNotification.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function handle(ReplyWasCreated $event): void
1212
{
1313
foreach ($event->reply->replyAble()->subscriptions() as $subscription) {
1414
if ($this->replyAuthorDoesNotMatchSubscriber($event->reply->author(), $subscription)) {
15-
$subscription->user()->notify(new NewReplyNotification($event->reply));
15+
$subscription->user()->notify(new NewReplyNotification($event->reply, $subscription));
1616
}
1717
}
1818
}

app/Mail/NewReplyEmail.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Mail;
44

55
use App\Models\Reply;
6+
use App\Models\Subscription;
67
use Illuminate\Bus\Queueable;
78
use Illuminate\Contracts\Queue\ShouldQueue;
89
use Illuminate\Mail\Mailable;
@@ -17,9 +18,15 @@ class NewReplyEmail extends Mailable implements ShouldQueue
1718
*/
1819
public $reply;
1920

20-
public function __construct(Reply $reply)
21+
/**
22+
* @var \App\Models\Subscription
23+
*/
24+
public $subscription;
25+
26+
public function __construct(Reply $reply, Subscription $subscription)
2127
{
2228
$this->reply = $reply;
29+
$this->subscription = $subscription;
2330
}
2431

2532
public function build()

app/Models/Subscription.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,25 @@
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\Relations\BelongsTo;
88
use Illuminate\Database\Eloquent\Relations\MorphTo;
9+
use Ramsey\Uuid\Uuid;
10+
use Ramsey\Uuid\UuidInterface;
11+
use Spatie\BinaryUuid\HasBinaryUuid;
12+
use Spatie\BinaryUuid\HasUuidPrimaryKey;
913

1014
class Subscription extends Model
1115
{
16+
use HasBinaryUuid, HasUuidPrimaryKey;
17+
1218
/**
1319
* {@inheritdoc}
1420
*/
1521
protected $table = 'subscriptions';
1622

23+
public function uuid(): UuidInterface
24+
{
25+
return Uuid::fromString($this->uuid_text);
26+
}
27+
1728
public function user(): User
1829
{
1930
return $this->userRelation;

app/Notifications/NewReplyNotification.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Mail\NewReplyEmail;
66
use App\Models\Reply;
7+
use App\Models\Subscription;
78
use App\User;
89
use Illuminate\Bus\Queueable;
910
use Illuminate\Notifications\Notification;
@@ -18,9 +19,15 @@ class NewReplyNotification extends Notification implements ShouldQueue
1819
*/
1920
public $reply;
2021

21-
public function __construct(Reply $reply)
22+
/**
23+
* @var \App\Models\Subscription
24+
*/
25+
public $subscription;
26+
27+
public function __construct(Reply $reply, Subscription $subscription)
2228
{
2329
$this->reply = $reply;
30+
$this->subscription = $subscription;
2431
}
2532

2633
public function via(User $user)
@@ -30,7 +37,7 @@ public function via(User $user)
3037

3138
public function toMail(User $user)
3239
{
33-
return (new NewReplyEmail($this->reply))
40+
return (new NewReplyEmail($this->reply, $this->subscription))
3441
->to($user->emailAddress(), $user->name());
3542
}
3643

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"lasserafn/php-initial-avatar-generator": "^2.0",
1616
"league/commonmark": "^0.15.2",
1717
"roave/security-advisories": "dev-master",
18+
"spatie/laravel-binary-uuid": "^1.1",
1819
"spatie/laravel-robots-middleware": "^1.0",
1920
"tijsverkoyen/akismet": "^1.1"
2021
},

composer.lock

Lines changed: 67 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

database/migrations/2017_10_18_193001_create_subscriptions_table.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class CreateSubscriptionsTable extends Migration
99
public function up()
1010
{
1111
Schema::create('subscriptions', function (Blueprint $table) {
12-
$table->increments('id');
12+
$table->uuid('uuid');
13+
$table->primary('uuid');
1314
$table->integer('user_id')->unsigned();
1415
$table->integer('subscriptionable_id');
1516
$table->string('subscriptionable_type')->default('');
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
@component('mail::message')
22

3-
**{{ $reply->replyAble()->author()->name() }}** has replied to this thread.
3+
**{{ $reply->author()->name() }}** has replied to this thread.
44

55
@component('mail::panel')
66
@md($reply->body())
77
@endcomponent
88

99
You are receiving this because you are subscribed to this thread.<br>
1010
[View it on Laravel.io]({{ route('thread', $reply->replyAble()->slug()) }}),
11-
or [unsubscribe]({{ route('threads.unsubscribe', $reply->replyAble()->slug()) }}).
11+
or [unsubscribe]({{ route('subscriptions.unsubscribe', $subscription->uuid()->toString()) }}).
1212

1313
@endcomponent

routes/bindings.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
Route::bind('reply', function ($id) {
77
return App\Models\Reply::findOrFail($id);
88
});
9+
Route::bind('subscription', function ($uuid) {
10+
return App\Models\Subscription::withUuid($uuid)->firstOrFail();
11+
});
912
Route::bind('tag', function ($slug) {
1013
return App\Models\Tag::findBySlug($slug);
1114
});

0 commit comments

Comments
 (0)