]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/user_blocks_controller.rb
Add frozen_string_literal comments to ruby files
[rails.git] / app / controllers / api / user_blocks_controller.rb
1 # frozen_string_literal: true
2
3 module Api
4   class UserBlocksController < ApiController
5     before_action :check_api_writable, :only => :create
6     before_action :authorize, :only => :create
7
8     authorize_resource
9
10     before_action :set_request_formats
11
12     def show
13       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
14
15       @user_block = UserBlock.find(params[:id])
16     rescue ActiveRecord::RecordNotFound
17       raise OSM::APINotFoundError
18     end
19
20     def create
21       raise OSM::APIBadUserInput, "No user was given" unless params[:user]
22
23       user = User.visible.find_by(:id => params[:user])
24       raise OSM::APINotFoundError unless user
25       raise OSM::APIBadUserInput, "No reason was given" unless params[:reason]
26       raise OSM::APIBadUserInput, "No period was given" unless params[:period]
27
28       period = Integer(params[:period], :exception => false)
29       raise OSM::APIBadUserInput, "Period should be a number of hours" unless period
30
31       max_period = UserBlock::PERIODS.max
32       raise OSM::APIBadUserInput, "Period must be between 0 and #{max_period}" if period.negative? || period > max_period
33       raise OSM::APIBadUserInput, "Needs_view must be true if provided" unless params[:needs_view].nil? || params[:needs_view] == "true"
34
35       ends_at = Time.now.utc + period.hours
36       needs_view = params[:needs_view] == "true"
37       @user_block = UserBlock.create(
38         :user => user,
39         :creator => current_user,
40         :reason => params[:reason],
41         :ends_at => ends_at,
42         :deactivates_at => (ends_at unless needs_view),
43         :needs_view => needs_view
44       )
45       render :show
46     end
47   end
48 end