diff options
author | Thibaut Girka <thib@sitedethib.com> | 2018-05-19 22:41:10 +0200 |
---|---|---|
committer | Thibaut Girka <thib@sitedethib.com> | 2018-05-19 22:41:10 +0200 |
commit | b481e4fac1c564b8008f6f1d0eea1727ec9faa08 (patch) | |
tree | 6917de2e59e69f5ad0dc9b1471a7c7c8659c1af8 /app/models/web | |
parent | 625c4f36ef394215e65e19157bfaf60e7de94b5f (diff) | |
parent | 6aa5ea1b5dff54941682cb006ac2b11ab7b77988 (diff) |
Merge branch 'master' into glitch-soc/merge-upstream
Conflicts: config/locales/simple_form.ja.yml
Diffstat (limited to 'app/models/web')
-rw-r--r-- | app/models/web/push_subscription.rb | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/app/models/web/push_subscription.rb b/app/models/web/push_subscription.rb index df549c6d3..867bc9519 100644 --- a/app/models/web/push_subscription.rb +++ b/app/models/web/push_subscription.rb @@ -21,8 +21,8 @@ class Web::PushSubscription < ApplicationRecord has_one :session_activation def push(notification) - I18n.with_locale(associated_user.locale || I18n.default_locale) do - push_payload(message_from(notification), 48.hours.seconds) + I18n.with_locale(associated_user&.locale || I18n.default_locale) do + push_payload(payload_for_notification(notification), 48.hours.seconds) end end @@ -46,16 +46,22 @@ class Web::PushSubscription < ApplicationRecord @associated_access_token = if access_token_id.nil? find_or_create_access_token.token else - access_token + access_token.token end end + class << self + def unsubscribe_for(application_id, resource_owner) + access_token_ids = Doorkeeper::AccessToken.where(application_id: application_id, resource_owner_id: resource_owner.id, revoked_at: nil) + .pluck(:id) + + where(access_token_id: access_token_ids).delete_all + end + end + private def push_payload(message, ttl = 5.minutes.seconds) - # TODO: Make sure that the payload does not - # exceed 4KB - Webpush::PayloadTooLarge - Webpush.payload_send( message: Oj.dump(message), endpoint: endpoint, @@ -70,16 +76,20 @@ class Web::PushSubscription < ApplicationRecord ) end - def message_from(notification) - serializable_resource = ActiveModelSerializers::SerializableResource.new(notification, serializer: Web::NotificationSerializer, scope: self, scope_name: :current_push_subscription) - serializable_resource.as_json + def payload_for_notification(notification) + ActiveModelSerializers::SerializableResource.new( + notification, + serializer: Web::NotificationSerializer, + scope: self, + scope_name: :current_push_subscription + ).as_json end def find_or_create_access_token Doorkeeper::AccessToken.find_or_create_for( Doorkeeper::Application.find_by(superapp: true), session_activation.user_id, - Doorkeeper::OAuth::Scopes.from_string('read write follow'), + Doorkeeper::OAuth::Scopes.from_string('read write follow push'), Doorkeeper.configuration.access_token_expires_in, Doorkeeper.configuration.refresh_token_enabled? ) |