rpush 4.2.0 → 7.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +307 -163
  3. data/README.md +105 -19
  4. data/lib/generators/templates/add_adm.rb +1 -1
  5. data/lib/generators/templates/add_alert_is_json_to_rapns_notifications.rb +2 -2
  6. data/lib/generators/templates/add_app_to_rapns.rb +2 -2
  7. data/lib/generators/templates/add_fail_after_to_rpush_notifications.rb +1 -1
  8. data/lib/generators/templates/add_gcm.rb +11 -25
  9. data/lib/generators/templates/add_rpush.rb +33 -83
  10. data/lib/generators/templates/add_wpns.rb +1 -1
  11. data/lib/generators/templates/create_rapns_apps.rb +1 -1
  12. data/lib/generators/templates/create_rapns_feedback.rb +3 -9
  13. data/lib/generators/templates/create_rapns_notifications.rb +3 -9
  14. data/lib/generators/templates/rename_rapns_to_rpush.rb +9 -33
  15. data/lib/generators/templates/rpush.rb +5 -4
  16. data/lib/generators/templates/rpush_2_0_0_updates.rb +10 -18
  17. data/lib/generators/templates/rpush_2_1_0_updates.rb +1 -1
  18. data/lib/generators/templates/rpush_2_6_0_updates.rb +1 -1
  19. data/lib/generators/templates/rpush_2_7_0_updates.rb +1 -1
  20. data/lib/generators/templates/rpush_3_0_0_updates.rb +1 -1
  21. data/lib/generators/templates/rpush_3_0_1_updates.rb +1 -1
  22. data/lib/generators/templates/rpush_3_1_0_add_pushy.rb +1 -1
  23. data/lib/generators/templates/rpush_3_1_1_updates.rb +1 -1
  24. data/lib/generators/templates/rpush_3_2_0_add_apns_p8.rb +1 -1
  25. data/lib/generators/templates/rpush_3_2_4_updates.rb +1 -1
  26. data/lib/generators/templates/rpush_3_3_0_updates.rb +1 -1
  27. data/lib/generators/templates/rpush_3_3_1_updates.rb +3 -3
  28. data/lib/generators/templates/rpush_4_1_0_updates.rb +1 -1
  29. data/lib/generators/templates/rpush_4_1_1_updates.rb +1 -1
  30. data/lib/generators/templates/rpush_4_2_0_updates.rb +1 -1
  31. data/lib/rpush/cli.rb +1 -1
  32. data/lib/rpush/client/active_model/adm/data_validator.rb +1 -1
  33. data/lib/rpush/client/active_model/apns/app.rb +1 -17
  34. data/lib/rpush/client/active_model/apns/device_token_format_validator.rb +2 -2
  35. data/lib/rpush/client/active_model/apns/notification.rb +13 -1
  36. data/lib/rpush/client/active_model/apns/notification_payload_size_validator.rb +15 -0
  37. data/lib/rpush/client/active_model/apns2/app.rb +7 -1
  38. data/lib/rpush/client/active_model/apns2/notification.rb +14 -0
  39. data/lib/rpush/client/active_model/certificate_private_key_validator.rb +19 -0
  40. data/lib/rpush/client/active_model/gcm/expiry_collapse_key_mutual_inclusion_validator.rb +1 -1
  41. data/lib/rpush/client/active_model/gcm/notification.rb +2 -2
  42. data/lib/rpush/client/active_model/payload_data_size_validator.rb +1 -1
  43. data/lib/rpush/client/active_model/registration_ids_count_validator.rb +1 -1
  44. data/lib/rpush/client/active_model/webpush/app.rb +41 -0
  45. data/lib/rpush/client/active_model/webpush/notification.rb +66 -0
  46. data/lib/rpush/client/active_model.rb +5 -1
  47. data/lib/rpush/client/active_record/apns/active_record_serializable_notification.rb +65 -0
  48. data/lib/rpush/client/active_record/apns/notification.rb +1 -57
  49. data/lib/rpush/client/active_record/apns2/notification.rb +4 -1
  50. data/lib/rpush/client/active_record/apnsp8/notification.rb +1 -0
  51. data/lib/rpush/client/active_record/webpush/app.rb +11 -0
  52. data/lib/rpush/client/active_record/webpush/notification.rb +12 -0
  53. data/lib/rpush/client/active_record.rb +4 -0
  54. data/lib/rpush/client/redis/apns2/notification.rb +1 -0
  55. data/lib/rpush/client/redis/apnsp8/notification.rb +2 -0
  56. data/lib/rpush/client/redis/pushy/notification.rb +0 -1
  57. data/lib/rpush/client/redis/webpush/app.rb +15 -0
  58. data/lib/rpush/client/redis/webpush/notification.rb +15 -0
  59. data/lib/rpush/client/redis.rb +3 -0
  60. data/lib/rpush/configuration.rb +3 -2
  61. data/lib/rpush/daemon/apns/feedback_receiver.rb +1 -1
  62. data/lib/rpush/daemon/apns2/delivery.rb +14 -2
  63. data/lib/rpush/daemon/apnsp8/delivery.rb +14 -3
  64. data/lib/rpush/daemon/app_runner.rb +1 -1
  65. data/lib/rpush/daemon/batch.rb +12 -5
  66. data/lib/rpush/daemon/delivery.rb +1 -2
  67. data/lib/rpush/daemon/store/active_record/reconnectable.rb +1 -1
  68. data/lib/rpush/daemon/store/active_record.rb +11 -7
  69. data/lib/rpush/daemon/store/redis.rb +6 -6
  70. data/lib/rpush/daemon/string_helpers.rb +1 -1
  71. data/lib/rpush/daemon/webpush/delivery.rb +114 -0
  72. data/lib/rpush/daemon/webpush.rb +10 -0
  73. data/lib/rpush/daemon.rb +4 -1
  74. data/lib/rpush/logger.rb +2 -1
  75. data/lib/rpush/version.rb +3 -3
  76. data/spec/functional/apns2_spec.rb +99 -2
  77. data/spec/functional/gcm_priority_spec.rb +40 -0
  78. data/spec/functional/retry_spec.rb +1 -1
  79. data/spec/functional/webpush_spec.rb +31 -0
  80. data/spec/spec_helper.rb +3 -1
  81. data/spec/support/active_record_setup.rb +4 -3
  82. data/spec/support/config/database.yml +4 -4
  83. data/spec/support/simplecov_helper.rb +2 -2
  84. data/spec/unit/client/active_record/adm/app_spec.rb +2 -54
  85. data/spec/unit/client/active_record/adm/notification_spec.rb +2 -39
  86. data/spec/unit/client/active_record/apns/app_spec.rb +3 -26
  87. data/spec/unit/client/active_record/apns/feedback_spec.rb +1 -5
  88. data/spec/unit/client/active_record/apns/notification_spec.rb +29 -293
  89. data/spec/unit/client/active_record/apns2/app_spec.rb +5 -0
  90. data/spec/unit/client/active_record/apns2/notification_spec.rb +65 -0
  91. data/spec/unit/client/active_record/apnsp8/notification_spec.rb +28 -0
  92. data/spec/unit/client/active_record/app_spec.rb +1 -26
  93. data/spec/unit/client/active_record/gcm/app_spec.rb +3 -1
  94. data/spec/unit/client/active_record/gcm/notification_spec.rb +6 -88
  95. data/spec/unit/client/active_record/notification_spec.rb +3 -11
  96. data/spec/unit/client/active_record/pushy/app_spec.rb +2 -13
  97. data/spec/unit/client/active_record/pushy/notification_spec.rb +2 -55
  98. data/spec/unit/client/active_record/shared/app.rb +14 -0
  99. data/spec/unit/{notification_shared.rb → client/active_record/shared/notification.rb} +12 -7
  100. data/spec/unit/client/active_record/webpush/app_spec.rb +6 -0
  101. data/spec/unit/client/active_record/webpush/notification_spec.rb +6 -0
  102. data/spec/unit/client/active_record/wns/badge_notification_spec.rb +1 -11
  103. data/spec/unit/client/active_record/wns/raw_notification_spec.rb +3 -12
  104. data/spec/unit/client/active_record/wpns/app_spec.rb +3 -1
  105. data/spec/unit/client/active_record/wpns/notification_spec.rb +2 -17
  106. data/spec/unit/client/redis/adm/app_spec.rb +5 -0
  107. data/spec/unit/client/redis/adm/notification_spec.rb +5 -0
  108. data/spec/unit/client/redis/apns/app_spec.rb +5 -0
  109. data/spec/unit/client/redis/apns/feedback_spec.rb +5 -0
  110. data/spec/unit/client/redis/apns/notification_spec.rb +50 -0
  111. data/spec/unit/client/redis/apns2/app_spec.rb +4 -0
  112. data/spec/unit/client/redis/apns2/notification_spec.rb +50 -0
  113. data/spec/unit/client/redis/apnsp8/notification_spec.rb +29 -0
  114. data/spec/unit/client/redis/app_spec.rb +5 -0
  115. data/spec/unit/client/redis/gcm/app_spec.rb +5 -0
  116. data/spec/unit/client/redis/gcm/notification_spec.rb +5 -0
  117. data/spec/unit/client/redis/notification_spec.rb +5 -0
  118. data/spec/unit/client/redis/pushy/app_spec.rb +5 -0
  119. data/spec/unit/client/redis/pushy/notification_spec.rb +5 -0
  120. data/spec/unit/client/redis/webpush/app_spec.rb +5 -0
  121. data/spec/unit/client/redis/webpush/notification_spec.rb +5 -0
  122. data/spec/unit/client/redis/wns/badge_notification_spec.rb +5 -0
  123. data/spec/unit/client/redis/wns/raw_notification_spec.rb +22 -0
  124. data/spec/unit/client/redis/wpns/app_spec.rb +5 -0
  125. data/spec/unit/client/redis/wpns/notification_spec.rb +5 -0
  126. data/spec/unit/client/shared/adm/app.rb +51 -0
  127. data/spec/unit/client/shared/adm/notification.rb +39 -0
  128. data/spec/unit/client/shared/apns/app.rb +29 -0
  129. data/spec/unit/client/shared/apns/feedback.rb +9 -0
  130. data/spec/unit/client/shared/apns/notification.rb +277 -0
  131. data/spec/unit/client/shared/app.rb +17 -0
  132. data/spec/unit/client/shared/gcm/app.rb +4 -0
  133. data/spec/unit/client/shared/gcm/notification.rb +77 -0
  134. data/spec/unit/client/shared/notification.rb +10 -0
  135. data/spec/unit/client/shared/pushy/app.rb +17 -0
  136. data/spec/unit/client/shared/pushy/notification.rb +55 -0
  137. data/spec/unit/client/shared/webpush/app.rb +33 -0
  138. data/spec/unit/client/shared/webpush/notification.rb +83 -0
  139. data/spec/unit/client/shared/wns/badge_notification.rb +15 -0
  140. data/spec/unit/client/shared/wns/raw_notification.rb +21 -0
  141. data/spec/unit/client/shared/wpns/app.rb +4 -0
  142. data/spec/unit/client/shared/wpns/notification.rb +18 -0
  143. data/spec/unit/daemon/apnsp8/delivery_spec.rb +53 -0
  144. data/spec/unit/daemon/batch_spec.rb +50 -2
  145. data/spec/unit/daemon/delivery_spec.rb +10 -0
  146. data/spec/unit/daemon/pushy/delivery_spec.rb +5 -3
  147. data/spec/unit/daemon/shared/store.rb +312 -0
  148. data/spec/unit/daemon/store/active_record/reconnectable_spec.rb +7 -7
  149. data/spec/unit/daemon/store/active_record_spec.rb +6 -287
  150. data/spec/unit/daemon/store/redis_spec.rb +2 -291
  151. data/spec/unit/daemon/webpush/delivery_spec.rb +144 -0
  152. data/spec/unit_spec_helper.rb +3 -0
  153. metadata +145 -18
  154. data/lib/rpush/client/active_model/apns/binary_notification_validator.rb +0 -16
