From 74e06924fea7b1f359b0f90f4bb3e825e7fd73a2 Mon Sep 17 00:00:00 2001 From: Tran Van Hieu Date: Fri, 12 Jul 2024 00:03:12 +0700 Subject: [PATCH 1/4] feat: update migrate table --- README.md | 6 ++++ .../create_push_subscriptions_table.php.stub | 3 +- ...ate_push_subscriptions_uuid_table.php.stub | 35 +++++++++++++++++++ src/WebPushServiceProvider.php | 4 +++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 migrations/create_push_subscriptions_uuid_table.php.stub diff --git a/README.md b/README.md index dc6dd8a..99f95f9 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/migrations/create_push_subscriptions_table.php.stub b/migrations/create_push_subscriptions_table.php.stub index 550a98f..78b8a47 100644 --- a/migrations/create_push_subscriptions_table.php.stub +++ b/migrations/create_push_subscriptions_table.php.stub @@ -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(); }); diff --git a/migrations/create_push_subscriptions_uuid_table.php.stub b/migrations/create_push_subscriptions_uuid_table.php.stub new file mode 100644 index 0000000..cc30ff6 --- /dev/null +++ b/migrations/create_push_subscriptions_uuid_table.php.stub @@ -0,0 +1,35 @@ +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')); + } +} diff --git a/src/WebPushServiceProvider.php b/src/WebPushServiceProvider.php index 008e037..503e609 100644 --- a/src/WebPushServiceProvider.php +++ b/src/WebPushServiceProvider.php @@ -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'); } } } From 0499504eb0bab72ceb14ea2cd8caa1e3e824ecef Mon Sep 17 00:00:00 2001 From: Tran Van Hieu Date: Fri, 12 Jul 2024 00:19:31 +0700 Subject: [PATCH 2/4] feat: update for new keys web push --- src/HasPushSubscriptions.php | 16 +++++++--------- src/PushSubscription.php | 3 +-- src/WebPushChannel.php | 17 +++++++++++------ 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/HasPushSubscriptions.php b/src/HasPushSubscriptions.php index b5dac57..d751382 100644 --- a/src/HasPushSubscriptions.php +++ b/src/HasPushSubscriptions.php @@ -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; } @@ -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, ]); } diff --git a/src/PushSubscription.php b/src/PushSubscription.php index f6eaeab..a772b00 100644 --- a/src/PushSubscription.php +++ b/src/PushSubscription.php @@ -20,8 +20,7 @@ class PushSubscription extends Model */ protected $fillable = [ 'endpoint', - 'public_key', - 'auth_token', + 'keys', 'content_encoding', ]; diff --git a/src/WebPushChannel.php b/src/WebPushChannel.php index c810d00..4e35f08 100644 --- a/src/WebPushChannel.php +++ b/src/WebPushChannel.php @@ -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(); From c133da51e67315f499291140da7b814ba7a9ec08 Mon Sep 17 00:00:00 2001 From: Tran Van Hieu Date: Fri, 12 Jul 2024 00:23:14 +0700 Subject: [PATCH 3/4] feat: update new keys for test --- tests/ChannelTest.php | 4 +--- tests/HasPushSubscriptionsTest.php | 12 +++++------- tests/PushSubscriptionModelTest.php | 6 ++---- tests/TestCase.php | 3 +-- 4 files changed, 9 insertions(+), 16 deletions(-) diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index 7c7b7a4..8267c79 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -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); @@ -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); diff --git a/tests/HasPushSubscriptionsTest.php b/tests/HasPushSubscriptionsTest.php index eb69b33..1bd9dd2 100644 --- a/tests/HasPushSubscriptionsTest.php +++ b/tests/HasPushSubscriptionsTest.php @@ -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 */ diff --git a/tests/PushSubscriptionModelTest.php b/tests/PushSubscriptionModelTest.php index 39b549d..e39efc2 100644 --- a/tests/PushSubscriptionModelTest.php +++ b/tests/PushSubscriptionModelTest.php @@ -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); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 1eedc9c..e1728ad 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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', ]); } From 4f4a389f74705b9a2a8afb0174135c2b93548710 Mon Sep 17 00:00:00 2001 From: Tran Van Hieu Date: Fri, 12 Jul 2024 01:01:56 +0700 Subject: [PATCH 4/4] fix: format issues --- src/HasPushSubscriptions.php | 2 +- src/WebPushChannel.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HasPushSubscriptions.php b/src/HasPushSubscriptions.php index d751382..f3d1bdb 100644 --- a/src/HasPushSubscriptions.php +++ b/src/HasPushSubscriptions.php @@ -29,7 +29,7 @@ public function updatePushSubscription($endpoint, $keys = null, $contentEncoding if ($subscription && $this->ownsPushSubscription($subscription)) { $subscription->update([ 'keys' => $keys, - 'content_encoding' => $contentEncoding + 'content_encoding' => $contentEncoding, ]); return $subscription; diff --git a/src/WebPushChannel.php b/src/WebPushChannel.php index 4e35f08..fa3aaca 100644 --- a/src/WebPushChannel.php +++ b/src/WebPushChannel.php @@ -57,7 +57,7 @@ public function send($notifiable, Notification $notification) [ 'endpoint' => $subscription->endpoint, 'keys' => json_decode($subscription->keys, true), - 'contentEncoding' => $subscription->content_encoding + 'contentEncoding' => $subscription->content_encoding, ] ), $payload,