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
31 # entry. Then we pre-fill out the subject and the <title>
32 @title = @subject = params[:title]
34 # The default /message/new/$user view
35 @title = t 'message.new.title'
39 @title = t'message.no_such_user.title'
40 render :action => 'no_such_user', :status => :not_found
44 # Allow the user to reply to another message.
46 message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
47 @body = "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}"
48 @title = @subject = "Re: #{message.title.sub(/^Re:\s*/, '')}"
49 @to_user = User.find(message.from_user_id)
50 render :action => 'new'
51 rescue ActiveRecord::RecordNotFound
52 @title = t'message.no_such_user.title'
53 render :action => 'no_such_user', :status => :not_found
58 @title = t 'message.read.title'
59 @message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
60 @message.message_read = true if @message.to_user_id == @user.id
62 rescue ActiveRecord::RecordNotFound
63 @title = t'message.no_such_user.title'
64 render :action => 'no_such_user', :status => :not_found
67 # Display the list of messages that have been sent to the user.
69 @title = t 'message.inbox.title'
70 if @user and params[:display_name] == @user.display_name
72 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
76 # Display the list of messages that the user has sent to other users.
78 @title = t 'message.outbox.title'
79 if @user and params[:display_name] == @user.display_name
81 redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
85 # Set the message as being read or unread.
87 if params[:message_id]
88 id = params[:message_id]
89 message = Message.find_by_id(id)
90 if params[:mark] == 'unread'
92 notice = t 'message.mark.as_unread'
95 notice = t 'message.mark.as_read'
97 message.message_read = message_read
99 flash[:notice] = notice
100 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
103 rescue ActiveRecord::RecordNotFound
104 @title = t'message.no_such_user.title'
105 render :action => 'no_such_user', :status => :not_found
108 # Delete the message.
110 if params[:message_id]
111 id = params[:message_id]
112 message = Message.find_by_id(id)
113 message.from_user_visible = false if message.sender == @user
114 message.to_user_visible = false if message.recipient == @user
116 flash[:notice] = t 'message.delete.deleted'
119 redirect_to params[:referer]
121 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
125 rescue ActiveRecord::RecordNotFound
126 @title = t'message.no_such_user.title'
127 render :action => 'no_such_user', :status => :not_found