1 class MessagesController < ApplicationController
6 before_action :authorize_web
7 before_action :set_locale
11 before_action :lookup_user, :only => [:new, :create]
12 before_action :check_database_readable
13 before_action :check_database_writable, :only => [:new, :create, :reply, :mark, :destroy]
14 before_action :allow_thirdparty_images, :only => [:new, :create, :show]
19 @message = Message.find(params[:id])
21 if @message.recipient == current_user || @message.sender == current_user
22 @message.message_read = true if @message.recipient == current_user
25 flash[:notice] = t ".wrong_user", :user => current_user.display_name
26 redirect_to login_path(:referer => request.fullpath)
28 rescue ActiveRecord::RecordNotFound
29 @title = t "messages.no_such_message.title"
30 render :action => "no_such_message", :status => :not_found
33 # Allow the user to write a new message to another user.
35 @message = Message.new(message_params.merge(:recipient => @user))
40 @message = Message.new(message_params)
41 @message.recipient = @user
42 @message.sender = current_user
43 @message.sent_on = Time.now.utc
45 if current_user.sent_messages.where("sent_on >= ?", Time.now.utc - 1.hour).count >= current_user.max_messages_per_hour
46 flash.now[:error] = t ".limit_exceeded"
47 render :action => "new"
49 flash[:notice] = t ".message_sent"
50 UserMailer.message_notification(@message).deliver_later
51 redirect_to :action => :inbox
53 @title = t "messages.new.title"
54 render :action => "new"
58 # Destroy the message.
60 @message = Message.where("to_user_id = ? OR from_user_id = ?", current_user.id, current_user.id).find(params[:id])
61 @message.from_user_visible = false if @message.sender == current_user
62 @message.to_user_visible = false if @message.recipient == current_user
63 if @message.save && !request.xhr?
64 flash[:notice] = t ".destroyed"
66 referer = safe_referer(params[:referer]) if params[:referer]
68 redirect_to referer || { :action => :inbox }
70 rescue ActiveRecord::RecordNotFound
71 @title = t "messages.no_such_message.title"
72 render :action => "no_such_message", :status => :not_found
75 # Allow the user to reply to another message.
77 message = Message.find(params[:message_id])
79 if message.recipient == current_user
80 message.update(:message_read => true)
82 @message = Message.new(
83 :recipient => message.sender,
84 :title => "Re: #{message.title.sub(/^Re:\s*/, '')}",
85 :body => "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}"
88 @title = @message.title
90 render :action => "new"
92 flash[:notice] = t ".wrong_user", :user => current_user.display_name
93 redirect_to login_path(:referer => request.fullpath)
95 rescue ActiveRecord::RecordNotFound
96 @title = t "messages.no_such_message.title"
97 render :action => "no_such_message", :status => :not_found
100 # Display the list of messages that have been sent to the user.
105 # Display the list of messages that the user has sent to other users.
110 # Set the message as being read or unread.
112 @message = Message.where("to_user_id = ? OR from_user_id = ?", current_user.id, current_user.id).find(params[:message_id])
113 if params[:mark] == "unread"
115 notice = t ".as_unread"
118 notice = t ".as_read"
120 @message.message_read = message_read
121 if @message.save && !request.xhr?
122 flash[:notice] = notice
123 redirect_to :action => :inbox
125 rescue ActiveRecord::RecordNotFound
126 @title = t "messages.no_such_message.title"
127 render :action => "no_such_message", :status => :not_found
133 # return permitted message parameters
135 params.require(:message).permit(:title, :body)
136 rescue ActionController::ParameterMissing
137 ActionController::Parameters.new.permit(:title, :body)