]> git.openstreetmap.org Git - rails.git/commitdiff
Add show user block api endpoint
authorAnton Khorev <tony29@yandex.ru>
Fri, 8 Sep 2023 12:25:15 +0000 (15:25 +0300)
committerAnton Khorev <tony29@yandex.ru>
Sat, 9 Sep 2023 12:53:22 +0000 (15:53 +0300)
app/abilities/api_ability.rb
app/controllers/api/user_blocks_controller.rb [new file with mode: 0644]
app/views/api/user_blocks/_user_block.xml.builder [new file with mode: 0644]
app/views/api/user_blocks/show.xml.builder [new file with mode: 0644]
config/routes.rb
test/controllers/api/user_blocks_controller_test.rb [new file with mode: 0644]

index 9b274ec845d299f2ee6af894e80cb0f3ac0dac3a..fe39f5eb5aa2475a4d1c56fa9456e9a8842d117a 100644 (file)
@@ -21,6 +21,7 @@ class ApiAbility
       can [:history, :version], OldNode
       can [:history, :version], OldWay
       can [:history, :version], OldRelation
+      can [:show], UserBlock
     end
 
     if user&.active?
diff --git a/app/controllers/api/user_blocks_controller.rb b/app/controllers/api/user_blocks_controller.rb
new file mode 100644 (file)
index 0000000..19fd4b4
--- /dev/null
@@ -0,0 +1,18 @@
+module Api
+  class UserBlocksController < ApiController
+    before_action :check_api_readable
+
+    authorize_resource
+
+    around_action :api_call_handle_error, :api_call_timeout
+    before_action :set_request_formats
+
+    def show
+      raise OSM::APIBadUserInput, "No id was given" unless params[:id]
+
+      @user_block = UserBlock.find(params[:id])
+    rescue ActiveRecord::RecordNotFound
+      raise OSM::APINotFoundError
+    end
+  end
+end
diff --git a/app/views/api/user_blocks/_user_block.xml.builder b/app/views/api/user_blocks/_user_block.xml.builder
new file mode 100644 (file)
index 0000000..a41dc56
--- /dev/null
@@ -0,0 +1,14 @@
+attrs = {
+  "id" => user_block.id,
+  "created_at" => user_block.created_at.xmlschema,
+  "updated_at" => user_block.updated_at.xmlschema,
+  "ends_at" => user_block.ends_at.xmlschema,
+  "needs_view" => user_block.needs_view
+}
+
+xml.user_block(attrs) do
+  xml.user :uid => user_block.user_id, :user => user_block.user.display_name
+  xml.creator :uid => user_block.creator_id, :user => user_block.creator.display_name
+  xml.revoker :uid => user_block.revoker_id, :user => user_block.revoker.display_name if user_block.revoker
+  xml.reason user_block.reason
+end
diff --git a/app/views/api/user_blocks/show.xml.builder b/app/views/api/user_blocks/show.xml.builder
new file mode 100644 (file)
index 0000000..eff666b
--- /dev/null
@@ -0,0 +1,5 @@
+xml.instruct!
+
+xml.osm(OSM::API.new.xml_root_attributes) do |osm|
+  osm << (render(@user_block) || "")
+end
index 415f8a01bd9b5dc1b677fe8e3261b51ea709c8f4..404e7b0a3fcf6345fec4f22af26a022676f91392 100644 (file)
@@ -102,6 +102,8 @@ OpenStreetMap::Application.routes.draw do
     post "notes/editPOIexec" => "api/notes#comment"
     get "notes/getGPX" => "api/notes#index", :format => "gpx"
     get "notes/getRSSfeed" => "api/notes#feed", :format => "rss"
+
+    resources :user_blocks, :only => [:show], :constraints => { :id => /\d+/ }, :controller => "api/user_blocks", :as => :api_user_blocks
   end
 
   # Data browsing
diff --git a/test/controllers/api/user_blocks_controller_test.rb b/test/controllers/api/user_blocks_controller_test.rb
new file mode 100644 (file)
index 0000000..eef54e9
--- /dev/null
@@ -0,0 +1,26 @@
+require "test_helper"
+
+module Api
+  class UserBlocksControllerTest < ActionDispatch::IntegrationTest
+    def test_routes
+      assert_routing(
+        { :path => "/api/0.6/user_blocks/1", :method => :get },
+        { :controller => "api/user_blocks", :action => "show", :id => "1" }
+      )
+    end
+
+    def test_show
+      block = create(:user_block)
+
+      get api_user_block_path(:id => block)
+      assert_response :success
+      assert_select "user_block[id='#{block.id}']", 1
+    end
+
+    def test_show_not_found
+      get api_user_block_path(:id => 123)
+      assert_response :not_found
+      assert_equal "text/plain", @response.media_type
+    end
+  end
+end