]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/old_controller.rb
Simplify partial rendering
[rails.git] / app / controllers / api / old_controller.rb
1 # this class pulls together the logic for all the old_* controllers
2 # into one place. as it turns out, the API methods for historical
3 # nodes, ways and relations are basically identical.
4 module Api
5   class OldController < ApiController
6     require "xml/libxml"
7
8     before_action :setup_user_auth, :only => [:history, :version]
9     before_action :authorize, :only => [:redact]
10
11     authorize_resource
12
13     before_action :check_api_readable
14     before_action :check_api_writable, :only => [:redact]
15     around_action :api_call_handle_error, :api_call_timeout
16     before_action :lookup_old_element, :except => [:history]
17     before_action :lookup_old_element_versions, :only => [:history]
18
19     def history
20       # the .where() method used in the lookup_old_element_versions
21       # call won't throw an error if no records are found, so we have
22       # to do that ourselves.
23       raise OSM::APINotFoundError if @elements.empty?
24
25       visible_elements = if show_redactions?
26                            @elements
27                          else
28                            @elements.unredacted
29                          end
30
31       @elems = []
32
33       visible_elements.each do |element|
34         @elems << element
35       end
36
37       # Render the result
38       respond_to do |format|
39         format.xml
40       end
41     end
42
43     def version
44       if @old_element.redacted? && !show_redactions?
45         head :forbidden
46
47       else
48         response.last_modified = @old_element.timestamp
49
50         @elems = [@old_element]
51
52         # Render the result
53         respond_to do |format|
54           format.xml
55         end
56       end
57     end
58
59     def redact
60       redaction_id = params["redaction"]
61       if redaction_id.nil?
62         # if no redaction ID was provided, then this is an unredact
63         # operation.
64         @old_element.redact!(nil)
65       else
66         # if a redaction ID was specified, then set this element to
67         # be redacted in that redaction.
68         redaction = Redaction.find(redaction_id.to_i)
69         @old_element.redact!(redaction)
70       end
71
72       # just return an empty 200 OK for success
73       head :ok
74     end
75
76     private
77
78     def show_redactions?
79       current_user&.moderator? && params[:show_redactions] == "true"
80     end
81   end
82 end