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]
 
  11     before_action :set_request_formats
 
  13     # Dump the details on a message given in params[:id]
 
  15       @message = Message.includes(:sender, :recipient).find(params[:id])
 
  17       raise OSM::APIAccessDenied if current_user.id != @message.from_user_id && current_user.id != @message.to_user_id
 
  20       respond_to do |format|
 
  26     # Create a new message from current user
 
  28       # Check the arguments are sane
 
  29       raise OSM::APIBadUserInput, "No title was given" if params[:title].blank?
 
  30       raise OSM::APIBadUserInput, "No body was given" if params[:body].blank?
 
  32       # Extract the arguments
 
  33       if params[:recipient_id]
 
  34         recipient_id = params[:recipient_id].to_i
 
  35         recipient = User.find(recipient_id)
 
  36       elsif params[:recipient]
 
  37         recipient_display_name = params[:recipient]
 
  38         recipient = User.find_by(:display_name => recipient_display_name)
 
  40         raise OSM::APIBadUserInput, "No recipient was given"
 
  43       raise OSM::APIRateLimitExceeded if current_user.sent_messages.where(:sent_on => (Time.now.utc - 1.hour)..).count >= current_user.max_messages_per_hour
 
  45       @message = Message.new(:sender => current_user,
 
  46                              :recipient => recipient,
 
  47                              :sent_on => Time.now.utc,
 
  48                              :title => params[:title],
 
  49                              :body => params[:body],
 
  50                              :body_format => "markdown")
 
  53       UserMailer.message_notification(@message).deliver_later if @message.notify_recipient?
 
  55       # Return a copy of the new message
 
  56       respond_to do |format|
 
  57         format.xml { render :action => :show }
 
  58         format.json { render :action => :show }
 
  62     # Update read status of a message
 
  64       @message = Message.find(params[:id])
 
  65       read_status_idx = %w[true false].index params[:read_status]
 
  67       raise OSM::APIBadUserInput, "Invalid value of `read_status` was given" if read_status_idx.nil?
 
  68       raise OSM::APIAccessDenied unless current_user.id == @message.to_user_id
 
  70       @message.message_read = read_status_idx.zero?
 
  73       # Return a copy of the message
 
  74       respond_to do |format|
 
  75         format.xml { render :action => :show }
 
  76         format.json { render :action => :show }
 
  80     # Delete message by marking it as not visible for the current user
 
  82       @message = Message.find(params[:id])
 
  83       if current_user.id == @message.from_user_id
 
  84         @message.from_user_visible = false
 
  85       elsif current_user.id == @message.to_user_id
 
  86         @message.to_user_visible = false
 
  88         raise OSM::APIAccessDenied
 
  93       # Return a copy of the message
 
  94       respond_to do |format|
 
  95         format.xml { render :action => :show }
 
  96         format.json { render :action => :show }