@@ -1,4 +1,4 @@
1
- class RenameRapnsToRpush < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
1
+ class RenameRapnsToRpush < ActiveRecord::Migration[5.0]
2
2
  module Rpush
3
3
  class App < ActiveRecord::Base
4
4
  self.table_name = 'rpush_apps'
@@ -18,24 +18,12 @@ class RenameRapnsToRpush < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Mig
18
18
  rename_table :rapns_apps, :rpush_apps
19
19
  rename_table :rapns_feedback, :rpush_feedback
20
20
 
21
- if ActiveRecord.version >= Gem::Version.new('5.1')
22
- if index_name_exists?(:rpush_notifications, :index_rapns_notifications_multi)
23
- rename_index :rpush_notifications, :index_rapns_notifications_multi, :index_rpush_notifications_multi
24
- end
25
- else
26
- if index_name_exists?(:rpush_notifications, :index_rapns_notifications_multi, true)
27
- rename_index :rpush_notifications, :index_rapns_notifications_multi, :index_rpush_notifications_multi
28
- end
21
+ if index_name_exists?(:rpush_notifications, :index_rapns_notifications_multi)
22
+ rename_index :rpush_notifications, :index_rapns_notifications_multi, :index_rpush_notifications_multi
29
23
  end
30
24
 
