/
votes_controller.rb
74 lines (53 loc) · 1.96 KB
/
votes_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true
module DiscourseVoting
class VotesController < ::ApplicationController
before_action :ensure_logged_in
def who
params.require(:topic_id)
topic = Topic.find(params[:topic_id].to_i)
guardian.ensure_can_see!(topic)
render json: MultiJson.dump(who_voted(topic))
end
def vote
topic_id = params["topic_id"].to_i
topic = Topic.find_by(id: topic_id)
raise Discourse::InvalidAccess if !topic.can_vote? || topic.user_voted?(current_user)
guardian.ensure_can_see!(topic)
voted = false
unless current_user.reached_voting_limit?
DiscourseVoting::Vote.find_or_create_by(user: current_user, topic_id: topic_id)
topic.update_vote_count
voted = true
end
obj = {
can_vote: !current_user.reached_voting_limit?,
vote_limit: current_user.vote_limit,
vote_count: topic.topic_vote_count&.votes_count&.to_i,
who_voted: who_voted(topic),
alert: current_user.alert_low_votes?,
votes_left: [(current_user.vote_limit - current_user.vote_count), 0].max
}
render json: obj, status: voted ? 200 : 403
end
def unvote
topic_id = params["topic_id"].to_i
topic = Topic.find_by(id: topic_id)
guardian.ensure_can_see!(topic)
DiscourseVoting::Vote.destroy_by(user: current_user, topic_id: topic_id)
topic.update_vote_count
obj = {
can_vote: !current_user.reached_voting_limit?,
vote_limit: current_user.vote_limit,
vote_count: topic.topic_vote_count&.votes_count&.to_i,
who_voted: who_voted(topic),
votes_left: [(current_user.vote_limit - current_user.vote_count), 0].max
}
render json: obj
end
protected
def who_voted(topic)
return nil unless SiteSetting.voting_show_who_voted
ActiveModel::ArraySerializer.new(topic.who_voted, scope: guardian, each_serializer: BasicUserSerializer)
end
end
end