* Remove extraneous comment (see 9361d2 and 5c18f8) * Flexible, multiple reaction types * Fix reaction counts * Re-use svg for active state for now * Update yml, update spec * Possible temp fix for failing tests * Colorize reaction icon svgs * Reaction engagement above post title * Index reactions engagements (for logged-out) * Maybe readinglist is special * Try using crayons' dropdown as a drawer? * readinglist isn't really public now * feat: update the styles for the reaction drawer * Grey background highlight, turn off border/shadow * Read our feature flag docs, saw this was recommended * Missed fifth emoji: party/tada * Fix JS test errors * Update test with tada * Suppress flashing engagements when no public reactions * Liberate jump-to-comments from unicorn replacement * 'Add reaction' on tooltip * Don't show reaction emoji on index unless it's been used * rubocop * Update heart+ total count when toggling * Do not include 'readinglist' in drawer/public counts * Fix semi-public readinglist so that icon is badged for current user * Tweak heart-plus svg * Style tweak: border on active reaction * Show reacted icon on drawer trigger for 1.5 sec * Tweak styles for engagements bar * Style tweaks for multiple engagements (#index) * Trying to get size working through crayons/inline_svg * Sparkle hearts * Restore unicorn * Make heart-plus-active work when user has an active reaction * Try 'hoverdown' a dropdown that activates with hover * Long touch? * Tap *outside* the drawer to close * Mobile reaction drawer is also supposed to be columns * More reaction count cleanup * Final emoticons maybe? * Fix reaction bug when feature disabled * Remove readinglist from public reaction counts * Update specs for new reaction categories * Shuffle makes specs flaky * Order does not matter * rubocop * Update to preserve readinglist analytics * Shuffle makes specs flaky * Fix flickering images, remove icon highlight for now * Don't update total for readinglist * reactions_by_user_id * Try renaming this observer function * Try unid ids for SVGs * Remove local test file * Simplistic test for the unique svg transform * Fix javascript for current SVG * Signifcant string literals in this case, rubocop * Use the right expected output Co-authored-by: Ridhwana <ridhwana.khan16@gmail.com>
105 lines
3.3 KiB
Ruby
105 lines
3.3 KiB
Ruby
class ReactionsController < ApplicationController
|
|
before_action :set_cache_control_headers, only: [:index], unless: -> { current_user }
|
|
before_action :authorize_for_reaction, :check_limit, only: [:create]
|
|
after_action :verify_authorized
|
|
|
|
NEGATIVE_CATEGORIES = %w[thumbsdown vomit].freeze
|
|
MODERATION_CATEGORIES = %w[thumbsup thumbsdown vomit].freeze
|
|
|
|
def index
|
|
skip_authorization
|
|
|
|
if params[:article_id]
|
|
id = params[:article_id]
|
|
|
|
reactions = reactions_by_user_id(session_current_user_id) if session_current_user_id
|
|
reactions ||= Reaction.none
|
|
|
|
result = { article_reaction_counts: Reaction.count_for_article(id) }
|
|
else
|
|
comments = Comment
|
|
.where(commentable_id: params[:commentable_id], commentable_type: params[:commentable_type])
|
|
.select(%i[id public_reactions_count])
|
|
|
|
reaction_counts = comments.map do |comment|
|
|
{ id: comment.id, count: comment.public_reactions_count }
|
|
end
|
|
|
|
reactions = if session_current_user_id
|
|
comment_ids = reaction_counts.pluck(:id) # rubocop:disable Rails/PluckId
|
|
cached_user_public_comment_reactions(current_user, comment_ids)
|
|
else
|
|
Reaction.none
|
|
end
|
|
|
|
result = { public_reaction_counts: reaction_counts }
|
|
end
|
|
|
|
render json: {
|
|
current_user: { id: session_current_user_id },
|
|
reactions: reactions
|
|
}.merge(result).to_json
|
|
|
|
set_surrogate_key_header params.to_s unless session_current_user_id
|
|
end
|
|
|
|
# @todo Extract this method into a service class (or classes)
|
|
def create
|
|
remove_count_cache_key
|
|
|
|
result = ReactionHandler.toggle(params, current_user: current_user)
|
|
|
|
if result.success?
|
|
render json: { result: result.action, category: result.category }
|
|
else
|
|
render json: { error: result.errors_as_sentence, status: 422 }, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def cached_user_public_comment_reactions(user, comment_ids)
|
|
cache = Rails.cache.fetch("cached-user-#{user.id}-reaction-ids-#{user.public_reactions_count}",
|
|
expires_in: 24.hours) do
|
|
user.reactions.public_category.where(reactable_type: "Comment").each_with_object({}) do |r, h|
|
|
h[r.reactable_id] = r.attributes
|
|
end
|
|
end
|
|
cache.slice(*comment_ids).values
|
|
end
|
|
|
|
private
|
|
|
|
def check_limit
|
|
rate_limit!(:reaction_creation)
|
|
end
|
|
|
|
def authorize_for_reaction
|
|
# A present assumption is that who can give a reaction is not dependent on the reactable;
|
|
# however it is dependent on the category of the reaction.
|
|
policy_query = ReactionPolicy.policy_query_for(category: params[:category])
|
|
authorize(Reaction, policy_query)
|
|
end
|
|
|
|
def reactions_by_user_id(user_id = session_current_user_id)
|
|
id = params[:article_id]
|
|
|
|
public_reactions = Reaction.public_category.where(
|
|
reactable_id: id,
|
|
reactable_type: "Article",
|
|
user_id: user_id,
|
|
)
|
|
readinglist = Reaction.where(
|
|
reactable_id: id,
|
|
reactable_type: "Article",
|
|
category: "readinglist", user_id: user_id
|
|
)
|
|
|
|
public_reactions + readinglist
|
|
end
|
|
|
|
# TODO: should this move to toggle service? refactor?
|
|
def remove_count_cache_key
|
|
return unless params[:reactable_type] == "Article"
|
|
|
|
Rails.cache.delete "count_for_reactable-Article-#{params[:reactable_id]}"
|
|
end
|
|
end
|