]> git.openstreetmap.org Git - rails.git/blob - app/controllers/message_controller.rb
Fix typo...
[rails.git] / app / controllers / message_controller.rb
1 class MessageController < ApplicationController
2   layout "site"
3
4   before_action :authorize_web
5   before_action :set_locale
6   before_action :require_user
7   before_action :lookup_this_user, :only => [:new]
8   before_action :check_database_readable
9   before_action :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(message_params)
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_now
28           redirect_to :controller => "message", :action => "inbox", :display_name => @user.display_name
29         end
30       end
31     end
32
33     @message ||= Message.new(:recipient => @this_user)
34     @title = t "message.new.title"
35   end
36
37   # Allow the user to reply to another message.
38   def reply
39     message = Message.find(params[:message_id])
40
41     if message.to_user_id == @user.id
42       message.update_attribute(:message_read, true)
43
44       @message = Message.new(
45         :recipient => message.sender,
46         :title => "Re: #{message.title.sub(/^Re:\s*/, '')}",
47         :body => "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}"
48       )
49
50       @title = @message.title
51
52       render :action => "new"
53     else
54       flash[:notice] = t "message.reply.wrong_user", :user => @user.display_name
55       redirect_to :controller => "user", :action => "login", :referer => request.fullpath
56     end
57   rescue ActiveRecord::RecordNotFound
58     @title = t "message.no_such_message.title"
59     render :action => "no_such_message", :status => :not_found
60   end
61
62   # Show a message
63   def read
64     @title = t "message.read.title"
65     @message = Message.find(params[:message_id])
66
67     if @message.to_user_id == @user.id || @message.from_user_id == @user.id
68       @message.message_read = true if @message.to_user_id == @user.id
69       @message.save
70     else
71       flash[:notice] = t "message.read.wrong_user", :user => @user.display_name
72       redirect_to :controller => "user", :action => "login", :referer => request.fullpath
73     end
74   rescue ActiveRecord::RecordNotFound
75     @title = t "message.no_such_message.title"
76     render :action => "no_such_message", :status => :not_found
77   end
78
79   # Display the list of messages that have been sent to the user.
80   def inbox
81     @title = t "message.inbox.title"
82     if @user && params[:display_name] == @user.display_name
83     else
84       redirect_to :controller => "message", :action => "inbox", :display_name => @user.display_name
85     end
86   end
87
88   # Display the list of messages that the user has sent to other users.
89   def outbox
90     @title = t "message.outbox.title"
91     if @user && params[:display_name] == @user.display_name
92     else
93       redirect_to :controller => "message", :action => "outbox", :display_name => @user.display_name
94     end
95   end
96
97   # Set the message as being read or unread.
98   def mark
99     @message = Message.where("to_user_id = ? OR from_user_id = ?", @user.id, @user.id).find(params[:message_id])
100     if params[:mark] == "unread"
101       message_read = false
102       notice = t "message.mark.as_unread"
103     else
104       message_read = true
105       notice = t "message.mark.as_read"
106     end
107     @message.message_read = message_read
108     if @message.save && !request.xhr?
109       flash[:notice] = notice
110       redirect_to :controller => "message", :action => "inbox", :display_name => @user.display_name
111     end
112   rescue ActiveRecord::RecordNotFound
113     @title = t "message.no_such_message.title"
114     render :action => "no_such_message", :status => :not_found
115   end
116
117   # Delete the message.
118   def delete
119     @message = Message.where("to_user_id = ? OR from_user_id = ?", @user.id, @user.id).find(params[:message_id])
120     @message.from_user_visible = false if @message.sender == @user
121     @message.to_user_visible = false if @message.recipient == @user
122     if @message.save && !request.xhr?
123       flash[:notice] = t "message.delete.deleted"
124
125       if params[:referer]
126         redirect_to params[:referer]
127       else
128         redirect_to :controller => "message", :action => "inbox", :display_name => @user.display_name
129       end
130     end
131   rescue ActiveRecord::RecordNotFound
132     @title = t "message.no_such_message.title"
133     render :action => "no_such_message", :status => :not_found
134   end
135
136   private
137
138   ##
139   # return permitted message parameters
140   def message_params
141     params.require(:message).permit(:title, :body)
142   end
143 end