31
- if ActiveRecord.version >= Gem::Version.new('5.1')
32
- if index_name_exists?(:rpush_feedback, :index_rapns_feedback_on_device_token)
33
- rename_index :rpush_feedback, :index_rapns_feedback_on_device_token, :index_rpush_feedback_on_device_token
34
- end
35
- else
36
- if index_name_exists?(:rpush_feedback, :index_rapns_feedback_on_device_token, true)
37
- rename_index :rpush_feedback, :index_rapns_feedback_on_device_token, :index_rpush_feedback_on_device_token
38
- end
25
+ if index_name_exists?(:rpush_feedback, :index_rapns_feedback_on_device_token)
26
+ rename_index :rpush_feedback, :index_rapns_feedback_on_device_token, :index_rpush_feedback_on_device_token
39
27
  end
40
28
 
41
29
  update_type(RenameRapnsToRpush::Rpush::Notification, 'Rapns::Apns::Notification', 'Rpush::Apns::Notification')
@@ -60,24 +48,12 @@ class RenameRapnsToRpush < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Mig
60
48
  update_type(RenameRapnsToRpush::Rpush::App, 'Rpush::Adm::App', 'Rapns::Adm::App')
61
49
  update_type(RenameRapnsToRpush::Rpush::App, 'Rpush::Wpns::App', 'Rapns::Wpns::App')
