Class: CommentsController

Inherits:
ApplicationController show all
Defined in:
/var/apps/qpixel/app/controllers/comments_controller.rb

Overview

rubocop:disable Metrics/ClassLength Provides mainly web actions for using and making comments.

Instance Method Summary collapse

Methods inherited from ApplicationController

#dashboard, #keyboard_tools, #upload

Instance Method Details

#archive_threadObject



255
256
257
258
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 255

def archive_thread
  status = @comment_thread.update(archived: true, archived_by: current_user)
  restrict_thread_response(@comment_thread, status)
end

#createObject



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
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 75

def create
  body = params[:content]
  pings = check_for_pings(@comment_thread, body)

  @comment = Comment.new(post: @post, content: body, user: current_user,
                         comment_thread: @comment_thread, has_reference: false)

  status = @comment.save

  if status
    apply_pings(pings)
    @comment_thread.thread_follower.each do |follower|
      next if follower.user.same_as?(current_user)
      next if pings.include? follower.user_id

      thread_url = comment_thread_url(@comment_thread, host: @comment_thread.community.host)
      existing_notification = follower.user.notifications.where(is_read: false)
                                      .where('link LIKE ?', "#{thread_url}%")
      next if existing_notification.exists?

      follower.user.create_notification("There are new comments in a followed thread '#{@comment_thread.title}' " \
                                        "on the post '#{@comment.root.title}'",
                                        helpers.comment_link(@comment))
    end
  else
    flash[:danger] = @comment.errors.full_messages.join(', ')
  end

  if params[:inline] == 'true'
    redirect_to helpers.generic_share_link(@post, comment_id: status ? @comment.id : nil,
                                                  thread_id: @comment_thread.id)
  else
    redirect_to comment_thread_path(@comment_thread.id)
  end
end

#create_threadObject



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
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 32

def create_thread
  title = params[:title]
  unless title.present?
    title = helpers.generate_thread_title(params[:body])
  end

  body = params[:body]

  @comment_thread = CommentThread.new(title: helpers.strip_markdown(title, strip_leading_quote: true), post: @post)
  @comment = Comment.new(post: @post, content: body, user: current_user, comment_thread: @comment_thread)

  pings = check_for_pings(@comment_thread, body)

  success = ActiveRecord::Base.transaction do
    thread_success = @comment_thread.save
    comment_success = @comment.save
    full_success = thread_success && comment_success

    unless full_success
      raise ActiveRecord::Rollback
    end

    full_success
  end

  if success
    notification = "New comment thread on #{@comment.root.title}: #{@comment_thread.title}"

    NewThreadFollower.where(post: @post).each do |ntf|
      unless ntf.user.same_as?(current_user) || pings.include?(ntf.user_id)
        ntf.user.create_notification(notification, helpers.comment_link(@comment))
      end
      ThreadFollower.create(user: ntf.user, comment_thread: @comment_thread)
    end

    apply_pings(pings)
  else
    flash[:danger] = "Could not create comment thread: #{(@comment_thread.errors.full_messages \
                                                         + @comment.errors.full_messages).join(', ')}"
  end
  redirect_to helpers.generic_share_link(@post)
end

#delete_threadObject



260
261
262
263
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 260

def delete_thread
  status = @comment_thread.update(deleted: true, deleted_by: current_user)
  restrict_thread_response(@comment_thread, status)
end

#destroyObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 134

def destroy
  if @comment.update(deleted: true)
    @comment_thread = @comment.comment_thread

    unless current_user.same_as?(@comment.user)
      audit('comment_delete', @comment, "content <<#{@comment.content}>>")
    end

    respond_to do |format|
      format.html { redirect_to comment_thread_path(@comment_thread.id) }
      format.json { render json: { status: 'success' } }
    end
  else
    respond_to do |format|
      format.html do
        flash[:danger] = I18n.t('comments.errors.delete_comment_server_error')
        redirect_to comment_thread_path(@comment_thread.id)
      end
      format.json { render json: { status: 'failed' }, status: :internal_server_error }
    end
  end
end

#follow_threadObject



265
266
267
268
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 265

def follow_thread
  status = @comment_thread.add_follower(current_user)
  restrict_thread_response(@comment_thread, status)
end

#lock_threadObject



270
271
272
273
274
275
276
277
278
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 270

def lock_thread
  lu = nil
  unless params[:duration].blank?
    lu = params[:duration].to_i.days.from_now
  end

  status = @comment_thread.update(locked: true, locked_by: current_user, locked_until: lu)
  restrict_thread_response(@comment_thread, status)
end

#pingableObject



349
350
351
352
353
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 349

def pingable
  thread = params[:id] == '-1' ? CommentThread.new(post_id: params[:post]) : CommentThread.find(params[:id])
  users = User.where(id: thread.pingable)
  render json: users.to_h { |u| [u.username, u.id] }
end

#postObject



318
319
320
321
322
323
324
325
326
327
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 318

def post
  @post = Post.find(params[:post_id])
  @comment_threads = CommentThread.accessible_to(current_user, @post)
                                  .where(post: @post)
                                  .priority_order
  respond_to do |format|
    format.html { render layout: false }
    format.json { render json: @comment_threads }
  end
end

#post_followObject



329
330
331
332
333
334
335
336
337
338
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 329

def post_follow
  if NewThreadFollower.where(post: @post, user: current_user).none?
    NewThreadFollower.create(post: @post, user: current_user)
  end

  respond_to do |format|
    format.html { redirect_to post_path(@post) }
    format.json { render json: { status: 'success' } }
  end
end

#post_unfollowObject



340
341
342
343
344
345
346
347
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 340

