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 @to_user = User.find(message.to_user_id)
39 render :action => 'new'
40 rescue ActiveRecord::RecordNotFound
41 render :action => 'no_such_user', :status => :not_found
46 @title = 'read message'
47 @message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
48 @message.message_read = true if @message.to_user_id == @user.id
50 rescue ActiveRecord::RecordNotFound
51 render :action => 'no_such_user', :status => :not_found
54 # Display the list of messages that have been sent to the user.
57 if @user and params[:display_name] == @user.display_name
59 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
63 # Display the list of messages that the user has sent to other users.
66 if @user and params[:display_name] == @user.display_name
68 redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
72 # Set the message as being read or unread.
74 if params[:message_id]
75 id = params[:message_id]
76 message = Message.find_by_id(id)
77 if params[:mark] == 'unread'
84 message.message_read = message_read
86 flash[:notice] = "Message marked as #{mark_type}"
87 redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
90 rescue ActiveRecord::RecordNotFound
91 render :action => 'no_such_user', :status => :not_found