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 before_action :set_request_formats
16 @messages = Message.includes(:sender, :recipient).where(:to_user_id => current_user.id)
23 @messages = Message.includes(:sender, :recipient).where(:from_user_id => current_user.id)
28 # Dump the details on a message given in params[:id]
30 @message = Message.includes(:sender, :recipient).find(params[:id])
32 raise OSM::APIAccessDenied if current_user.id != @message.from_user_id && current_user.id != @message.to_user_id
35 respond_to do |format|
41 # Create a new message from current user
43 # Check the arguments are sane
44 raise OSM::APIBadUserInput, "No title was given" if params[:title].blank?
45 raise OSM::APIBadUserInput, "No body was given" if params[:body].blank?
47 # Extract the arguments
48 if params[:recipient_id]
49 recipient_id = params[:recipient_id].to_i
50 recipient = User.find(recipient_id)
51 elsif params[:recipient]
52 recipient_display_name = params[:recipient]
53 recipient = User.find_by(:display_name => recipient_display_name)
55 raise OSM::APIBadUserInput, "No recipient was given"
58 raise OSM::APIRateLimitExceeded if current_user.sent_messages.where(:sent_on => Time.now.utc - 1.hour..).count >= current_user.max_messages_per_hour
60 @message = Message.new(:sender => current_user,
61 :recipient => recipient,
62 :sent_on => Time.now.utc,
63 :title => params[:title],
64 :body => params[:body],
65 :body_format => "markdown")
68 UserMailer.message_notification(@message).deliver_later if @message.notify_recipient?
70 # Return a copy of the new message
71 respond_to do |format|
72 format.xml { render :action => :show }
73 format.json { render :action => :show }
77 # Update read status of a message
79 @message = Message.find(params[:id])
80 read_status_idx = %w[true false].index params[:read_status]
82 raise OSM::APIBadUserInput, "Invalid value of `read_status` was given" if read_status_idx.nil?
83 raise OSM::APIAccessDenied unless current_user.id == @message.to_user_id
85 @message.message_read = read_status_idx.zero?
88 # Return a copy of the message
89 respond_to do |format|
90 format.xml { render :action => :show }
91 format.json { render :action => :show }
95 # Delete message by marking it as not visible for the current user
97 @message = Message.find(params[:id])
98 if current_user.id == @message.from_user_id
99 @message.from_user_visible = false
100 elsif current_user.id == @message.to_user_id
101 @message.to_user_visible = false
103 raise OSM::APIAccessDenied
108 # Return a copy of the message
109 respond_to do |format|
110 format.xml { render :action => :show }
111 format.json { render :action => :show }
118 @messages = @messages.where(:muted => false)
119 if params[:order].nil? || params[:order] == "newest"
120 @messages = @messages.where(:id => ..params[:from_id]) unless params[:from_id].nil?
121 @messages = @messages.order(:id => :desc)
122 elsif params[:order] == "oldest"
123 @messages = @messages.where(:id => params[:from_id]..) unless params[:from_id].nil?
124 @messages = @messages.order(:id => :asc)
126 raise OSM::APIBadUserInput, "Invalid order specified"
129 limit = params[:limit]
131 limit = Settings.default_message_query_limit
132 elsif !limit.to_i.positive? || limit.to_i > Settings.max_message_query_limit
133 raise OSM::APIBadUserInput, "Messages limit must be between 1 and #{Settings.max_message_query_limit}"
138 @messages = @messages.limit(limit)
141 respond_to do |format|