Browse Source

Validate Web::PushSubscription (#11971)

abcang 3 months ago
parent
commit
07b057eabb

+ 4
- 0
app/models/web/push_subscription.rb View File

@@ -20,6 +20,10 @@ class Web::PushSubscription < ApplicationRecord
20 20
 
21 21
   has_one :session_activation, foreign_key: 'web_push_subscription_id', inverse_of: :web_push_subscription
22 22
 
23
+  validates :endpoint, presence: true
24
+  validates :key_p256dh, presence: true
25
+  validates :key_auth, presence: true
26
+
23 27
   def push(notification)
24 28
     I18n.with_locale(associated_user&.locale || I18n.default_locale) do
25 29
       push_payload(payload_for_notification(notification), 48.hours.seconds)

+ 18
- 0
db/post_migrate/20190927124642_remove_invalid_web_push_subscription.rb View File

@@ -0,0 +1,18 @@
1
+# frozen_string_literal: true
2
+
3
+class RemoveInvalidWebPushSubscription < ActiveRecord::Migration[5.2]
4
+  disable_ddl_transaction!
5
+
6
+  def up
7
+    invalid_web_push_subscriptions = Web::PushSubscription.where(endpoint: '')
8
+                                                          .or(Web::PushSubscription.where(key_p256dh: ''))
9
+                                                          .or(Web::PushSubscription.where(key_auth: ''))
10
+                                                          .preload(:session_activation)
11
+    invalid_web_push_subscriptions.find_each do |web_push_subscription|
12
+      web_push_subscription.session_activation&.update!(web_push_subscription_id: nil)
13
+      web_push_subscription.destroy!
14
+    end
15
+  end
16
+
17
+  def down; end
18
+end

+ 1
- 1
db/schema.rb View File

@@ -10,7 +10,7 @@
10 10
 #
11 11
 # It's strongly recommended that you check this file into your version control system.
12 12
 
13
-ActiveRecord::Schema.define(version: 2019_09_17_213523) do
13
+ActiveRecord::Schema.define(version: 2019_09_27_124642) do
14 14
 
15 15
   # These are extensions that must be enabled in order to support this database
16 16
   enable_extension "plpgsql"

Loading…
Cancel
Save