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 @to_user = User.find_by_display_name(params[:display_name])
18 if @user.sent_messages.where("sent_on >= ?", Time.now.getutc - 1.hour).count >= MAX_MESSAGES_PER_HOUR
19 flash[:error] = t 'message.new.limit_exceeded'
21 @message = Message.new(params[:message])
22 @message.to_user_id = @to_user.id
23 @message.from_user_id = @user.id
24 @message.sent_on = Time.now.getutc
27 flash[:notice] = t 'message.new.message_sent'
28 Notifier.message_notification(@message).deliver
29 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
34 # ?title= is set when someone reponds to this user's diary
35 # entry. Then we pre-fill out the subject and the <title>
36 @title = @subject = params[:title]
38 # The default /message/new/$user view
39 @title = t 'message.new.title'
43 render_unknown_user params[:display_name]
47 # Allow the user to reply to another message.
49 message = Message.find(params[:message_id])
51 if message.to_user_id == @user.id then
52 @body = "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}"
53 @title = @subject = "Re: #{message.title.sub(/^Re:\s*/, '')}"
54 @to_user = User.find(message.from_user_id)
56 render :action => 'new'
58 flash[:notice] = t 'message.reply.wrong_user', :user => @user.display_name
59 redirect_to :controller => "user", :action => "login", :referer => request.fullpath
61 rescue ActiveRecord::RecordNotFound
62 @title = t'message.no_such_message.title'
63 render :action => 'no_such_message', :status => :not_found
68 @title = t 'message.read.title'
69 @message = Message.find(params[:message_id])
71 if @message.to_user_id == @user.id or @message.from_user_id == @user.id then
72 @message.message_read = true if @message.to_user_id == @user.id
75 flash[:notice] = t 'message.read.wrong_user', :user => @user.display_name
76 redirect_to :controller => "user", :action => "login", :referer => request.fullpath
78 rescue ActiveRecord::RecordNotFound
79 @title = t'message.no_such_message.title'
80 render :action => 'no_such_message', :status => :not_found
83 # Display the list of messages that have been sent to the user.
85 @title = t 'message.inbox.title'
86 if @user and params[:display_name] == @user.display_name
88 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
92 # Display the list of messages that the user has sent to other users.
94 @title = t 'message.outbox.title'
95 if @user and params[:display_name] == @user.display_name
97 redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
101 # Set the message as being read or unread.
103 if params[:message_id]
104 id = params[:message_id]
105 @message = Message.where(:id => id).where("to_user_id = ? OR from_user_id = ?", @user.id, @user.id).first
106 if params[:mark] == 'unread'
108 notice = t 'message.mark.as_unread'
111 notice = t 'message.mark.as_read'
113 @message.message_read = message_read
116 flash[:notice] = notice
117 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
121 rescue ActiveRecord::RecordNotFound
122 @title = t'message.no_such_message.title'
123 render :action => 'no_such_message', :status => :not_found
126 # Delete the message.
128 if params[:message_id]
129 id = params[:message_id]
130 message = Message.where(:id => id).where("to_user_id = ? OR from_user_id = ?", @user.id, @user.id).first
131 message.from_user_visible = false if message.sender == @user
132 message.to_user_visible = false if message.recipient == @user
134 flash[:notice] = t 'message.delete.deleted'
137 redirect_to params[:referer]
139 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
143 rescue ActiveRecord::RecordNotFound
144 @title = t'message.no_such_message.title'
145 render :action => 'no_such_message', :status => :not_found