def post_unfollow
  NewThreadFollower.where(post: @post, user: current_user).destroy_all

  respond_to do |format|
    format.html { redirect_to post_path(@post) }
    format.json { render json: { status: 'success' } }
  end
end

#showObject



180
181
182
183
184
185
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 180

def show
  respond_to do |format|
    format.html { render partial: 'comments/comment', locals: { comment: @comment } }
    format.json { render json: @comment }
  end
end

#threadObject



187
188
189
190
191
192
193
194
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 187

def thread
  if stale?(last_modified: @comment_thread.last_activity.utc)
    respond_to do |format|
      format.html { render 'comments/thread' }
      format.json { render json: @comment_thread }
    end
  end
end

#thread_contentObject



196
197
198
199
200
201
202
203
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 196

def thread_content
  if stale?(last_modified: @comment_thread.last_activity.utc)
    render partial: 'comment_threads/expanded',
           locals: { inline: params[:inline] == 'true',
                     show_deleted: params[:show_deleted_comments] == '1',
                     thread: @comment_thread }
  end
end

#thread_followersObject



205
206
207
208
209
210
211
212
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 205

def thread_followers
  @followers = ThreadFollower.where(comment_thread: @comment_thread).joins(:user, user: :community_user)
                             .includes(:user, user: [:community_user, :avatar_attachment])
  respond_to do |format|
    format.html { render layout: false }
    format.json { render json: @followers }
  end
end

#thread_renameObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 214

def thread_rename
  if @comment_thread.read_only? && !current_user.at_least_moderator?
    flash[:danger] = 'This thread has been locked.'
    redirect_to comment_thread_path(@comment_thread.id)
    return
  end

  orig_title = @comment_thread.title
  title = helpers.strip_markdown(params[:title], strip_leading_quote: true)

  if orig_title == title
    flash[:danger] = I18n.t('comments.errors.no_rename_thread_to_current_title')
    redirect_to comment_thread_path(@comment_thread.id)
    return
  end

  status = @comment_thread.update(title: title)

  if status
    # Comment is owned by System so regular users can't delete it. Without
    # this record, the title would be attributed to the thread creator,
    # which can be abused.
    log_msg = Comment.new(post: @post,
                          content: "Thread renamed from \"#{orig_title}\" to \"#{title}\" by @##{current_user.id}",
                          user: helpers.system_user,
                          comment_thread: @comment_thread,
                          has_reference: false)
    comment_status = log_msg.save
  end

  unless status
    flash[:danger] = I18n.t('comments.errors.rename_thread_generic')
  end

  unless comment_status
    flash[:danger] = I18n.t('comments.errors.comment_not_posted')
  end

  redirect_to comment_thread_path(@comment_thread.id)
end

#thread_unrestrictObject



304
305
306
307
308
309
310
311
312
313
314
315
316
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 304

def thread_unrestrict
  # TODO: remove this wrapper action entirely (callbacks need to be moved, routes assigned, etc)
  case params[:type]
  when 'lock'
    unlock_thread
  when 'archive'
    unarchive_thread
  when 'delete'
    undelete_thread
  else
    not_found!
  end
end

#unarchive_threadObject



280
281
282
283
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 280

def unarchive_thread
  status = @comment_thread.update(archived: false, archived_by: nil, ever_archived_before: true)
  restrict_thread_response(@comment_thread, status)
end

#undeleteObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 157

def undelete
  if @comment.update(deleted: false)
    @comment_thread = @comment.comment_thread

    unless current_user.same_as?(@comment.user)
      audit('comment_undelete', @comment, "content <<#{@comment.content}>>")
    end

    respond_to do |format|
      format.html { redirect_to comment_thread_path(@comment_thread.id) }
      format.json { render json: { status: 'success' } }
    end
  else
    respond_to do |format|
      format.html do
        flash[:danger] = I18n.t('comments.errors.undelete_comment_server_error')
        redirect_to comment_thread_path(@comment_thread.id)
      end
      format.json { render json: { status: 'failed' }, status: :internal_server_error }
    end
  end
end

#undelete_threadObject



285
286
287
288
289
290
291
292
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 285

def undelete_thread
  if @comment_thread.deleted_by.at_least_moderator? && !current_user.at_least_moderator?
    render json: { status: 'error', message: I18n.t('comments.errors.mod_only_undelete') }
    return
  end
  status = @comment_thread.update(deleted: false, deleted_by: nil)
  restrict_thread_response(@comment_thread, status)
end

#unfollow_threadObject



294
295
296
297
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 294

def unfollow_thread
  status = @comment_thread.remove_follower(current_user)
  restrict_thread_response(@comment_thread, status)
end

#unlock_threadObject



299
300
301
302
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 299

def unlock_thread
  status = @comment_thread.update(locked: false, locked_by: nil, locked_until: nil)
  restrict_thread_response(@comment_thread, status)
end

#updateObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File '/var/apps/qpixel/app/controllers/comments_controller.rb', line 111

def update
  @post = @comment.post
  @comment_thread = @comment.comment_thread
  before = @comment.content
  before_pings = check_for_pings(@comment_thread, before)
  if @comment.update comment_params
    unless current_user.same_as?(@comment.user)
      audit('comment_update', @comment, "from <<#{before}>>\nto <<#{@comment.content}>>")
    end

    after_pings = check_for_pings(@comment_thread, @comment.content)
    apply_pings(after_pings - before_pings - @comment_thread.thread_follower.to_a)

    render json: { status: 'success',
                   comment: render_to_string(partial: 'comments/comment',
                                             locals: { comment: @comment, pingable: after_pings }) }
  else
    render json: { status: 'failed',
                   message: "Comment failed to save (#{@comment.errors.full_messages.join(', ')})" },
           status: :internal_server_error
  end
end