2017-07-13 22:15:32 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: web_push_subscriptions
|
|
|
|
#
|
2018-04-23 11:29:17 +02:00
|
|
|
# id :bigint(8) not null, primary key
|
2017-07-13 22:15:32 +02:00
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
2017-07-18 16:25:40 +02:00
|
|
|
require 'webpush'
|
|
|
|
|
2017-07-13 22:15:32 +02:00
|
|
|
class Web::PushSubscription < ApplicationRecord
|
|
|
|
has_one :session_activation
|
|
|
|
|
|
|
|
def push(notification)
|
2017-09-01 13:35:23 +02:00
|
|
|
I18n.with_locale(session_activation.user.locale || I18n.default_locale) do
|
|
|
|
push_payload(message_from(notification), 48.hours.seconds)
|
|
|
|
end
|
2017-07-13 22:15:32 +02:00
|
|
|
end
|
|
|
|
|
2017-07-19 22:37:52 +02:00
|
|
|
def pushable?(notification)
|
2017-11-17 02:06:26 +01:00
|
|
|
data&.key?('alerts') && data['alerts'][notification.type.to_s]
|
2017-07-19 22:37:52 +02:00
|
|
|
end
|
|
|
|
|
2017-07-13 22:15:32 +02:00
|
|
|
def as_payload
|
2017-09-01 13:35:23 +02:00
|
|
|
payload = { id: id, endpoint: endpoint }
|
2017-11-17 02:06:26 +01:00
|
|
|
payload[:alerts] = data['alerts'] if data&.key?('alerts')
|
2017-07-13 22:15:32 +02:00
|
|
|
payload
|
|
|
|
end
|
|
|
|
|
2017-09-01 13:35:23 +02:00
|
|
|
def access_token
|
|
|
|
find_or_create_access_token.token
|
2017-07-13 22:15:32 +02:00
|
|
|
end
|
|
|
|
|
2017-09-01 13:35:23 +02:00
|
|
|
private
|
2017-07-13 22:15:32 +02:00
|
|
|
|
2017-09-01 13:35:23 +02:00
|
|
|
def push_payload(message, ttl = 5.minutes.seconds)
|
|
|
|
# TODO: Make sure that the payload does not
|
|
|
|
# exceed 4KB - Webpush::PayloadTooLarge
|
2017-07-13 22:15:32 +02:00
|
|
|
|
|
|
|
Webpush.payload_send(
|
2017-09-01 13:35:23 +02:00
|
|
|
message: Oj.dump(message),
|
2017-07-13 22:15:32 +02:00
|
|
|
endpoint: endpoint,
|
|
|
|
p256dh: key_p256dh,
|
|
|
|
auth: key_auth,
|
2017-09-01 13:35:23 +02:00
|
|
|
ttl: ttl,
|
2017-07-13 22:15:32 +02:00
|
|
|
vapid: {
|
2017-09-02 16:50:42 +02:00
|
|
|
subject: "mailto:#{::Setting.site_contact_email}",
|
2017-07-13 22:15:32 +02:00
|
|
|
private_key: Rails.configuration.x.vapid_private_key,
|
|
|
|
public_key: Rails.configuration.x.vapid_public_key,
|
2017-09-01 13:35:23 +02:00
|
|
|
}
|
2017-07-13 22:15:32 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-09-01 13:35:23 +02:00
|
|
|
def message_from(notification)
|
|
|
|
serializable_resource = ActiveModelSerializers::SerializableResource.new(notification, serializer: Web::NotificationSerializer, scope: self, scope_name: :current_push_subscription)
|
|
|
|
serializable_resource.as_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_or_create_access_token
|
2017-07-13 22:15:32 +02:00
|
|
|
Doorkeeper::AccessToken.find_or_create_for(
|
|
|
|
Doorkeeper::Application.find_by(superapp: true),
|
2017-09-01 13:35:23 +02:00
|
|
|
session_activation.user_id,
|
2017-07-13 22:15:32 +02:00
|
|
|
Doorkeeper::OAuth::Scopes.from_string('read write follow'),
|
|
|
|
Doorkeeper.configuration.access_token_expires_in,
|
|
|
|
Doorkeeper.configuration.refresh_token_enabled?
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|