]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/messages_controller.rb
Add frozen_string_literal comments to ruby files
[rails.git] / app / controllers / api / messages_controller.rb
1 # frozen_string_literal: true
2
3 # The MessagesController is the RESTful interface to Message objects
4
5 module Api
6   class MessagesController < ApiController
7     before_action :authorize
8
9     before_action :check_api_writable, :only => [:create, :update, :destroy]
10
11     authorize_resource
12
13     before_action :set_request_formats
14
15     # Dump the details on a message given in params[:id]
16     def show
17       @message = Message.includes(:sender, :recipient).find(params[:id])
18
19       raise OSM::APIAccessDenied if current_user.id != @message.from_user_id && current_user.id != @message.to_user_id
20
21       # Render the result
22       respond_to do |format|
23         format.xml
24         format.json
25       end
26     end
27
28     # Create a new message from current user
29     def create
30       # Check the arguments are sane
31       raise OSM::APIBadUserInput, "No title was given" if params[:title].blank?
32       raise OSM::APIBadUserInput, "No body was given" if params[:body].blank?
33
34       # Extract the arguments
35       if params[:recipient_id]
36         recipient_id = params[:recipient_id].to_i
37         recipient = User.find(recipient_id)
38       elsif params[:recipient]
39         recipient_display_name = params[:recipient]
40         recipient = User.find_by(:display_name => recipient_display_name)
41       else
42         raise OSM::APIBadUserInput, "No recipient was given"
43       end
44
45       raise OSM::APIRateLimitExceeded if current_user.sent_messages.where(:sent_on => (Time.now.utc - 1.hour)..).count >= current_user.max_messages_per_hour
46
47       @message = Message.new(:sender => current_user,
48                              :recipient => recipient,
49                              :sent_on => Time.now.utc,
50                              :title => params[:title],
51                              :body => params[:body],
52                              :body_format => "markdown")
53       @message.save!
54
55       UserMailer.message_notification(@message).deliver_later if @message.notify_recipient?
56
57       # Return a copy of the new message
58       respond_to do |format|
59         format.xml { render :action => :show }
60         format.json { render :action => :show }
61       end
62     end
63
64     # Update read status of a message
65     def update
66       @message = Message.find(params[:id])
67       read_status_idx = %w[true false].index params[:read_status]
68
69       raise OSM::APIBadUserInput, "Invalid value of `read_status` was given" if read_status_idx.nil?
70       raise OSM::APIAccessDenied unless current_user.id == @message.to_user_id
71
72       @message.message_read = read_status_idx.zero?
73       @message.save!
74
75       # Return a copy of the message
76       respond_to do |format|
77         format.xml { render :action => :show }
78         format.json { render :action => :show }
79       end
80     end
81
82     # Delete message by marking it as not visible for the current user
83     def destroy
84       @message = Message.find(params[:id])
85       if current_user.id == @message.from_user_id
86         @message.from_user_visible = false
87       elsif current_user.id == @message.to_user_id
88         @message.to_user_visible = false
89       else
90         raise OSM::APIAccessDenied
91       end
92
93       @message.save!
94
95       # Return a copy of the message
96       respond_to do |format|
97         format.xml { render :action => :show }
98         format.json { render :action => :show }
99       end
100     end
101   end
102 end