62
50
 
63
- if ActiveRecord.version >= Gem::Version.new('5.1')
64
- if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi)
65
- rename_index :rpush_notifications, :index_rpush_notifications_multi, :index_rapns_notifications_multi
66
- end
67
- else
68
- if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi, true)
69
- rename_index :rpush_notifications, :index_rpush_notifications_multi, :index_rapns_notifications_multi
70
- end
51
+ if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi)
52
+ rename_index :rpush_notifications, :index_rpush_notifications_multi, :index_rapns_notifications_multi
71
53
  end
72
54
 
73
- if ActiveRecord.version >= Gem::Version.new('5.1')
74
- if index_name_exists?(:rpush_feedback, :index_rpush_feedback_on_device_token)
75
- rename_index :rpush_feedback, :index_rpush_feedback_on_device_token, :index_rapns_feedback_on_device_token
76
- end
77
- else
78
- if index_name_exists?(:rpush_feedback, :index_rpush_feedback_on_device_token, true)
79
- rename_index :rpush_feedback, :index_rpush_feedback_on_device_token, :index_rapns_feedback_on_device_token
80
- end
55
+ if index_name_exists?(:rpush_feedback, :index_rpush_feedback_on_device_token)
56
+ rename_index :rpush_feedback, :index_rpush_feedback_on_device_token, :index_rapns_feedback_on_device_token
81
57
  end
82
58
 
83
59
  rename_table :rpush_notifications, :rapns_notifications
@@ -26,6 +26,10 @@ Rpush.configure do |config|
26
26
  # Define a custom logger.
27
27
  # config.logger = MyLogger.new
28
28
 
29
+ # By default in foreground mode logs are directed both to the logger and to stdout.
30
+ # If the logger goes to stdout, you can disable foreground logging to avoid duplication.
31
+ # config.foreground_logging = false
32
+
29
33
  # config.apns.feedback_receiver.enabled = true
30
34
  # config.apns.feedback_receiver.frequency = 60
31
35
 
@@ -42,10 +46,7 @@ Rpush.reflect do |on|
42
46
  # Called when a notification is queued internally for delivery.
43
47
  # The internal queue for each app runner can be inspected:
44
48
  #
45
- # Rpush::Daemon::AppRunner.runners.each do |app_id, runner|
46
- # runner.app
47
- # runner.queue_size
48
- # end
49
+ # Rpush::Daemon::AppRunner.status
49
50
  #
50
51
  # on.notification_enqueued do |notification|
51
52
  # end
@@ -1,4 +1,4 @@
1
- class Rpush200Updates < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
1
+ class Rpush200Updates < ActiveRecord::Migration[5.0]
2
2
  module Rpush
3
3
  class App < ActiveRecord::Base
4
4
  self.table_name = 'rpush_apps'
@@ -17,14 +17,8 @@ class Rpush200Updates < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migrat
17
17
  add_column :rpush_notifications, :processing, :boolean, null: false, default: false
18
18
  add_column :rpush_notifications, :priority, :integer, null: true
19
19
 
20
- if ActiveRecord.version >= Gem::Version.new('5.1')
21
- if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi)
22
- remove_index :rpush_notifications, name: :index_rpush_notifications_multi
23
- end
24
- else
25
- if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi, true)
26
- remove_index :rpush_notifications, name: :index_rpush_notifications_multi
27
- end
20
+ if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi)
21
+ remove_index :rpush_notifications, name: :index_rpush_notifications_multi
28
22
  end
29
23
 
30
24
  add_index :rpush_notifications, [:delivered, :failed], name: 'index_rpush_notifications_multi', where: 'NOT delivered AND NOT failed'
@@ -52,14 +46,8 @@ class Rpush200Updates < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migrat
52
46
  change_column :rpush_feedback, :app_id, :string
53
47
  rename_column :rpush_feedback, :app_id, :app
54
48
 
55
- if ActiveRecord.version >= Gem::Version.new('5.1')
56
- if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi)
57
- remove_index :rpush_notifications, name: :index_rpush_notifications_multi
58
- end
59
- else
60
- if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi, true)
61
- remove_index :rpush_notifications, name: :index_rpush_notifications_multi
62
- end
49
+ if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi)
50
+ remove_index :rpush_notifications, name: :index_rpush_notifications_multi
63
51
  end
64
52
 
