-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
comment_controller.rb
161 lines (144 loc) · 4.66 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
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
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.to_s
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
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 plain: 'success'
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
else
comment.likes.create(user_id: @user_id, emoji_type: @emoji_type)
end
@likes = comment.likes.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
end