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