65
53
  add_index :rpush_notifications, [:app_id, :delivered, :failed, :deliver_after], name: 'index_rpush_notifications_multi'
@@ -70,7 +58,11 @@ class Rpush200Updates < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migrat
70
58
 
71
59
  def self.adapter_name
72
60
  env = (defined?(Rails) && Rails.env) ? Rails.env : 'development'
73
- Hash[ActiveRecord::Base.configurations[env].map { |k,v| [k.to_sym,v] }][:adapter]
61
+ if ActiveRecord::VERSION::MAJOR > 6
62
+ ActiveRecord::Base.configurations.configs_for(env_name: env).first.configuration_hash[:adapter]
63
+ else
64
+ Hash[ActiveRecord::Base.configurations[env].map { |k,v| [k.to_sym,v] }][:adapter]
65
+ end
74
66
  end
75
67
 
76
68
  def self.postgresql?
@@ -1,4 +1,4 @@
1
- class Rpush210Updates < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
1
+ class Rpush210Updates < ActiveRecord::Migration[5.0]
2
2
  def self.up
3
3
  add_column :rpush_notifications, :url_args, :text, null: true
4
4
  add_column :rpush_notifications, :category, :string, null: true
@@ -1,4 +1,4 @@
1
- class Rpush260Updates < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
1
+ class Rpush260Updates < ActiveRecord::Migration[5.0]
2
2
  def self.up
3
3
  add_column :rpush_notifications, :content_available, :boolean, default: false
4
4
  end
@@ -1,4 +1,4 @@
1
- class Rpush270Updates < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
1
+ class Rpush270Updates < ActiveRecord::Migration[5.0]
2
2
  def self.up
3
3
  change_column :rpush_notifications, :alert, :text
4
4
  add_column :rpush_notifications, :notification, :text
@@ -1,4 +1,4 @@
1
- class Rpush300Updates < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
1
+ class Rpush300Updates < ActiveRecord::Migration[5.0]
2
2
  def self.up
3
3
  add_column :rpush_notifications, :mutable_content, :boolean, default: false
4
4
  change_column :rpush_notifications, :sound, :string, default: nil
@@ -1,4 +1,4 @@
1
- class Rpush301Updates < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
1
+ class Rpush301Updates < ActiveRecord::Migration[5.0]
2
2
  def self.up
3
3
  change_column_null :rpush_notifications, :mutable_content, false
4
4
  change_column_null :rpush_notifications, :content_available, false
@@ -1,4 +1,4 @@
1
- class Rpush310AddPushy < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
1
+ class Rpush310AddPushy < ActiveRecord::Migration[5.0]
2
2
  def self.up
3
3
  add_column :rpush_notifications, :external_device_id, :string, null: true
4
4
  end
@@ -1,4 +1,4 @@
1
- class Rpush311Updates < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
1
+ class Rpush311Updates < ActiveRecord::Migration[5.0]
2
2
  def self.up
3
3
  change_table :rpush_notifications do |t|
4
4
  t.remove_index name: 'index_rpush_notifications_multi'
@@ -1,4 +1,4 @@
1
- class Rpush320AddApnsP8 < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
1
+ class Rpush320AddApnsP8 < ActiveRecord::Migration[5.0]
2
2
  def self.up
3
3
  add_column :rpush_apps, :apn_key, :string, null: true
4
4
  add_column :rpush_apps, :apn_key_id, :string, null: true
@@ -1,4 +1,4 @@
1
- class Rpush324Updates < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
1
+ class Rpush324Updates < ActiveRecord::Migration[5.0]
2
2
  def self.up
3
3
  change_column :rpush_apps, :apn_key, :text, null: true
4
4
  end
@@ -1,4 +1,4 @@
1
- class Rpush330Updates < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
1
+ class Rpush330Updates < ActiveRecord::Migration[5.0]
2
2
  def self.up
3
3
  add_column :rpush_notifications, :thread_id, :string, null: true
4
4
  end
@@ -1,11 +1,11 @@
1
- class Rpush331Updates < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration[5.0] : ActiveRecord::Migration
1
+ class Rpush331Updates < ActiveRecord::Migration[5.0]
2
2
  def self.up
3
3
  change_column :rpush_notifications, :device_token, :string, null: true
4
4
  change_column :rpush_feedback, :device_token, :string, null: true
5
5
  end
6
6
 
7
7
  def self.down
8
- change_column :rpush_notifications, :device_token, :string, { null: true, limit: 64 }
9
- change_column :rpush_feedback, :device_token, :string, { null: true, limit: 64 }
8
+ change_column :rpush_notifications, :device_token, :string, null: true, limit: 64
9
+ change_column :rpush_feedback, :device_token, :string, null: true, limit: 64
10
10
  end
11
11
  end
