]> git.openstreetmap.org Git - rails.git/blob - app/controllers/messages_controller.rb
Merge remote-tracking branch 'upstream/pull/5538'
[rails.git] / app / controllers / messages_controller.rb
1 class MessagesController < ApplicationController
2   include UserMethods
3
4   layout "site"
5
6   before_action :authorize_web
7   before_action :set_locale
8
9   authorize_resource
10
11   before_action :lookup_user, :only => [:new, :create]
12   before_action :check_database_readable
13   before_action :check_database_writable, :only => [:new, :create, :destroy]
14
15   allow_thirdparty_images :only => [:new, :create, :show]
16
17   # Show a message
18   def show
19     @title = t ".title"
20     @message = Message.find(params[:id])
21
22     if @message.recipient == current_user || @message.sender == current_user
23       @message.message_read = true if @message.recipient == current_user
24       @message.save
25     else
26       flash[:notice] = t ".wrong_user", :user => current_user.display_name
27       redirect_to login_path(:referer => request.fullpath)
28     end
29   rescue ActiveRecord::RecordNotFound
30     @title = t "messages.no_such_message.title"
31     render :action => "no_such_message", :status => :not_found
32   end
33
34   # Allow the user to write a new message to another user.
35   def new
36     @message = Message.new(message_params.merge(:recipient => @user))
37     @title = t ".title"
38   end
39
40   def create
41     @message = Message.new(message_params)
42     @message.recipient = @user
43     @message.sender = current_user
44     @message.sent_on = Time.now.utc
45
46     if current_user.sent_messages.where(:sent_on => Time.now.utc - 1.hour..).count >= current_user.max_messages_per_hour
47       flash.now[:error] = t ".limit_exceeded"
48       render :action => "new"
49     elsif @message.save
50       flash[:notice] = t ".message_sent"
51       UserMailer.message_notification(@message).deliver_later if @message.notify_recipient?
52       redirect_to messages_outbox_path
53     else
54       @title = t "messages.new.title"
55       render :action => "new"
56     end
57   end
58
59   # Destroy the message.
60   def destroy
61     @message = Message.where(:recipient => current_user).or(Message.where(:sender => current_user.id)).find(params[:id])
62     @message.from_user_visible = false if @message.sender == current_user
63     @message.to_user_visible = false if @message.recipient == current_user
64     if @message.save
65       flash[:notice] = t ".destroyed"
66
67       referer = safe_referer(params[:referer]) if params[:referer]
68
69       redirect_to referer || messages_inbox_path, :status => :see_other
70     end
71   rescue ActiveRecord::RecordNotFound
72     @title = t "messages.no_such_message.title"
73     render :action => "no_such_message", :status => :not_found
74   end
75
76   private
77
78   ##
79   # return permitted message parameters
80   def message_params
81     params.require(:message).permit(:title, :body)
82   rescue ActionController::ParameterMissing
83     ActionController::Parameters.new.permit(:title, :body)
84   end
85 end