2017-07-13 13:15:32 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: web_push_subscriptions
|
|
|
|
#
|
2018-05-11 02:49:12 -07:00
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# endpoint :string not null
|
|
|
|
# key_p256dh :string not null
|
|
|
|
# key_auth :string not null
|
|
|
|
# data :json
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# access_token_id :bigint(8)
|
|
|
|
# user_id :bigint(8)
|
2017-07-13 13:15:32 -07:00
|
|
|
#
|
|
|
|
|
|
|
|
class Web::PushSubscription < ApplicationRecord
|
2018-05-11 02:49:12 -07:00
|
|
|
belongs_to :user, optional: true
|
|
|
|
belongs_to :access_token, class_name: 'Doorkeeper::AccessToken', optional: true
|
|
|
|
|
2018-06-09 12:11:14 -07:00
|
|
|
has_one :session_activation, foreign_key: 'web_push_subscription_id', inverse_of: :web_push_subscription
|
2017-07-13 13:15:32 -07:00
|
|
|
|
2019-09-27 06:24:13 -07:00
|
|
|
validates :endpoint, presence: true
|
|
|
|
validates :key_p256dh, presence: true
|
|
|
|
validates :key_auth, presence: true
|
|
|
|
|
2017-07-13 13:15:32 -07:00
|
|
|
def push(notification)
|
2018-05-19 05:46:47 -07:00
|
|
|
I18n.with_locale(associated_user&.locale || I18n.default_locale) do
|
|
|
|
push_payload(payload_for_notification(notification), 48.hours.seconds)
|
2017-09-01 04:35:23 -07:00
|
|
|
end
|
2017-07-13 13:15:32 -07:00
|
|
|
end
|
|
|
|
|
2017-07-19 13:37:52 -07:00
|
|
|
def pushable?(notification)
|
2018-05-11 02:49:12 -07:00
|
|
|
data&.key?('alerts') && ActiveModel::Type::Boolean.new.cast(data['alerts'][notification.type.to_s])
|
2017-07-19 13:37:52 -07:00
|
|
|
end
|
|
|
|
|
2018-05-11 02:49:12 -07:00
|
|
|
def associated_user
|
|
|
|
return @associated_user if defined?(@associated_user)
|
|
|
|
|
|
|
|
@associated_user = if user_id.nil?
|
|
|
|
session_activation.user
|
|
|
|
else
|
|
|
|
user
|
|
|
|
end
|
2017-07-13 13:15:32 -07:00
|
|
|
end
|
|
|
|
|
2018-05-11 02:49:12 -07:00
|
|
|
def associated_access_token
|
|
|
|
return @associated_access_token if defined?(@associated_access_token)
|
|
|
|
|
|
|
|
@associated_access_token = if access_token_id.nil?
|
|
|
|
find_or_create_access_token.token
|
|
|
|
else
|
2018-05-19 05:46:47 -07:00
|
|
|
access_token.token
|
2018-05-11 02:49:12 -07:00
|
|
|
end
|
2017-07-13 13:15:32 -07:00
|
|
|
end
|
|
|
|
|
2018-05-19 12:05:08 -07:00
|
|
|
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
|
|
|
|
|
2017-09-01 04:35:23 -07:00
|
|
|
private
|
2017-07-13 13:15:32 -07:00
|
|
|
|
2017-09-01 04:35:23 -07:00
|
|
|
def push_payload(message, ttl = 5.minutes.seconds)
|
2017-07-13 13:15:32 -07:00
|
|
|
Webpush.payload_send(
|
2017-09-01 04:35:23 -07:00
|
|
|
message: Oj.dump(message),
|
2017-07-13 13:15:32 -07:00
|
|
|
endpoint: endpoint,
|
|
|
|
p256dh: key_p256dh,
|
|
|
|
auth: key_auth,
|
2017-09-01 04:35:23 -07:00
|
|
|
ttl: ttl,
|
2018-12-16 18:14:13 -08:00
|
|
|
ssl_timeout: 10,
|
|
|
|
open_timeout: 10,
|
|
|
|
read_timeout: 10,
|
2017-07-13 13:15:32 -07:00
|
|
|
vapid: {
|
2017-09-02 07:50:42 -07:00
|
|
|
subject: "mailto:#{::Setting.site_contact_email}",
|
2017-07-13 13:15:32 -07:00
|
|
|
private_key: Rails.configuration.x.vapid_private_key,
|
|
|
|
public_key: Rails.configuration.x.vapid_public_key,
|
2017-09-01 04:35:23 -07:00
|
|
|
}
|
2017-07-13 13:15:32 -07:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-05-19 05:46:47 -07:00
|
|
|
def payload_for_notification(notification)
|
|
|
|
ActiveModelSerializers::SerializableResource.new(
|
|
|
|
notification,
|
|
|
|
serializer: Web::NotificationSerializer,
|
|
|
|
scope: self,
|
|
|
|
scope_name: :current_push_subscription
|
|
|
|
).as_json
|
2017-09-01 04:35:23 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_or_create_access_token
|
2017-07-13 13:15:32 -07:00
|
|
|
Doorkeeper::AccessToken.find_or_create_for(
|
|
|
|
Doorkeeper::Application.find_by(superapp: true),
|
2017-09-01 04:35:23 -07:00
|
|
|
session_activation.user_id,
|
2018-05-19 05:46:47 -07:00
|
|
|
Doorkeeper::OAuth::Scopes.from_string('read write follow push'),
|
2017-07-13 13:15:32 -07:00
|
|
|
Doorkeeper.configuration.access_token_expires_in,
|
|
|
|
Doorkeeper.configuration.refresh_token_enabled?
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|