]> git.openstreetmap.org Git - rails.git/blob - app/controllers/message_controller.rb
Making amf controller act more like the xml apis
[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   # The user_id param is the id of the user that the message is being sent to.
11   def new
12     @title = 'send message'
13     @to_user = User.find(params[:user_id])
14     if params[:message]
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
19    
20       if @message.save
21         flash[:notice] = 'Message sent'
22         Notifier::deliver_message_notification(@message)
23         redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
24       end
25     else
26       @title = params[:title]
27     end
28   rescue ActiveRecord::RecordNotFound
29     render :action => 'no_such_user', :status => :not_found
30   end
31
32   # Allow the user to reply to another message.
33   def reply
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
41   end
42
43   # Show a message
44   def read
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
48     @message.save
49   rescue ActiveRecord::RecordNotFound
50     render :action => 'no_such_user', :status => :not_found
51   end
52
53   # Display the list of messages that have been sent to the user.
54   def inbox
55     @title = 'inbox'
56     if @user and params[:display_name] == @user.display_name
57     else
58       redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
59     end
60   end
61
62   # Display the list of messages that the user has sent to other users.
63   def outbox
64     @title = 'outbox'
65     if @user and params[:display_name] == @user.display_name
66     else
67       redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
68     end
69   end
70
71   # Set the message as being read or unread.
72   def mark
73     if params[:message_id]
74       id = params[:message_id]
75       message = Message.find_by_id(id)
76       if params[:mark] == 'unread'
77         message_read = false 
78         mark_type = 'unread'
79       else
80         message_read = true
81         mark_type = 'read'
82       end
83       message.message_read = message_read
84       if message.save
85         flash[:notice] = "Message marked as #{mark_type}"
86         redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
87       end
88     end
89   rescue ActiveRecord::RecordNotFound
90     render :action => 'no_such_user', :status => :not_found
91   end
92 end