]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/changeset_comments_controller.rb
Merge branch 'master' into feature/add-communities-page
[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
21       # Extract the arguments
22       id = params[:id].to_i
23       body = params[:text]
24
25       # Find the changeset and check it is valid
26       changeset = Changeset.find(id)
27       raise OSM::APIChangesetNotYetClosedError, changeset if changeset.open?
28
29       # Add a comment to the changeset
30       comment = changeset.comments.create(:changeset => changeset,
31                                           :body => body,
32                                           :author => current_user)
33
34       # Notify current subscribers of the new comment
35       changeset.subscribers.visible.each do |user|
36         UserMailer.changeset_comment_notification(comment, user).deliver_later if current_user != user
37       end
38
39       # Add the commenter to the subscribers if necessary
40       changeset.subscribers << current_user unless changeset.subscribers.exists?(current_user.id)
41
42       # Return a copy of the updated changeset
43       @changeset = changeset
44       render "api/changesets/changeset"
45
46       respond_to do |format|
47         format.xml
48         format.json
49       end
50     end
51
52     ##
53     # Sets visible flag on comment to false
54     def destroy
55       # Check the arguments are sane
56       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
57
58       # Extract the arguments
59       id = params[:id].to_i
60
61       # Find the changeset
62       comment = ChangesetComment.find(id)
63
64       # Hide the comment
65       comment.update(:visible => false)
66
67       # Return a copy of the updated changeset
68       @changeset = comment.changeset
69       render "api/changesets/changeset"
70
71       respond_to do |format|
72         format.xml
73         format.json
74       end
75     end
76
77     ##
78     # Sets visible flag on comment to true
79     def restore
80       # Check the arguments are sane
81       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
82
83       # Extract the arguments
84       id = params[:id].to_i
85
86       # Find the changeset
87       comment = ChangesetComment.find(id)
88
89       # Unhide the comment
90       comment.update(:visible => true)
91
92       # Return a copy of the updated changeset
93       @changeset = comment.changeset
94       render "api/changesets/changeset"
95
96       respond_to do |format|
97         format.xml
98         format.json
99       end
100     end
101   end
102 end