]> git.openstreetmap.org Git - rails.git/blob - app/controllers/message_controller.rb
add fixme
[rails.git] / app / controllers / message_controller.rb
1 class MessageController < ApplicationController
2   layout 'site'
3
4   before_filter :authorize_web
5   before_filter :require_user
6
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
9   # clicks send.
10   def new
11     @title = 'send message'
12     if params[: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
17    
18       if @message.save
19         flash[:notice] = 'Message sent'
20         Notifier::deliver_message_notification(@message)
21         redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
22       end
23     else
24       @title = params[:title]
25     end
26   end
27
28   # Allow the user to reply to another message.
29   def reply
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
37   end
38
39   # Show a message
40   def read
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
44     @message.save
45   rescue ActiveRecord::RecordNotFound
46     render :nothing => true, :status => :not_found
47   end
48
49   # Display the list of messages that have been sent to the user.
50   def inbox
51     @title = 'inbox'
52     if @user and params[:display_name] == @user.display_name
53     else
54       redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
55     end
56   end
57
58   # Display the list of messages that the user has sent to other users.
59   def outbox
60     @title = 'outbox'
61     if @user and params[:display_name] == @user.display_name
62     else
63       redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
64     end
65   end
66
67   # Set the message as being read or unread.
68   def mark
69     if params[:message_id]
70       id = params[:message_id]
71       message = Message.find_by_id(id)
72       if params[:mark] == 'unread'
73         message_read = 0 
74         mark_type = 'unread'
75       else
76         message_read = 1
77         mark_type = 'read'
78       end
79       message.message_read = message_read
80       if message.save
81         flash[:notice] = "Message marked as #{mark_type}"
82         redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
83       end
84     end
85   end
86 end