summaryrefslogtreecommitdiff
path: root/app/controllers/posts/locations_controller.rb
blob: 587000a3b24b02f7f52d673842a61a2575dd9eea (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
# frozen_string_literal: true

class Posts::LocationsController < ApplicationController
  before_action :filter_banned_users
  before_action :load_post
  before_action :check_auth, except: [:show]
  before_action :load_location, only: [:update, :destroy]

  before_action -> { ratelimit 1, 5.seconds, t('booru.errors.metadata_flooding', seconds: 5) }, unless: -> { current_user&.staff? }

  def show
    respond_to do |format|
      format.json { render json: @post.locations }
    end
  end

  def create
    @location = @post.locations.create!(location_params)

    flash[:notice] = 'Location successfully created.'
    redirect_to @post
  end

  def update
    @location = @post.locations.find_by_location(location_params[:location])

    @location.update!(id_at_location: location_params[:id_at_location])

    flash[:notice] = 'Location successfully updated.'

    redirect_to @post
  end

  def destroy
    @location.destroy!

    flash[:notice] = 'Location successfully destroyed.'
    redirect_to @post
  end

  private

  def location_params
    params.require(:location).permit(:location, :id_at_location)
  end

  def load_post
    @post = Post.find(params[:port_id])
  end

  def load_location
    @location = @post.locations.find_by(location: location_params[:location])
  end

  def check_auth
    authorize! :fuck_up, @post
      # authorize! :update_metadata, @post
  end
end