@@ -1,4 +1,4 @@
1
- class Rpush410Updates < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration["#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"] : ActiveRecord::Migration
1
+ class Rpush410Updates < ActiveRecord::Migration["#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"]
2
2
  def self.up
3
3
  add_column :rpush_notifications, :dry_run, :boolean, null: false, default: false
4
4
  end
@@ -1,4 +1,4 @@
1
- class Rpush411Updates < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration["#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"] : ActiveRecord::Migration
1
+ class Rpush411Updates < ActiveRecord::Migration["#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"]
2
2
  def self.up
3
3
  add_column :rpush_apps, :feedback_enabled, :boolean, default: true
4
4
  end
@@ -1,4 +1,4 @@
1
- class Rpush420Updates < ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Migration["#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"] : ActiveRecord::Migration
1
+ class Rpush420Updates < ActiveRecord::Migration["#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"]
2
2
  def self.up
3
3
  add_column :rpush_notifications, :sound_is_json, :boolean, null: true, default: false
4
4
  end
data/lib/rpush/cli.rb CHANGED
@@ -14,7 +14,7 @@ module Rpush
14
14
  end
15
15
 
16
16
  class_option :config, type: :string, aliases: '-c', default: default_config_path
17
- class_option 'rails-env', type: :string, aliases: '-e', default: 'development'
17
+ class_option 'rails-env', type: :string, aliases: '-e', default: ENV.fetch('RAILS_ENV', 'development')
18
18
 
19
19
  option :foreground, type: :boolean, aliases: '-f', default: false
20
20
  option 'pid-file', type: :string, aliases: '-p'
@@ -5,7 +5,7 @@ module Rpush
5
5
  class DataValidator < ::ActiveModel::Validator
6
6
  def validate(record)
7
7
  return unless record.collapse_key.nil? && record.data.nil?
8
- record.errors[:data] << 'must be set unless collapse_key is specified'
8
+ record.errors.add :data, 'must be set unless collapse_key is specified'
9
9
  end
10
10
  end
11
11
  end
@@ -7,29 +7,13 @@ module Rpush
7
7
  base.instance_eval do
8
8
  validates :environment, presence: true, inclusion: { in: %w(development production sandbox) }
9
9
  validates :certificate, presence: true
10
- validate :certificate_has_matching_private_key
10
+ validates_with Rpush::Client::ActiveModel::CertificatePrivateKeyValidator
11
11
  end
12
12
  end
13
13
 
14
14
  def service_name
15
15
  'apns'
16
16
  end
17
-
18
- private
19
-
20
- def certificate_has_matching_private_key
21
- result = false
22
- if certificate.present?
23
- begin
24
- x509 = OpenSSL::X509::Certificate.new(certificate)
25
- pkey = OpenSSL::PKey::RSA.new(certificate, password)
26
- result = x509 && pkey
27
- rescue OpenSSL::OpenSSLError
28
- errors.add :certificate, 'value must contain a certificate and a private key.'
29
- end
30
- end
31
- result
32
- end
33
17
  end
34
18
  end
35
19
  end
@@ -4,8 +4,8 @@ module Rpush
4
4
  module Apns
5
5
  class DeviceTokenFormatValidator < ::ActiveModel::Validator
6
6
  def validate(record)
7
- return if record.device_token =~ /^[a-z0-9]\w+$/i
8
- record.errors[:device_token] << "is invalid"
7
+ return if record.device_token =~ /\A[a-z0-9]\w+\z/i
8
+ record.errors.add :device_token, "is invalid"
9
9
  end
10
10
  end
11
11
  end
@@ -7,15 +7,23 @@ module Rpush
7
7
  APNS_PRIORITY_IMMEDIATE = 10
8
8
  APNS_PRIORITY_CONSERVE_POWER = 5
9
9
  APNS_PRIORITIES = [APNS_PRIORITY_IMMEDIATE, APNS_PRIORITY_CONSERVE_POWER]
10
+ MAX_PAYLOAD_BYTESIZE = 2048
11
+
12
+ module ClassMethods
13
+ def max_payload_bytesize
14
+ MAX_PAYLOAD_BYTESIZE
15
+ end
16
+ end
10
17
 
11
18
  def self.included(base)
19
+ base.extend ClassMethods
12
20
  base.instance_eval do
13
21
  validates :device_token, presence: true
14
22
  validates :badge, numericality: true, allow_nil: true
15
23
  validates :priority, inclusion: { in: APNS_PRIORITIES }, allow_nil: true
16
24
 
17
25
  validates_with Rpush::Client::ActiveModel::Apns::DeviceTokenFormatValidator
18
- validates_with Rpush::Client::ActiveModel::Apns::BinaryNotificationValidator
26
+ validates_with Rpush::Client::ActiveModel::Apns::NotificationPayloadSizeValidator
19
27
 
