]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/relations_controller.rb
Lock changeset in api destroy element actions
[rails.git] / app / controllers / api / relations_controller.rb
1 module Api
2   class RelationsController < ApiController
3     before_action :check_api_writable, :only => [:create, :update, :destroy]
4     before_action :authorize, :only => [:create, :update, :destroy]
5
6     authorize_resource
7
8     before_action :require_public_data, :only => [:create, :update, :destroy]
9     before_action :set_request_formats, :except => [:create, :update, :destroy]
10     before_action :check_rate_limit, :only => [:create, :update, :destroy]
11
12     def index
13       raise OSM::APIBadUserInput, "The parameter relations is required, and must be of the form relations=id[,id[,id...]]" unless params["relations"]
14
15       ids = params["relations"].split(",").collect(&:to_i)
16
17       raise OSM::APIBadUserInput, "No relations were given to search for" if ids.empty?
18
19       @relations = Relation.includes(:relation_members, :element_tags).find(ids)
20
21       # Render the result
22       respond_to do |format|
23         format.xml
24         format.json
25       end
26     end
27
28     def show
29       relation = Relation.includes(:relation_members, :element_tags).find(params[:id])
30
31       response.last_modified = relation.timestamp unless params[:full]
32
33       @nodes = []
34       @ways = []
35       @relations = []
36
37       if relation.visible
38         if params[:full]
39           # with parameter :full
40           # returns representation of one relation object plus all its
41           # members, plus all nodes part of member ways
42
43           # first find the ids of nodes, ways and relations referenced by this
44           # relation - note that we exclude this relation just in case.
45
46           node_ids = relation.members.select { |m| m[0] == "Node" }.pluck(1)
47           way_ids = relation.members.select { |m| m[0] == "Way" }.pluck(1)
48           relation_ids = relation.members.select { |m| m[0] == "Relation" && m[1] != relation.id }.pluck(1)
49
50           # next load the relations and the ways.
51
52           relations = Relation.where(:id => relation_ids).includes(:element_tags)
53           ways = Way.where(:id => way_ids).includes(:way_nodes, :element_tags)
54
55           # now additionally collect nodes referenced by ways. Note how we
56           # recursively evaluate ways but NOT relations.
57
58           way_node_ids = ways.collect do |way|
59             way.way_nodes.collect(&:node_id)
60           end
61           node_ids += way_node_ids.flatten
62           nodes = Node.where(:id => node_ids.uniq).includes(:element_tags)
63
64           @nodes = []
65           nodes.each do |node|
66             next unless node.visible? # should be unnecessary if data is consistent.
67
68             @nodes << node
69           end
70
71           ways.each do |way|
72             next unless way.visible? # should be unnecessary if data is consistent.
73
74             @ways << way
75           end
76
77           relations.each do |rel|
78             next unless rel.visible? # should be unnecessary if data is consistent.
79
80             @relations << rel
81           end
82         end
83
84         # finally add self
85         @relations << relation
86
87         # Render the result
88         respond_to do |format|
89           format.xml
90           format.json
91         end
92       else
93         head :gone
94       end
95     end
96
97     def create
98       relation = Relation.from_xml(request.raw_post, :create => true)
99
100       Changeset.transaction do
101         relation.changeset&.lock!
102         relation.create_with_history current_user
103       end
104       render :plain => relation.id.to_s
105     end
106
107     def update
108       relation = Relation.find(params[:id])
109       new_relation = Relation.from_xml(request.raw_post)
110
111       raise OSM::APIBadUserInput, "The id in the url (#{relation.id}) is not the same as provided in the xml (#{new_relation.id})" unless new_relation && new_relation.id == relation.id
112
113       Changeset.transaction do
114         new_relation.changeset&.lock!
115         relation.update_from(new_relation, current_user)
116       end
117       render :plain => relation.version.to_s
118     end
119
120     def destroy
121       relation = Relation.find(params[:id])
122       new_relation = Relation.from_xml(request.raw_post)
123       if new_relation && new_relation.id == relation.id
124         Changeset.transaction do
125           new_relation.changeset&.lock!
126           relation.delete_with_history!(new_relation, current_user)
127         end
128         render :plain => relation.version.to_s
129       else
130         head :bad_request
131       end
132     end
133   end
134 end