Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for new web push #205

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ Next publish the migration with:
php artisan vendor:publish --provider="NotificationChannels\WebPush\WebPushServiceProvider" --tag="migrations"
```

If you are using UUID for primary key of users table you can use:

``` bash
php artisan vendor:publish --provider="NotificationChannels\WebPush\WebPushServiceProvider" --tag="uuid-migrations"
```

Run the migrate command to create the necessary table:

``` bash
Expand Down
3 changes: 1 addition & 2 deletions migrations/create_push_subscriptions_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class CreatePushSubscriptionsTable extends Migration
$table->bigIncrements('id');
$table->morphs('subscribable');
$table->string('endpoint', 500)->unique();
$table->string('public_key')->nullable();
$table->string('auth_token')->nullable();
$table->string('keys', 500)->nullable();
$table->string('content_encoding')->nullable();
$table->timestamps();
});
Expand Down
35 changes: 35 additions & 0 deletions migrations/create_push_subscriptions_uuid_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePushSubscriptionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::connection(config('webpush.database_connection'))->create(config('webpush.table_name'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->uuidMorphs('subscribable');
$table->string('endpoint', 500)->unique();
$table->string('keys', 500)->nullable();
$table->string('content_encoding')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::connection(config('webpush.database_connection'))->dropIfExists(config('webpush.table_name'));
}
}
16 changes: 7 additions & 9 deletions src/HasPushSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ public function pushSubscriptions()
* Update (or create) subscription.
*
* @param string $endpoint
* @param string|null $key
* @param string|null $token
* @param string|null $keys
* @param string|null $contentEncoding
* @return \NotificationChannels\WebPush\PushSubscription
*/
public function updatePushSubscription($endpoint, $key = null, $token = null, $contentEncoding = null)
public function updatePushSubscription($endpoint, $keys = null, $contentEncoding = null)
{
$subscription = app(config('webpush.model'))->findByEndpoint($endpoint);

if ($subscription && $this->ownsPushSubscription($subscription)) {
$subscription->public_key = $key;
$subscription->auth_token = $token;
$subscription->content_encoding = $contentEncoding;
$subscription->save();
$subscription->update([
'keys' => $keys,
'content_encoding' => $contentEncoding,
]);

return $subscription;
}
Expand All @@ -42,8 +41,7 @@ public function updatePushSubscription($endpoint, $key = null, $token = null, $c

return $this->pushSubscriptions()->create([
'endpoint' => $endpoint,
'public_key' => $key,
'auth_token' => $token,
'keys' => $keys,
'content_encoding' => $contentEncoding,
]);
}
Expand Down
3 changes: 1 addition & 2 deletions src/PushSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class PushSubscription extends Model
*/
protected $fillable = [
'endpoint',
'public_key',
'auth_token',
'keys',
'content_encoding',
];

Expand Down
17 changes: 11 additions & 6 deletions src/WebPushChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@ public function send($notifiable, Notification $notification)

/** @var \NotificationChannels\WebPush\PushSubscription $subscription */
foreach ($subscriptions as $subscription) {
$this->webPush->queueNotification(new Subscription(
$subscription->endpoint,
$subscription->public_key,
$subscription->auth_token,
$subscription->content_encoding
), $payload, $options);
$this->webPush->queueNotification(
Subscription::create(
[
'endpoint' => $subscription->endpoint,
'keys' => json_decode($subscription->keys, true),
'contentEncoding' => $subscription->content_encoding,
]
),
$payload,
$options
);
}

$reports = $this->webPush->flush();
Expand Down
4 changes: 4 additions & 0 deletions src/WebPushServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ protected function definePublishing()
$this->publishes([
__DIR__.'/../migrations/create_push_subscriptions_table.php.stub' => database_path("migrations/{$timestamp}_create_push_subscriptions_table.php"),
], 'migrations');

$this->publishes([
__DIR__.'/../migrations/create_push_subscriptions_uuid_table.php.stub' => database_path("migrations/{$timestamp}_create_push_subscriptions_table.php"),
], 'uuid-migrations');
}
}
}
4 changes: 1 addition & 3 deletions tests/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public function notification_can_be_sent()
->withArgs(function (Subscription $subscription, string $payload, array $options = [], array $auth = []) use ($message) {
$this->assertInstanceOf(Subscription::class, $subscription);
$this->assertEquals('endpoint', $subscription->getEndpoint());
$this->assertEquals('key', $subscription->getPublicKey());
$this->assertEquals('token', $subscription->getAuthToken());
$this->assertEquals('aesgcm', $subscription->getContentEncoding());
$this->assertSame($message->getOptions(), $options);
$this->assertSame(json_encode($message->toArray()), $payload);
Expand All @@ -47,7 +45,7 @@ public function notification_can_be_sent()
yield new MessageSentReport(new Request('POST', 'endpoint'), null, true);
})());

$this->testUser->updatePushSubscription('endpoint', 'key', 'token', 'aesgcm');
$this->testUser->updatePushSubscription('endpoint', 'keys', 'aesgcm');

$channel->send($this->testUser, $notification);

Expand Down
12 changes: 5 additions & 7 deletions tests/HasPushSubscriptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,23 @@ public function model_has_subscriptions()
/** @test */
public function subscription_can_be_created()
{
$this->testUser->updatePushSubscription('foo', 'key', 'token', 'aesgcm');
$this->testUser->updatePushSubscription('foo', 'keys', 'aesgcm');
$subscription = $this->testUser->pushSubscriptions()->first();

$this->assertEquals('foo', $subscription->endpoint);
$this->assertEquals('key', $subscription->public_key);
$this->assertEquals('token', $subscription->auth_token);
$this->assertEquals('keys', $subscription->keys);
$this->assertEquals('aesgcm', $subscription->content_encoding);
}

/** @test */
public function exiting_subscription_can_be_updated_by_endpoint()
{
$this->testUser->updatePushSubscription('foo', 'key', 'token');
$this->testUser->updatePushSubscription('foo', 'major-key', 'another-token');
$this->testUser->updatePushSubscription('foo', 'keys', 'aesgcm');
$this->testUser->updatePushSubscription('foo', 'major-keys', 'aesgcm');
$subscriptions = $this->testUser->pushSubscriptions()->where('endpoint', 'foo')->get();

$this->assertEquals(1, count($subscriptions));
$this->assertEquals('major-key', $subscriptions[0]->public_key);
$this->assertEquals('another-token', $subscriptions[0]->auth_token);
$this->assertEquals('major-keys', $subscriptions[0]->keys);
}

/** @test */
Expand Down
6 changes: 2 additions & 4 deletions tests/PushSubscriptionModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ public function attributes_are_fillable()
{
$subscription = new PushSubscription([
'endpoint' => 'endpoint',
'public_key' => 'key',
'auth_token' => 'token',
'keys' => 'keys',
'content_encoding' => 'aesgcm',
]);

$this->assertEquals('endpoint', $subscription->endpoint);
$this->assertEquals('key', $subscription->public_key);
$this->assertEquals('token', $subscription->auth_token);
$this->assertEquals('keys', $subscription->keys);
$this->assertEquals('aesgcm', $subscription->content_encoding);
}

Expand Down
3 changes: 1 addition & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public function createSubscription($user, $endpoint = 'endpoint')
return $user->pushSubscriptions()->create([
'user_id' => $user->id,
'endpoint' => $endpoint,
'public_key' => 'key',
'auth_token' => 'token',
'keys' => 'keys',
]);
}

Expand Down