]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/changeset_comments_controller.rb
9ecd22b456493dde5e2efcca96df383fb63e9d3d
[rails.git] / app / controllers / api / changeset_comments_controller.rb
1 module Api
2   class ChangesetCommentsController < ApplicationController
3     skip_before_action :verify_authenticity_token
4     before_action :authorize
5     before_action :api_deny_access_handler
6
7     authorize_resource
8
9     before_action :require_public_data, :only => [:create]
10     before_action :check_api_writable
11     before_action :check_api_readable, :except => [:create]
12     around_action :api_call_handle_error
13     around_action :api_call_timeout
14
15     ##
16     # Add a comment to a changeset
17     def create
18       # Check the arguments are sane
19       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
20       raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
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.is_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         Notifier.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 "changesets/changeset"
46     end
47
48     ##
49     # Sets visible flag on comment to false
50     def destroy
51       # Check the arguments are sane
52       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
53
54       # Extract the arguments
55       id = params[:id].to_i
56
57       # Find the changeset
58       comment = ChangesetComment.find(id)
59
60       # Hide the comment
61       comment.update(:visible => false)
62
63       # Return a copy of the updated changeset
64       @changeset = comment.changeset
65       render "changesets/changeset"
66     end
67
68     ##
69     # Sets visible flag on comment to true
70     def restore
71       # Check the arguments are sane
72       raise OSM::APIBadUserInput, "No id was given" unless params[:id]
73
74       # Extract the arguments
75       id = params[:id].to_i
76
77       # Find the changeset
78       comment = ChangesetComment.find(id)
79
80       # Unhide the comment
81       comment.update(:visible => true)
82
83       # Return a copy of the updated changeset
84       @changeset = comment.changeset
85       render "changesets/changeset"
86     end
87   end
88 end