0
0
Fork 0

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
This commit is contained in:
Eugen Rochko 2021-04-15 05:00:25 +02:00 committed by GitHub
parent c968d22ee9
commit ce2148c571
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 170 additions and 51 deletions

View file

@ -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