]> git.openstreetmap.org Git - rails.git/blob - app/controllers/message_controller.rb
Add some strategic rel="nofollow" attributes to the data browser pages
[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         if @user.sent_messages.count(:conditions => ["sent_on >= ?", Time.now.getutc - 1.hour]) >= APP_CONFIG['max_messages_per_hour']
19           flash[:error] = t 'message.new.limit_exceeded'
20         else
21           @message = Message.new(params[:message])
22           @message.to_user_id = @to_user.id
23           @message.from_user_id = @user.id
24           @message.sent_on = Time.now.getutc
25
26           if @message.save
27             flash[:notice] = t 'message.new.message_sent'
28             Notifier::deliver_message_notification(@message)
29             redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
30           end
31         end
32       else
33         if params[:title]
34           # ?title= is set when someone reponds to this user's diary
35           # entry. Then we pre-fill out the subject and the <title>
36           @title = @subject = params[:title]
37         else
38           # The default /message/new/$user view
39           @title = t 'message.new.title'
40         end
41       end
42     else
43       @title = t'message.no_such_user.title'
44       render :action => 'no_such_user', :status => :not_found
45     end
46   end
47
48   # Allow the user to reply to another message.
49   def reply
50     message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
51     @body = "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}" 
52     @title = @subject = "Re: #{message.title.sub(/^Re:\s*/, '')}"
53     @to_user = User.find(message.from_user_id)
54     render :action => 'new'
55   rescue ActiveRecord::RecordNotFound
56     @title = t'message.no_such_user.title'
57     render :action => 'no_such_user', :status => :not_found
58   end
59
60   # Show a message
61   def read
62     @title = t 'message.read.title'
63     @message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
64     @message.message_read = true if @message.to_user_id == @user.id
65     @message.save
66   rescue ActiveRecord::RecordNotFound
67     @title = t'message.no_such_user.title'
68     render :action => 'no_such_user', :status => :not_found
69   end
70
71   # Display the list of messages that have been sent to the user.
72   def inbox
73     @title = t 'message.inbox.title'
74     if @user and params[:display_name] == @user.display_name
75     else
76       redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
77     end
78   end
79
80   # Display the list of messages that the user has sent to other users.
81   def outbox
82     @title = t 'message.outbox.title'
83     if @user and params[:display_name] == @user.display_name
84     else
85       redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
86     end
87   end
88
89   # Set the message as being read or unread.
90   def mark
91     if params[:message_id]
92       id = params[:message_id]
93       message = Message.find_by_id(id)
94       if params[:mark] == 'unread'
95         message_read = false 
96         notice = t 'message.mark.as_unread'
97       else
98         message_read = true
99         notice = t 'message.mark.as_read'
100       end
101       message.message_read = message_read
102       if message.save
103         if request.xhr?
104           render :update do |page|
105             page.replace "inbox-count", :partial => "message_count"
106             page.replace "inbox-#{message.id}", :partial => "message_summary", :object => message
107           end
108         else
109           flash[:notice] = notice
110           redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
111         end
112       end
113     end
114   rescue ActiveRecord::RecordNotFound
115     @title = t'message.no_such_user.title'
116     render :action => 'no_such_user', :status => :not_found
117   end
118
119   # Delete the message.
120   def delete
121     if params[:message_id]
122       id = params[:message_id]
123       message = Message.find_by_id(id)
124       message.from_user_visible = false if message.sender == @user
125       message.to_user_visible = false if message.recipient == @user
126       if message.save
127         flash[:notice] = t 'message.delete.deleted'
128
129         if params[:referer]
130           redirect_to params[:referer]
131         else
132           redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
133         end
134       end
135     end
136   rescue ActiveRecord::RecordNotFound
137     @title = t'message.no_such_user.title'
138     render :action => 'no_such_user', :status => :not_found
139   end
140 end