about summary refs log tree commit diff
path: root/app/controllers/api/web
diff options
context:
space:
mode:
authorSorin Davidoi <sorin.davidoi@gmail.com>2017-07-23 23:27:23 +0200
committerEugen Rochko <eugen@zeonfederated.com>2017-07-23 23:27:23 +0200
commit9075c90c463d02b789d8dcee69115d11ca203ce3 (patch)
tree195d19d0be5db08a29ff374bd3cb75f84e006328 /app/controllers/api/web
parent63a2566007351bc5f8772518b0664343c9095bb6 (diff)
feat: Enable push subscription for mobile devices by default (#4321)
Diffstat (limited to 'app/controllers/api/web')
-rw-r--r--app/controllers/api/web/push_subscriptions_controller.rb23
1 files changed, 18 insertions, 5 deletions
diff --git a/app/controllers/api/web/push_subscriptions_controller.rb b/app/controllers/api/web/push_subscriptions_controller.rb
index 8425db7b4..d66237feb 100644
--- a/app/controllers/api/web/push_subscriptions_controller.rb
+++ b/app/controllers/api/web/push_subscriptions_controller.rb
@@ -6,8 +6,8 @@ class Api::Web::PushSubscriptionsController < Api::BaseController
   before_action :require_user!
 
   def create
-    params.require(:data).require(:endpoint)
-    params.require(:data).require(:keys).require([:auth, :p256dh])
+    params.require(:subscription).require(:endpoint)
+    params.require(:subscription).require(:keys).require([:auth, :p256dh])
 
     active_session = current_session
 
@@ -16,10 +16,23 @@ class Api::Web::PushSubscriptionsController < Api::BaseController
       active_session.update!(web_push_subscription: nil)
     end
 
+    # Mobile devices do not support regular notifications, so we enable push notifications by default
+    alerts_enabled = active_session.detection.device.mobile? || active_session.detection.device.tablet?
+
+    data = {
+      alerts: {
+        follow: alerts_enabled,
+        favourite: alerts_enabled,
+        reblog: alerts_enabled,
+        mention: alerts_enabled,
+      },
+    }
+
     web_subscription = ::Web::PushSubscription.create!(
-      endpoint: params[:data][:endpoint],
-      key_p256dh: params[:data][:keys][:p256dh],
-      key_auth: params[:data][:keys][:auth]
+      endpoint: params[:subscription][:endpoint],
+      key_p256dh: params[:subscription][:keys][:p256dh],
+      key_auth: params[:subscription][:keys][:auth],
+      data: data
     )
 
     active_session.update!(web_push_subscription: web_subscription)