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]
15 allow_thirdparty_images :only => [:new, :create, :show]
20 @message = Message.find(params[:id])
22 if @message.recipient == current_user || @message.sender == current_user
23 @message.message_read = true if @message.recipient == current_user
26 flash[:notice] = t ".wrong_user", :user => current_user.display_name
27 redirect_to login_path(:referer => request.fullpath)
29 rescue ActiveRecord::RecordNotFound
30 @title = t "messages.no_such_message.title"
31 render :action => "no_such_message", :status => :not_found
34 # Allow the user to write a new message to another user.
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 if @message.notify_recipient?
52 redirect_to :action => :inbox
54 @title = t "messages.new.title"
55 render :action => "new"
59 # Destroy the message.
61 @message = Message.where(:recipient => current_user).or(Message.where(:sender => 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
65 flash[:notice] = t ".destroyed"
67 referer = safe_referer(params[:referer]) if params[:referer]
69 redirect_to referer || { :action => :inbox }, :status => :see_other
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"
92 elsif message.sender == current_user
93 @message = Message.new(
94 :recipient => message.recipient,
95 :title => "Re: #{message.title.sub(/^Re:\s*/, '')}",
96 :body => "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}"
99 @title = @message.title
101 render :action => "new"
103 flash[:notice] = t ".wrong_user", :user => current_user.display_name
104 redirect_to login_path(:referer => request.fullpath)
106 rescue ActiveRecord::RecordNotFound
107 @title = t "messages.no_such_message.title"
108 render :action => "no_such_message", :status => :not_found
111 # Display the list of messages that have been sent to the user.
116 # Display the list of messages that the user has sent to other users.
121 # Display the list of muted messages received by the user.
125 redirect_to inbox_messages_path if current_user.muted_messages.none?
128 # Set the message as being read or unread.
130 @message = current_user.messages.unscope(:where => :muted).find(params[:message_id])
131 if params[:mark] == "unread"
133 notice = t ".as_unread"
136 notice = t ".as_read"
138 @message.message_read = message_read
140 flash[:notice] = notice
142 redirect_to muted_messages_path, :status => :see_other
144 redirect_to inbox_messages_path, :status => :see_other
147 rescue ActiveRecord::RecordNotFound
148 @title = t "messages.no_such_message.title"
149 render :action => "no_such_message", :status => :not_found
152 # Moves message into Inbox by unsetting the muted-flag
154 message = current_user.muted_messages.find(params[:message_id])
157 flash[:notice] = t(".notice")
159 flash[:error] = t(".error")
162 if current_user.muted_messages.none?
163 redirect_to inbox_messages_path
165 redirect_to muted_messages_path
172 # return permitted message parameters
174 params.require(:message).permit(:title, :body)
175 rescue ActionController::ParameterMissing
176 ActionController::Parameters.new.permit(:title, :body)