1 class MessagesController < ApplicationController
4 before_action :authorize_web
5 before_action :set_locale
9 before_action :lookup_user, :only => [:new, :create]
10 before_action :check_database_readable
11 before_action :check_database_writable, :only => [:new, :create, :reply, :mark, :destroy]
12 before_action :allow_thirdparty_images, :only => [:new, :create, :show]
17 @message = Message.find(params[:id])
19 if @message.recipient == current_user || @message.sender == current_user
20 @message.message_read = true if @message.recipient == current_user
23 flash[:notice] = t ".wrong_user", :user => current_user.display_name
24 redirect_to login_path(:referer => request.fullpath)
26 rescue ActiveRecord::RecordNotFound
27 @title = t "messages.no_such_message.title"
28 render :action => "no_such_message", :status => :not_found
31 # Allow the user to write a new message to another user. This action also
32 # deals with the sending of that message to the other user when the user
34 # The display_name param is the display name of the user that the message is being sent to.
36 @message = Message.new(message_params.merge(:recipient => @user))
41 @message = Message.new(message_params)
42 @message.recipient = @user
43 @message.sender = current_user
44 @message.sent_on = Time.now.utc
46 if current_user.sent_messages.where("sent_on >= ?", Time.now.utc - 1.hour).count >= current_user.max_messages_per_hour
47 flash.now[:error] = t ".limit_exceeded"
48 render :action => "new"
50 flash[:notice] = t ".message_sent"
51 UserMailer.message_notification(@message).deliver_later
52 redirect_to :action => :inbox
54 @title = t "messages.new.title"
55 render :action => "new"
59 # Destroy the message.
61 @message = Message.where("to_user_id = ? OR from_user_id = ?", current_user.id, current_user.id).find(params[:id])
62 @message.from_user_visible = false if @message.sender == current_user
63 @message.to_user_visible = false if @message.recipient == current_user
64 if @message.save && !request.xhr?
65 flash[:notice] = t ".destroyed"
67 referer = safe_referer(params[:referer]) if params[:referer]
69 redirect_to referer || { :action => :inbox }
71 rescue ActiveRecord::RecordNotFound
72 @title = t "messages.no_such_message.title"
73 render :action => "no_such_message", :status => :not_found
76 # Allow the user to reply to another message.
78 message = Message.find(params[:message_id])
80 if message.recipient == current_user
81 message.update(:message_read => true)
83 @message = Message.new(
84 :recipient => message.sender,
85 :title => "Re: #{message.title.sub(/^Re:\s*/, '')}",
86 :body => "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}"
89 @title = @message.title
91 render :action => "new"
93 flash[:notice] = t ".wrong_user", :user => current_user.display_name
94 redirect_to login_path(:referer => request.fullpath)
96 rescue ActiveRecord::RecordNotFound
97 @title = t "messages.no_such_message.title"
98 render :action => "no_such_message", :status => :not_found
101 # Display the list of messages that have been sent to the user.
106 # Display the list of messages that the user has sent to other users.
111 # Set the message as being read or unread.
113 @message = Message.where("to_user_id = ? OR from_user_id = ?", current_user.id, current_user.id).find(params[:message_id])
114 if params[:mark] == "unread"
116 notice = t ".as_unread"
119 notice = t ".as_read"
121 @message.message_read = message_read
122 if @message.save && !request.xhr?
123 flash[:notice] = notice
124 redirect_to :action => :inbox
126 rescue ActiveRecord::RecordNotFound
127 @title = t "messages.no_such_message.title"
128 render :action => "no_such_message", :status => :not_found
134 # return permitted message parameters
136 params.require(:message).permit(:title, :body)
137 rescue ActionController::ParameterMissing
138 ActionController::Parameters.new.permit(:title, :body)