]> git.openstreetmap.org Git - rails.git/blob - app/controllers/message_controller.rb
Edgemaster & shaunmcdonald pointed out that my ad-hoc hackery was
[rails.git] / app / controllers / message_controller.rb
1 class MessageController < ApplicationController
2   layout 'site'
3
4   before_filter :authorize_web
5   before_filter :set_locale
6   before_filter :require_user
7   before_filter :check_database_readable
8   before_filter :check_database_writable, :only => [:new, :reply, :mark]
9
10   # Allow the user to write a new message to another user. This action also 
11   # deals with the sending of that message to the other user when the user
12   # clicks send.
13   # The user_id param is the id of the user that the message is being sent to.
14   def new
15     @title = t 'message.new.title'
16     @to_user = User.find(params[:user_id])
17     if params[:message]
18       @message = Message.new(params[:message])
19       @message.to_user_id = @to_user.id
20       @message.from_user_id = @user.id
21       @message.sent_on = Time.now.getutc
22    
23       if @message.save
24         flash[:notice] = t 'message.new.message_sent'
25         Notifier::deliver_message_notification(@message)
26         redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
27       end
28     else
29       @title = params[:title]
30     end
31   rescue ActiveRecord::RecordNotFound
32     @title = t'message.no_such_user.title'
33     render :action => 'no_such_user', :status => :not_found
34   end
35
36   # Allow the user to reply to another message.
37   def reply
38     message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
39     @body = "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}" 
40     @title = "Re: #{message.title.sub(/^Re:\s*/, '')}"
41     @to_user = User.find(message.from_user_id)
42     render :action => 'new'
43   rescue ActiveRecord::RecordNotFound
44     @title = t'message.no_such_user.title'
45     render :action => 'no_such_user', :status => :not_found
46   end
47
48   # Show a message
49   def read
50     @title = t 'message.read.title'
51     @message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
52     @message.message_read = true if @message.to_user_id == @user.id
53     @message.save
54   rescue ActiveRecord::RecordNotFound
55     @title = t'message.no_such_user.title'
56     render :action => 'no_such_user', :status => :not_found
57   end
58
59   # Display the list of messages that have been sent to the user.
60   def inbox
61     @title = t 'message.inbox.title'
62     if @user and params[:display_name] == @user.display_name
63     else
64       redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
65     end
66   end
67
68   # Display the list of messages that the user has sent to other users.
69   def outbox
70     @title = t 'message.outbox.title'
71     if @user and params[:display_name] == @user.display_name
72     else
73       redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
74     end
75   end
76
77   # Set the message as being read or unread.
78   def mark
79     if params[:message_id]
80       id = params[:message_id]
81       message = Message.find_by_id(id)
82       if params[:mark] == 'unread'
83         message_read = false 
84         notice = t 'message.mark.as_unread'
85       else
86         message_read = true
87         notice = t 'message.mark.as_read'
88       end
89       message.message_read = message_read
90       if message.save
91         flash[:notice] = notice
92         redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
93       end
94     end
95   rescue ActiveRecord::RecordNotFound
96     @title = t'message.no_such_user.title'
97     render :action => 'no_such_user', :status => :not_found
98   end
99 end