20
28
  base.const_set('APNS_DEFAULT_EXPIRY', APNS_DEFAULT_EXPIRY) unless base.const_defined?('APNS_DEFAULT_EXPIRY')
21
29
  base.const_set('APNS_PRIORITY_IMMEDIATE', APNS_PRIORITY_IMMEDIATE) unless base.const_defined?('APNS_PRIORITY_IMMEDIATE')
@@ -44,6 +52,10 @@ module Rpush
44
52
  self.data = (data || {}).merge(CONTENT_AVAILABLE_KEY => true)
45
53
  end
46
54
 
55
+ def content_available?
56
+ (self.data || {})[CONTENT_AVAILABLE_KEY]
57
+ end
58
+
47
59
  def as_json(options = nil) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
48
60
  json = ActiveSupport::OrderedHash.new
49
61
 
@@ -0,0 +1,15 @@
1
+ module Rpush
2
+ module Client
3
+ module ActiveModel
4
+ module Apns
5
+ class NotificationPayloadSizeValidator < ::ActiveModel::Validator
6
+ def validate(record)
7
+ limit = record.class.max_payload_bytesize
8
+ return unless record.payload.bytesize > limit
9
+ record.errors.add :base, "APN notification cannot be larger than #{limit} bytes. Try condensing your alert and device attributes."
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -3,7 +3,13 @@ module Rpush
3
3
  module ActiveModel
4
4
  module Apns2
5
5
  module App
6
- extend Rpush::Client::ActiveModel::Apns::App
6
+ def self.included(base)
7
+ base.instance_eval do
8
+ validates :environment, presence: true, inclusion: { in: %w(development production sandbox) }
9
+ validates :certificate, presence: true
10
+ validates_with Rpush::Client::ActiveModel::CertificatePrivateKeyValidator
11
+ end
12
+ end
7
13
 
8
14
  def service_name
9
15
  'apns2'
@@ -3,6 +3,20 @@ module Rpush
3
3
  module ActiveModel
4
4
  module Apns2
5
5
  include Rpush::Client::ActiveModel::Apns
6
+
7
+ module Notification
8
+ MAX_PAYLOAD_BYTESIZE = 4096
9
+
10
+ module ClassMethods
11
+ def max_payload_bytesize
12
+ MAX_PAYLOAD_BYTESIZE
13
+ end
14
+ end
15
+
16
+ def self.included(base)
17
+ base.extend ClassMethods
18
+ end
19
+ end
6
20
  end
7
21
  end
8
22
  end
@@ -0,0 +1,19 @@
1
+ module Rpush
2
+ module Client
3
+ module ActiveModel
4
+ class CertificatePrivateKeyValidator < ::ActiveModel::Validator
5
+ def validate(record)
6
+ if record.certificate.present?
7
+ begin
8
+ x509 = OpenSSL::X509::Certificate.new(record.certificate)
9
+ pkey = OpenSSL::PKey::RSA.new(record.certificate, record.password)
10
+ x509 && pkey
11
+ rescue OpenSSL::OpenSSLError
12
+ record.errors.add :certificate, 'value must contain a certificate and a private key.'
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -5,7 +5,7 @@ module Rpush
5
5
  class ExpiryCollapseKeyMutualInclusionValidator < ::ActiveModel::Validator
6
6
  def validate(record)
7
7
  return unless record.collapse_key && !record.expiry
8
- record.errors[:expiry] << 'must be set when using a collapse_key'
8
+ record.errors.add :expiry, 'must be set when using a collapse_key'
9
9
  end
10
10
  end
11
11
  end
@@ -26,9 +26,9 @@ module Rpush
26
26
  # I'm not happy about it, but this will have to do until I can take a further look.
27
27
  def priority=(priority)
28
28
  case priority
29
- when 'high'
29
+ when 'high', GCM_PRIORITY_HIGH
30
30
  super(GCM_PRIORITY_HIGH)
31
- when 'normal'
31
+ when 'normal', GCM_PRIORITY_NORMAL
32
32
  super(GCM_PRIORITY_NORMAL)
33
33
  else
34
34
  errors.add(:priority, 'must be one of either "normal" or "high"')
@@ -5,7 +5,7 @@ module Rpush
5
5
  def validate(record)
6
6
  limit = options[:limit] || 1024
7
7
  return unless record.data && record.payload_data_size > limit
8
- record.errors[:base] << "Notification payload data cannot be larger than #{limit} bytes."
8
+ record.errors.add :base, "Notification payload data cannot be larger than #{limit} bytes."
9
9
  end
10
10
  end
11
11
  end
@@ -5,7 +5,7 @@ module Rpush
5
5
  def validate(record)
6
6
  limit = options[:limit] || 100
7
7
  return unless record.registration_ids && record.registration_ids.size > limit
