1 class MessageController < ApplicationController
4 before_filter :authorize_web
5 before_filter :set_locale
6 before_filter :require_user
7 before_filter :check_database_readable
8 before_filter :check_database_writable, :only => [:new, :reply, :mark]
10 # Allow the user to write a new message to another user. This action also
11 # deals with the sending of that message to the other user when the user
13 # The display_name param is the display name of the user that the message is being sent to.
15 @title = t 'message.new.title'
16 @to_user = User.find_by_display_name(params[:display_name])
19 @message = Message.new(params[:message])
20 @message.to_user_id = @to_user.id
21 @message.from_user_id = @user.id
22 @message.sent_on = Time.now.getutc
25 flash[:notice] = t 'message.new.message_sent'
26 Notifier::deliver_message_notification(@message)
27 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
30 @title = params[:title]
33 @title = t'message.no_such_user.title'
34 render :action => 'no_such_user', :status => :not_found
38 # Allow the user to reply to another message.
40 message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
41 @body = "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}"
42 @title = "Re: #{message.title.sub(/^Re:\s*/, '')}"
43 @to_user = User.find(message.from_user_id)
44 render :action => 'new'
45 rescue ActiveRecord::RecordNotFound
46 @title = t'message.no_such_user.title'
47 render :action => 'no_such_user', :status => :not_found
52 @title = t 'message.read.title'
53 @message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
54 @message.message_read = true if @message.to_user_id == @user.id
56 rescue ActiveRecord::RecordNotFound
57 @title = t'message.no_such_user.title'
58 render :action => 'no_such_user', :status => :not_found
61 # Display the list of messages that have been sent to the user.
63 @title = t 'message.inbox.title'
64 if @user and params[:display_name] == @user.display_name
66 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
70 # Display the list of messages that the user has sent to other users.
72 @title = t 'message.outbox.title'
73 if @user and params[:display_name] == @user.display_name
75 redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
79 # Set the message as being read or unread.
81 if params[:message_id]
82 id = params[:message_id]
83 message = Message.find_by_id(id)
84 if params[:mark] == 'unread'
86 notice = t 'message.mark.as_unread'
89 notice = t 'message.mark.as_read'
91 message.message_read = message_read
93 flash[:notice] = notice
94 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
97 rescue ActiveRecord::RecordNotFound
98 @title = t'message.no_such_user.title'
99 render :action => 'no_such_user', :status => :not_found