]> git.openstreetmap.org Git - rails.git/blob - app/controllers/message_controller.rb
Add feed action to relevant filters
[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.where("sent_on >= ?", Time.now.getutc - 1.hour).count >= 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.message_notification(@message).deliver
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])
51
52     if message.to_user_id == @user.id then
53       @body = "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}" 
54       @title = @subject = "Re: #{message.title.sub(/^Re:\s*/, '')}"
55       @to_user = User.find(message.from_user_id)
56
57       render :action => 'new'
58     else
59       flash[:notice] = t 'message.reply.wrong_user', :user => @user.display_name
60       redirect_to :controller => "user", :action => "login", :referer => request.fullpath
61     end
62   rescue ActiveRecord::RecordNotFound
63     @title = t'message.no_such_message.title'
64     render :action => 'no_such_message', :status => :not_found
65   end
66
67   # Show a message
68   def read
69     @title = t 'message.read.title'
70     @message = Message.find(params[:message_id])
71
72     if @message.to_user_id == @user.id or @message.from_user_id == @user.id then
73       @message.message_read = true if @message.to_user_id == @user.id
74       @message.save
75     else
76       flash[:notice] = t 'message.read.wrong_user', :user => @user.display_name
77       redirect_to :controller => "user", :action => "login", :referer => request.fullpath
78     end
79   rescue ActiveRecord::RecordNotFound
80     @title = t'message.no_such_message.title'
81     render :action => 'no_such_message', :status => :not_found
82   end
83
84   # Display the list of messages that have been sent to the user.
85   def inbox
86     @title = t 'message.inbox.title'
87     if @user and params[:display_name] == @user.display_name
88     else
89       redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
90     end
91   end
92
93   # Display the list of messages that the user has sent to other users.
94   def outbox
95     @title = t 'message.outbox.title'
96     if @user and params[:display_name] == @user.display_name
97     else
98       redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
99     end
100   end
101
102   # Set the message as being read or unread.
103   def mark
104     if params[:message_id]
105       id = params[:message_id]
106       message = Message.where(:id => id).where("to_user_id = ? OR from_user_id = ?", @user.id, @user.id).first
107       if params[:mark] == 'unread'
108         message_read = false 
109         notice = t 'message.mark.as_unread'
110       else
111         message_read = true
112         notice = t 'message.mark.as_read'
113       end
114       message.message_read = message_read
115       if message.save
116         if request.xhr?
117           render :update do |page|
118             page.replace "inboxanchor", :partial => "layouts/inbox"
119             page.replace "inbox-count", :partial => "message_count"
120             page.replace "inbox-#{message.id}", :partial => "message_summary", :object => message
121           end
122         else
123           flash[:notice] = notice
124           redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
125         end
126       end
127     end
128   rescue ActiveRecord::RecordNotFound
129     @title = t'message.no_such_message.title'
130     render :action => 'no_such_message', :status => :not_found
131   end
132
133   # Delete the message.
134   def delete
135     if params[:message_id]
136       id = params[:message_id]
137       message = Message.where(:id => id).where("to_user_id = ? OR from_user_id = ?", @user.id, @user.id).first
138       message.from_user_visible = false if message.sender == @user
139       message.to_user_visible = false if message.recipient == @user
140       if message.save
141         flash[:notice] = t 'message.delete.deleted'
142
143         if params[:referer]
144           redirect_to params[:referer]
145         else
146           redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
147         end
148       end
149     end
150   rescue ActiveRecord::RecordNotFound
151     @title = t'message.no_such_message.title'
152     render :action => 'no_such_message', :status => :not_found
153   end
154 end