]> git.openstreetmap.org Git - rails.git/blob - app/controllers/message_controller.rb
Added redactions controller test and fixed a bug in the controller
[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 :lookup_this_user, :only => [:new]
8   before_filter :check_database_readable
9   before_filter :check_database_writable, :only => [:new, :reply, :mark]
10
11   # Allow the user to write a new message to another user. This action also
12   # deals with the sending of that message to the other user when the user
13   # clicks send.
14   # The display_name param is the display name of the user that the message is being sent to.
15   def new
16     if params[:message]
17       if @user.sent_messages.where("sent_on >= ?", Time.now.getutc - 1.hour).count >= MAX_MESSAGES_PER_HOUR
18         flash[:error] = t 'message.new.limit_exceeded'
19       else
20         @message = Message.new(params[:message])
21         @message.to_user_id = @this_user.id
22         @message.from_user_id = @user.id
23         @message.sent_on = Time.now.getutc
24
25         if @message.save
26           flash[:notice] = t 'message.new.message_sent'
27           Notifier.message_notification(@message).deliver
28           redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
29         end
30       end
31     else
32       @title = t 'message.new.title'
33     end
34   end
35
36   # Allow the user to reply to another message.
37   def reply
38     message = Message.find(params[:message_id])
39
40     if message.to_user_id == @user.id then
41       @body = "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}"
42       @title = @subject = "Re: #{message.title.sub(/^Re:\s*/, '')}"
43       @this_user = User.find(message.from_user_id)
44
45       render :action => 'new'
46     else
47       flash[:notice] = t 'message.reply.wrong_user', :user => @user.display_name
48       redirect_to :controller => "user", :action => "login", :referer => request.fullpath
49     end
50   rescue ActiveRecord::RecordNotFound
51     @title = t'message.no_such_message.title'
52     render :action => 'no_such_message', :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])
59
60     if @message.to_user_id == @user.id or @message.from_user_id == @user.id then
61       @message.message_read = true if @message.to_user_id == @user.id
62       @message.save
63     else
64       flash[:notice] = t 'message.read.wrong_user', :user => @user.display_name
65       redirect_to :controller => "user", :action => "login", :referer => request.fullpath
66     end
67   rescue ActiveRecord::RecordNotFound
68     @title = t'message.no_such_message.title'
69     render :action => 'no_such_message', :status => :not_found
70   end
71
72   # Display the list of messages that have been sent to the user.
73   def inbox
74     @title = t 'message.inbox.title'
75     if @user and params[:display_name] == @user.display_name
76     else
77       redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
78     end
79   end
80
81   # Display the list of messages that the user has sent to other users.
82   def outbox
83     @title = t 'message.outbox.title'
84     if @user and params[:display_name] == @user.display_name
85     else
86       redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
87     end
88   end
89
90   # Set the message as being read or unread.
91   def mark
92     @message = Message.where("to_user_id = ? OR from_user_id = ?", @user.id, @user.id).find(params[:message_id])
93     if params[:mark] == 'unread'
94       message_read = false
95       notice = t 'message.mark.as_unread'
96     else
97       message_read = true
98       notice = t 'message.mark.as_read'
99     end
100     @message.message_read = message_read
101     if @message.save
102       if not request.xhr?
103         flash[:notice] = notice
104         redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
105       end
106     end
107   rescue ActiveRecord::RecordNotFound
108     @title = t'message.no_such_message.title'
109     render :action => 'no_such_message', :status => :not_found
110   end
111
112   # Delete the message.
113   def delete
114     message = Message.where("to_user_id = ? OR from_user_id = ?", @user.id, @user.id).find(params[:message_id])
115     message.from_user_visible = false if message.sender == @user
116     message.to_user_visible = false if message.recipient == @user
117     if message.save
118       flash[:notice] = t 'message.delete.deleted'
119
120       if params[:referer]
121         redirect_to params[:referer]
122       else
123         redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
124       end
125     end
126   rescue ActiveRecord::RecordNotFound
127     @title = t'message.no_such_message.title'
128     render :action => 'no_such_message', :status => :not_found
129   end
130 end