docbrown/app/controllers/reactions_controller.rb
Andy Zhao acd3b92b32
Add sink user service and specs for mass vomits (#6006)
* Add sink user service and specs for mass vomits

* Use new score calculation

* Lower articles' scores instead and make async

* Add lower score functionality to controllers

* Remove unused variable

* Use correct var and add missing arg

* Remove unused status

* Add composite index for reactable_type and ID

* Use alias instead of additional boolean

* Use medium priority for sink worker

* Log to honeybadger and not logs

* Log the error and not a string
2020-02-17 10:29:50 -05:00

89 lines
3.3 KiB
Ruby

class ReactionsController < ApplicationController
before_action :set_cache_control_headers, only: [:index], unless: -> { current_user }
after_action :verify_authorized
def index
skip_authorization
if params[:article_id]
id = params[:article_id]
reactions = if session_current_user_id.present?
Reaction.where(reactable_id: id,
reactable_type: "Article",
user_id: session_current_user_id).
where("points > ?", 0)
else
[]
end
render json:
{
current_user: { id: session_current_user_id },
article_reaction_counts: Reaction.count_for_article(id),
reactions: reactions
}.to_json
else
comments = Comment.where(
commentable_id: params[:commentable_id],
commentable_type: params[:commentable_type],
).select(%i[id positive_reactions_count])
comment_ids = comments.map(&:id)
reaction_counts = comments.map { |c| { id: c.id, count: c.positive_reactions_count } }
reactions = session_current_user_id ? cached_user_positive_reactions(current_user).where(reactable_id: comment_ids) : []
render json:
{
current_user: { id: session_current_user_id },
positive_reaction_counts: reaction_counts,
reactions: reactions
}.to_json
end
set_surrogate_key_header params.to_s unless session_current_user_id
end
def create
authorize Reaction
Rails.cache.delete "count_for_reactable-#{params[:reactable_type]}-#{params[:reactable_id]}"
category = params[:category] || "like"
reaction = Reaction.where(
user_id: current_user.id,
reactable_id: params[:reactable_id],
reactable_type: params[:reactable_type],
category: category,
).first
if reaction
current_user.touch
reaction.destroy
Moderator::SinkArticles.call(reaction.user_id) if vomit_reaction_on_user?(reaction)
Notification.send_reaction_notification_without_delay(reaction, reaction.reactable.user)
Notification.send_reaction_notification_without_delay(reaction, reaction.reactable.organization) if organization_article?(reaction)
@result = "destroy"
else
reaction = Reaction.create!(
user_id: current_user.id,
reactable_id: params[:reactable_id],
reactable_type: params[:reactable_type],
category: category,
)
@result = "create"
Moderator::SinkArticles.call(reaction.user_id) if vomit_reaction_on_user?(reaction)
Notification.send_reaction_notification(reaction, reaction.reactable.user)
Notification.send_reaction_notification(reaction, reaction.reactable.organization) if organization_article?(reaction)
end
render json: { result: @result, category: category }
end
def cached_user_positive_reactions(user)
Rails.cache.fetch("cached_user_reactions-#{user.id}-#{user.updated_at}", expires_in: 24.hours) do
Reaction.where(user_id: user.id).
where("points > ?", 0)
end
end
private
def organization_article?(reaction)
reaction.reactable_type == "Article" && reaction.reactable.organization.present?
end
def vomit_reaction_on_user?(reaction)
reaction.reactable_type == "User" && reaction.category == "vomit"
end
end