1 # frozen_string_literal: true
 
   3 # The MessagesController is the RESTful interface to Message objects
 
   6   class MessagesController < ApiController
 
   7     before_action :authorize
 
   9     before_action :check_api_writable, :only => [:create, :update, :destroy]
 
  13     before_action :set_request_formats
 
  15     # Dump the details on a message given in params[:id]
 
  17       @message = Message.includes(:sender, :recipient).find(params[:id])
 
  19       raise OSM::APIAccessDenied if current_user.id != @message.from_user_id && current_user.id != @message.to_user_id
 
  22       respond_to do |format|
 
  28     # Create a new message from current user
 
  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?
 
  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)
 
  42         raise OSM::APIBadUserInput, "No recipient was given"
 
  45       raise OSM::APIRateLimitExceeded if current_user.sent_messages.where(:sent_on => (Time.now.utc - 1.hour)..).count >= current_user.max_messages_per_hour
 
  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")
 
  55       UserMailer.message_notification(@message).deliver_later if @message.notify_recipient?
 
  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 }
 
  64     # Update read status of a message
 
  66       @message = Message.find(params[:id])
 
  67       read_status_idx = %w[true false].index params[:read_status]
 
  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
 
  72       @message.message_read = read_status_idx.zero?
 
  75       # Return a copy of the message
 
  76       respond_to do |format|
 
  77         format.xml { render :action => :show }
 
  78         format.json { render :action => :show }
 
  82     # Delete message by marking it as not visible for the current user
 
  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
 
  90         raise OSM::APIAccessDenied
 
  95       # Return a copy of the message
 
  96       respond_to do |format|
 
  97         format.xml { render :action => :show }
 
  98         format.json { render :action => :show }