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