about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2021-04-15 05:00:25 +0200
committerGitHub <noreply@github.com>2021-04-15 05:00:25 +0200
commitce2148c57111981be455a9953d3bb589cf53967f (patch)
tree1fd87e2525e1c7989828a91ec7cde777e82504da /app/models
parentc968d22ee95fd9a37695b896cd86b7608689ead1 (diff)
Add `policy` param to `POST /api/v1/push/subscriptions` (#16040)
With possible values `all`, `followed`, `follower`, and `none`,
control from whom notifications will generate a Web Push alert
Diffstat (limited to 'app/models')
-rw-r--r--app/models/web/push_subscription.rb23
1 files changed, 22 insertions, 1 deletions
diff --git a/app/models/web/push_subscription.rb b/app/models/web/push_subscription.rb
index 7609b1bfc..6e46573ae 100644
--- a/app/models/web/push_subscription.rb
+++ b/app/models/web/push_subscription.rb
@@ -47,7 +47,7 @@ class Web::PushSubscription < ApplicationRecord
   end
 
   def pushable?(notification)
-    ActiveModel::Type::Boolean.new.cast(data&.dig('alerts', notification.type.to_s))
+    policy_allows_notification?(notification) && alert_enabled_for_notification_type?(notification)
   end
 
   def associated_user
@@ -100,4 +100,25 @@ class Web::PushSubscription < ApplicationRecord
   def contact_email
     @contact_email ||= ::Setting.site_contact_email
   end
+
+  def alert_enabled_for_notification_type?(notification)
+    truthy?(data&.dig('alerts', notification.type.to_s))
+  end
+
+  def policy_allows_notification?(notification)
+    case data&.dig('policy')
+    when nil, 'all'
+      true
+    when 'none'
+      false
+    when 'followed'
+      notification.account.following?(notification.from_account)
+    when 'follower'
+      notification.from_account.following?(notification.account)
+    end
+  end
+
+  def truthy?(val)
+    ActiveModel::Type::Boolean.new.cast(val)
+  end
 end