-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
comment_controller.rb
229 lines (200 loc) · 6.68 KB
/
comment_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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
class CommentController < ApplicationController
include CommentHelper
respond_to :html, :xml, :json
before_action :require_user, only: %i(create update delete)
def index
@pagy, comments = pagy(Comment.joins(:node, :user)
.order('timestamp DESC')
.where('node.status = ?', 1), items: 30)
@normal_comments = comments.where('comments.status = 1')
if logged_in_as(%w(admin moderator))
@moderated_comments = comments.where('comments.status = 4')
end
render template: 'comments/index'
end
# handle some errors!!!!!!
# create node comments
def create
@node = Node.find params[:id]
@body = params[:body]
@user = current_user
begin
@comment = create_comment(@node, @user, @body)
if params[:reply_to].present?
@comment.reply_to = params[:reply_to].to_i
@comment.save
end
@comment_count = @node.comments.published.count
respond_to do |format|
@answer_id = 0
format.js do
render 'comments/create'
end
format.html do
if request.xhr?
render partial: 'notes/comment', locals: { comment: @comment }
else
tagnames = @node.tagnames.map do |tagname|
"<a href='/subscribe/tag/#{tagname}'>#{tagname}</a>"
end
tagnames = tagnames.join(', ')
tagnames = "Click to subscribe to updates on these tags or topics: #{tagnames}" unless tagnames.empty?
flash[:notice] = "Comment posted.#{tagnames}"
redirect_to "#{@node.path}#last" # to last comment
end
end
end
rescue CommentError
flash.now[:error] = 'The comment could not be saved.'
render plain: 'failure', status: :bad_request
end
end
def create_by_token
@node = Node.find params[:id]
@user = User.find_by(username: params[:username])
@body = params[:body]
@token = request.headers["HTTP_TOKEN"]
if @user && @user.token == @token
begin
# The create_comment is a function that has been defined inside the
# CommentHelper module inside app/helpers/comment_helper.rb and can be
# used in here because the module was `include`d right at the beginning
@comment = create_comment(@node, @user, @body)
respond_to do |format|
format.all { head :created }
end
rescue CommentError
respond_to do |format|
format.all { head :bad_request }
end
end
else
respond_to do |format|
format.all { head :unauthorized }
end
end
end
def update
@comment = Comment.find params[:id]
comments_node_and_path
if @comment.uid == current_user.uid
# should abstract ".comment" to ".body" for future migration to native db
@comment.comment = params[:body]
if @comment.save
flash[:notice] = 'Comment updated.'
redirect_to "#{@path}?_=#{Time.now.to_i}"
else
flash[:error] = 'The comment could not be updated.'
redirect_to @path
end
else
flash[:error] = 'Only the author of the comment can edit it.'
redirect_to @path
end
end
def delete
@comment = Comment.find params[:id]
comments_node_and_path
if current_user.uid == @node.uid ||
@comment.uid == current_user.uid ||
logged_in_as(%w(admin moderator))
if @comment.destroy
@comment_count = @node.comments.published.count
respond_with do |format|
if params[:type] && params[:type] == 'question'
@answer_id = @comment.aid
format.js { render 'comments/delete.js.erb' }
else
format.html do
if request.xhr?
render json: { comment_count: @comment_count }
else
flash[:notice] = 'Comment deleted.'
redirect_to "/#{@node.path}"
end
end
end
end
else
flash[:error] = 'The comment could not be deleted.'
render plain: 'failure'
end
else
prompt_login 'Only the comment or post author can delete this comment'
end
end
def like_comment
@comment_id = params["comment_id"].to_i
@user_id = params["user_id"].to_i
@emoji_type = params["emoji_type"]
comment = Comment.where(cid: @comment_id).first
like = comment.likes.where(user_id: @user_id, emoji_type: @emoji_type)
@is_liked = like.size.positive?
if like.size.positive?
like.first.destroy
@notification = "Removed '#{@emoji_type&.titleize}' Reaction."
else
comment.likes.create(user_id: @user_id, emoji_type: @emoji_type)
@notification = "Reacted with '#{@emoji_type&.titleize}' to Comment."
end
# select likes from users that aren't banned (status = 0)
@likes = comment.likes.joins(:user).select(:emoji_type, :status).where("emoji_type IS NOT NULL").where("status != 0").group(:emoji_type).size
@user_reactions_map = comment.user_reactions_map
respond_with do |format|
format.js do
render template: 'comments/like_comment'
end
end
end
def react_create
@node = Node.find params[:id]
@body = params[:body]
@user = current_user
begin
@comment = create_comment(@node, @user, @body)
if params[:reply_to].present?
@comment.reply_to = params[:reply_to].to_i
@comment.save
end
new_comment = helpers.get_react_comments([@comment])
render json: { comment: new_comment }
rescue CommentError
flash.now[:error] = 'The comment could not be saved.'
render plain: 'failure', status: :bad_request
end
end
def react_delete
@comment = Comment.find params[:id]
comments_node_and_path
if current_user.uid == @node.uid ||
@comment.uid == current_user.uid ||
logged_in_as(%w(admin moderator))
if @comment.destroy
render json: { success: true }
else
flash[:error] = 'The comment could not be deleted.'
render plain: 'failure'
end
else
prompt_login 'Only the comment or post author can delete this comment'
end
end
def react_update
@comment = Comment.find params[:id]
comments_node_and_path
if @comment.uid == current_user.uid
# should abstract ".comment" to ".body" for future migration to native db
@comment.comment = params[:body]
if @comment.save
new_comment = helpers.get_react_comments([@comment])
render json: { comment: new_comment }
else
flash[:error] = 'The comment could not be updated.'
redirect_to @path
end
else
flash[:error] = 'Only the author of the comment can edit it.'
redirect_to @path
end
end
end