1 class MessageController < ApplicationController
4 before_filter :authorize_web
5 before_filter :require_user
7 # Allow the user to write a new message to another user. This action also
8 # deals with the sending of that message to the other user when the user
10 # The user_id param is the id of the user that the message is being sent to.
12 @title = 'send message'
13 @to_user = User.find(params[:user_id])
15 @message = Message.new(params[:message])
16 @message.to_user_id = @to_user.id
17 @message.from_user_id = @user.id
18 @message.sent_on = Time.now
21 flash[:notice] = 'Message sent'
22 Notifier::deliver_message_notification(@message)
23 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
26 @title = params[:title]
28 rescue ActiveRecord::RecordNotFound
29 render :action => 'no_such_user', :status => :not_found
32 # Allow the user to reply to another message.
34 message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
35 @body = "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}"
36 @title = "Re: #{message.title.sub(/^Re:\s*/, '')}"
37 @user_id = message.from_user_id
38 render :action => 'new'
39 rescue ActiveRecord::RecordNotFound
40 render :action => 'no_such_user', :status => :not_found
45 @title = 'read message'
46 @message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
47 @message.message_read = true if @message.to_user_id == @user.id
49 rescue ActiveRecord::RecordNotFound
50 render :action => 'no_such_user', :status => :not_found
53 # Display the list of messages that have been sent to the user.
56 if @user and params[:display_name] == @user.display_name
58 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
62 # Display the list of messages that the user has sent to other users.
65 if @user and params[:display_name] == @user.display_name
67 redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
71 # Set the message as being read or unread.
73 if params[:message_id]
74 id = params[:message_id]
75 message = Message.find_by_id(id)
76 if params[:mark] == 'unread'
83 message.message_read = message_read
85 flash[:notice] = "Message marked as #{mark_type}"
86 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
89 rescue ActiveRecord::RecordNotFound
90 render :action => 'no_such_user', :status => :not_found