]> git.openstreetmap.org Git - rails.git/blob - app/controllers/message_controller.rb
translated "use map" as "finna รก korti" or "find on map"
[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 user_id param is the id of the user that the message is being sent to.
14   def new
15     @title = 'send message'
16     @to_user = User.find(params[:user_id])
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] = '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       @title = params[:title]
30     end
31   rescue ActiveRecord::RecordNotFound
32     render :action => 'no_such_user', :status => :not_found
33   end
34
35   # Allow the user to reply to another message.
36   def reply
37     message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
38     @body = "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}" 
39     @title = "Re: #{message.title.sub(/^Re:\s*/, '')}"
40     @to_user = User.find(message.from_user_id)
41     render :action => 'new'
42   rescue ActiveRecord::RecordNotFound
43     render :action => 'no_such_user', :status => :not_found
44   end
45
46   # Show a message
47   def read
48     @title = 'read message'
49     @message = Message.find(params[:message_id], :conditions => ["to_user_id = ? or from_user_id = ?", @user.id, @user.id ])
50     @message.message_read = true if @message.to_user_id == @user.id
51     @message.save
52   rescue ActiveRecord::RecordNotFound
53     render :action => 'no_such_user', :status => :not_found
54   end
55
56   # Display the list of messages that have been sent to the user.
57   def inbox
58     @title = 'inbox'
59     if @user and params[:display_name] == @user.display_name
60     else
61       redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
62     end
63   end
64
65   # Display the list of messages that the user has sent to other users.
66   def outbox
67     @title = 'outbox'
68     if @user and params[:display_name] == @user.display_name
69     else
70       redirect_to :controller => 'message', :action => 'outbox', :display_name => @user.display_name
71     end
72   end
73
74   # Set the message as being read or unread.
75   def mark
76     if params[:message_id]
77       id = params[:message_id]
78       message = Message.find_by_id(id)
79       if params[:mark] == 'unread'
80         message_read = false 
81         mark_type = 'unread'
82       else
83         message_read = true
84         mark_type = 'read'
85       end
86       message.message_read = message_read
87       if message.save
88         flash[:notice] = "Message marked as #{mark_type}"
89         redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name
90       end
91     end
92   rescue ActiveRecord::RecordNotFound
93     render :action => 'no_such_user', :status => :not_found
94   end
95 end