Skip to content

Commit 445da74

Browse files
committed
improvements for unsubscribe and more
1 parent 9ee43f5 commit 445da74

22 files changed

+122
-73
lines changed

CHANGELOG.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
# Changelog
22

3+
## v0.7 - 2025-07-31
4+
5+
### Added
6+
7+
- Added a conditional in the listener to do a check for the unsubscribed_at date
8+
- Update README.md file and removed section about publishing resources
9+
- Added information in the README.md file about the `unsubscribed` confirmation page
10+
- Changed `redirect()->back()->with('success||error', 'Message') to an actual unsubscribed page to provide visualisation after unsubscribing
11+
- Moved BaseNotification out of a "Notifications" directory further up to root to have a cleaner name space
12+
- Renamed NotificationController to UnsubscribeController and made it an invokable controller
13+
- Removed queueing on the abstract class BaseNotifications
14+
315
## v0.6 - 2025-07-26
416

517
- Remove ray from composer.json
6-
- Added Laravel 11 compatibility and updated README
18+
- Added Laravel 11 compatibility and updated `README.md` file
719

820
## v0.4 - 2025-06-09
921

README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ Categorization of emails and model specific subscriptions are possible.
1919
composer require eugenefvdm/notification-subscriptions
2020
```
2121

22-
After installing the package, publish and run the migrations:
22+
After installing the package, run the migrations:
2323

2424
```bash
25-
php artisan vendor:publish --tag="notification-subscriptions-migrations"
2625
php artisan migrate
2726
```
2827

2928
This will create the `notification_templates` and `notification_subscriptions` tables.
3029

31-
If you want to customize the unsubscribe link, publish the view component:
30+
If you want to customize the unsubscribe link or `unsubscribed` confirmation blade, publish the views:
3231

3332
```bash
3433
php artisan vendor:publish --tag="notification-subscriptions-views"
@@ -160,7 +159,7 @@ class ProductPriceNotification extends BaseNotification
160159

161160
Any new notification will be automatically subscribed when used the first time.
162161

163-
### Unsubscribe Links in Blades
162+
### Adding the Unsubscribe Link in Blades
164163

165164
For unsubscribe links, modify the `toMail` method in the notification class:
166165

@@ -183,7 +182,21 @@ Then add this to your blade:
183182
<x-notification-subscriptions::unsubscribe :subscription="$subscription" />
184183
```
185184

186-
The default controller action for unsubscribe is to redirect back with a session variable value of `error` or `status`.
185+
The invokable controller for unsubscribe will direct the user to a generic `unsubcribed.blade.php` file that may be customized. The variables returned to this blade are:
186+
187+
```php
188+
$result = [
189+
'success' => 'true|false',
190+
'message' => '$message',
191+
'template' => NotificationTemplate instance
192+
]
193+
```
194+
Possible `$message` values are:
195+
196+
- Subscription not found
197+
- The $template->name_with_spaces notification cannot be unsubscribed
198+
- You are already unsubscribed from the '$template->name_with_spaces' notification
199+
- Successfully unsubscribed from the '$template->name_with_spaces' notification
187200

188201
## Testing
189202

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@props(['subscription'])
22

3-
<div style="text-align: center; margin: 1em 0;">
3+
<div style="text-align: center; margin: 1em 0; font-size: 0.9em;">
44
<a href="{{ route('notifications.unsubscribe', ['uuid' => $subscription->uuid]) }}">Unsubscribe</a>
55
</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
<title>Unsubscribe</title>
4+
</head>
5+
<body>
6+
<h1>Unsubscribe</h1>
7+
<p>{{ $result['message'] }}</p>
8+
</body>
9+
</html>

routes/web.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

3-
use Eugenefvdm\NotificationSubscriptions\Http\Controllers\NotificationController;
3+
use Eugenefvdm\NotificationSubscriptions\Http\Controllers\UnsubscribeController;
44
use Illuminate\Support\Facades\Route;
55

