- Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathprotips_controller.rb
212 lines (180 loc) · 5.29 KB
/
protips_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
classProtipsController < ApplicationController
before_action:store_location
before_action:require_login,only: [:new,:create,:edit,:update]
defhome
redirect_to(trending_url)ifsigned_in?
@protips=Protip.all_time_popular + Protip.recently_most_viewed(20)
protips_store_data
end
defindex
order_by=(params[:order_by] ||= :score)
@protips=Protip.
includes(:user).
visible_to(current_user).
order({order_by=>:desc}).
page(params[:page])
ifparams[:order_by] == :score
@protips=@protips.where('likes_count > 2')
end
ifparams[:topic]
tags=Category::children(params[:topic].downcase)
tags=params[:topic].downcaseiftags.empty?
@protips=@protips.with_any_tagged(tags)
end
protips_store_data
end
defprotips_store_data
data={
protips: {items: serialize(@protips)},
}
ifcurrent_user
hearted_protips=current_user.likes.
where(likable_id: @protips.map(&:id)).
pluck(:likable_id).
map{|id| dom_id(Protip,id)}
data[:hearts]={
items: hearted_protips,
}
end
store_data(data)
end
defspam
@protips=Protip.spam.pageparams[:page]
renderaction: 'index'
end
defmark_spam
@protip=Protip.find_by_public_id!(params[:protip_id])
@protip.user.bad_user!
Rails.cache.clear# TODO: This is a little excessive
flash[:notice]="Marked as spam"
redirect_toslug_protips_url(id: @protip.public_id,slug: @protip.slug)
end
defshow
return(@protip=Protip.random.first)ifparams[:id] == 'random'
@protip=Protip.includes(:comments).visible_to(current_user).find_by_public_id!(params[:id])
@comments=@protip.comments.visible_to(current_user)
data={
currentProtip: {item: serialize(@protip)},
comments: {items: serialize(@comments)}
}
ifcurrent_user
hearted_protips=current_user.likes.
where(likable_id: @protip.id).
pluck(:likable_id).
map{|id| dom_id(Protip,id)}
hearted_comments=current_user.likes.where(
likable_id: @comments.map(&:id)
).pluck(:likable_id).map{|id| dom_id(Comment,id)}
data[:hearts]={
items: hearted_protips + hearted_comments,
}
end
store_data(data)
respond_todo |format|
format.htmldo
seo_url=slug_protips_url(id: @protip.public_id,slug: @protip.slug)
returnredirect_to(seo_url,status: 301)unlessslugs_match?
update_view_count(@protip)
fresh_when(etag_key_for_protip)
end
format.json{render(json: @protip)}
end
end
defnew
@protip=Protip.new
end
defedit
@protip=Protip.find_by_public_id!(params[:id])
returnhead(:forbidden)unlesscurrent_user.can_edit?(@protip)
renderaction: 'new'
end
defupdate
@protip=Protip.find_by_public_id!(params[:id])
returnhead(:forbidden)unlesscurrent_user.can_edit?(@protip)
@protip.assign_attributes(protip_params)
add_spam_fields(@protip)
if !captcha_valid_user?(params["g-recaptcha-response"],remote_ip)
flash.now[:notice]="Let us know if you're human below :D"
renderaction: 'new'
return
end
ifspam?
@protip.bad_content=true
current_user.update!(bad_user: true)
end
if@protip.save
redirect_toprotip_url(@protip)
else
renderaction: 'new'
end
end
defcreate
@protip=Protip.new(protip_params)
@protip.user=current_user
add_spam_fields(@protip)
if !captcha_valid_user?(params["g-recaptcha-response"],remote_ip)
flash.now[:notice]="Let us know if you're human below :D"
renderaction: 'new'
return
end
ifspam?
@protip.bad_content=true
current_user.update!(bad_user: true)
end
if@protip.save
redirect_toprotip_url(@protip)
else
renderaction: 'new'
end
end
defdestroy
@protip=Protip.find_by_public_id!(params[:id])
returnhead(:forbidden)unlesscurrent_user.can_edit?(@protip)
@protip.destroy
redirect_toprofile_protips_url(username: @protip.user.username,anchor: 'protips')
end
protected
defadd_spam_fields(article)
article.assign_attributes(
user_agent: request.user_agent,
user_ip: remote_ip,
referrer: request.referer,
)
end
defslugs_match?
params[:slug] == @protip.slug
end
defprotip_params
params.require(:protip).permit(:editable_tags,:body,:title)
end
defupdate_view_count(protip)
if !browser.bot? && browser.known?
recently_viewed=cookies[:viewd_protips].to_s.split(':')
if !recently_viewed.include?(protip.public_id)
protip.increment_view_count!
recently_viewed << protip.public_id
end
cookies[:viewd_protips]={
value: recently_viewed.join(':'),
expires: 10.minutes.from_now
}
end
end
defetag_key_for_protip
{
etag: [@protip,current_user,'v2'],
last_modified: @protip.updated_at.utc,
public: false
}
end
defspam?
flags=Spaminator.new.protip_flags(@protip)
ifflags.any?
logger.info"[SPAM BLOCK] \"#{@protip.title}\"#{flags.inspect}"
true
else
logger.info"[SPAM ALLOW] \"#{@protip.title}\""
false
end
end
end