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