6-
Route::get('/notifications/unsubscribe/{uuid}', [NotificationController::class, 'unsubscribe'])
7-
->name('notifications.unsubscribe');
6+
Route::get('/notifications/unsubscribe/{uuid}', UnsubscribeController::class)
7+
->name('notifications.unsubscribe');

src/Notifications/BaseNotification.php renamed to src/BaseNotification.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
<?php
22

3-
namespace Eugenefvdm\NotificationSubscriptions\Notifications;
3+
namespace Eugenefvdm\NotificationSubscriptions;
44

55
use Eugenefvdm\NotificationSubscriptions\Models\NotificationSubscription;
66
use Eugenefvdm\NotificationSubscriptions\Models\NotificationTemplate;
77
use App\Models\User;
88
use Eugenefvdm\NotificationSubscriptions\Enums\RepeatFrequency;
9-
use Illuminate\Bus\Queueable;
10-
use Illuminate\Contracts\Queue\ShouldQueue;
119
use Illuminate\Database\Eloquent\Model;
1210
use Illuminate\Notifications\Notification;
1311
use Illuminate\Support\Carbon;
1412

15-
abstract class BaseNotification extends Notification implements ShouldQueue
16-
{
17-
use Queueable;
18-
13+
abstract class BaseNotification extends Notification
14+
{
1915
/**
2016
* customModel may be overridden by child classes and used for model specific notifications.
2117
*

src/Http/Controllers/NotificationController.php

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Eugenefvdm\NotificationSubscriptions\Http\Controllers;
4+
5+
use Eugenefvdm\NotificationSubscriptions\Models\NotificationSubscription;
6+
use Illuminate\Routing\Controller;
7+
8+
class UnsubscribeController extends Controller
9+
{
10+
/**
11+
* Unsubscribe a user from a notification template.
12+
*/
13+
public function __invoke(string $uuid)
14+
{
15+
$subscription = NotificationSubscription::where('uuid', $uuid)->first();
16+
17+
if (!$subscription) {
18+
$result = [
19+
'success' => false,
20+
'message' => "Subscription not found",
21+
];
22+
} else if (!$subscription->canBeUnsubscribed()) {
23+
$result = [
24+
'success' => false,
25+
'message' => "'{$subscription->template->name_with_spaces}' cannot be unsubscribed",
26+
];
27+
} else if ($subscription->unsubscribed_at) {
28+
$result = [
29+
'success' => false,
30+
'message' => "You are already unsubscribed from '{$subscription->template->name_with_spaces}'",
31+
];
32+
} else {
33+
$subscription->unsubscribe();
34+
35+
$result = [
36+
'success' => true,
37+
'message' => "Successfully unsubscribed from '{$subscription->template->name_with_spaces}'",
38+
];
39+
}
40+
41+
$result['template'] = $subscription->template;
42+
43+
return view('notification-subscriptions::unsubscribed', compact('result'));
44+
}
45+
}

src/Listeners/AfterSendingListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Eugenefvdm\NotificationSubscriptions\Listeners;
44

5-
use Eugenefvdm\NotificationSubscriptions\Notifications\BaseNotification;
5+
use Eugenefvdm\NotificationSubscriptions\BaseNotification;
66
use Illuminate\Notifications\Events\NotificationSent;
77

88
class AfterSendingListener

src/Listeners/BeforeSendingListener.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Eugenefvdm\NotificationSubscriptions\Listeners;
44

5-
use Eugenefvdm\NotificationSubscriptions\Notifications\BaseNotification;
5+
use Eugenefvdm\NotificationSubscriptions\BaseNotification;
66
use Eugenefvdm\NotificationSubscriptions\Models\NotificationTemplate;
77
use Illuminate\Notifications\Events\NotificationSending;
88

@@ -54,6 +54,11 @@ public function handle(NotificationSending $event): bool
5454

5555
$subscription = $query->first();
5656

57+
// Check if the user has unsubscribed from this notification
58+
if ($subscription->unsubscribed_at) {
59+
return false;
60+
}
61+
5762
// If the subscription has reached the max count, prevent the notification from being sent
5863
if ($subscription->isMaxCountReached()) {
5964
return false;

0 commit comments

Comments
 (0)