1 class MessagesController < ApplicationController
4 before_action :authorize_web
5 before_action :set_locale
6 before_action :require_user
7 before_action :lookup_user, :only => [:new, :create]
8 before_action :check_database_readable
9 before_action :check_database_writable, :only => [:new, :create, :reply, :mark, :destroy]
10 before_action :allow_thirdparty_images, :only => [:new, :create, :show]
12 # Allow the user to write a new message to another user. This action also
13 # deals with the sending of that message to the other user when the user
15 # The display_name param is the display name of the user that the message is being sent to.
17 @message = Message.new(message_params.merge(:recipient => @user))
22 @message = Message.new(message_params)
23 @message.recipient = @user
24 @message.sender = current_user
25 @message.sent_on = Time.now.getutc
27 if current_user.sent_messages.where("sent_on >= ?", Time.now.getutc - 1.hour).count >= MAX_MESSAGES_PER_HOUR
28 flash[:error] = t ".limit_exceeded"
30 flash[:notice] = t ".message_sent"
31 Notifier.message_notification(@message).deliver_now
32 redirect_to :action => :inbox
35 render :action => "new"
38 # Allow the user to reply to another message.
40 message = Message.find(params[:message_id])
42 if message.recipient == current_user
43 message.update(:message_read => true)
45 @message = Message.new(
46 :recipient => message.sender,
47 :title => "Re: #{message.title.sub(/^Re:\s*/, '')}",
48 :body => "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}"
51 @title = @message.title
53 render :action => "new"
55 flash[:notice] = t ".wrong_user", :user => current_user.display_name
56 redirect_to :controller => "user", :action => "login", :referer => request.fullpath
58 rescue ActiveRecord::RecordNotFound
59 @title = t "messages.no_such_message.title"
60 render :action => "no_such_message", :status => :not_found
66 @message = Message.find(params[:id])
68 if @message.recipient == current_user || @message.sender == current_user
69 @message.message_read = true if @message.recipient == current_user
72 flash[:notice] = t ".wrong_user", :user => current_user.display_name
73 redirect_to :controller => "user", :action => "login", :referer => request.fullpath
75 rescue ActiveRecord::RecordNotFound
76 @title = t "messages.no_such_message.title"
77 render :action => "no_such_message", :status => :not_found
80 # Display the list of messages that have been sent to the user.
85 # Display the list of messages that the user has sent to other users.
90 # Set the message as being read or unread.
92 @message = Message.where("to_user_id = ? OR from_user_id = ?", current_user.id, current_user.id).find(params[:message_id])
93 if params[:mark] == "unread"
95 notice = t ".as_unread"
100 @message.message_read = message_read
101 if @message.save && !request.xhr?
102 flash[:notice] = notice
103 redirect_to :action => :inbox
105 rescue ActiveRecord::RecordNotFound
106 @title = t "messages.no_such_message.title"
107 render :action => "no_such_message", :status => :not_found
110 # Destroy the message.
112 @message = Message.where("to_user_id = ? OR from_user_id = ?", current_user.id, current_user.id).find(params[:message_id])
113 @message.from_user_visible = false if @message.sender == current_user
114 @message.to_user_visible = false if @message.recipient == current_user
115 if @message.save && !request.xhr?
116 flash[:notice] = t ".destroyed"
119 redirect_to params[:referer]
121 redirect_to :action => :inbox
124 rescue ActiveRecord::RecordNotFound
125 @title = t "messages.no_such_message.title"
126 render :action => "no_such_message", :status => :not_found
132 # return permitted message parameters
134 params.require(:message).permit(:title, :body)
135 rescue ActionController::ParameterMissing
136 ActionController::Parameters.new.permit(:title, :body)