blob: 74c5c5f9e5fef307a55e23f7e80583f7a4bcdfcd (
plain)
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
75
76
77
78
79
80
|
# frozen_string_literal: true
class Api::V2::InteractionsController < Api::V2::ApiController
skip_authorization_check
before_action :filter_banned_users
before_action :require_user
before_action :load_interactable, only: [:vote, :fave, :hide]
before_action :load_many_interactables, only: [:interacted]
def vote
case params[:value]
when 'up'
@engine.vote!(:up)
when 'down'
@engine.vote!(:down)
when 'false'
@engine.unvote!
end
@interactable.update_index
render json: return_values(@interactable)
end
def fave
@engine.fave!(params[:value] == 'true')
@interactable.update_index
render json: return_values(@interactable)
end
def hide
@engine.hide!(params[:value] == 'true')
@interactable.update_index
render json: return_values(@interactable)
end
def interacted
render json: (@interactions || []).to_json
end
private
def load_interactable
@interactable = Post.find_by hidden_from_users: false, id: params[:id]
head(:not_found) if @interactable.nil?
@engine = Booru::VotingEngine.new(current_user, @interactable)
end
def load_many_interactables
@interactable_ids = (params[:ids].split(',')
.map { |x| Integer(x) rescue false }
.compact
.uniq[0..49] rescue nil)
if @interactable_ids
@interactions = PostQuery.interactions(@interactable_ids, current_user.id)
else
head :bad_request
end
end
#
# Returns the values that should be sent back to whoever just did some favin'/votin'
# @param interactable [Interactable] the thing to get data from
#
# @return [Hash] a ready-for-JSON serialized set of values
def return_values(interactable)
{
score: interactable.score,
favourites: interactable.faves_count,
upvotes: interactable.upvotes_count,
downvotes: interactable.downvotes_count,
votes: -1
}
end
end
|