- Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathcomments_controller.rb
100 lines (85 loc) · 2.96 KB
/
comments_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
classCommentsController < ApplicationController
before_action:require_login,only: [:create,:destroy]
if !Rails.env.test?
invisible_captchaonly: [:create],on_spam: :on_spam_detected
end
defindex
@comments=Comment.visible_to(current_user).order(created_at: :desc)
respond_todo |format|
format.html{
# TODO: do we need this check?
returnhead(:forbidden)unlessadmin?
@comments=@comments.on_protips.page(params[:page])
}
format.json{
@comments=@comments.
where(article_id: params[:article_id]).
limit(10)
@comments=@comments.where('created_at < ?',Time.at(params[:before].to_i))unlessparams[:before].blank?
}
end
end
defspam
returnhead(:forbidden)unlessadmin?
@comments=Comment.order(created_at: :desc).where("body ILike '%<a %'").page(params[:page])
renderaction: 'index'
end
defshow
@comment=Comment.find(params[:id])
end
defcreate
ifComment.where(user: current_user).find_by('created_at > ?',ENV.fetch('COMMENTS_THROTTLE',3).to_i.minutes.ago)
flash[:error]="You're posting comments too often, please wait a minute and try again"
redirect_to_protip_comment_form
return
end
@article=Article.find(comment_params[:article_id])
@comment=Comment.new(comment_params)
@comment.user=current_user
if !@comment.save
flash[:error]="Your comment did not save. #{@comment.errors.full_messages.join(' ')}"
flash[:data]=@comment.body
redirect_to_protip_comment_form
else
@article.subscribe!(current_user)
notify_comment_added!
respond_todo |format|
format.html{redirect_tourl_for(@comment.url_params)}
format.json{renderjson: json}
end
end
end
defdestroy
@comment=Comment.find(params[:id])
returnhead(:forbidden)unlesscurrent_user.can_edit?(@comment)
@comment.destroy
redirect_to_protip_comment_form
end
protected
defredirect_to_protip_comment(comment)
redirect_to"#{request.referer}##{comment.dom_id}"
end
defredirect_to_protip_comment_form
redirect_to"#{request.referer}#new-comment"
end
defcomment_params
params.require(:comment).permit(:body,:article_id)
end
defnotify_comment_added!
# TODO: this won't work for large comments, we should just push the comment id
json=render_to_string(template: 'comments/_comment.json.jbuilder',locals: {comment: @comment})
Notification.comment_added!(@article,json,params[:socket_id])
# TODO: move to job
email_recipients.eachdo |to|
logger.info(event: 'email-notify',email: to,comment: @comment.id)
CommentMailer.new_comment(to,@comment).deliver_now
end
end
defemail_recipients
User.where(id: (@article.subscribers - [@comment.user_id]))
end
defon_spam_detected
@article=Article.find(comment_params[:article_id])
redirect_toseo_protip_path(@article)
end
end