]> git.openstreetmap.org Git - rails.git/blob - app/controllers/message_controller.rb
Rather than referring how to view a bbox history, just redirect to the global history.
[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     @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
42   end
43
44   # Show a message
45   def read
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
49     @message.save
50   rescue ActiveRecord::RecordNotFound
51     render :action => 'no_such_user', :status => :not_found
52   end
53
54   # Display the list of messages that have been sent to the user.
55   def inbox
56     @title = 'inbox'
57     if @user and params[:display_name] == @user.display_name
58     else
59       redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
60     end
61   end
62
63   # Display the list of messages that the user has sent to other users.
64   def outbox
65     @title = 'outbox'
66     if @user and params[:display_name] == @user.display_name
67     else
68       redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
69     end
70   end
71
72   # Set the message as being read or unread.
73   def mark
74     if params[:message_id]
75       id = params[:message_id]
76       message = Message.find_by_id(id)
77       if params[:mark] == 'unread'
78         message_read = false 
79         mark_type = 'unread'
80       else
81         message_read = true
82         mark_type = 'read'
83       end
84       message.message_read = message_read
85       if message.save
86         flash[:notice] = "Message marked as #{mark_type}"
87         redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
88       end
89     end
90   rescue ActiveRecord::RecordNotFound
91     render :action => 'no_such_user', :status => :not_found
92   end
93 end