forked from berserker/microblog
parent
1992575d57
commit
3f075c7794
13 changed files with 112 additions and 2 deletions
@ -0,0 +1,18 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
|
||||||
|
class Api::V1::DevicesController < ApiController |
||||||
|
before_action -> { doorkeeper_authorize! :read } |
||||||
|
before_action :require_user! |
||||||
|
|
||||||
|
respond_to :json |
||||||
|
|
||||||
|
def register |
||||||
|
Device.where(account: current_account, registration_id: params[:registration_id]).first_or_create!(account: current_account, registration_id: params[:registration_id]) |
||||||
|
render_empty |
||||||
|
end |
||||||
|
|
||||||
|
def unregister |
||||||
|
Device.where(account: current_account, registration_id: params[:registration_id]).delete_all |
||||||
|
render_empty |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,7 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
|
||||||
|
class Device < ApplicationRecord |
||||||
|
belongs_to :account |
||||||
|
|
||||||
|
validates :account, :registration_id, presence: true |
||||||
|
end |
@ -0,0 +1,28 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
|
||||||
|
class SendPushNotificationService < BaseService |
||||||
|
def call(notification) |
||||||
|
return if ENV['FCM_API_KEY'].blank? |
||||||
|
|
||||||
|
devices = Device.where(account: notification.account).pluck(:registration_id) |
||||||
|
fcm = FCM.new(ENV['FCM_API_KEY']) |
||||||
|
|
||||||
|
response = fcm.send(devices, data: { notification_id: notification.id }, collapse_key: :notifications, priority: :high) |
||||||
|
handle_response(response) |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def handle_response(response) |
||||||
|
update_canonical_ids(response[:canonical_ids]) if response[:canonical_ids] |
||||||
|
remove_bad_ids(response[:not_registered_ids]) if response[:not_registered_ids] |
||||||
|
end |
||||||
|
|
||||||
|
def update_canonical_ids(ids) |
||||||
|
ids.each { |pair| Device.find_by(registration_id: pair[:old]).update(registration_id: pair[:new]) } |
||||||
|
end |
||||||
|
|
||||||
|
def remove_bad_ids(bad_ids) |
||||||
|
Device.where(registration_id: bad_ids).delete_all |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,11 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
|
||||||
|
class PushNotificationWorker |
||||||
|
include Sidekiq::Worker |
||||||
|
|
||||||
|
def perform(notification_id) |
||||||
|
SendPushNotificationService.new.call(Notification.find(notification_id)) |
||||||
|
rescue ActiveRecord::RecordNotFound |
||||||
|
true |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,13 @@ |
|||||||
|
class CreateDevices < ActiveRecord::Migration[5.0] |
||||||
|
def change |
||||||
|
create_table :devices do |t| |
||||||
|
t.integer :account_id, null: false |
||||||
|
t.string :registration_id, null: false, default: '' |
||||||
|
|
||||||
|
t.timestamps |
||||||
|
end |
||||||
|
|
||||||
|
add_index :devices, :registration_id |
||||||
|
add_index :devices, :account_id |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,3 @@ |
|||||||
|
Fabricator(:device) do |
||||||
|
registration_id "12345678" |
||||||
|
end |
@ -1,3 +1,3 @@ |
|||||||
Fabricator(:domain_block) do |
Fabricator(:domain_block) do |
||||||
domain "MyString" |
domain "example.com" |
||||||
end |
end |
||||||
|
@ -0,0 +1,5 @@ |
|||||||
|
require 'rails_helper' |
||||||
|
|
||||||
|
RSpec.describe Device, type: :model do |
||||||
|
|
||||||
|
end |
Loading…
Reference in new issue