8
- record.errors[:base] << "Number of registration_ids cannot be larger than #{limit}."
8
+ record.errors.add :base, "Number of registration_ids cannot be larger than #{limit}."
9
9
  end
10
10
  end
11
11
  end
@@ -0,0 +1,41 @@
1
+ module Rpush
2
+ module Client
3
+ module ActiveModel
4
+ module Webpush
5
+ module App
6
+
7
+ class VapidKeypairValidator < ::ActiveModel::Validator
8
+ def validate(record)
9
+ return if record.vapid_keypair.blank?
10
+ keypair = record.vapid
11
+ %i[ subject public_key private_key ].each do |key|
12
+ unless keypair.key?(key)
13
+ record.errors.add(:vapid_keypair, "must have a #{key} entry")
14
+ end
15
+ end
16
+ rescue
17
+ record.errors.add(:vapid_keypair, 'must be valid JSON')
18
+ end
19
+ end
20
+
21
+ def self.included(base)
22
+ base.class_eval do
23
+ alias_attribute :vapid_keypair, :certificate
24
+ validates :vapid_keypair, presence: true
25
+ validates_with VapidKeypairValidator
26
+ end
27
+ end
28
+
29
+ def service_name
30
+ 'webpush'
31
+ end
32
+
33
+ def vapid
34
+ @vapid ||= JSON.parse(vapid_keypair).symbolize_keys
35
+ end
36
+
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,66 @@
1
+ module Rpush
2
+ module Client
3
+ module ActiveModel
4
+ module Webpush
5
+ module Notification
6
+
7
+ class RegistrationValidator < ::ActiveModel::Validator
8
+ KEYS = %i[ endpoint keys ].freeze
9
+ def validate(record)
10
+ return if record.registration_ids.blank?
11
+ return if record.registration_ids.size > 1
12
+ reg = record.registration_ids.first
13
+ unless reg.is_a?(Hash) &&
14
+ (KEYS-reg.keys).empty? &&
15
+ reg[:endpoint].is_a?(String) &&
16
+ reg[:keys].is_a?(Hash)
17
+ record.errors.add(:base, 'Registration must have :endpoint (String) and :keys (Hash) keys')
18
+ end
19
+ end
20
+ end
21
+
22
+ def self.included(base)
23
+ base.instance_eval do
24
+ alias_attribute :time_to_live, :expiry
25
+
26
+ validates :registration_ids, presence: true
27
+ validates :data, presence: true
28
+ validates :time_to_live, numericality: { only_integer: true, greater_than: 0 }, allow_nil: true
29
+
30
+ validates_with Rpush::Client::ActiveModel::PayloadDataSizeValidator, limit: 4096
31
+ validates_with Rpush::Client::ActiveModel::RegistrationIdsCountValidator, limit: 1
32
+ validates_with RegistrationValidator
33
+ end
34
+ end
35
+
36
+ def data=(value)
37
+ value = value.stringify_keys if value.respond_to?(:stringify_keys)
38
+ super value
39
+ end
40
+
41
+ def subscription
42
+ @subscription ||= registration_ids.first.deep_symbolize_keys
43
+ end
44
+
45
+ def message
46
+ data['message'].presence if data
47
+ end
48
+
49
+ # https://webpush-wg.github.io/webpush-protocol/#urgency
50
+ def urgency
51
+ data['urgency'].presence if data
52
+ end
53
+
54
+ def as_json(_options = nil)
55
+ {
56
+ 'data' => data,
57
+ 'time_to_live' => time_to_live,
58
+ 'registration_ids' => registration_ids
59
+ }
60
+ end
61
+
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -3,11 +3,12 @@ require 'active_model'
3
3
  require 'rpush/client/active_model/notification'
4
4
  require 'rpush/client/active_model/payload_data_size_validator'
5
5
  require 'rpush/client/active_model/registration_ids_count_validator'
6
+ require 'rpush/client/active_model/certificate_private_key_validator'
6
7
 
7
- require 'rpush/client/active_model/apns/binary_notification_validator'
8
8
  require 'rpush/client/active_model/apns/device_token_format_validator'
9
9
  require 'rpush/client/active_model/apns/app'
10
10
  require 'rpush/client/active_model/apns/notification'
11
+ require 'rpush/client/active_model/apns/notification_payload_size_validator'
11
12
 
12
13
  require 'rpush/client/active_model/apns2/app'
13
14
  require 'rpush/client/active_model/apns2/notification'
@@ -32,3 +33,6 @@ require 'rpush/client/active_model/wns/notification'
32
33
  require 'rpush/client/active_model/pushy/app'
33
34
  require 'rpush/client/active_model/pushy/notification'
34
35
  require 'rpush/client/active_model/pushy/time_to_live_validator'
36
+
37
+ require 'rpush/client/active_model/webpush/app'
38
+ require 'rpush/client/active_model/webpush/notification'