1 # The MessagesController is the RESTful interface to Message objects
4 class MessagesController < ApiController
5 before_action :authorize
7 before_action :check_api_writable, :only => [:create, :update, :destroy]
8 before_action :check_api_readable, :except => [:create, :update, :destroy]
12 around_action :api_call_handle_error, :api_call_timeout
14 before_action :set_request_formats
18 @messages = Message.includes(:sender, :recipient).where(:to_user_id => current_user.id)
25 @messages = Message.includes(:sender, :recipient).where(:from_user_id => current_user.id)
30 # Dump the details on a message given in params[:id]
32 @message = Message.includes(:sender, :recipient).find(params[:id])
34 raise OSM::APIAccessDenied if current_user.id != @message.from_user_id && current_user.id != @message.to_user_id
37 respond_to do |format|
43 # Create a new message from current user
45 # Check the arguments are sane
46 raise OSM::APIBadUserInput, "No title was given" if params[:title].blank?
47 raise OSM::APIBadUserInput, "No body was given" if params[:body].blank?
49 # Extract the arguments
50 if params[:recipient_id]
51 recipient_id = params[:recipient_id].to_i
52 recipient = User.find(recipient_id)
53 elsif params[:recipient]
54 recipient_display_name = params[:recipient]
55 recipient = User.find_by(:display_name => recipient_display_name)
57 raise OSM::APIBadUserInput, "No recipient was given"
60 raise OSM::APIRateLimitExceeded if current_user.sent_messages.where(:sent_on => Time.now.utc - 1.hour..).count >= current_user.max_messages_per_hour
62 @message = Message.new(:sender => current_user,
63 :recipient => recipient,
64 :sent_on => Time.now.utc,
65 :title => params[:title],
66 :body => params[:body],
67 :body_format => "markdown")
70 UserMailer.message_notification(@message).deliver_later if @message.notify_recipient?
72 # Return a copy of the new message
73 respond_to do |format|
74 format.xml { render :action => :show }
75 format.json { render :action => :show }
79 # Update read status of a message
81 @message = Message.find(params[:id])
82 read_status_idx = %w[true false].index params[:read_status]
84 raise OSM::APIBadUserInput, "Invalid value of `read_status` was given" if read_status_idx.nil?
85 raise OSM::APIAccessDenied unless current_user.id == @message.to_user_id
87 @message.message_read = read_status_idx.zero?
90 # Return a copy of the message
91 respond_to do |format|
92 format.xml { render :action => :show }
93 format.json { render :action => :show }
97 # Delete message by marking it as not visible for the current user
99 @message = Message.find(params[:id])
100 if current_user.id == @message.from_user_id
101 @message.from_user_visible = false
102 elsif current_user.id == @message.to_user_id
103 @message.to_user_visible = false
105 raise OSM::APIAccessDenied
110 # Return a copy of the message
111 respond_to do |format|
112 format.xml { render :action => :show }
113 format.json { render :action => :show }
120 @messages = @messages.where(:muted => false)
121 if params[:order].nil? || params[:order] == "newest"
122 @messages = @messages.where(:id => ..params[:from_id]) unless params[:from_id].nil?
123 @messages = @messages.order(:id => :desc)
124 elsif params[:order] == "oldest"
125 @messages = @messages.where(:id => params[:from_id]..) unless params[:from_id].nil?
126 @messages = @messages.order(:id => :asc)
128 raise OSM::APIBadUserInput, "Invalid order specified"
131 limit = params[:limit]
133 limit = Settings.default_message_query_limit
134 elsif !limit.to_i.positive? || limit.to_i > Settings.max_message_query_limit
135 raise OSM::APIBadUserInput, "Messages limit must be between 1 and #{Settings.max_message_query_limit}"
140 @messages = @messages.limit(limit)
143 respond_to do |format|