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