X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/be0e33862fedbf02e52a9e126cac31dfb5775b43..06b8558aaf911d643a7ab986dcc50d40f774d6b6:/app/controllers/api/amf_controller.rb diff --git a/app/controllers/api/amf_controller.rb b/app/controllers/api/amf_controller.rb index 2a878d248..c903cc8e7 100644 --- a/app/controllers/api/amf_controller.rb +++ b/app/controllers/api/amf_controller.rb @@ -36,10 +36,9 @@ # * version conflict when POIs and ways are reverted module Api - class AmfController < ApplicationController + class AmfController < ApiController include Potlatch - skip_before_action :verify_authenticity_token before_action :check_api_writable # AMF Controller implements its own authentication and authorization checks @@ -88,19 +87,19 @@ module Api result = [-5, nil] else case message - when "putway" then + when "putway" orn = renumberednodes.dup result = putway(renumberednodes, *args) result[4] = renumberednodes.reject { |k, _v| orn.key?(k) } renumberedways[result[2]] = result[3] if result[0].zero? && result[2] != result[3] - when "putrelation" then + when "putrelation" result = putrelation(renumberednodes, renumberedways, *args) - when "deleteway" then + when "deleteway" result = deleteway(*args) - when "putpoi" then + when "putpoi" result = putpoi(*args) renumberednodes[result[2]] = result[3] if result[0].zero? && result[2] != result[3] - when "startchangeset" then + when "startchangeset" result = startchangeset(*args) end @@ -115,25 +114,23 @@ module Api def amf_handle_error(call, rootobj, rootid) yield - rescue OSM::APIAlreadyDeletedError => ex - [-4, ex.object, ex.object_id] - rescue OSM::APIVersionMismatchError => ex - [-3, [rootobj, rootid], [ex.type.downcase, ex.id, ex.latest]] - rescue OSM::APIUserChangesetMismatchError => ex - [-2, ex.to_s] - rescue OSM::APIBadBoundingBox => ex - [-2, "Sorry - I can't get the map for that area. The server said: #{ex}"] - rescue OSM::APIError => ex - [-1, ex.to_s] - rescue StandardError => ex - [-2, "An unusual error happened (in #{call}). The server said: #{ex}"] - end - - def amf_handle_error_with_timeout(call, rootobj, rootid) + rescue OSM::APIAlreadyDeletedError => e + [-4, e.object, e.object_id] + rescue OSM::APIVersionMismatchError => e + [-3, [rootobj, rootid], [e.type.downcase, e.id, e.latest]] + rescue OSM::APIUserChangesetMismatchError => e + [-2, e.to_s] + rescue OSM::APIBadBoundingBox => e + [-2, "Sorry - I can't get the map for that area. The server said: #{e}"] + rescue OSM::APIError => e + [-1, e.to_s] + rescue StandardError => e + [-2, "An unusual error happened (in #{call}). The server said: #{e}"] + end + + def amf_handle_error_with_timeout(call, rootobj, rootid, &block) amf_handle_error(call, rootobj, rootid) do - OSM::Timer.timeout(API_TIMEOUT, OSM::APITimeoutError) do - yield - end + OSM::Timer.timeout(Settings.api_timeout, OSM::APITimeoutError, &block) end end @@ -227,14 +224,14 @@ module Api loaded_lang = "en" # Load English defaults - en = YAML.safe_load(File.open(Rails.root.join("config", "potlatch", "locales", "en.yml")))["en"] + en = YAML.safe_load(File.open(Rails.root.join("config/potlatch/locales/en.yml")))["en"] if lang == "en" - return [loaded_lang, en] + [loaded_lang, en] else # Use English as a fallback begin - other = YAML.safe_load(File.open(Rails.root.join("config", "potlatch", "locales", "#{lang}.yml")))[lang] + other = YAML.safe_load(File.open(Rails.root.join("config/potlatch/locales/#{lang}.yml")))[lang] loaded_lang = lang rescue StandardError other = en @@ -242,7 +239,7 @@ module Api # We have to return a flat list and some of the keys won't be # translated (probably) - return [loaded_lang, en.merge(other)] + [loaded_lang, en.merge(other)] end end @@ -570,7 +567,7 @@ module Api new_relation.members = typedmembers new_relation.tags = tags new_relation.visible = visible - new_relation.changeset_id = changeset_id + new_relation.changeset_id = changeset_id.to_i new_relation.version = version if relid <= 0 @@ -654,7 +651,7 @@ module Api id = renumberednodes[id] if renumberednodes[id] node = Node.new - node.changeset_id = changeset_id + node.changeset_id = changeset_id.to_i node.lat = lat node.lon = lon node.tags = a[4] @@ -688,7 +685,7 @@ module Api new_way = Way.new new_way.tags = attributes new_way.nds = pointlist - new_way.changeset_id = changeset_id + new_way.changeset_id = changeset_id.to_i new_way.version = wayversion if originalway <= 0 new_way.create_with_history(user) @@ -706,7 +703,7 @@ module Api deletednodes.each do |id, v| node = Node.find(id.to_i) new_node = Node.new - new_node.changeset_id = changeset_id + new_node.changeset_id = changeset_id.to_i new_node.version = v.to_i new_node.id = id.to_i begin @@ -759,7 +756,7 @@ module Api # We always need a new node, based on the data that has been sent to us new_node = Node.new - new_node.changeset_id = changeset_id + new_node.changeset_id = changeset_id.to_i new_node.version = version new_node.lat = lat new_node.lon = lon @@ -835,7 +832,7 @@ module Api old_way = Way.find(way_id) delete_way = Way.new delete_way.version = way_version - delete_way.changeset_id = changeset_id + delete_way.changeset_id = changeset_id.to_i delete_way.id = way_id old_way.delete_with_history!(delete_way, user) @@ -844,7 +841,7 @@ module Api deletednodes.each do |id, v| node = Node.find(id.to_i) new_node = Node.new - new_node.changeset_id = changeset_id + new_node.changeset_id = changeset_id.to_i new_node.version = v.to_i new_node.id = id.to_i begin @@ -869,7 +866,7 @@ module Api # not just the id, hence this abstraction def getuser(token) - if token =~ /^(.+)\:(.+)$/ + if token =~ /^(.+):(.+)$/ User.authenticate(:username => Regexp.last_match(1), :password => Regexp.last_match(2)) else User.authenticate(:token => token) @@ -877,7 +874,7 @@ module Api end def getlocales - @getlocales ||= Locale.list(Dir.glob(Rails.root.join("config", "potlatch", "locales", "*")).collect { |f| File.basename(f, ".yml") }) + @getlocales ||= Locale.list(Dir.glob(Rails.root.join("config/potlatch/locales/*")).collect { |f| File.basename(f, ".yml") }) end ## @@ -907,21 +904,21 @@ module Api # Alternative SQL queries for getway/whichways def sql_find_ways_in_area(bbox) - sql = <<-SQL - SELECT DISTINCT current_ways.id AS wayid,current_ways.version AS version - FROM current_way_nodes - INNER JOIN current_nodes ON current_nodes.id=current_way_nodes.node_id - INNER JOIN current_ways ON current_ways.id =current_way_nodes.id - WHERE current_nodes.visible=TRUE - AND current_ways.visible=TRUE - AND #{OSM.sql_for_area(bbox, 'current_nodes.')} + sql = <<~SQL + SELECT DISTINCT current_ways.id AS wayid,current_ways.version AS version + FROM current_way_nodes + INNER JOIN current_nodes ON current_nodes.id=current_way_nodes.node_id + INNER JOIN current_ways ON current_ways.id =current_way_nodes.id + WHERE current_nodes.visible=TRUE + AND current_ways.visible=TRUE + AND #{OSM.sql_for_area(bbox, 'current_nodes.')} SQL ActiveRecord::Base.connection.select_all(sql).collect { |a| [a["wayid"].to_i, a["version"].to_i] } end def sql_find_pois_in_area(bbox) pois = [] - sql = <<-SQL + sql = <<~SQL SELECT current_nodes.id,current_nodes.latitude*0.0000001 AS lat,current_nodes.longitude*0.0000001 AS lon,current_nodes.version FROM current_nodes LEFT OUTER JOIN current_way_nodes cwn ON cwn.node_id=current_nodes.id @@ -942,7 +939,7 @@ module Api def sql_find_relations_in_area_and_ways(bbox, way_ids) # ** It would be more Potlatchy to get relations for nodes within ways # during 'getway', not here - sql = <<-SQL + sql = <<~SQL SELECT DISTINCT cr.id AS relid,cr.version AS version FROM current_relations cr INNER JOIN current_relation_members crm ON crm.id=cr.id @@ -950,13 +947,13 @@ module Api WHERE #{OSM.sql_for_area(bbox, 'cn.')} SQL unless way_ids.empty? - sql += <<-SQL - UNION - SELECT DISTINCT cr.id AS relid,cr.version AS version - FROM current_relations cr - INNER JOIN current_relation_members crm ON crm.id=cr.id - WHERE crm.member_type='Way' - AND crm.member_id IN (#{way_ids.join(',')}) + sql += <<~SQL + UNION + SELECT DISTINCT cr.id AS relid,cr.version AS version + FROM current_relations cr + INNER JOIN current_relation_members crm ON crm.id=cr.id + WHERE crm.member_type='Way' + AND crm.member_id IN (#{way_ids.join(',')}) SQL end ActiveRecord::Base.connection.select_all(sql).collect { |a| [a["relid"].to_i, a["version"].to_i] } @@ -964,7 +961,7 @@ module Api def sql_get_nodes_in_way(wayid) points = [] - sql = <<-SQL + sql = <<~SQL SELECT latitude*0.0000001 AS lat,longitude*0.0000001 AS lon,current_nodes.id,current_nodes.version FROM current_way_nodes,current_nodes WHERE current_way_nodes.id=#{wayid.to_i}