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
11 @title = 'send message'
13 @message = Message.new(params[:message])
14 @message.to_user_id = params[:user_id]
15 @message.from_user_id = @user.id
16 @message.sent_on = Time.now
19 flash[:notice] = 'Message sent'
20 Notifier::deliver_message_notification(@message)
21 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
24 @title = params[:title]
28 # Allow the user to reply to another message.
30 message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
31 @body = "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}"
32 @title = "Re: #{message.title.sub(/^Re:\s*/, '')}"
33 @user_id = message.from_user_id
34 render :action => 'new'
35 rescue ActiveRecord::RecordNotFound
36 render :nothing => true, :status => :not_found
41 @title = 'read message'
42 @message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
43 @message.message_read = 1 if @message.to_user_id == @user.id
45 rescue ActiveRecord::RecordNotFound
46 render :nothing => true, :status => :not_found
49 # Display the list of messages that have been sent to the user.
52 if @user and params[:display_name] == @user.display_name
54 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
58 # Display the list of messages that the user has sent to other users.
61 if @user and params[:display_name] == @user.display_name
63 redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
67 # Set the message as being read or unread.
69 if params[:message_id]
70 id = params[:message_id]
71 message = Message.find_by_id(id)
72 if params[:mark] == 'unread'
79 message.message_read = message_read
81 flash[:notice] = "Message marked as #{mark_type}"
82 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name