docbrown/app/controllers/devices_controller.rb
Fernando Valverde cf3bde4259
Add mobile push notifications to Forem (#12419)
* First commit with iOS PN working

* RPush cleanup worker + unique jobs config

* Remove rpush tables from schema.rb

* PR feedback

* Feature flag and test for route

* Tests and feature flag PushNotification ::Send

* Update app/controllers/devices_controller.rb

Co-authored-by: Michael Kohl <citizen428@dev.to>

* Update spec/routing/devices_routes_spec.rb

Co-authored-by: Michael Kohl <citizen428@dev.to>

* Update spec/services/push_notifications/send_spec.rb

Co-authored-by: Michael Kohl <citizen428@dev.to>

* PR feedback

* Set Rpush driver and url

* More PR feedback

* Apply suggestions from code review

Co-authored-by: Jamie Gaskins <jgaskins@gmail.com>

* PR feedback from Rhymes

* Don’t double render

* Sure

Co-authored-by: Josh Puetz <hi@joshpuetz.com>
Co-authored-by: Josh Puetz <josh@dev.to>
Co-authored-by: Michael Kohl <citizen428@dev.to>
Co-authored-by: Jamie Gaskins <jgaskins@gmail.com>
2021-03-12 14:08:18 -06:00

45 lines
1.1 KiB
Ruby

class DevicesController < ApplicationController
# Devices can only be created for authenticated users.
# This replaces the Authenticated Users Pusher Beams solution.
# See: https://github.com/forem/forem/pull/12419/files#r563906038
before_action :authenticate_user!, only: [:create]
rescue_from ActiveRecord::ActiveRecordError do |exc|
render json: { error: exc.message, status: 422 }, status: :unprocessable_entity
end
def create
device = Device.find_or_create_by(device_params)
if device.persisted?
head :ok
else
render json: { error: device.errors_as_sentence }, status: :bad_request
end
end
def destroy
device = Device.find_by(device_params)
unless device
render json: { error: "Not Found", status: 404 }, status: :not_found
return
end
device&.destroy
if device&.destroyed?
head :no_content
else
render json: { error: device.errors_as_sentence }, status: :bad_request
end
end
private
def device_params
{
user: current_user,
token: params[:token],
platform: params[:platform],
app_bundle: params[:app_bundle]
}
end
end