]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/old_elements_controller.rb
Merge remote-tracking branch 'upstream/pull/4559'
[rails.git] / app / controllers / api / old_elements_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 OldElementsController < ApiController
6     before_action :check_api_readable
7     before_action :check_api_writable, :only => [:redact]
8     before_action :setup_user_auth, :only => [:history, :show]
9     before_action :authorize, :only => [:redact]
10
11     authorize_resource
12
13     around_action :api_call_handle_error, :api_call_timeout
14     before_action :lookup_old_element, :except => [:history]
15     before_action :lookup_old_element_versions, :only => [:history]
16
17     before_action :set_request_formats, :except => [:redact]
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       # determine visible elements
26       @elems = if show_redactions?
27                  @elements
28                else
29                  @elements.unredacted
30                end
31
32       # Render the result
33       respond_to do |format|
34         format.xml
35         format.json
36       end
37     end
38
39     def show
40       if @old_element.redacted? && !show_redactions?
41         head :forbidden
42
43       else
44         response.last_modified = @old_element.timestamp
45
46         # Render the result
47         respond_to do |format|
48           format.xml
49           format.json
50         end
51       end
52     end
53
54     def redact
55       redaction_id = params["redaction"]
56       if redaction_id.nil?
57         # if no redaction ID was provided, then this is an unredact
58         # operation.
59         @old_element.redact!(nil)
60       else
61         # if a redaction ID was specified, then set this element to
62         # be redacted in that redaction.
63         redaction = Redaction.find(redaction_id.to_i)
64         @old_element.redact!(redaction)
65       end
66
67       # just return an empty 200 OK for success
68       head :ok
69     end
70
71     private
72
73     def show_redactions?
74       current_user&.moderator? && params[:show_redactions] == "true"
75     end
76   end
77 end