summaryrefslogtreecommitdiff
path: root/app/controllers/posts/hides_controller.rb
blob: bce7f0094f40ad22ec616c33ee48cfdd1f92d77d (plain)
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
# frozen_string_literal: true

class Posts::HidesController < ApplicationController
  before_action :load_post
  before_action :check_auth

  def create
    PostHider.new(@post, user: current_user, reason: params[:deletion_reason]).save

    redirect_to post_path(@post), notice: t('posts.destroy.success', id: @post.id)
  end

  def update
    if params[:deletion_reason].blank?

      flash[:error] = t('posts.errors.blank_deletion_reason')
      respond_to do |format|
        format.html { redirect_to post_path(@post) }
        format.json { head :error }
      end
      return
    end

    @post.update_columns(deletion_reason: params[:deletion_reason])

    notice = t('posts.change_hide_reason.success')

    respond_to do |format|
      format.html { redirect_to post_path(@post), notice: notice }
      format.json { head :ok }
    end
  end

  # TODO: this is a mess
  def destroy
    if @post.duplicate_id
      @dup_image = Post.find_by id: @post.duplicate_id
      dr = @post.duplicate_reports.find_by(duplicate_of_post_id: @dup_image.id) rescue nil
      if dr
        dr.reject!(current_user)
      else
        # manually set as non-dupe
        @post.duplicate_id = nil
        PostUnhider.new(@post).save
      end
    else
      PostUnhider.new(@post).save
    end

    respond_to do |format|
      format.html { redirect_to post_path(@post) }
      format.json { head :ok }
    end
  end

  private

  def load_post
    @post = Post.find_by!(id: params[:post_id], destroyed_content: false)
  end

  def check_auth
    authorize! :hide, @post
  end
end