]> git.openstreetmap.org Git - rails.git/blob - app/controllers/message_controller.rb
Call the set_locale filter for oauth methods which display web
[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
31           # entry. Then we pre-fill out the subject and the <title>
32           @title = @subject = params[:title]
33         else
34           # The default /message/new/$user view
35           @title = t 'message.new.title'
36         end
37       end
38     else
39       @title = t'message.no_such_user.title'
40       render :action => 'no_such_user', :status => :not_found
41     end
42   end
43
44   # Allow the user to reply to another message.
45   def reply
46     message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
47     @body = "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}" 
48     @title = @subject = "Re: #{message.title.sub(/^Re:\s*/, '')}"
49     @to_user = User.find(message.from_user_id)
50     render :action => 'new'
51   rescue ActiveRecord::RecordNotFound
52     @title = t'message.no_such_user.title'
53     render :action => 'no_such_user', :status => :not_found
54   end
55
56   # Show a message
57   def read
58     @title = t 'message.read.title'
59     @message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
60     @message.message_read = true if @message.to_user_id == @user.id
61     @message.save
62   rescue ActiveRecord::RecordNotFound
63     @title = t'message.no_such_user.title'
64     render :action => 'no_such_user', :status => :not_found
65   end
66
67   # Display the list of messages that have been sent to the user.
68   def inbox
69     @title = t 'message.inbox.title'
70     if @user and params[:display_name] == @user.display_name
71     else
72       redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
73     end
74   end
75
76   # Display the list of messages that the user has sent to other users.
77   def outbox
78     @title = t 'message.outbox.title'
79     if @user and params[:display_name] == @user.display_name
80     else
81       redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
82     end
83   end
84
85   # Set the message as being read or unread.
86   def mark
87     if params[:message_id]
88       id = params[:message_id]
89       message = Message.find_by_id(id)
90       if params[:mark] == 'unread'
91         message_read = false 
92         notice = t 'message.mark.as_unread'
93       else
94         message_read = true
95         notice = t 'message.mark.as_read'
96       end
97       message.message_read = message_read
98       if message.save
99         flash[:notice] = notice
100         redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
101       end
102     end
103   rescue ActiveRecord::RecordNotFound
104     @title = t'message.no_such_user.title'
105     render :action => 'no_such_user', :status => :not_found
106   end
107
108   # Delete the message.
109   def delete
110     if params[:message_id]
111       id = params[:message_id]
112       message = Message.find_by_id(id)
113       message.from_user_visible = false if message.sender == @user
114       message.to_user_visible = false if message.recipient == @user
115       if message.save
116         flash[:notice] = t 'message.delete.deleted'
117
118         if params[:referer]
119           redirect_to params[:referer]
120         else
121           redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
122         end
123       end
124     end
125   rescue ActiveRecord::RecordNotFound
126     @title = t'message.no_such_user.title'
127     render :action => 'no_such_user', :status => :not_found
128   end
129 end