]> git.openstreetmap.org Git - rails.git/blob - app/controllers/messages_controller.rb
Update annotations after 91a37235ac748df7dc241b215790d6cbecd652f3
[rails.git] / app / controllers / messages_controller.rb
1 class MessagesController < ApplicationController
2   layout "site"
3
4   before_action :authorize_web
5   before_action :set_locale
6
7   authorize_resource
8
9   before_action :lookup_user, :only => [:new, :create]
10   before_action :check_database_readable
11   before_action :check_database_writable, :only => [:new, :create, :reply, :mark, :destroy]
12   before_action :allow_thirdparty_images, :only => [:new, :create, :show]
13
14   # Allow the user to write a new message to another user. This action also
15   # deals with the sending of that message to the other user when the user
16   # clicks send.
17   # The display_name param is the display name of the user that the message is being sent to.
18   def new
19     @message = Message.new(message_params.merge(:recipient => @user))
20     @title = t ".title"
21   end
22
23   def create
24     @message = Message.new(message_params)
25     @message.recipient = @user
26     @message.sender = current_user
27     @message.sent_on = Time.now.getutc
28
29     if current_user.sent_messages.where("sent_on >= ?", Time.now.getutc - 1.hour).count >= Settings.max_messages_per_hour
30       flash[:error] = t ".limit_exceeded"
31       render :action => "new"
32     elsif @message.save
33       flash[:notice] = t ".message_sent"
34       Notifier.message_notification(@message).deliver_later
35       redirect_to :action => :inbox
36     else
37       render :action => "new"
38     end
39   end
40
41   # Allow the user to reply to another message.
42   def reply
43     message = Message.find(params[:message_id])
44
45     if message.recipient == current_user
46       message.update(:message_read => true)
47
48       @message = Message.new(
49         :recipient => message.sender,
50         :title => "Re: #{message.title.sub(/^Re:\s*/, '')}",
51         :body => "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}"
52       )
53
54       @title = @message.title
55
56       render :action => "new"
57     else
58       flash[:notice] = t ".wrong_user", :user => current_user.display_name
59       redirect_to :controller => "users", :action => "login", :referer => request.fullpath
60     end
61   rescue ActiveRecord::RecordNotFound
62     @title = t "messages.no_such_message.title"
63     render :action => "no_such_message", :status => :not_found
64   end
65
66   # Show a message
67   def show
68     @title = t ".title"
69     @message = Message.find(params[:id])
70
71     if @message.recipient == current_user || @message.sender == current_user
72       @message.message_read = true if @message.recipient == current_user
73       @message.save
74     else
75       flash[:notice] = t ".wrong_user", :user => current_user.display_name
76       redirect_to :controller => "users", :action => "login", :referer => request.fullpath
77     end
78   rescue ActiveRecord::RecordNotFound
79     @title = t "messages.no_such_message.title"
80     render :action => "no_such_message", :status => :not_found
81   end
82
83   # Display the list of messages that have been sent to the user.
84   def inbox
85     @title = t ".title"
86   end
87
88   # Display the list of messages that the user has sent to other users.
89   def outbox
90     @title = t ".title"
91   end
92
93   # Set the message as being read or unread.
94   def mark
95     @message = Message.where("to_user_id = ? OR from_user_id = ?", current_user.id, current_user.id).find(params[:message_id])
96     if params[:mark] == "unread"
97       message_read = false
98       notice = t ".as_unread"
99     else
100       message_read = true
101       notice = t ".as_read"
102     end
103     @message.message_read = message_read
104     if @message.save && !request.xhr?
105       flash[:notice] = notice
106       redirect_to :action => :inbox
107     end
108   rescue ActiveRecord::RecordNotFound
109     @title = t "messages.no_such_message.title"
110     render :action => "no_such_message", :status => :not_found
111   end
112
113   # Destroy the message.
114   def destroy
115     @message = Message.where("to_user_id = ? OR from_user_id = ?", current_user.id, current_user.id).find(params[:id])
116     @message.from_user_visible = false if @message.sender == current_user
117     @message.to_user_visible = false if @message.recipient == current_user
118     if @message.save && !request.xhr?
119       flash[:notice] = t ".destroyed"
120
121       if params[:referer]
122         redirect_to params[:referer]
123       else
124         redirect_to :action => :inbox
125       end
126     end
127   rescue ActiveRecord::RecordNotFound
128     @title = t "messages.no_such_message.title"
129     render :action => "no_such_message", :status => :not_found
130   end
131
132   private
133
134   ##
135   # return permitted message parameters
136   def message_params
137     params.require(:message).permit(:title, :body)
138   rescue ActionController::ParameterMissing
139     ActionController::Parameters.new.permit(:title, :body)
140   end
141 end