1 class MessageController < ApplicationController
4 before_filter :authorize_web
5 before_filter :set_locale
6 before_filter :require_user
7 before_filter :lookup_this_user, :only => [:new]
8 before_filter :check_database_readable
9 before_filter :check_database_writable, :only => [:new, :reply, :mark]
11 # Allow the user to write a new message to another user. This action also
12 # deals with the sending of that message to the other user when the user
14 # The display_name param is the display name of the user that the message is being sent to.
17 if @user.sent_messages.where("sent_on >= ?", Time.now.getutc - 1.hour).count >= MAX_MESSAGES_PER_HOUR
18 flash[:error] = t 'message.new.limit_exceeded'
20 @message = Message.new(params[:message])
21 @message.to_user_id = @this_user.id
22 @message.from_user_id = @user.id
23 @message.sent_on = Time.now.getutc
26 flash[:notice] = t 'message.new.message_sent'
27 Notifier.message_notification(@message).deliver
28 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
32 @title = t 'message.new.title'
36 # Allow the user to reply to another message.
38 message = Message.find(params[:message_id])
40 if message.to_user_id == @user.id then
41 message.update_attribute(:message_read, true)
43 @body = "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}"
44 @title = @subject = "Re: #{message.title.sub(/^Re:\s*/, '')}"
45 @this_user = User.find(message.from_user_id)
47 render :action => 'new'
49 flash[:notice] = t 'message.reply.wrong_user', :user => @user.display_name
50 redirect_to :controller => "user", :action => "login", :referer => request.fullpath
52 rescue ActiveRecord::RecordNotFound
53 @title = t'message.no_such_message.title'
54 render :action => 'no_such_message', :status => :not_found
59 @title = t 'message.read.title'
60 @message = Message.find(params[:message_id])
62 if @message.to_user_id == @user.id or @message.from_user_id == @user.id then
63 @message.message_read = true if @message.to_user_id == @user.id
66 flash[:notice] = t 'message.read.wrong_user', :user => @user.display_name
67 redirect_to :controller => "user", :action => "login", :referer => request.fullpath
69 rescue ActiveRecord::RecordNotFound
70 @title = t'message.no_such_message.title'
71 render :action => 'no_such_message', :status => :not_found
74 # Display the list of messages that have been sent to the user.
76 @title = t 'message.inbox.title'
77 if @user and params[:display_name] == @user.display_name
79 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
83 # Display the list of messages that the user has sent to other users.
85 @title = t 'message.outbox.title'
86 if @user and params[:display_name] == @user.display_name
88 redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
92 # Set the message as being read or unread.
94 @message = Message.where("to_user_id = ? OR from_user_id = ?", @user.id, @user.id).find(params[:message_id])
95 if params[:mark] == 'unread'
97 notice = t 'message.mark.as_unread'
100 notice = t 'message.mark.as_read'
102 @message.message_read = message_read
105 flash[:notice] = notice
106 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
109 rescue ActiveRecord::RecordNotFound
110 @title = t'message.no_such_message.title'
111 render :action => 'no_such_message', :status => :not_found
114 # Delete the message.
116 message = Message.where("to_user_id = ? OR from_user_id = ?", @user.id, @user.id).find(params[:message_id])
117 message.from_user_visible = false if message.sender == @user
118 message.to_user_visible = false if message.recipient == @user
120 flash[:notice] = t 'message.delete.deleted'
123 redirect_to params[:referer]
125 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
128 rescue ActiveRecord::RecordNotFound
129 @title = t'message.no_such_message.title'
130 render :action => 'no_such_message', :status => :not_found