]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/changeset_comments_controller.rb
Merge remote-tracking branch 'upstream/pull/4759'
[rails.git] / app / controllers / api / changeset_comments_controller.rb
1 module Api
2   class ChangesetCommentsController < ApiController
3     before_action :check_api_writable
4     before_action :check_api_readable, :except => [:create]
5     before_action :authorize
6
7     authorize_resource
8
9     before_action :require_public_data, :only => [:create]
10     before_action :set_request_formats
11     around_action :api_call_handle_error
12     around_action :api_call_timeout
13
14     ##
15     # Add a comment to a changeset
16     def create
17       # Check the arguments are sane
18       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
19       raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
20       raise OSM::APIRateLimitExceeded if rate_limit_exceeded?
21
22       # Extract the arguments
23       id = params[:id].to_i
24       body = params[:text]
25
26       # Find the changeset and check it is valid
27       changeset = Changeset.find(id)
28       raise OSM::APIChangesetNotYetClosedError, changeset if changeset.open?
29
30       # Add a comment to the changeset
31       comment = changeset.comments.create(:changeset => changeset,
32                                           :body => body,
33                                           :author => current_user)
34
35       # Notify current subscribers of the new comment
36       changeset.subscribers.visible.each do |user|
37         UserMailer.changeset_comment_notification(comment, user).deliver_later if current_user != user
38       end
39
40       # Add the commenter to the subscribers if necessary
41       changeset.subscribers << current_user unless changeset.subscribers.exists?(current_user.id)
42
43       # Return a copy of the updated changeset
44       @changeset = changeset
45       render "api/changesets/show"
46
47       respond_to do |format|
48         format.xml
49         format.json
50       end
51     end
52
53     ##
54     # Sets visible flag on comment to false
55     def destroy
56       # Check the arguments are sane
57       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
58
59       # Extract the arguments
60       id = params[:id].to_i
61
62       # Find the changeset
63       comment = ChangesetComment.find(id)
64
65       # Hide the comment
66       comment.update(:visible => false)
67
68       # Return a copy of the updated changeset
69       @changeset = comment.changeset
70       render "api/changesets/show"
71
72       respond_to do |format|
73         format.xml
74         format.json
75       end
76     end
77
78     ##
79     # Sets visible flag on comment to true
80     def restore
81       # Check the arguments are sane
82       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
83
84       # Extract the arguments
85       id = params[:id].to_i
86
87       # Find the changeset
88       comment = ChangesetComment.find(id)
89
90       # Unhide the comment
91       comment.update(:visible => true)
92
93       # Return a copy of the updated changeset
94       @changeset = comment.changeset
95       render "api/changesets/show"
96
97       respond_to do |format|
98         format.xml
99         format.json
100       end
101     end
102
103     private
104
105     ##
106     # Check if the current user has exceed the rate limit for comments
107     def rate_limit_exceeded?
108       recent_comments = current_user.changeset_comments.where("created_at >= ?", Time.now.utc - 1.hour).count
109
110       recent_comments >= current_user.max_changeset_comments_per_hour
111     end
112   end
113 end