docbrown/app/controllers/api/v0/reactions_controller.rb
rhymes e588fa7ece Code cleanups (#659)
* Initial automatic cleanup with rubocop

* Fix syntax error introduced by rubocop

* Cleanup seeds file

* Cleanup lib folder

* Exclude bin folder because it contains auto generated files

* Make Rubocop a little bit more chatty

* Block length should not include comments in the count

* Cleanup config folder

* Cleanup specs

* Updated Rubocop version and generated a todo file

* Fix broken ArticlesApi spec

* Fix tests

* Restored rubocop pre-commit hook
2018-08-07 11:00:13 -04:00

43 lines
1.2 KiB
Ruby

module Api
module V0
class ReactionsController < ApplicationController
skip_before_action :verify_authenticity_token
def create
@user = valid_user
unless @user
render json: { message: "invalid_user" }, status: 422
return
end
Rails.cache.delete "count_for_reactable-#{params[:reactable_type]}-#{params[:reactable_id]}"
@reaction = Reaction.create(
user_id: @user.id,
reactable_id: params[:reactable_id],
reactable_type: params[:reactable_type],
category: params[:category] || "like",
)
render json: { reaction: @reaction.to_json }
end
def onboarding
verify_authenticity_token
reactable_ids = JSON.parse(params[:articles]).map { |article| article["id"] }
reactable_ids.each do |article_id|
Reaction.delay.create(
user_id: current_user.id,
reactable_id: article_id,
reactable_type: "Article",
category: "readinglist",
)
end
end
private
def valid_user
user = User.find_by_secret(params[:key])
user = nil if !user.has_role?(:super_admin)
user
end
end
end
end