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 @message = Message.new(params[:message])
19 @message.to_user_id = @to_user.id
20 @message.from_user_id = @user.id
21 @message.sent_on = Time.now.getutc
24 flash[:notice] = t 'message.new.message_sent'
25 Notifier::deliver_message_notification(@message)
26 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
30 # ?title= is set when someone reponds to this user's diary entry
31 @title = params[:title]
33 # The default /message/new/$user view
34 @title = t 'message.new.title'
38 @title = t'message.no_such_user.title'
39 render :action => 'no_such_user', :status => :not_found
43 # Allow the user to reply to another message.
45 message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
46 @body = "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}"
47 @title = "Re: #{message.title.sub(/^Re:\s*/, '')}"
48 @to_user = User.find(message.from_user_id)
49 render :action => 'new'
50 rescue ActiveRecord::RecordNotFound
51 @title = t'message.no_such_user.title'
52 render :action => 'no_such_user', :status => :not_found
57 @title = t 'message.read.title'
58 @message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
59 @message.message_read = true if @message.to_user_id == @user.id
61 rescue ActiveRecord::RecordNotFound
62 @title = t'message.no_such_user.title'
63 render :action => 'no_such_user', :status => :not_found
66 # Display the list of messages that have been sent to the user.
68 @title = t 'message.inbox.title'
69 if @user and params[:display_name] == @user.display_name
71 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
75 # Display the list of messages that the user has sent to other users.
77 @title = t 'message.outbox.title'
78 if @user and params[:display_name] == @user.display_name
80 redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
84 # Set the message as being read or unread.
86 if params[:message_id]
87 id = params[:message_id]
88 message = Message.find_by_id(id)
89 if params[:mark] == 'unread'
91 notice = t 'message.mark.as_unread'
94 notice = t 'message.mark.as_read'
96 message.message_read = message_read
98 flash[:notice] = notice
99 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
102 rescue ActiveRecord::RecordNotFound
103 @title = t'message.no_such_user.title'
104 render :action => 'no_such_user', :status => :not_found