2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
class PostVotesController < ApplicationController
|
2023-06-12 12:42:40 -04:00
|
|
|
before_action :member_only
|
2024-12-08 22:28:31 -05:00
|
|
|
before_action :moderator_only, only: %i[index lock]
|
2022-11-28 10:21:40 -05:00
|
|
|
before_action :admin_only, only: [:delete]
|
2024-12-08 22:28:31 -05:00
|
|
|
before_action :ensure_lockdown_disabled
|
2018-04-02 13:51:26 -04:00
|
|
|
skip_before_action :api_check
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
def create
|
2010-10-27 20:16:43 -04:00
|
|
|
@post = Post.find(params[:post_id])
|
2019-04-10 23:08:36 -04:00
|
|
|
@post_vote = VoteManager.vote!(post: @post, user: CurrentUser.user, score: params[:score])
|
2023-04-05 03:34:00 -04:00
|
|
|
if @post_vote == :need_unvote && !params[:no_unvote].to_s.truthy?
|
2019-04-10 23:08:36 -04:00
|
|
|
VoteManager.unvote!(post: @post, user: CurrentUser.user)
|
|
|
|
end
|
2020-02-21 23:01:23 -05:00
|
|
|
render json: {score: @post.score, up: @post.up_score, down: @post.down_score, our_score: @post_vote != :need_unvote ? @post_vote.score : 0}
|
2022-10-08 06:04:17 -04:00
|
|
|
rescue UserVote::Error, ActiveRecord::RecordInvalid => x
|
2020-06-20 06:56:48 -04:00
|
|
|
render_expected_error(422, x)
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|
2016-03-14 19:52:49 -04:00
|
|
|
|
2016-10-04 17:42:48 -04:00
|
|
|
def destroy
|
|
|
|
@post = Post.find(params[:post_id])
|
2019-03-31 16:25:55 -04:00
|
|
|
VoteManager.unvote!(post: @post, user: CurrentUser.user)
|
2022-10-08 06:04:17 -04:00
|
|
|
rescue UserVote::Error => x
|
2020-06-20 06:56:48 -04:00
|
|
|
render_expected_error(422, x)
|
2016-10-04 17:42:48 -04:00
|
|
|
end
|
2019-04-06 16:13:50 -04:00
|
|
|
|
|
|
|
def index
|
2021-10-30 17:03:27 -04:00
|
|
|
@post_votes = PostVote.includes(:user).search(search_params).paginate(params[:page], limit: 100)
|
2019-04-06 16:13:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def lock
|
2023-08-01 13:32:24 -04:00
|
|
|
ids = params[:ids].split(",")
|
2019-04-06 16:13:50 -04:00
|
|
|
|
|
|
|
ids.each do |id|
|
|
|
|
VoteManager.lock!(id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete
|
2023-08-01 13:32:24 -04:00
|
|
|
ids = params[:ids].split(",")
|
2019-04-06 16:13:50 -04:00
|
|
|
|
|
|
|
ids.each do |id|
|
|
|
|
VoteManager.admin_unvote!(id)
|
|
|
|
end
|
|
|
|
end
|
2022-12-23 10:35:37 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def search_params
|
2023-08-03 16:01:53 -04:00
|
|
|
permitted_params = %i[post_id user_name user_id post_creator_id post_creator_name timeframe score]
|
2022-12-23 10:35:37 -05:00
|
|
|
permitted_params += %i[user_ip_addr duplicates_only order] if CurrentUser.is_admin?
|
|
|
|
permit_search_params permitted_params
|
|
|
|
end
|
2024-12-08 22:28:31 -05:00
|
|
|
|
|
|
|
def ensure_lockdown_disabled
|
|
|
|
access_denied if Security::Lockdown.votes_disabled? && !CurrentUser.is_staff?
|
|
|
|
end
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|