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