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