Skip to content

Commit dce28b4

Browse files
committed
work on channel speific messages
1 parent 3e60b28 commit dce28b4

20 files changed

Lines changed: 186 additions & 129 deletions

‎composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"nesbot/carbon": "~1.20",
2828
"paragonie/random_compat": "~1.4|~2.0",
2929
"psy/psysh": "0.7.*",
30+
"ramsey/uuid": "~3.0",
3031
"swiftmailer/swiftmailer": "~5.1",
3132
"symfony/console": "3.1.*",
3233
"symfony/debug": "3.1.*",

‎src/Illuminate/Auth/Notifications/ResetPassword.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Auth\Notifications;
44

55
use Illuminate\Notifications\Notification;
6+
use Illuminate\Notifications\Messages\MailMessage;
67

78
class ResetPassword extends Notification
89
{
@@ -36,13 +37,14 @@ public function via($notifiable)
3637
}
3738

3839
/**
39-
* Build the notification message.
40+
* Build the mail representation of the notification.
4041
*
41-
* @return \Illuminate\Notifications\Message
42+
* @return \Illuminate\Notifications\Messages\MailMessage
4243
*/
43-
public function message()
44+
public function toMail()
4445
{
45-
return $this->line([
46+
return (new MailMessage)
47+
->line([
4648
'You are receiving this email because we received a password reset request for your account.',
4749
'Click the button below to reset your password:',
4850
])

‎src/Illuminate/Foundation/Console/stubs/notification.stub‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ namespace DummyNamespace;
55
use Illuminate\Bus\Queueable;
66
use Illuminate\Notifications\Notification;
77
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Notifications\Messages\MailMessage;
9+
use Illuminate\Notifications\Messages\DatabaseMessage;
10+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
811

912
class DummyClass extends Notification
1013
{
@@ -32,15 +35,29 @@ class DummyClass extends Notification
3235
}
3336

3437
/**
35-
* Get the notification message.
38+
* Get the mail representation of the notification.
3639
*
3740
* @param mixed $notifiable
38-
* @return \Illuminate\Notifications\Message
41+
* @return \Illuminate\Notifications\Messages\MailMessage
3942
*/
40-
public function message($notifiable)
43+
public function toMail($notifiable)
4144
{
42-
return $this->line('The introduction to the notification.')
45+
return (new MailMessage)
46+
->line('The introduction to the notification.')
4347
->action('Notification Action', 'https://laravel.com')
4448
->line('Thank you for using our application!');
4549
}
50+
51+
/**
52+
* Get the database representation of the notification.
53+
*
54+
* @param mixed $notifiable
55+
* @return \Illuminate\Notifications\Messages\DatabaseMessage
56+
*/
57+
public function toDatabase($notifiable)
58+
{
59+
return new DatabaseMessage([
60+
//
61+
]);
62+
}
4663
}

‎src/Illuminate/Notifications/Channels/BroadcastChannel.php‎

Lines changed: 0 additions & 46 deletions
This file was deleted.

‎src/Illuminate/Notifications/Channels/DatabaseChannel.php‎

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,30 @@
33
namespace Illuminate\Notifications\Channels;
44

55
use Illuminate\Notifications\Notification;
6+
use Illuminate\Contracts\Events\Dispatcher;
7+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
8+
use Illuminate\Notifications\Events\DatabaseNotificationCreated;
69

710
class DatabaseChannel
811
{
12+
/**
13+
* The event dispatcher.
14+
*
15+
* @var \Illuminate\Contracts\Events\Dispatcher
16+
*/
17+
protected $events;
18+
19+
/**
20+
* Create a new database channel.
21+
*
22+
* @param \Illuminate\Contracts\Events\Dispatcher $events
23+
* @return void
24+
*/
25+
public function __construct(Dispatcher $events)
26+
{
27+
$this->events = $events;
28+
}
29+
930
/**
1031
* Send the given notification.
1132
*
@@ -16,7 +37,13 @@ class DatabaseChannel
1637
public function send($notifiables, Notification $notification)
1738
{
1839
foreach ($notifiables as $notifiable) {
19-
$this->createNotification($notifiable, $notification);
40+
$databaseNotification = $this->createNotification($notifiable, $notification);
41+
42+
if ($notification instanceof ShouldBroadcast) {
43+
$this->events->fire(new DatabaseNotificationCreated(
44+
$notifiable, $notification, $databaseNotification
45+
));
46+
}
2047
}
2148
}
2249

@@ -29,15 +56,12 @@ public function send($notifiables, Notification $notification)
2956
*/
3057
protected function createNotification($notifiable, Notification $notification)
3158
{
32-
$message = $notification->message($notifiable);
59+
$message = $notification->toDatabase($notifiable);
3360

3461
return $notifiable->routeNotificationFor('database')->create([
62+
'id' => $message->id,
3563
'type' => get_class($notification),
36-
'level' => $message->level,
37-
'intro' => $message->introLines,
38-
'outro' => $message->outroLines,
39-
'action_text' => $message->actionText,
40-
'action_url' => $message->actionUrl,
64+
'data' => $message->data,
4165
'read' => false,
4266
]);
4367
}

‎src/Illuminate/Notifications/Channels/MailChannel.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function send($notifiables, Notification $notification)
4040
continue;
4141
}
4242

43-
$message = $notification->asMail($notifiable);
43+
$message = $notification->toMail($notifiable);
4444

4545
$this->mailer->send($message->view, $message->toArray(), function ($m) use ($notifiable, $notification, $message) {
4646
$m->to($notifiable->routeNotificationFor('mail'));

‎src/Illuminate/Notifications/Channels/NexmoSmsChannel.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function send($notifiables, Notification $notification)
6565
*/
6666
protected function formatNotification($notifiable, $notification)
6767
{
68-
$message = $notification->message($notifiable);
68+
$message = $notification->toNexmo($notifiable);
6969

7070
$actionText = $message->actionText
7171
? $message->actionText.': ' : '';

‎src/Illuminate/Notifications/Channels/SlackWebhookChannel.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Illuminate\Notifications\Channels;
44

55
use GuzzleHttp\Client as HttpClient;
6-
use Illuminate\Notifications\Message;
76
use Illuminate\Notifications\Notification;
7+
use Illuminate\Notifications\Messages\SlackMessage;
88

99
class SlackWebhookChannel
1010
{
@@ -40,7 +40,7 @@ public function send($notifiables, Notification $notification)
4040
continue;
4141
}
4242

43-
$message = $notification->message($notifiable);
43+
$message = $notification->toSlack($notifiable);
4444

4545
$this->http->post($url, [
4646
'json' => [
@@ -60,10 +60,10 @@ public function send($notifiables, Notification $notification)
6060
/**
6161
* Format the given notification message.
6262
*
63-
* @param \Illuminate\Notifications\Message $message
63+
* @param \Illuminate\Notifications\Messages\SlackMessage $message
6464
* @return string
6565
*/
66-
protected function format(Message $message)
66+
protected function format(SlackMessage $message)
6767
{
6868
$text = trim(implode(PHP_EOL.PHP_EOL, $message->introLines));
6969

‎src/Illuminate/Notifications/DatabaseNotification.php‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
class DatabaseNotification extends Model
88
{
9+
/**
10+
* Indicates if the IDs are auto-incrementing.
11+
*
12+
* @var bool
13+
*/
14+
public $incrementing = false;
15+
916
/**
1017
* The table associated with the model.
1118
*
@@ -26,8 +33,7 @@ class DatabaseNotification extends Model
2633
* @var array
2734
*/
2835
protected $casts = [
29-
'intro' => 'array',
30-
'outro' => 'array',
36+
'data' => 'array',
3137
'read' => 'boolean',
3238
];
3339

‎src/Illuminate/Notifications/Events/DatabaseNotificationCreated.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ public function __construct($notifiable, $notification, $databaseNotification)
5555
*/
5656
public function broadcastOn()
5757
{
58-
return [new PrivateChannel($this->channelName())];
58+
if (! empty($channels = $this->notification->broadcastOn())) {
59+
return $channels;
60+
} else {
61+
return [new PrivateChannel($this->channelName())];
62+
}
5963
}
6064

6165
/**

0 commit comments

Comments
 (0)